position:relative leaves an empty space

Css

Css Problem Overview


Code is here: http://lasers.org.ru/vs/example.html

How to remove an empty space under main block (#page)?

Css Solutions


Solution 1 - Css

Another trick which worked fine for me is to use a negative margin-bottom in the relative element that you have moved. No need to go with absolute positioning.

Something like:

position: relative;
top: -200px;
left: 100px;
margin-bottom: -200px;

Similar (if not identical) to the solution I see now, from green.

Solution 2 - Css

Well, don't use relative positioning then. The element still takes up space where it originally was when using relative positioning, and you can't get rid of that. You can for example use absolute positioning instead, or make the elements float beside each other.

I played around with the layout a bit, and I suggest that you change these three rules to:

#layout { width: 636px; margin: 0 auto; }
#menu { position: absolute; width: 160px; margin-left: 160px; }
#page { width: 600px; padding: 8px 16px; border: 2px solid #404241; }

Solution 3 - Css

#page 
{ 
     overflow:hidden; 
}

Solution 4 - Css

Try this rule:

#page {
  border: 2px solid #404241;
  bottom: 0;
  padding: 8px 16px;
  position: absolute;
  top: 70px;
  width: 600px;
}

I changed position to absolute, this allows you to use the bottom: 0 property.

Solution 5 - Css

#page {
  padding-bottom: 0;
}

no bottom padding

Solution 6 - Css

My answer is late but it may help others with a similar issue that I had.

I had a <div> with position: relative; where all the child elements have the position: absolute; style. This caused around 20px of white space to appear on my page.

To get around this I added margin-top: -20px; to the next sibling element after the <div> with position: relative;.

If you have a sibling element before, you can use margin-bottom: -20px;

section {
  height: 200px;
}

<h2>Extra Whitespace</h2>
<section>
  <div>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
  </div>
  <div style="position:relative;">
    <div style="position:relative; top: -20px; left:100px;">ABSOLUTE</div>
  </div>
  <div>
    Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  </div>
</section>


<h2>No Whitespace margin-top</h2>
<section>
  <div>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
  </div>
  <div style="position:relative;">
    <div style="position:relative; top: -20px; left:100px;">ABSOLUTE</div>
  </div>
  <div style="margin-top:-20px;">
    Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  </div>
</section>


<h2>No Whitespace margin-bottom</h2>
<section>
  <div style="margin-bottom:-20px;">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
  </div>
  <div style="position:relative;">
    <div style="position:relative; top: -20px; left:100px;">ABSOLUTE</div>
  </div>
  <div>
    Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  </div>
</section>

Solution 7 - Css

The best solution if you don't want to leave spaces below (relative)

Is to use margin-top and position: sticky

#page {
     margin-top: -280px;
     position: sticky;
}

Solution 8 - Css

I had a similar problem. The easiest way is to replace top on margin-top for #page.

Solution 9 - Css

I had the same issue. Negative margin didn't work for me as it left a massive white area where it used to be. I solved this problem in my case by manually entering height.

.maincontent {
    height: 675px;
}

Solution 10 - Css

I was able to get rid of the whitespaces using the following framework:

HTML-CSS framework

And here is the markup

<div id="the-force-moved-element>I've been moved</div>
<div id="the-hack-part-1">
	<div id="the-hack-part-2>The quick brown fox jumps over the lazy dog</div>
</div>
<p>Lorem ipsum dolor sit amet...</p>

Solution 11 - Css

This question seems to be well answered - however all the answers above had bad side effects in my layout. This is what really worked for me:

.moveUp {
    position: relative;
}
.moveUp > * {
    position: absolute;
    width: 100%;
    top: -75px;
}

/** This part is just design - ignore it ... ****/
.box1, .box2, .box3 {
    height: 100px;
    color: white;
}
.box1 {
    background: red;
}
.box2 {
    background: blue;
    height: 50px;
}
.box3 {
    background: green;
}

<div class="box1">Box 1</div>
<div class="moveUp"><div class="box2">Box 2 - 75px up</div></div>
<div class="box3">Box 3</div>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKirView Question on Stackoverflow
Solution 1 - CssplangView Answer on Stackoverflow
Solution 2 - CssGuffaView Answer on Stackoverflow
Solution 3 - CssCarol McKayView Answer on Stackoverflow
Solution 4 - CssSalman AView Answer on Stackoverflow
Solution 5 - CssalexView Answer on Stackoverflow
Solution 6 - CssGabe GatesView Answer on Stackoverflow
Solution 7 - CssztvmarkView Answer on Stackoverflow
Solution 8 - CssgreenView Answer on Stackoverflow
Solution 9 - CssUkrVolk11View Answer on Stackoverflow
Solution 10 - CssAbel CallejoView Answer on Stackoverflow
Solution 11 - CssGerfriedView Answer on Stackoverflow