HTML/CSS Position sticky is not working properly - html

I am trying to make a div position: sticky; but it doesn't work, here's my code:
body {
height: 1000px;
}
header {
position: absolute;
width: 100%;
z-index: 100;
}
.top-navbar {
height: 100px;
background-color: green;
}
nav {
position: -webkit-sticky !important;
position: sticky;
top: 0;
align-self: flex-start;
background-color: orange;
}
<header>
<div class="top-navbar">
<h2>top navbar</h2>
</div>
<nav>
<h1>My navbar</h1>
</nav>
</header>

Temani has a great solution, but I wanted to point out the reason your solution isn't working. Position sticky is "relative to its nearest scrolling ancestor and containing block (nearest block-level ancestor)". In your case, that's the header, and the nav is already stuck to it. However, if you move the nav below the header (as in the example below), you'll see it works:
body {
height: 1000px;
}
.top-navbar {
height: 100px;
background-color: green;
}
nav {
position: sticky;
top: 0;
background-color: orange;
}
<header>
<div class="top-navbar">
<h2>top navbar</h2>
</div>
</header>
<nav>
<h1>My navbar</h1>
</nav>

it seems you only want to keep the nav visible on scroll. In this case do it like below. Move sticky to header and consider a negative value for top equal to the height of top-navbar
body {
height: 1000px;
}
header {
position: sticky;
top: -100px;
}
.top-navbar {
height: 100px;
background-color: green;
}
nav {
background-color: orange;
}
h1,h2 {
margin:0;
}
<header>
<div class="top-navbar">
<h2>top navbar</h2>
</div>
<nav>
<h1>My navbar</h1>
</nav>
</header>

Related

Fixed navbar hiding header

Whenever I set my navbar to fixed and my header height is 100vh, my header seems to be under the navbar. How can I make the navbar not cover my header and make header 100vh
.nav {
height: 80px;
position: fixed;
top: 0;
left: 0;
width: 100%;
background: red;
}
.hero {
background: blue;
height: 100vh;
margin-top: 80px;
}
* {
margin: 0;
}
<div class="nav">
</div>
<div class="hero">
</div>
Use position: sticky instead of position: fixed. This way your main container will always be 100vh.
In css I made edits.
* {
margin: 0;
}
.nav {
height: 80px;
position: sticky;
top: 0;
/*left: 0;*/
width: 100%;
background: red;
}
.hero {
background: blue;
min-height: 100vh;
/*margin-top: 80px;*/
}
<div class="nav">
</div>
<div class="hero">
</div>
Try setting the nav to position:sticky; instead
example: https://jsfiddle.net/sq5ae3u1/

Div elements in position relative will not stack

