html - 基于内容问题的高度相对定位

标签 html css

你好,我正在努力实现这个 EXAMPLE

所以在这种情况下,您可以看到,我们有 3 个 div。顶部的宽度为 100%,高度为自动。当然也会相对定位。

第二个相同,但在这种情况下,我将添加一个虚拟文本来定义 div 的高度。

第三个和第一个一样,问题是,他们没有结盟。没有一个在另一个之前。有人能告诉我我的代码有什么问题吗?谢谢

#block1 {
  position:relative;
  width:100%;
  height:auto;
  background-color: blue;
}

#img1 {
  width:100px;
  height:100px;
  position:absolute;
  left:50%;
  transform:translate(-50%);
  -webkit-transform:translate(-50%);
}

#block2 {
  position:relative;
  width:100%;
  height:auto;
  background-color: orange;
}

.dummytext {
  font-family: 'Ralleway, sans-serif';
  font-size: 20px;
  text-align: justify;
  margin:10px;
}

#block3 {
  position:relative;
  width:100%;
  height:auto;
  background-color: brown;
}

#img2 {
  width:100px;
  height:90px;
  position:absolute;
  left:50%;
  transform:translate(-50%);
  -webkit-transform:translate(-50%);
}
<div id="block1">
  <img src="images/img1.png" id="img1"/>
</div>

<div id="block2">
  <h3 class="dummytext">Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, </h3>
</div>

<div id="block3">
  <img id="img2" src="images/img2.png"/>
</div>

谁能帮我理解一下?

最佳答案

这是因为您的图像绝对定位,不会影响容器的大小。 您应该删除图像的绝对位置。

试试这个:

div {
  text-align: center; /* center everything inside the divs */
}
#block1 {
  width: 100%;
  height: auto;
  background-color: blue;
}
#img1 {
  width: 100px;
  height: 100px;
}
#block2 {
  width: 100%;
  height: auto;
  background-color: orange;
}
.dummytext {
  font-family: 'Ralleway, sans-serif';
  font-size: 20px;
  text-align: justify;
  margin: 10px;
}
#block3 {
  width: 100%;
  height: auto;
  background-color: brown;
}
#img2 {
  width: 100px;
  height: 90px;
}
<div id="block1">
  <img src="images/img1.png" id="img1" />
</div>

<div id="block2">
  <h3 class="dummytext">Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, Text Example, </h3>
</div>

<div id="block3">
  <img id="img2" src="images/img2.png" />
</div>

关于html - 基于内容问题的高度相对定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39131082/

相关文章:

html - 居中谷歌地图 iframe

html - 在对齐的文本中设置一个换行符,并且仍然让它对齐

javascript - 通过 HTML 调用嵌套的 JavaScript 函数(简单)

javascript - 无法访问 3d 立方体内部的对象

html - 将元素锚定到 Ionic 菜单的底部

jquery - 使用jquery拖动div

javascript - 禁用在手机上以较低宽度自动播放的视频

javascript - 外部 jquery 可拖动/可调整大小的 div 不允许我选择内部跨度的文本

html - 滚动条不显示 - BootStrap

html - 使用 bootstrap css 和网格系统以所有媒体大小为中心的 Div