Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

minus 20 pixels at top and bottom of div

Tags:

html

css

As a bare bones examples I have those 2 really simple divs:

enter image description here

(the green one is inside the red one)

Now how can I subtract 20 pixels from the bottom and the top of the green div?

html:

<div id="container">
    <div id="rows">
    </div>    
</div>

css:

#container {
    width: 200px;
    height: 300px;
    background: red;
}

#rows {
    width: 50%;
    height: 100%;
    /* margin-top: 20px;
        margin-bottom: 20px; */

   /*  padding-top: 20px;
    padding-bottom: 20px; */    

/*     top: 20px;
    bottom: 20px;  
    height: auto; */

    background: green;

}

http://jsfiddle.net/clankill3r/2L6c2bLf/1/

like image 937
clankill3r Avatar asked Jun 16 '15 13:06

clankill3r


People also ask

How do you keep items at the bottom of a div?

Set the position of div at the bottom of its container can be done using bottom, and position property. Set position value to absolute and bottom value to zero to placed a div at the bottom of container.

How do I stretch a div to the bottom of the page?

If you need to make a <div> element extend to the bottom and fill the page, try the following. Use viewport units and set the height and width of the <body> element to “100vh” and “100vw” respectively. Both the margin and padding of the <html> and <body> elements must be set to 0.

What determines div size?

By default a div is a block element, it means its width will be 100% of its container width, and its height will expand to cover all of its children. In case its children has a larger width than the div 's container width, the div itself does not expand horizontally but its children spill outside its boundary instead.


1 Answers

Add padding to #container

padding: 20px 0px;

Edit:

as suggested by @Adam you should contain also

box-sizing: border-box;

to stylesheet if you want to preserve box height

like image 85
Robert Avatar answered Nov 30 '22 22:11

Robert