I am trying to create a very simple html/css to get the basics correct. I have a couple of problems:
- The header has a top/bottom margin. Why? shouldn’t it be 0?
- The footer has a top/bottom padding. Again, shouldn’t it be 0?
- Setting the body to have a margin/padding of 0 seems to have no effect. Why?
- I’m trying to get the vertical scroll to scroll only
<main>
and leave the header & nav always visible (footer appears to always be visible). What else do I need to do?
body {
margin: 0 0 0 0;
padding: 0 0 0 0;
}
.my-header {
background-color: lightcoral;
margin: 0;
padding: 0;
}
.my-nav {
background-color: lightgreen;
margin: 0;
padding: 0;
}
nav ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
nav li {
float: left;
padding: 0px 16px 0px 16px;
}
.my-main {
flex: 1;
overflow: auto;
height: 100%;
background-color: lightyellow;
margin: 0;
padding: 0;
}
.my-footer {
z-index: 100;
background-color: lightblue;
margin: 0;
padding: 0;
position: fixed;
bottom: 0;
width: 100%;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="main.css" />
<title>Basics</title>
</head>
<body>
<header class="my-header">
<p>Nicolay Copy</p>
</header>
<nav class="my-nav">
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li style="float:right"><a class="active" href="#about">About</a></li>
</ul>
</nav>
<main class="my-main">
<p>Four score and seven years ago our fathers brought forth, upon this continent, a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.</p>
<p>Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived, and so dedicated, can long endure. We are met on a great battle field of that war. We come to dedicate a portion of it, as a final resting place for those who died here, that the nation might live. This we may, in all propriety do.</p>
<p>But, in a larger sense, we can not dedicate we can not consecrate we can not hallow, this ground The brave men, living and dead, who struggled here, have hallowed it, far above our poor power to add or detract. The world will little note, nor long remember what we say here; while it can never forget what they did here.</p>
<p>It is rather for us, the living, we here be dedicated to the great task remaining before us that, from these honored dead we take increased devotion to that cause for which they here, gave the last full measure of devotion that we here highly resolve these dead shall not have died in vain; that the nation, shall have a new birth of freedom, and that government of the people, by the people, for the people, shall not perish from the earth.</p>
</main>
<footer class="my-footer">
<p>Lincoln speech text is in the public domain; the organization, remaining text, and photo on this page are copyright 2020 Abraham Lincoln Online. All rights reserved.</p>
</footer>
</body>
</html>
2
Answers
use the *{} universal selector for more info read:- https://developer.mozilla.org/en-US/docs/Web/CSS/Universal_selectors
and only main body scroll and header or footer are always visible..
header and footer set position sticky make sure add one property- top/right/left/right
https://developer.mozilla.org/en-US/docs/Web/CSS/position
Most of your problems come from what @Azu explains in the comments: Some html tags have default styles set by the browser. These styles come from the user agent stylesheet. To make their life easier, many developers like using a CSS Reset, which is simply a set of CSS rules that are added before your own stylesheet and that take care of clearing out some of the decisions the browser took (for example, the default body margin). Some popular CSS Resets are normalize.css or the Meyer Reset.
Right now, you are still seeing some margins because the user agent stylesheet gives
<p>
tags a top and bottom margin. You can either set all<p>
tags to not have a margin or use a CSS Reset to do it for you.I am unsure what you mean with regards to giving the body a margin/padding of 0 not having an effect. I am currently running your code on Chrome and your definition of:
Has the intended effect of removing the default 8px of margin that Chrome gives the body tag.
With regards to your 4th point, I am unsure what you mean but I can see two possibilities:
Option 1: Set the <main> as the only scrollable element
By default, the body is the element which can be scrolled, however you can set any element to scroll its contents. This is achieved with the
overflow
css property. In your case, you would need to disable scroll on the body, and then set your main to be scrollable (you must also give it a maximum height, otherwise it will just adjust its height to always fit the total content without a need for scroll). The tricky part of this is getting to know what that max-height should be, as it needs to be the total height of the window minus the header and footer. Javascript helps here, but for simplicity’s sake, we can set an arbitrary height in pixels:This goes most of the way, except you set your footer with a fixed position. As I understand, you are new to css, so I recommend you read about CSS Normal Flow. In summary, this is the way css lays out content one on top of the other. However, certain values of the
position
property, take an element out of that normal flow and allow it to be on top of other elements (as is the case with fixed). Therefore, we must remove that part of your css:This would be one way to achieve what I believe you wish in your 4th point.
Option 2: Make the header and footer stick as you scroll the body
Another option would be to keep the normal body scroll but simply define that both the header and footer should always stay visible during scrolling. This could be done with fixed position as you did before but I would recommend instead using sticky position. This has the benefit that the element still occupies space in the normal flow, so nothing will overlap unless you are currently scrolling. Starting from your css (and ignoring the previous option) you would need:
I hope this clears up some of your questions! Like some pointed out in the comments, you can use the Web Inspector on your browser to check out which css rules are being applied, so you can better debug your styles.