Why are my header and main sections separated by white space? [duplicate] - html

This question already has answers here:
CSS margin terror; Margin adds space outside parent element [duplicate]
(7 answers)
Closed 8 months ago.
I am trying to create a hero box but my herobox and navbar have white space inbetween. I can not get rid of it! I am guesing it has to do with flexbox and/or grid but i'm not sure.
I colored the nav purple and the herobox blue to try to figure why they don't follow each other. I tried messing with the margin and display in CSS.
Chrome inspection of elements:
My code so far:
body {
font-family: sans-serif;
margin: 0;
}
main {
margin-top: none;
}
/*NAVIGATION BAR*/
header {
height: fit-content;
}
.topnav {
overflow: hidden;
background-color: blueviolet;
}
.left {
padding: 20px;
float: left;
width: 50%;
box-sizing: border-box;
text-decoration: none;
text-align: left;
}
.right {
padding: 20px;
float: right;
width: 50%;
box-sizing: border-box;
text-decoration: none;
text-align: right;
}
#media screen and (max-width: 800px) {
.left,
.right {
width: 100%;
/* The width is 100%, when the viewport is 800px or smaller */
}
}
/*HERO BOX*/
.hero {
background-color: aqua;
}
h1 {
font-size: 15vw;
}
<header>
<!--NAVIGATION BAR-->
<nav>
<div class="topnav">
<div class="left">
<a href="#Coupons">
<p>Coupons!</p>
</a>
</div>
<div class="right">
<a href="#Order">
<p>Order Online!</p>
</a>
</div>
</div>
</nav>
</header>
<main>
<div class="hero">
<h1>Super Restaurant!</h1>
<button>View our menu!</button>
</div>
</main>

Solution
Set the h1 to margin-top: 0.
Explanation
The h1 has a margin-top that is creating the space with the header section.
This is happening because, even though the h1 is a descendant of the main element, its top margin is superseding the top margins of its ancestors (.hero and main).
And this is happening because of the rules of margin collapsing.
ยง 8.3.1 Collapsing margins
In CSS, the adjoining margins of two or more boxes (which might or
might not be siblings) can combine to form a single margin.
Margins that combine this way are said to collapse, and the resulting combined margin is called a collapsed margin.
Adjoining vertical margins collapse, except [in certain cases].
Horizontal margins never collapse.
The top margin of an in-flow block element collapses with its first
in-flow block-level child's top margin if the element has no top
border, no top padding, and the child has no clearance.

It appears that the margin top on the header is causing the problem. try giving it a margin of 0 and giving it padding if you need and see what happens
h1 {
font-size: 15vw;
margin-top: 0;
}

Extra margin is from h1 (commented in code, info here).
Useful snippet for making completely own styling:
*{
box-sizing: border-box;
margin: 0;
padding: 0;
}
Removes all margins and padding and also makes all elements same box-sizing.
Also, for example, re-create nav using grid styles, try to avoid float in positioning of elements, its intended for positioning images in text or etc.
body {
font-family: sans-serif;
margin: 0;
}
main {
margin-top: none;
}
/*NAVIGATION BAR*/
header {
height: fit-content;
}
.topnav {
overflow: hidden;
background-color: blueviolet;
display:grid;
grid-template-columns:1fr 1fr;
}
.left {
padding: 20px;
place-self: center start;
box-sizing: border-box;
text-decoration: none;
}
.right {
padding: 20px;
place-self: center end;
box-sizing: border-box;
text-decoration: none;
text-align: right;
}
#media screen and (max-width: 800px) {
.topnav{
grid-template-columns:1fr;
}
}
/*HERO BOX*/
.hero {
background-color: aqua;
}
h1 {
font-size: 15vw;
margin-top:0; /* fix */
}
<header>
<!--NAVIGATION BAR-->
<nav>
<div class="topnav">
<div class="left">
<p>Coupons!</p>
</div>
<div class="right">
<p>Order Online!</p>
</div>
</div>
</nav>
</header>
<main>
<div class="hero">
<h1>Super Restaurant!</h1>
<button>View our menu!</button>
</div>
</main>

Related

Why setting padding makes two horizontal div vertical? [duplicate]

