首页 > 解决方案 > 组合显示问题:内联;和位置:绝对;

问题描述

我在此处的 W3school 网站链接中找到了此示例

我正在尝试将图像的显示从“块”更改为“内联”,并发现 .container 和 .overlay 变得比图像大一点。

为什么会这样?

<!DOCTYPE html>
<html>
<head>
<style>
.container {
  position: relative;
  width: 50%;
}

.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: 1;
}

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

<h2>Fade in Overlay</h2>

<div class="container">
  <img src='https://www.w3schools.com/css/img_avatar.png' alt="Avatar" class="image">
  <div class="overlay">
    <div class="text">Hello World</div>
  </div>
</div>

</body>
</html>

标签: css-positiondisplay

解决方案


连同. vertical-align: bottom;_display: inline;


推荐阅读