Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move Div up by 20px - But keep the border

Tags:

html

css

i have three <div>s and want to move the second one up.

Currently i'm doing this with position: relative; top: -20px; - That works pretty well.

Only downside is: There's a gap (of 20px) between the second <div> and the third <div> (which is under the second div).

So, i want to keep the border around all three divs, so that top: -20px is not an alternative for the third row.

I have this illustrated here: http://jsfiddle.net/w2PGF/1/

My Markup:

<div id="border">
    <div class="firstRow">foo</div>
    <div class="secondRow">bar</div>
    <div class="thirdRow">foobar</div>
</div>​

My CSS:

#border {
    border: 5px solid #000;
}
.firstRow {
    background-color: cyan;
    border: 3px solid red;
    height: 50px;
}
.secondRow {
    position: relative;
    top: -20px;
    border: 3px solid yellow;
    background-color: grey;
    height: 50px;
}
.thirdRow {
    background-color: blue;
    border: 3px solid blue;
    height: 50px;
}

Thanks in advance. ​

like image 680
Stefan Avatar asked Aug 04 '12 13:08

Stefan


People also ask

How do I keep my div on the same line?

Set size and make inline Because they're block elements, when reducing the size of Div one to make room for the other div, you're left with space next to Div one and Div two below Div one. To move the div up to the next line both div's need to have the inline-block display setting as shown below.

How do I move a div to the left in CSS?

If position: absolute; or position: fixed; - the left property sets the left edge of an element to a unit to the left of the left edge of its nearest positioned ancestor.


2 Answers

.secondRow { margin-bottom: -20px }
like image 110
Anthony Mills Avatar answered Nov 02 '22 18:11

Anthony Mills


Remove the position: relative and instead of top: -20px you should add margin-top: -20px

Like so: fiddle

like image 45
Bram Vanroy Avatar answered Nov 02 '22 20:11

Bram Vanroy