In summary, I made a container div (parent) with a position: relative, then added 3 children divs with position: absolute. I am now trying to add another div that is below all of this, i.e. the next section of a website. But now the next div appears under the first main "parent" div. From endless searching on here and google I though a main div with position relative would not destroy the flow, but obviously it did or else I would't be posting.
I now want to have another div outside of the parent so that it will go under this first div and make for a nice, scolling website. Please look at my CSS and help me understand why the absolute elements inside a relative element messed up the flow. (I'm new to CSS, so any pro tips are appreciated!)
Here is an image of the website so you get a feel
*{
margin: 0;
padding: 0;
border: 0;
}
body {
font-family: 'Noto Sans HK', sans-serif;
}
/* Arranging the parent and child elements so
images can overlap */
.child {
top: 0;
}
.child-1 {
position: absolute;
left: 0;
z-index: 100;
}
.child-2 {
position: absolute;
right: 0;
z-index: 1;
}
.child-3 {
position: absolute;
padding-top: 38%;
left: 0;
}
#parent {
position: relative;
height: auto;
}
.hero-text {
position: absolute;
text-align: center;
right: 10vw;
top: 28vw;
z-index: 9;
font-size: 3.5vw;
float: right;
color: white;
}
/* Responsive viewport area,
Logo resize based on the screen size */
#logo_png {
max-width: 25vw;
}
#hero_img {
max-width: 85vw;
}
#green_circle_png {
max-width: 40vw;
}
/* NAV BAR STYLING */
#container {
position: absolute;
z-index: 900;
width: 95%;
margin: 0 auto;
}
nav {
float: right;
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
margin-left: 5vw; /* margin-left obly touches the left margin, not L & R */
padding-top: 25px;
position: relative;
}
nav a {
color: white;
text-decoration: none;
text-transform: uppercase;
font-size: 1.4vw;
}
nav a:hover {
color: black;
}
.p1 {
color: #f5f7ff;
font-size: 10vw;
}
#test {
position: relative;
}
<body>
<div id="parent">
<div class="child child-1">
<h1>
<a href='THIS WILL BE LINK TO HOME PAGE'>
<img id="logo_png" src="C:\Users\rebec\Desktop\LBS WEBSITE\Images\lbs_blue_circle_logo_1500x1500.png" alt="Little Big Scientists"/>
</a>
</h1>
</div>
<div class="child child-2">
<h1>
<img id="hero_img" src="Images/circle_hands_lbsphoto.png" alt="Little Big Scientists"/>
</h1>
</div>
<div class="child child-3">
<h1>
<img id="green_circle_png" src="Images/green_circle_lbswebsite.png" alt="Little Big Scientists"/>
</h1>
</div>
<div class="hero-text">
<p>We’re on a mission to teach,
<br>guide, and empower the next
<br> generation of scientists
</p>
</div>
<!-- Div for Nav Bar-->
<div id="container">
<nav>
<ul>
<li>Mission</li>
<li>About</li>
<li>Events</li>
<li>Donate</li>
<li>Contact</li>
</ul>
</nav>
</div>
</div>
<div id="test">
<h2 class="p1">Inspiring Education</h2>
</div>
<h2 class="p1">HELP MEEEE</h2>
</body>
I don't think there's necessarily anything wrong with using absolute positioning for some elements. The main problem you are experiencing is that because everything inside #parent is absolute positioning #parent collapses and has zero height. If you want to do overlapping circles, absolute positioning is a valid way to do it, but you have to expressly set a height for #parent.
Below is a modified copy of your code. I want to emphasize it is a very rough starting point, and by no means is it complete, but I think it demonstrates some of the things you can do. Even with absolute positioning of the circle elements it is still fairly responsive and it could be made fully responsive by adding appropriate media queries to the css.
*{
margin: 0;
padding: 0;
border: 0;
box-sizing: border-box;
}
body {
font-family: 'Noto Sans HK', sans-serif;
}
/* Arranging the parent and child elements so
images can overlap */
.child {
position: absolute;
}
.child-1 {
top: -75px;
left: -75px;
z-index: 100;
width: 300px;
height: 300px;
max-width: 50vw;
max-height: 50vw;
background: blue;
border-radius: 50%;
}
.child-1 h1 {
position: absolute;
right: 10%;
bottom: 20%;
background: white;
}
.child-2 {
right: -40vw;
top: -50vw;
z-index: 1;
width: 120vw;
height: 120vw;
background: center / contain no-repeat url("./Images/circle_hands_lbsphoto.png"), content-box linear-gradient(lightgray, lightgray);
border-radius: 50%;
}
.child-3 {
top: 60vh;
left: -5vw;
width: 550px;
height: 550px;
max-width: 50vw;
max-height: 50vw;
border-radius: 50%;
background: lightgreen;
}
#parent {
position: relative;
min-height: 100vh;
height: 550px;
width: 100vw;
overflow: hidden;
}
.hero-text {
position: absolute;
text-align: center;
left: 40%;
transform: translateX(-50%);
top: 60%;
z-index: 9;
font-size: 3.5vw;
color: white;
}
/* Responsive viewport area,
Logo resize based on the screen size */
#logo_png {
max-width: 25vw;
}
#hero_img {
max-width: 85vw;
}
#green_circle_png {
max-width: 40vw;
position: absolute;
bottom: 20%;
left: 20%;
}
/* NAV BAR STYLING */
#container {
z-index: 900;
width: 100%;
margin: 0 auto;
position: sticky;
top: 0;
background: #fff;
}
nav {
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
margin-left: 5vw; /* margin-left obly touches the left margin, not L & R */
padding-top: 25px;
position: relative;
}
nav a {
color: black;
text-decoration: none;
text-transform: uppercase;
font-size: 1.4vw;
}
nav a:hover {
color: black;
}
.p1 {
color: #f5f7ff;
font-size: 10vw;
}
#test {
position: relative;
}
<body>
<div id="parent">
<div class="child child-1">
<h1>
<a href='THIS WILL BE LINK TO HOME PAGE'>
<img id="logo_png" src="C:\Users\rebec\Desktop\LBS WEBSITE\Images\lbs_blue_circle_logo_1500x1500.png" alt="Little Big Scientists 1"/>
</a>
</h1>
</div>
<div class="child child-2">
<img id="hero_img" alt="Little Big Scientists 2"/>
<div class="hero-text">
<p>We’re on a mission to teach,
<br>guide, and empower the next
<br> generation of scientists
</p>
</div>
</div>
<div class="child child-3">
<h1>
<img id="green_circle_png" src="Images/green_circle_lbswebsite.png" alt="Little Big Scientists 3"/>
</h1>
</div>
</div>
<!-- Div for Nav Bar-->
<div id="container">
<nav>
<ul>
<li>Mission</li>
<li>About</li>
<li>Events</li>
<li>Donate</li>
<li>Contact</li>
</ul>
</nav>
</div>
<div id="test">
<h2 class="p1">Inspiring Education</h2>
</div>
<h2 class="p1">HELP MEEEE</h2>
</body>

