如何防止图像在父母边框之外缩放?

时间:2016-11-29 07:04:00

标签: html css html5

我提供了不同服务的列表,每个服务都有图像,标题和描述。我已将其设置为鼠标悬停时图像缩放到1.3倍。缩放有效,但在执行此操作时会忽略父级边框。

我已经抓住了GIF的实际情况。

Example of image scaling outside of parent's borders

以下是我正在使用的代码。



.cleaningservices {
    max-width: 250px;
    max-height: 250px;

}

    /* Default state of the image */
    .hover01 img {
      -webkit-transform: scale(1);
      transform: scale(1);
      -webkit-transition: .3s ease-in-out;
      transition: .3s ease-in-out;
    }

    /* Scale the image to 1.3 it's size on mouse hover */
    .hover01:hover img {
      -webkit-transform: scale(1.3);
      transform: scale(1.3);
    }

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.11.2/css/bootstrap-select.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="row">
  <div class="cleaningservices">
    <div class="col-sm-6 col-md-4">
      <div class="thumbnail hover01">
        <img src="https://bosworthco.com/wp-content/uploads/2016/10/cleaning-268134_960_720.jpg" alt="deep house cleaning">
        <div class="caption">
          <h3>Deep Cleaning</h3>
          <p>Usually, on our first visit we will most likely have to complete a deep cleaning. It is a far more intensive cleaning that our regular cleaning. After first deep cleaning we will continue with one of our regularly scheduled cleanings below.</p>
        </div>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

在鼠标悬停时,我必须做些什么更改才能使图像在父级边框内缩放?

3 个答案:

答案 0 :(得分:1)

创建一个设置overflow: hidden;的img容器。我在body上添加了背景色,以便您看到它有效。

body {
  background-color: #f0f0f0 !important;
  padding: 50px;
}
.noverflow {
  overflow: hidden;
}
.thumbnail {
  background-color: #fff;
}
.hover01 img {
  -webkit-transform: scale(1);
  transform: scale(1);
  -webkit-transition: .3s ease-in-out;
  transition: .3s ease-in-out;
}
.hover01:hover img {
  -webkit-transform: scale(1.1);
  transform: scale(1.1);
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.11.2/css/bootstrap-select.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
  <div class="cleaningservices">
    <div class="col-sm-6 col-md-4">
      <div class="thumbnail hover01">
        <div class="noverflow"><img src="http://fiusms.fiu.edu/wp-content/uploads/Residential-Cleaning-Watauga-TX.jpg" alt="deep house cleaning"></div>
        <div class="caption">
          <h3>Deep Cleaning</h3>
          <p>Usually, on our first visit we will most likely have to complete a deep cleaning. It is a far more intensive cleaning that our regular cleaning. After first deep cleaning we will continue with one of our regularly scheduled cleanings below.</p>
        </div>
      </div>
    </div>
  </div>
</div>

答案 1 :(得分:1)

只需使用overflow: hidden;

.hover01 img {
  -webkit-transform: scale(1);
  transform: scale(1);
  -webkit-transition: .3s ease-in-out;
  transition: .3s ease-in-out;
  overflow: hidden;
}

答案 2 :(得分:1)

您可以将图片添加到div并提供div overflow:hidden;

.hover01 img {
    -webkit-transform: scale(1);
    transform: scale(1);
    -webkit-transition: .3s ease-in-out;
    transition: .3s ease-in-out;
    width:100%;
}
.hover01:hover img {
    -webkit-transform: scale(1.3);
    transform: scale(1.3);
}

.img-parent{
    overflow:hidden;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.11.2/css/bootstrap-select.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
   <div class="cleaningservices">
      <div class="col-sm-6 col-md-4">
	  <div class="thumbnail hover01">
	     <div class="img-parent">
	        <img src="http://fiusms.fiu.edu/wp-content/uploads/Residential-Cleaning-Watauga-TX.jpg" alt="deep house cleaning">
             </div>
	     <div class="caption">
		<h3>Deep Cleaning</h3>
		<p>Usually, on our first visit we will most likely have to complete a deep cleaning. It is a far more intensive cleaning that our regular cleaning. After first deep cleaning we will continue with one of our regularly scheduled cleanings below.</p>
	     </div>
	  </div>
       </div>
    </div>
</div>