如何删除图像周围自动生成的HTML容器边距

时间:2017-06-29 17:56:25

标签: html css

我试图删除此图片周围自动生成的容器边距。下面是我用来制作它的代码。您可以查看网站here。我尝试添加margin and padding item to the body element,但它没有解决问题。

enter image description here



.container {
  position: relative;
  width: 240px;
  height: 240px;
}

.image {
  display: block;
  width: 100%;
  height: auto;
}

.overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100%;
  width: 100%;
  opacity: 0;
  transition: .5s ease;
  background-color: #008CBA;
}

.container:hover .overlay {
  opacity: 0.85;
}

.text {
  color: white;
  font-size: 20px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
}

<div class="container">
  <img src="./img/headshots/Exec_DMoon.jpg" width="240" height="240" alt="Photo of David Moon, Assistant Vice President for Financial Affairs" class="image">
  <div class="overlay">
    <div class="text"><b>David Moon</b> Assistant Vice President for Financial Affairs, <a class="usa-external_link" target="_blank" href="mailto:davidmoon826@gwmail.gwu.edu">Email</a></div>
  </div>
</div>
&#13;
&#13;
&#13;

这是所需的输出

enter image description here

我做错了什么?

3 个答案:

答案 0 :(得分:0)

对此最简单的解决方法,imo:将您想要的项目包含在div中的网格中,并提供div display: flexflex-wrap: wrap。祝你好运!

答案 1 :(得分:0)

好吧,只需将float: left添加到.container

即可

(实现你在&#34下显示的内容;这是所需的输出&#34;)

答案 2 :(得分:0)

Johannes 的答案几乎奏效了,但它引起的问题是文本会将自身重新定位到开放的间隙中(见下图),而不是在所有图像下方格式化。

enter image description here

解决方案是在display: inline-block; 中使用.container,建议使用 Adrian

&#13;
&#13;
<!DOCTYPE html>
<html>
<head>
<style>
.container {
  position: relative;
  width: 50%;
  height: 50%;
  display: inline-block;
}

.image {
  display: block;
  width: 100%;
  height: auto;
}

.overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100%;
  width: 100%;
  opacity: 0;
  transition: .5s ease;
  background-color: #008CBA;
}

.container:hover .overlay {
  opacity: 0.8;
}

.text {
  color: white;
  font-size: 25px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
}
</style>
</head>
<body>

<h2>Fade in Overlay</h2>
<p>Hover over the image to see the effect.</p>

<div class="container">
  <img src="img_avatar.png" alt="Avatar" class="image">
  <div class="overlay">
    <div class="text">Hello World</div>
  </div>
</div>

<div class="container">
  <img src="img_avatar.png" alt="Avatar" class="image">
  <div class="overlay">
    <div class="text">Hello World</div>
  </div>
</div>

<div class="container">
  <img src="img_avatar.png" alt="Avatar" class="image">
  <div class="overlay">
    <div class="text">Hello World</div>
  </div>
</div>

<h2>Fade in Overlay</h2>
<p>Hover over the image to see the effect.</p>

</body>
</html>
&#13;
&#13;
&#13;