Reputation: 99
There is some extra space after div with id "header" element (second div). If I remove p, no space between div element. how to kill space between two div element without removing p element and why it act like it?
body {
margin: 0px;
padding: 0px;
}
div#page {
width: 960px;
margin: 10px auto;
}
div#header {
width: 960px;
height: 80px;
background-color: lightgray;
}
div#main {
height: 400px;
width: 960px;
background-color: antiquewhite;
}
<div id="page">
<div id="header">header</div>
<div id="main">
<p>we make your business</p>
<p>Con panna organic americano grinder single origin white mug chicory arabica breve cortado. In sit, aromatic lungo shop body redeye.</p>
<form action="" method="post">
<button>about us</button>
</form>
</div>
</div>
Upvotes: 1
Views: 7489
Reputation: 11930
quick solution
add
padding-top:1px;
to div#main
smart solution
use flex, benefits: responsive, less code, more readable, modern
/* the main content will take all remaining space, makin it responsive */
html,
body {
height: 100%;
}
body {
margin: 0px;
padding: 0px;
}
div#page {
/* make page fill all available space*/
height: 100%;
/* change predefined value to 100%, and adjust spaces */
width: 100%;
padding: 0 10px;
margin: 10px auto;
/* flex usage itself */
display: flex;
/* place divs horizontally */
flex-direction: column;
}
div#header {
height: 80px;
/* header will not resize when window resized*/
flex-shrink: 0;
background-color: lightgray;
}
div#main {
/* responsive container, remove width/height, any predefined values */
background-color: antiquewhite;
flex: 1;
}
<div id="page">
<div id="header">
header
</div>
<div id="main">
<p>we make your business</p>
<p>Con panna organic americano grinder single origin white mug chicory arabica breve cortado. In sit, aromatic lungo shop body redeye.</p>
<form action="" method="post">
<button>about us</button>
</form>
</div>
</div>
Upvotes: 0
Reputation: 8572
It's because of margin collapsing:
Parent and first/last child
If there is no border, padding, inline content, block_formatting_context created or clearance to separate the margin-top of a block from the margin-top of its first child block, or no border, padding, inline content, height, min-height, or max-height to separate the margin-bottom of a block from the margin-bottom of its last child, then those margins collapse. The collapsed margin ends up outside the parent.
You could remove margin-top
from your first <p>
element, and add padding-top
instead to div#main
:
body {
margin: 0px;
padding: 0px;
}
div#page {
width: 960px;
margin: 10px auto;
}
div#header {
width: 960px;
height: 80px;
background-color: lightgray;
}
div#main {
height: 400px;
width: 960px;
background-color: antiquewhite;
padding-top: 15px;
}
div#main p:first-child {
margin-top: 0;
}
<div id="page">
<div id="header">header</div>
<div id="main">
<p>we make your business</p>
<p>Con panna organic americano grinder single origin white mug chicory arabica breve cortado. In sit, aromatic lungo shop body redeye.</p>
<form action="" method="post">
<button>about us</button>
</form>
</div>
</div>
Upvotes: 2
Reputation: 875
Please try this
<!-- language: lang-css -->
body{
margin:0px;
padding:0px;
}
div#page{
width:960px;
margin:10px auto;
}
div#header{
width:960px;
height:80px;
background-color:lightgray;
}
div#main{
height:400px;
width:960px;
background-color:antiquewhite;
}
p.demo{
margin:0px;
}
<!-- language: lang-html -->
<div id="page">
<div id="header">
header
</div>
<div id="main">
<p class="demo">we make your business</p>
<p>Con panna organic americano grinder single origin white mug chicory arabica breve cortado. In sit, aromatic lungo shop body redeye.</p>
<form action="" method="post">
<button>about us
</button>
</form>
</div>
</div>
Upvotes: 0
Reputation: 435
just using * in stead of body element
*{
margin:0px;
padding:0px;
}
div#page{
width:960px;
margin:10px auto;
}
div#header{
width:960px;
height:80px;
background-color:lightgray;
}
div#main{
height:400px;
width:960px;
background-color:antiquewhite;
}
<div id="page">
<div id="header">
header
</div>
<div id="main">
<p>we make your business</p>
<p>Con panna organic americano grinder single origin white mug chicory arabica breve cortado. In sit, aromatic lungo shop body redeye.</p>
<form action="" method="post">
<button>
about us
</button>
</form>
</div>
</div>
Upvotes: 0
Reputation: 2314
The reason it happens is p
tags have an auto margin. More info here
Upvotes: -1
Reputation: 333
If you remove space between these two div then add this CSS:-
#main p:first-child {
margin: 0px;
}
Upvotes: 0