This question already has answers here:
Why does CSS padding increase size of element?
(6 answers)
Closed 2 years ago.
If i remove padding from below code two vertical div became horizontal, Why this is happening because as far as i know padding affects internal spacing. I am expecting two horizontally aligned divs from below code
<style>
#wrapper {
width:100%;
margin : 0;
}
#first-div {
width:50%;
margin : 0;
float : left ;
padding: 10px;
background-color: green;
color: white;
}
#second-div {
width:50%;
margin : 0;
float : left ;
background-color: blue;
color: white;
padding: 10px;
}
</style>
<div id="wrapper">
<div id="first-div" >
First div content here...
</div>
<div id="second-div" >
Second div content here...
</div>
</div>
<div id="wrapper">
<div id="first-div" >
First div content here...
</div>
<div id="second-div" >
Second div content here...
</div>
</div>
<style>
#wrapper {
width:100%;
margin : 0;
}
#first-div {
width:50%;
margin : 0;
float : right ;
padding: 10px;
background-color: green;
color: white;
box-sizing: border-box;
}
#second-div {
width:50%;
margin : 0;
float : right ;
background-color: blue;
color: white;
padding: 10px;
box-sizing: border-box;
}
</style>
You should said your div box-sizing as border-box
than it take padding after border internally.
It is because you are keeping the width at 50% and the extra padding is added additionally to that width making the element collapse to a new line (due to float: left)
This should not happen if you apply box-sizing: border-box to each div. This ensures that padding is included within the width of the element
try to use display flex instead float:
#wrapper {
width:100%;
margin : 0;
display: flex;
justify-content: flex-start;
flex-direction: column;
}
#first-div {
width:50%;
margin : 0;
background-color: green;
color: white;
padding: 10px;
}
#second-div {
width:50%;
margin : 0;
background-color: blue;
color: white;
padding: 10px;
}

How can I horizontally expand my footer div?

I want my footer div to take all the horiziontal space available. Here is my code, but it doesn't work. Hope you can help me!
HTML
<div id=footer>
NewCom France Copyright &copy 2020
</div>
CSS
#footer {
padding: 12px;
background-color: #999999;
text-align: center;
position: fixed;
bottom: 0;
text-align: center;
margin: 0 auto;
line-height: auto;
}
Margin on a fixed-position element does nothing, and margin on body won't affect it either, as position: fixed positions an element relative to the viewport, outside of the flow of any other elements.
To ensure full width, a fixed-position element will need to have its right and left properties set.
#footer {
/* positioning */
bottom: 0;
left: 0;
position: fixed;
right: 0;
/* other styling */
background-color: #999999;
line-height: auto;
padding: 12px;
text-align: center;
}
As a side note, you have duplicate text-align: center values in your original CSS.
Ordering your CSS properties in a consistent way (for instance, I like to alphabetize my CSS properties, but in the example above also grouped them by type) will help you avoid potentially hard-to-find bugs or duplicate properties.
Is there enough content to fill up 100vh? You can just set your footer to fill that space if not, or set your body height or min-height
I recommend a flexbox layout where the footer does not expand but rather the main content but here is what you are looking for:
body {
display: flex;
flex-direction: column;
background-color: gray;
height: 100vh;
margin: 0;
padding: 0;
}
header {
background-color: red;
}
main {
background-color: blue;
}
footer {
flex: 1 auto;
background-color: green;
}
<body>
<header>
<p>Hi in the header</p>
</header>
<main>
<p>Hi in the main</p>
</main>
<footer>
<p>Hi in the footer</p>
</footer>
</body>

will top margin and text align work for display-inline-block

I know both the left and right margins will work for display: inline and display-inline: block. But please clarify if top margin and text-align works for any of these. If yes, why?
Browsers treat inline elements differently when it comes to margins and padding. While you can add left and right margins/padding, you can not add them to the top or bottom of the element. This is because inline elements flow with the content on a page, the same way as a link or text. If you were able to set top/bottom margin/padding in inline elements, it would disrupt the flow of content.
As for text-align, this works on both inline-block, and inline elements. I have added a quick code example below showing text-align: center; on both display: inline; and display: inline-block; elements. This example also shows the top and bottom margins working for inline-block, and not working for inline.
.inline-block {
display: inline-block;
margin-top: 30px;
margin-bottom: 10px;
}
.inline {
display: inline;
margin-top: 30px;
margin-bottom: 10px;
}
/* Start demo styles (not required) */
* {
box-sizing: border-box;
}
html,body {
margin:0;
background: #95a5a6;
}
.container {
width:50%;
margin: 50px auto;
background: white;
padding: 10px 20px;
text-align: center;
font-family: arial;
font-size: 16px;
}
hr {
border: none;
height: 2px;
width: 100%;
background: black;
display: block;
}
/* End demo styles */
<div class="container">
<div class="inline-block">
Inline-Block Content
</div>
<hr>
<div class="inline">
Inline Content
</div>
</div>

