Harish Harish - 21 days ago 7
CSS Question

Avoid overlapping with absolute positioning in responsive

I'm trying to create a div with some basic text elements in it. My requirement is that the elements inside the div should be aligned according to the div, hence I used absolute positioning with my main div positioned as relative and gave percentage values to it so that they can be worked in responsive screens. I have even changed some of the top bottom percentages in media screens. However, in some cases when the screen size is changed one block of text or div is overlapping over the other. Is there a way to avoid this overlapping in responsive screens. Thank you in advance :)



.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>




Answer

Okay, I can give you a solution to easily fix it by setting min-height like this:

.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>

But what you are doing is totally not the right thing. You should never use position: absolute in this case. Instead, you need to use @media queries for this kind of layout.

Comments