【问题标题】:Avoid overlapping with absolute positioning in responsive避免在响应式中与绝对定位重叠
【发布时间】:2016-11-14 05:44:41
【问题描述】:

我正在尝试创建一个包含一些基本文本元素的 div。我的要求是 div 内的元素应该根据 div 对齐,因此我使用绝对定位,我的主 div 定位为相对,并为其提供百分比值,以便它们可以在响应式屏幕中工作。我什至更改了媒体屏幕中的一些顶部底部百分比。但是,在某些情况下,当屏幕尺寸发生变化时,一块文本或 div 会重叠在另一块上。有没有办法避免在响应式屏幕中出现这种重叠。提前谢谢你:)

.Heading{
  position:relative;
 }
.Heading h3{
  top:1%;
  position:absolute;
  left:0;
  right:0;
}
.Text{
    Position: absolute;
    top: 10%;
 }
.bottom-part{
  position:absolute;
  top:60%; 
}
<div class="main">
  <div class="Heading">
    <h3>Heading part</h3>
  </div>
  <div class="Text">
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
      has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
      publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
  </div>
  <div class="bottom-part">
    <h4>Here's the ending part</h4>
  </div>
</div>

【问题讨论】:

  • 你为什么要absolute这个div,你可以干这个float:left属性

标签: html css responsive-design media-queries


【解决方案1】:

好的,我可以给你一个解决方案,通过设置min-height 来轻松修复它:

.Heading {
  position: relative;
  min-height: 100px;
}
.Heading h3 {
  top: 1%;
  position: absolute;
  left: 0;
  right: 0;
}
.Text {
  Position: absolute;
  top: 10%;
}
.bottom-part {
  position: absolute;
  top: 60%;
}
<div class="main">
  <div class="Heading">
    <h3>Heading part</h3>
  </div>
  <div class="Text">
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
      has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
      publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
  </div>
  <div class="bottom-part">
    <h4>Here's the ending part</h4>
  </div>
</div>

但是你所做的完全不是正确的。在这种情况下,您永远不应该使用position: absolute。相反,您需要对这种布局使用@media 查询。

【讨论】:

  • 谢谢。我试试看。
  • @Harish Sure...:) 告诉我。
  • 谢谢。我已经完全删除了绝对定位。我使用了媒体查询并进行了一些更改。 @Praveen Kumar
猜你喜欢
  • 1970-01-01
  • 2023-03-11
  • 2017-03-29
  • 2014-01-31
  • 2015-08-18
  • 2017-08-27
  • 1970-01-01
  • 1970-01-01
  • 2019-02-06
相关资源
最近更新 更多