Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove whitespace that appears after relative positioning an element with CSS

The problem occurs is the following: After relative positioning an element with CSS I get a white-space of where the element was... I don't want the white-space!

    .thetext       {          width:400px;          background:yellow;          border: 1px dashed red;          margin:50px;          padding:5px;          font-weight:bold;      }      .whiteblob      {          position:relative;          top:-140px;          left:70px;          width:200px;          height:50px;          border: 4px solid green;          background:white;          font-size:2.5em;          color:red;                }      .footerallowedwhitespaceinblue      {          height:10px;          background-color:blue;      }      .footer      {          background-color:grey;          height:200px;      }
<div class="thetext"><script type="text/javascript">for(c=0;c<50;c++){document.write("Lorem ipsum dolor est, ");}</script>      </div>      <div class="whiteblob">          &nbsp;buy this!      </div>      <div class="footerallowedwhitespaceinblue">      </div>      <div class="footer">          The whitespace above is way to big! The buy this still takes up space whilst it is moved.      </div>

JSFiddle: http://jsfiddle.net/qqXQn/

As you can see in the example, the only whitespace I want is the whitespace caused by the thetext block by the margin of 50px; and the spacing by the footerallowedwhitespaceinblue(made blue so it's visible). The problem is... the whitespace is too big now because the "buy this" div still takes up space after it's been relatively positioned.

How do I solve this?

like image 782
Tschallacka Avatar asked Dec 05 '12 11:12

Tschallacka


People also ask

How do I remove spaces from a relative position in CSS?

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. "and you can't get rid of that." - you can. Just use negative margin, like in the plang's answer.

Is it bad to use position relative in CSS?

It's a bad idea imho as it changes the default behaviour of elements without clear indication and it will have unforseen consequences. If you really want to have most elements positioned relative, you might want to think about just making div and similar relative.


2 Answers

You can simply solve this by applying a negative margin that equals the width or height of the element.

For an element of 100px height that is positioned to the top you will apply margin-bottom:-100px;

For an element of 100px height that is positioned to the bottom you will apply margin-top:-100px;

For an element of 100px width that is positioned to the left you will apply margin-right:-100px;

For an element of 100px width that is positioned to the right you will apply margin-left:-100px;

cut & paste css snippets:

.top      {     postion:relative;     top:-100px;     height:25px;     margin-bottom:-25px;     } .bottom     {     postion:relative;     top:100px;     height:25px;     margin-top:-25px;     } .left     {     postion:relative;     left:-100px;     width:25px;     margin-right:-25px;     } .right     {     postion:relative;     left:100px;     width:25px;     margin-left:-25px;     } 

And the reworked example code becomes then:

.thetext   {      width:400px;      background:yellow;      border: 1px dashed red;      margin:50px;      padding:5px;      font-weight:bold;  }  .whiteblob  {      position:relative;      top:-140px;      left:70px;      width:200px;      height:50px;      margin-bottom:-50px;      border: 4px solid green;      background:white;      font-size:2.5em;      color:red;        }  .footerallowedwhitespaceinblue  {      height:10px;      background-color:blue;  }  .footer  {      background-color:grey;      height:200px;  }
<div class="thetext"><script type="text/javascript">for(c=0;c<50;c++){document.write("Lorem ipsum dolor est, ");}</script>  </div>  <div class="whiteblob">      &nbsp;buy this!  </div>  <div class="footerallowedwhitespaceinblue">  </div>  <div class="footer">  </div>

http://jsfiddle.net/qqXQn/1/

like image 97
Tschallacka Avatar answered Oct 10 '22 00:10

Tschallacka


Here is an example. In this case, the object was moved to the right and then up using a negative top value. Eliminating its trailing margin space required adding an equal negative-margin value.

 position:relative;  width:310px;  height:260px;  top:-332px;  margin-bottom:-332px;  left:538px; 
like image 43
user734063 Avatar answered Oct 09 '22 22:10

user734063