How to make sidebar appear in front of header?

How to make sidebar appear in front of header?
.header {
z-index: -10;
margin: auto;
width: auto;
height: 70px;
background-color: black;
}
.sidebar {
position: relative;
z-index: 10;
margin: auto;
height: 1335px;
width: 65px;
background-color: red;
}
<div id="wrapper">
<header>
<div class="header"></div>
<div class="sidebar"></div>
</header>
</div>
In your code, both elements are within the document flow. So the sidebar appears after the header, not overlapping. One method of making them overlap is to set one of them to position:absolute and remove it from the document flow.
body {
margin: 0;
}
.header {
height: 70px;
background-color: black;
}
.sidebar {
position: absolute;
top: 0;
left: 50%;
margin-left: -32.5px;
width: 65px;
height: 1335px;
background-color: red;
}
<div id="wrapper">
<header>
<div class="header"></div>
<div class="sidebar"></div>
</header>
</div>
For further reference, see:
Visual formatting model
Positioning

Absolutely positioned element falling outside of its parent container

I'm working on a project where I need to float the previous and next navigation elements to either side of a blog archive page title (green circles for this example). Sitting inside the green circle will be a span with an SVG background element - so the circle needs to be positioned.
I wanted to keep things semantic, so I've laid out my page (section) header as follows:
<header class="archive-box">
<nav class="archive-nav">
<div class="left-nav">
<a class="icon-bg" href="#" title="">
</a>
</div>
<div class="right-nav">
<a class="icon-bg" href="#" title="">
</a>
</div>
</nav>
<h2>Stuff and Things</h2>
</header>
CSS
.archive-box {
max-width: 900px;
height: 75px;
margin: 50px auto;
border: 1px solid;
position: relative;
}
.archive-nav {
position: absolute;
top: 0;
left: 0;
right: 0;
}
.left-nav {
position: absolute;
left: 0;
}
.right-nav {
position: absolute;
right: 0;
}
.icon-bg {
background-color: #9ccb3b;
border-radius: 50%;
height: 75px;
width: 75px;
position: absolute;
}
h2 {
text-align: center;
}
The right navigation element is going outside of its parent's container. I think it might have something to do with the fact that I've got multiple parent-child absolute elements. Is there another way to do this?
Here's the CodePen
Sometimes you need to have specific order of coding css positions. What i mean is if you paste the whole code and run it, it will be different if you wrote it step by step and saved it. It helped me couple of times when i was learning css.
Also try to put margin:auto in .right-nav and .left-nav
Is this how you want it to be? CodePen. Instead of using position: absolute, I've used float: left and float: right so that the left and the right menu items are positioned on the left and the right side respectively and the title is in the center.
section {
position: relative;
background-color: blue;
color: white;
width: 80%;
margin: auto;
}
article {
background-color: green;
height: 50px;
}
h1 {
color: white;
font-size: 30px;
}
nav {
top: 0;
font-size: 20px;
}
.left {
left: 0;
/* position: absolute; */
color: yellow;
}
.right {
right: 0;
/* position: absolute; */
color: pink;
}
h1 {
text-align: center;
}
.bg {
float: left
}
.bgtwo {
float: right;
}
<section>
<nav>
<div class="bg">
<div class="left">LEFT</div>
</div>
<div class="bgtwo">
<div class="right">RIGHT</div>
</div>
</nav>
<h1>Hello There</h1>
<article>
<p>Here is some stuff about things.</p>
</article>
</section>