Removing the default 8px margin from body

This question have been asked before I'm aware of, but unfortunately still haven't found a solution. Even though I tried using their solutions.
My problem is that I can't get rid of the default 8px margin on around the body/html tag. It looks like that it doesn't even respond to the changes I put in between the curly brackets.
Html:
<div class="header">
Header123
</div>
<div class="nav">
<ul>
<li>HOME</li>
<li>PROJECT</li>
</ul>
</div>
<div class="main">
Article
</div>
<div class="footer">Footer</div>
CSS:
body,html {
margin: 0;
text-align: center;
background-color: blue;
}
.container
{
margin: 0;
background-color: red;
}
.header
{
text-align: left;
height: 90px;
background-color: green;
margin: 0;
}
.nav
{
float: right;
}
.nav ul
{
list-style: none;
height: 30px;
padding: 0px;
margin: 0px;
}
.nav ul li
{
margin: 10px;
}
.footer
{
height:40px;
}
Link: https://jsfiddle.net/RasmusGodske/yg2gsa0t/
For HTML:
<body style="margin:0;padding:0">
For CSS:
body {margin:0;padding:0}
Probably you are having a difficulty because margin and padding are two different things. You should try both and use the one that suits your needs.
You can read more about margin vs padding from here
Remember: margin is outside an element's border; padding is inside.
You need to set the padding of the body element to zero. (The margin of the body element is meaningless; it'd represent a space outside the browser window!)
As an aside, you don't need to set any CSS properties on the html element. body already contains everything that you'd want to set properties on.
You can directly remove the margin using:
body { margin: 0; }

Float + Padding = Trouble

I have this problem where one element is floated and I cannot set a padding to the other element on the same line because it appears in the beginning and not where I want it to.
FIDDLE
HTML
<header>
<h1>John Doe</h1>
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Game</li>
<li>Contact</li>
</ul>
</nav>
</header>
<div class='body'>
<section class="body_heading">
<h2>About Me</h2>
</section>
<aside>
<section>Lorem ipsum</section>
</aside>
<section class="body_content">lorem ipsum</section>
</div>
CSS
html, body {
min-height: 100%;
}
body {
background: #f1f1f1;
}
header {
background: #CAE5FF;
float:left;
height: 100%;
color: gray;
}
header > * {
padding: 5% 15%;
}
header > nav {
}
header > nav > ul {
list-style: none;
padding: 0;
margin: 0;
}
header > nav > ul > li {
}
.body {
padding: 1%;
}
aside {
float: right;
}
The problem is between the header and the .bodyelement I want to be a little space but padding doesn't affect it.
Use below Options
.body{overflow:hidden;} /* or */
.body{margin-left:125px;} /* width of left contents + 10 or 20 for some space */
The padding of 1% that you have specified on the div.body is working perfectly fine.
The problem lies in the fact that the header is floated and does not have a width. Also, the div.body is not floated. So, the div.body takes up all the width while the content gets shifted because of the left floated header.
This can be seen here: http://jsfiddle.net/2fyqqwjx/1/
See the green border of div.body ?
A very simple solution (with your current markup) would be to give a width to the header and provide an equivalent margin-left to your div.body:
* { box-sizing: border: box; }
html, body {
height: 100%; overflow: hidden;
}
header {
float: left;
height: 100%;
width: 20%; /* give a fixed width here */
}
.body {
padding: 1%; margin-left: 20%; /* give a margin-left equivalent to header width */
}
See this demo: http://jsfiddle.net/uc0rh9d0/1/
.
.body{padding-left:35% //whatever you want }
Give float:left; to .body it will be enough
Add margin-right to your header in CSS.
Here is the example
header {
background: #CAE5FF;
float:left;
height: 100%;
color: gray;
margin-right: 10px;
}