make content div fill remaining height

I have a page layout in which I have a fixed header which can have any height and a footer positioned at the bottom of the page. I'm looking for a css solution so that the content div fills the remaining space (vertically). In the jsfiddle below I've tried to do this, but as you can see the content is behind the footer.
HTML:
<main>
<header>
<ol>
<li>bar</li>
<li>foo</li>
</ol>
</header>
<section>
<div class="content"><div>
</section>
<footer></footer>
</main>
CSS:
header {
background-color: #abc;
z-index: 1000;
position: fixed;
top: 0px;
right: 0px;
left: 0px;
}
html, body, main, section {
height: 100%;
display: block;
}
.content{
background-color: #000;
height: 100%;
}
footer {
background-color: #def;
bottom: 0;
display: block;
height: 54px;
position: absolute;
width: 100%;
}
Is this possible with pure css(3) ?
jsfiddle
It is a bit of an ugly solution, but if you make the margin-top of the content div as -54px and add a div inside it with padding-top:54px, it works as expected.
HTML:
<div class="content"><div class="contentwrapper"></div><div>
CSS:
.contentwrapper {
padding-top:54px;
}
.content{
background-color: #000;
height: 100%;
margin-top:-54px;
}
Fiddle: http://jsfiddle.net/dohqn8m4/1/
Here a diffrent approach:
HTML:
<header>
<ol>
<li>bar</li>
<li>foo</li>
</ol>
</header>
<main>
<section>
<div class="content"></div>
</section>
<div class="push"></div>
</main>
<footer></footer>
CSS:
html, body {
height: 100%;
min-height: 100%;
margin: 0;
padding: 0;
border: 0;
}
header {
background-color: #abc;
z-index: 1000;
position: fixed;
top: 0;
right: 0;
left: 0;
}
main {
min-height: 100%;
height: auto !important;
margin-bottom: -54px;
}
main > section{
padding-top: 72px;
}
.content {
background-color: #000;
}
.push {
height: 54px;
}
footer {
background-color: #def;
height: 54px;
}
Now the footer is always at the bottom aslong the content doesn't fill the hole page. In that case the "push" element provides enough space to deny overlapping of footer and content.
Your content div ist now placed under the footer through the padding. The height is actually 0 because of missing content. In my approach the content div fits always the content inserted.
Keep in mind that
a) for responsive purpose you had to know about the header height and adjust the padding of the section using media queries
b) the same for the footer. Adjust the height of the push element and adjust the margin-bottom value.
jsFiddle
Try positioning the content to be right above the footer
bottom: <footer-height>;
position: absolute;
I made sticky footer using this tutorial. I think it's easy and convenient to use.
CSS CODE
html {
position: relative;
min-height: 100%;
}
body {
margin: 0 0 100px; /* bottom = footer height */
}
footer {
position: absolute;
left: 0;
bottom: 0;
height: 100px;
width: 100%;
}
HTML CODE
<!DOCTYPE html>
<head>
<title></title>
</head>
<body>
<nav></nav>
<article>Lorem ipsum...</article>
<footer></footer>
</body>
</html>
DEMO URL