Sticky navigation breaks when encounters floated content? - html

For instructional purposes, I'm working with the most basic of CSS. I have a navigation bar set to position: sticky which works great UNTIL it interacts with a set of floated columns further down the page.
gif of site scrolling and breaking nav
I'm unsure if it's because of the float, or some poor calculation regarding the simplicity of my layout. Right now, the 2-column layout is made with:
#main-menu {
width: 100%;
height: auto;
text-align: right;
position: sticky;
position: -webkit-sticky;
top: 0px;
z-index: 2;
}
.column {
width: 50%;
float: left;
}
Using an inline-block display works and does not affect the sticky menu, but as expected I can not set them to 50% width. A fix would be great, but also an explanation on why I'm experiencing this.
Thank you!
#main-menu {
width: 100%;
height: auto;
background-color: #222222;
line-height: 2em;
text-align: right;
position: sticky;
position: -webkit-sticky; /* Safari */
top: 0px;
z-index: 2;
}
.site-name {
float: left;
margin: auto 1em;
padding: 0.5em 1em;
}
#main-menu a {
color: #FFFFFF;
text-decoration: none;
}
#main-menu .site-name a:hover {
color: #00BBBB;
}
.menu-item {
min-width: 2em;
text-align: center;
display: inline-block;
padding: 0.5em 1em;
}
.menu-item:hover {
background-color: #00BBBB;
}
#main {
padding: 2em 4em;
clear: both;
}
.column-lg {
width: 50%;
float: left;
}
<h1>Site Above Fold Content</h1>
<nav id="main-menu">
<div class="site-name">
Title
</div>
<div class="menu-item">
L1
</div>
<div class="menu-item">
L2
</div>
</nav>
<h2>Under Fold Content (Before Floated Columns)</h2>
<p>Gentrify woke irony +1 tote bag lo-fi drinking vinegar. Bushwick YOLO retro pinterest cloud bread skateboard. Small batch retro twee scenester roof party humblebrag celiac 8-bit direct trade franzen flannel cray. Kogi knausgaard godard selfies umami deep v, woke whatever 8-bit prism cred.</p>
<br><br>
<h3>BYE-BYE NAV!!</h3>
<div class="column-lg">
<h2>Lorem Ipusm</h2>
<div class="row">
<h3>1 Title Impsum Amet</h3>
<p>Lorem ipsum dolor amet bitters ethical microdosing, narwhal jean shorts venmo umami YOLO 90's trust fund activated charcoal lomo pok pok hammock. Man bun marfa blog narwhal letterpress food truck. Umami forage disrupt, snackwave DIY mlkshk aesthetic kogi bitters vice.</p>
</div>
<div class="row">
<h3>2 Title Impsum Amet</h3>
<p>2 Vegan williamsburg jianbing, gluten-free tote bag try-hard mixtape yuccie +1 everyday carry shabby chic umami vexillologist pop-up edison bulb. Whatever everyday carry listicle, coloring book hell of microdosing gastropub banh mi yuccie tumblr art party. Aesthetic hammock kitsch microdosing, viral biodiesel tumblr cliche beard readymade seitan. Copper mug chambray street art raclette shaman fam neutra.</p>
</div>
</div>
<div class="column-lg">
<h2>Lorem Ipusm</h2>
<div class="row">
<p>Gentrify woke irony +1 tote bag lo-fi drinking vinegar. Bushwick YOLO retro pinterest cloud bread skateboard. Small batch retro twee scenester roof party humblebrag celiac 8-bit direct trade franzen flannel cray. Kogi knausgaard godard selfies umami deep v, woke whatever 8-bit prism cred. Intelligentsia heirloom keytar, hot chicken synth tote bag vaporware williamsburg pok pok kickstarter 3 wolf moon selvage hoodie trust fund cronut. Occupy bicycle rights drinking vinegar small batch, vaporware taxidermy flannel live-edge marfa.</p>
</div>
</div>

The floated elements are no longer in the normal flow of the DOM, reducing the overall height of the body. Using inspector, you can see this (blue representing the height of the body):
Thus, as you encounter the floats, your position: sticky which is relative to the body, will appear to scroll.
A "fix" is to clear your floats. I applied the following clearfix to the body:
body:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
Here's the snippet with demo:
body:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
#main-menu {
width: 100%;
height: auto;
background-color: #222222;
line-height: 2em;
text-align: right;
position: sticky;
position: -webkit-sticky; /* Safari */
top: 0px;
z-index: 2;
}
.site-name {
float: left;
margin: auto 1em;
padding: 0.5em 1em;
}
#main-menu a {
color: #FFFFFF;
text-decoration: none;
}
#main-menu .site-name a:hover {
color: #00BBBB;
}
.menu-item {
min-width: 2em;
text-align: center;
display: inline-block;
padding: 0.5em 1em;
}
.menu-item:hover {
background-color: #00BBBB;
}
#main {
padding: 2em 4em;
clear: both;
}
.column-lg {
width: 50%;
float: left;
}
<h1>Site Above Fold Content</h1>
<nav id="main-menu">
<div class="site-name">
Title
</div>
<div class="menu-item">
L1
</div>
<div class="menu-item">
L2
</div>
</nav>
<h2>Under Fold Content (Before Floated Columns)</h2>
<p>Gentrify woke irony +1 tote bag lo-fi drinking vinegar. Bushwick YOLO retro pinterest cloud bread skateboard. Small batch retro twee scenester roof party humblebrag celiac 8-bit direct trade franzen flannel cray. Kogi knausgaard godard selfies umami deep v, woke whatever 8-bit prism cred.</p>
<br><br>
<h3>BYE-BYE NAV!!</h3>
<div class="column-lg">
<h2>Lorem Ipusm</h2>
<div class="row">
<h3>1 Title Impsum Amet</h3>
<p>Lorem ipsum dolor amet bitters ethical microdosing, narwhal jean shorts venmo umami YOLO 90's trust fund activated charcoal lomo pok pok hammock. Man bun marfa blog narwhal letterpress food truck. Umami forage disrupt, snackwave DIY mlkshk aesthetic kogi bitters vice.</p>
</div>
<div class="row">
<h3>2 Title Impsum Amet</h3>
<p>2 Vegan williamsburg jianbing, gluten-free tote bag try-hard mixtape yuccie +1 everyday carry shabby chic umami vexillologist pop-up edison bulb. Whatever everyday carry listicle, coloring book hell of microdosing gastropub banh mi yuccie tumblr art party. Aesthetic hammock kitsch microdosing, viral biodiesel tumblr cliche beard readymade seitan. Copper mug chambray street art raclette shaman fam neutra.</p>
</div>
</div>
<div class="column-lg">
<h2>Lorem Ipusm</h2>
<div class="row">
<p>Gentrify woke irony +1 tote bag lo-fi drinking vinegar. Bushwick YOLO retro pinterest cloud bread skateboard. Small batch retro twee scenester roof party humblebrag celiac 8-bit direct trade franzen flannel cray. Kogi knausgaard godard selfies umami deep v, woke whatever 8-bit prism cred. Intelligentsia heirloom keytar, hot chicken synth tote bag vaporware williamsburg pok pok kickstarter 3 wolf moon selvage hoodie trust fund cronut. Occupy bicycle rights drinking vinegar small batch, vaporware taxidermy flannel live-edge marfa.</p>
</div>
</div>

Related

How is space divided amongst flex items when flex-basis and flex-grow both are set?

I am going through a basic flexbox tutorial from MDN and cant get my head around on how is the remaining space divided amongst the flex item, when flex-basis is set.
In the live demo, I set the following code:
article {
flex: 1 200px;
}
article:nth-of-type(3) {
flex: 2 200px;
}
My browser screen is 1369px, after taking the scrollbar out I am left with 1349px. MDN says that
"Each flex item will first be given 200px of the available space. After that, the rest of the available space will be shared out according to the proportion units."
So the available space after min 200px is given to each article should be 1349px - 3*200px = 749px. Now, the article three should get half of the available space plus 200px, that is 200 + 749/2 = 575.5. Taking 10px margin space out from both sides we get 555.5px, but in my laptop it occupies 535.5px.
Below is the working demo:
* {
box-sizing: border-box;
}
html {
font-family: sans-serif;
}
body {
margin: 0;
}
header {
background: purple;
height: 100px;
}
h1 {
text-align: center;
color: white;
line-height: 100px;
margin: 0;
}
article {
padding: 10px;
margin: 10px;
background: aqua;
flex: 1 200px;
}
/* Add your flexbox CSS below here */
section {
display: flex;
}
article:nth-of-type(3) {
flex: 2 200px;
}
<header>
<h1>Sample flexbox example</h1>
</header>
<section>
<article>
<h2>First article</h2>
<p>Tacos actually microdosing, pour-over semiotics banjo chicharrones retro fanny pack portland everyday carry vinyl typewriter. Tacos PBR&B pork belly, everyday carry ennui pickled sriracha normcore hashtag polaroid single-origin coffee cold-pressed. PBR&B tattooed trust fund twee, leggings salvia iPhone photo booth health goth gastropub hammock.</p>
</article>
<article>
<h2>Second article</h2>
<p>Tacos actually microdosing, pour-over semiotics banjo chicharrones retro fanny pack portland everyday carry vinyl typewriter. Tacos PBR&B pork belly, everyday carry ennui pickled sriracha normcore hashtag polaroid single-origin coffee cold-pressed. PBR&B tattooed trust fund twee, leggings salvia iPhone photo booth health goth gastropub hammock.</p>
</article>
<article>
<h2>Third article</h2>
<p>Tacos actually microdosing, pour-over semiotics banjo chicharrones retro fanny pack portland everyday carry vinyl typewriter. Tacos PBR&B pork belly, everyday carry ennui pickled sriracha normcore hashtag polaroid single-origin coffee cold-pressed. PBR&B tattooed trust fund twee, leggings salvia iPhone photo booth health goth gastropub hammock.</p>
<p>Cray food truck brunch, XOXO +1 keffiyeh pickled chambray waistcoat ennui. Organic small batch paleo 8-bit. Intelligentsia umami wayfarers pickled, asymmetrical kombucha letterpress kitsch leggings cold-pressed squid chartreuse put a bird on it. Listicle pickled man bun cornhole heirloom art party.</p>
</article>
</section>
Please explain how is space divided amongst flex items when flex-basis and flex-grow both are set?
If I do article:nth-of-type(3) { flex: 2; } it doesn't give double space to article 3, why?
Margin should be considered initially and removed from the total width in order to calculate the free space. Not later, like you are doing.
So all your elements start with 200px width and 20px margin. The free sapce will be 1350px - 3*200px - 3*20x = 690px. Now, the article three should have a width equal to 2/4*690px + 200px = 545px (and not 555px)
without box-sizing you will have 1350px - 3*220px - 3*20x = 630px and the width will be 535px

Can I limit the width of text inside of a <p>?

In Markdown images are wrapped inside of paragraphs. I'd like images to be wider than text and fill the container's larger width. How can I solve this? Here is an example: https://sidey.now.sh/2019/08/31/difference-between-font-formats/
Ideally the text would only have a width of 40rem.
You can solve this using CSS grid:
.container {
margin:0 50px;
border:1px solid;
}
.container >p {
display:grid;
grid-template-columns:1fr minmax(0,20rem) 1fr;
}
/* this pseudo element will take the first column and force the text to be on the middle one */
p::before {
content:"";
}
/**/
p > img {
grid-column:1/span 3; /* the image should take all the 3 columns (full width) */
}
img {
max-width:100%;
}
<div class="container">
<p>Lorem ipsum dolor amet tousled viral art party blue bottle single-origin coffee cardigan, selvage man braid helvetica. Banh mi taxidermy meditation microdosing. Selvage cornhole YOLO, small batch vexillologist raclette VHS prism sustainable 8-bit ugh semiotics letterpress disrupt pop-up. Celiac shabby chic ugh, jianbing whatever kitsch tattooed edison bulb kogi irony etsy.</p>
<p><img src="https://images.unsplash.com/photo-1580792214064-6102bf1948d5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2167&q=80" alt="image"></p>
<p>Lorem ipsum dolor amet tousled viral art party blue bottle single-origin coffee cardigan, selvage man braid helvetica. Banh mi taxidermy meditation microdosing. Selvage cornhole YOLO, small batch vexillologist raclette VHS prism sustainable 8-bit ugh semiotics letterpress disrupt pop-up. Celiac shabby chic ugh, jianbing whatever kitsch tattooed edison bulb kogi irony etsy.</p>
</div>

filling/sizing empty div in bootstrap

Hi I´m working on this bootstrap grid below.
I want this first column col-1 to have the same height as the two columns at its right side.
max-heightdoesn't seems to do the trick and I've been using:
.col-1{
background-color: #555;
margin-bottom: -600px;
padding-bottom: 600px;
}
to get the look I want, but then part of the div ends behind the other div´s when the site is scaled down.
Can anyone advise me how I can get the result I want?
I somehow believe that there must be another way to do this than I'm using which is to adjust the margin-bottomand padding-bottom
A link to fiddle
/* GRID */
.col-1 {
background-color: #555;
margin-bottom: -600px;
padding-bottom: 600px;
}
.col-2 {
background-color: #558C89;
}
.col-3 {
background-color: #74AFAD;
}
.col-4 {
background-color: #D9853B;
}
.col-5 {
background-color: #ECECEA;
}
.col-6 {
background-color: #F3FAB6;
}
.col-7 {
background-color: #CBE32D;
}
.col-8 {
background-color: #A8CD1B;
}
.col-9 {
background-color: #005A31;
}
/****************************/
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<div class="container">
<div class="row">
<div class="col-md-6 col-1">
Some text here
</div>
<div class="col-md-6">
<div class="row">
<div class="col-md-6 col-2">
<p>Celiac 3 wolf moon williamsburg pitchfork, squid helvetica single-origin coffee brooklyn paleo vice biodiesel. Fingerstache celiac mumblecore, try-hard yuccie helvetica chartreuse. Chartreuse wayfarers vegan, blue bottle bicycle rights selfies
salvia ugh drinking vinegar 3 wolf moon you probably haven't heard of them. Cardigan fingerstache freegan 3 wolf moon vegan. Venmo umami 8-bit, cronut raw denim direct trade locavore vinyl seitan plaid tousled.</p>
</div>
<div class="col-md-6 col-3">
<p>Celiac 3 wolf moon williamsburg pitchfork, squid helvetica single-origin coffee brooklyn paleo vice biodiesel. Fingerstache celiac mumblecore, try-hard yuccie helvetica chartreuse. Chartreuse wayfarers vegan, blue bottle bicycle rights selfies
salvia ugh drinking vinegar 3 wolf moon you probably haven't heard of them. Cardigan fingerstache freegan 3 wolf moon vegan. Venmo umami 8-bit, cronut raw denim direct trade locavore vinyl seitan plaid tousled.</p>
</div>
<div class="col-md-6 col-4">
<p>Celiac 3 wolf moon williamsburg pitchfork, squid helvetica single-origin coffee brooklyn paleo vice biodiesel. Fingerstache celiac mumblecore, try-hard yuccie helvetica chartreuse. Chartreuse wayfarers vegan, blue bottle bicycle rights selfies
salvia ugh drinking vinegar 3 wolf moon you probably haven't heard of them. Cardigan fingerstache freegan 3 wolf moon vegan. Venmo umami 8-bit, cronut raw denim direct trade locavore vinyl seitan plaid tousled.</p>
</div>
<div class="col-md-6 col-5">
<p>Celiac 3 wolf moon williamsburg pitchfork, squid helvetica single-origin coffee brooklyn paleo vice biodiesel. Fingerstache celiac mumblecore, try-hard yuccie helvetica chartreuse. Chartreuse wayfarers vegan, blue bottle bicycle rights selfies
salvia ugh drinking vinegar 3 wolf moon you probably haven't heard of them. Cardigan fingerstache freegan 3 wolf moon vegan. Venmo umami 8-bit, cronut raw denim direct trade locavore vinyl seitan plaid tousled.</p>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-3 col-6">
<p>Celiac 3 wolf moon williamsburg pitchfork, squid helvetica single-origin coffee brooklyn paleo vice biodiesel. Fingerstache celiac mumblecore, try-hard yuccie helvetica chartreuse. Chartreuse wayfarers vegan, blue bottle bicycle rights selfies salvia
ugh drinking vinegar 3 wolf moon you probably haven't heard of them. Cardigan fingerstache freegan 3 wolf moon vegan. Venmo umami 8-bit, cronut raw denim direct trade locavore vinyl seitan plaid tousled.</p>
</div>
<div class="col-md-3 col-7">
<p>Celiac 3 wolf moon williamsburg pitchfork, squid helvetica single-origin coffee brooklyn paleo vice biodiesel. Fingerstache celiac mumblecore, try-hard yuccie helvetica chartreuse. Chartreuse wayfarers vegan, blue bottle bicycle rights selfies salvia
ugh drinking vinegar 3 wolf moon you probably haven't heard of them. Cardigan fingerstache freegan 3 wolf moon vegan. Venmo umami 8-bit, cronut raw denim direct trade locavore vinyl seitan plaid tousled.</p>
</div>
<div class="col-md-3 col-8">
<p>Celiac 3 wolf moon williamsburg pitchfork, squid helvetica single-origin coffee brooklyn paleo vice biodiesel. Fingerstache celiac mumblecore, try-hard yuccie helvetica chartreuse. Chartreuse wayfarers vegan, blue bottle bicycle rights selfies salvia
ugh drinking vinegar 3 wolf moon you probably haven't heard of them. Cardigan fingerstache freegan 3 wolf moon vegan. Venmo umami 8-bit, cronut raw denim direct trade locavore vinyl seitan plaid tousled.</p>
</div>
<div class="col-md-3 col-9">
<p>Celiac 3 wolf moon williamsburg pitchfork, squid helvetica single-origin coffee brooklyn paleo vice biodiesel. Fingerstache celiac mumblecore, try-hard yuccie helvetica chartreuse. Chartreuse wayfarers vegan, blue bottle bicycle rights selfies salvia
ugh drinking vinegar 3 wolf moon you probably haven't heard of them. Cardigan fingerstache freegan 3 wolf moon vegan. Venmo umami 8-bit, cronut raw denim direct trade locavore vinyl seitan plaid tousled.</p>
</div>
</div>
</div>
Actually nowadays with current CSS we have, this below happens to be a ugly hack.
.col-1{
background-color: #555;
margin-bottom: -600px;
padding-bottom: 600px;
}
So, use flexbox:
body,
p {
margin: 0 !important
}
body {
padding-top: 70px;
}
/* NAVBAR */
.navbar-header {
float: none;
}
.navbar-toggle {
display: block;
}
.navbar-collapse.collapse {
display: none!important;
}
.navbar-nav {
float: none!important;
}
.navbar-nav>li {
float: none;
}
.navbar-collapse.collapse.in {
display: block !important;
}
/****************************/
/* GRID */
span.glyphicon {
display: block;
text-align: center;
font-size: 5em;
padding-top: 15%;
}
.container > .row {
display: flex;
flex-wrap: wrap
}
#media (max-width: 992px) {
.container > .row > [class^="col-"] {
flex: 0 0 100%;
}
}
.col-1 {
background-color: #555;
border: solid 5px #fff;
}
.col-2 {
background-color: #558c89;
border: 5px solid #fff;
min-height: 250px;
}
.col-3 {
background-color: #74AFAD;
min-height: 250px;
padding: 5px;
border: solid 5px #fff;
}
.col-4 {
background-color: #D9853B;
min-height: 250px;
padding: 5px;
border: solid 5px #fff;
}
.col-5 {
background-color: #ECECEA;
min-height: 250px;
padding: 5px;
border: solid 5px #fff;
}
.col-6 {
background-color: #F3FAB6;
min-height: 250px;
border: solid 5px #fff;
}
.col-7 {
background-color: #CBE32D;
min-height: 250px;
border: solid 5px #fff;
}
.col-8 {
background-color: #A8CD1B;
min-height: 250px;
border: solid 5px #fff;
}
.col-9 {
background-color: #005A31;
min-height: 250px;
border: solid 5px #fff;
}
/****************************/
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-md-6 col-1 row-eq-height">
Daði Hall
</div>
<div class="col-md-6">
<div class="row">
<div class="col-md-6 col-2 text-center">
<p>Web design</p>
<span class="glyphicon glyphicon-console" aria-hidden="true"></span>
</div>
<div class="col-md-6 col-3">
<p>Blog</p>
<span class="glyphicon glyphicon-text-size" aria-hidden="true"></span>
</div>
<div class="col-md-6 col-4">
<p>CV</p>
<span class="glyphicon glyphicon-paperclip" aria-hidden="true"></span>
</div>
<div class="col-md-6 col-5">
<p>Photos</p>
<span class="glyphicon glyphicon-camera" aria-hidden="true"></span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-3 col-6">
<p>Celiac 3 wolf moon williamsburg pitchfork, squid helvetica single-origin coffee brooklyn paleo vice biodiesel. Fingerstache celiac mumblecore, try-hard yuccie helvetica chartreuse. Chartreuse wayfarers vegan, blue bottle bicycle rights selfies salvia
ugh drinking vinegar 3 wolf moon you probably haven't heard of them. Cardigan fingerstache freegan 3 wolf moon vegan. Venmo umami 8-bit, cronut raw denim direct trade locavore vinyl seitan plaid tousled.</p>
</div>
<div class="col-md-3 col-7">
<p>Celiac 3 wolf moon williamsburg pitchfork, squid helvetica single-origin coffee brooklyn paleo vice biodiesel. Fingerstache celiac mumblecore, try-hard yuccie helvetica chartreuse. Chartreuse wayfarers vegan, blue bottle bicycle rights selfies salvia
ugh drinking vinegar 3 wolf moon you probably haven't heard of them. Cardigan fingerstache freegan 3 wolf moon vegan. Venmo umami 8-bit, cronut raw denim direct trade locavore vinyl seitan plaid tousled.</p>
</div>
<div class="col-md-3 col-8">
<p>Celiac 3 wolf moon williamsburg pitchfork, squid helvetica single-origin coffee brooklyn paleo vice biodiesel. Fingerstache celiac mumblecore, try-hard yuccie helvetica chartreuse. Chartreuse wayfarers vegan, blue bottle bicycle rights selfies salvia
ugh drinking vinegar 3 wolf moon you probably haven't heard of them. Cardigan fingerstache freegan 3 wolf moon vegan. Venmo umami 8-bit, cronut raw denim direct trade locavore vinyl seitan plaid tousled.</p>
</div>
<div class="col-md-3 col-9">
<p>Celiac 3 wolf moon williamsburg pitchfork, squid helvetica single-origin coffee brooklyn paleo vice biodiesel. Fingerstache celiac mumblecore, try-hard yuccie helvetica chartreuse. Chartreuse wayfarers vegan, blue bottle bicycle rights selfies salvia
ugh drinking vinegar 3 wolf moon you probably haven't heard of them. Cardigan fingerstache freegan 3 wolf moon vegan. Venmo umami 8-bit, cronut raw denim direct trade locavore vinyl seitan plaid tousled.</p>
</div>
</div>
</div>
.col-1 {
background-color: #555;
}
#media(min-width: 992px) {
.col-1 {
margin-bottom: -600px;
padding-bottom: 600px;
}
}
JSFIDDLE

Vertically position three elements: flexible box, scroll box & fixed box

Is the following even possible with css? If not, what javascript events would be used even if the green box changes content?
I'd like to vertically position three boxes:
The first one (the green one) can have some variable amount of content, and the box should expand to fit the content. This box should be flush with the top of the browser.
The last one, the yellow one, will have a fixed amount of content (meaning I know what the content is at 'compile time'). This box should be flush with the bottom of the browser.
The middle one, the red one, will have a flexible amount of content, and should extend from the bottom of the green box to the top of the yellow box, and internally it needs to be scrollable if its content is larger than the box size.
This is what I've got so far, and it doesn't quite work - I can't seem to make the bottom of the yellow box flush with the bottom of the viewport in such a way as to let the red box take up all the extra space (i.e. position: absolute doesn't seem to help).
#container {
float: left;
height: 100%;
background-color:gray;
}
#header {
background-color: green;
}
#main {
overflow: auto;
background-color: red;
height: 70%;
}
#footer {
background-color:yellow;
}
<div id="container">
<div id="header">START OF HEADER CONTENT...</div>
<div id="main">Craft beer jean shorts...</div>
<div id="footer">footer some stuff goes here</div>
</div>
And this is what it looks like:
Here's a jsfiddle: https://jsfiddle.net/n6cmdcj3/, but note it might not help, as the html box doesn't obey the viewport size - not sure how to make it so
This can be done fairly easily with flexbox. Check out the support tables for compatibility.
html,
body {
width: 100%;
height: 100%;
}
#container {
float: left;
height: 100%;
background-color: purple;
position: relative;
display: flex;
flex-direction: column;
}
#header {
background-color: green;
}
#main {
overflow: hidden;
overflow-y: scroll;
-webkit-overflow-scrolling: touch;
background-color: red;
flex: 1;
flex-grow: 1;
}
#footer {
background-color:yellow;
}
<body>
<div id="container">
<div id="header">
START OF HEADER CONTENT and the viewer should be able to see all of it. Ipsum Blaster jango fett alderaan data dooku validium hypercube. Antilles mace windu uhura xindi millenium falcon bothan exterminate tylium ore. Endor maul skywalker everlasting matches antilles FTL tylium ore dooku. Paradox machine padawan speeder doctor who chewbacca AT-AT frack dagobah ice gun. Uhura data photon torpedo worf landspeeder starbuck hypercube cantina. END OF HEADER CONTENT
</div>
<div id="main">
Craft beer jean shorts beard green juice, kinfolk 8-bit hoodie single-origin coffee letterpress seitan four dollar toast hammock occupy selfies pug. Retro locavore meditation craft beer viral, vinyl health goth cred butcher echo park. Echo park portland helvetica roof party hammock, food truck messenger bag pop-up poutine master cleanse hella artisan etsy. Celiac polaroid before they sold out tousled chillwave farm-to-table, leggings craft beer mlkshk viral seitan vegan intelligentsia. Yuccie synth poutine next level food truck, meggings chambray aesthetic farm-to-table marfa helvetica dreamcatcher blue bottle mumblecore brunch. Fixie narwhal lomo, art party pinterest direct trade poutine mlkshk organic forage irony. Wolf kickstarter authentic dreamcatcher plaid.
Yuccie tilde try-hard selfies gentrify DIY. Offal celiac gentrify cornhole, chia beard scenester quinoa freegan marfa thundercats pour-over synth. Migas salvia franzen pabst, blog listicle freegan chia YOLO fashion axe locavore offal. Paleo whatever messenger bag, 3 wolf moon normcore aesthetic humblebrag pug narwhal lo-fi lomo lumbersexual. Chambray yuccie selfies, tattooed biodiesel pitchfork artisan mixtape actually iPhone single-origin coffee 8-bit master cleanse aesthetic. Kickstarter chillwave VHS tousled green juice. Meggings mumblecore gentrify chambray blue bottle brooklyn.
Four dollar toast sriracha hammock iPhone authentic, quinoa wayfarers pop-up taxidermy post-ironic next level offal YOLO chartreuse fingerstache. Forage salvia direct trade photo booth YOLO, fixie paleo sartorial deep v. Distillery art party pop-up meggings sartorial thundercats vice, portland jean shorts flannel readymade godard. 3 wolf moon single-origin coffee you probably haven't heard of them chillwave selfies cred. Scenester asymmetrical seitan blue bottle bitters banh mi swag, etsy disrupt 90's. Kombucha normcore echo park, photo booth bushwick stumptown retro before they sold out. Chillwave art party heirloom ugh.
Cliche authentic paleo fingerstache banjo actually artisan sriracha helvetica twee, trust fund portland PBR&B. Aesthetic pork belly pop-up bitters distillery, banh mi try-hard cred meditation letterpress schlitz 90's celiac neutra. Irony street art actually cliche cray asymmetrical. Bicycle rights kombucha beard crucifix, deep v cray 3 wolf moon listicle before they sold out shabby chic distillery pitchfork. Meh aesthetic tacos flannel, pug paleo DIY austin. Gastropub kinfolk cliche crucifix, swag post-ironic irony heirloom keytar thundercats 8-bit beard. You probably haven't heard of them migas marfa leggings normcore.
Four loko gentrify ramps, mixtape sartorial fashion axe ethical organic meditation williamsburg. Blue bottle freegan synth hoodie, swag bitters letterpress chillwave pop-up. Hella chicharrones next level ramps chillwave, portland freegan tattooed neutra disrupt austin 3 wolf moon kale chips roof party. Lumbersexual try-hard cold-pressed, affogato offal bushwick thundercats stumptown. Leggings knausgaard gastropub, raw denim bitters lo-fi four dollar toast tilde truffaut meh. Truffaut umami artisan, irony affogato tattooed literally yuccie pabst chia wolf hammock craft beer photo booth. Lomo roof party tumblr thundercats meggings ennui messenger bag, next level franzen synth kickstarter pork belly vegan.
</div>
<div id="footer">
footer some stuff goes here
</div>
</div>
</body>
#container {
float: left;
height: 100%;
background-color:gray;
position: relative;
}
#header {
background-color: green;
height: auto;
}
#main {
overflow: auto;
background-color: red;
height: 70%;
overflow-y: scroll
-webkit-overflow-y: scroll;
-moz-overflow-y: scroll;
}
#footer {
background-color:yellow;
height: 25px;
}
I would do it this way. No reason to get too complicated.
Link: https://jsfiddle.net/n6cmdcj3/3/
I would also utilize set pixels for the main id, since you are trying to establish a scrolling content div.
Correct me if I'm wrong but, I also believe these should be classes not id. Classes are for reusable names, for example, you would utilize a class to update all the color of the divs but you would not utilize an id. An ID would be used in the cases of you needing something specific that will not be implemented anywhere else on the site. It's just good programming practice and it pays out later on when you are using JavaScript.
ID = Single Change Element
Class = Reusable multiple Elements.
Josh Rutherford's answer is great for flexbox. If you want to use a more traditional layout, you will have to set the top element to a fixed height and use vh units. The CSS will look like this:
.container {
height: 100vh;
}
.top {
background-color: #f2f2f2;
height: 24vh;
overflow: hidden;
padding: 20px;
}
.middle {
background-color: #h2h2h2;
height: 55vh;
padding: 20px;
overflow-y: scroll;
}
.bottom {
background-color: #d2d2d2;
height: 3vh;
padding: 20px;
position: fixed;
bottom: 0;
width: 100%;
}
Demo on CodePen: http://codepen.io/staypuftman/pen/eJvdYv

How to have bootstrap fixed navbar not full width with banner on top

So I am just getting started with bootstrap, and I need help setting up a navbar.
What I want ideally, is to have a top centered banner, and just below it a navbar. When the user scrolls below, the banner will scroll offscreen but the navbar will stick to the top. If the user scrolls all the way up again, the banner will reappear. For mobile users, they would have the icon on the right and when pressed will create a drop down menu.
The banner and the top navbar are not full width. The banner is 960px long, and I have it setup using class="col-xs-12 col-md-10 col-md-offset-1". Aesthetically it looks good this way for me.
I have seen a few posts on this topic, but none really mixing all these requirements together (not full width, sticking to top, and banner logo on top of navbar). I tried reverse engineering a few of the approaches but I have had no luck at all.
At the moment, I am just using pill boxes because it was easy to do and format for me. Any help is greatly appreciated. Thanks!
UPDATE: Here is my code for what I currently have. I am happy with this setup, just would like the navbar to stick once it scrolls down. There are a lot of divs here as you will see, that I use for styling a page and shadows. Probably can be much more efficient, but as is it looks exactly like I want.
HTML:
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-md-10 col-md-offset-1 rcshadow">
<div class="row">
<img class="img-responsive center-block" src="../images/header.jpg">
<!-- Fixed navbar -->
<div class="navbar navbar-default navbar-rc">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"><span class="glyphicon glyphicon-home" /></span> Roll Call</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li><span class="glyphicon glyphicon-user"> New</span> <span class="glyphicon">Contact</span></li>
<li class="dropdown">
<span class="glyphicon glyphicon-search" /> Search<b class="caret"></b>
<ul class="dropdown-menu">
<li>Contact Search</li>
<li class="divider"></li>
<li class="dropdown-header">Quick Search</li>
<li><a href="#">
<form class="navbar-form" role="search">
<div class="input-group input-group-xs">
<input type="text" class="input" placeholder="Search by name..." name="srch-term" id="srch-term">
<div class="btn-group btn-group-xs">
<button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
</a></li>
</ul>
</li>
<li><span class="glyphicon glyphicon-list-alt" /> Attendance</li>
<li><span class="glyphicon glyphicon-calendar" /> Planner</li>
<li><span class="glyphicon glyphicon-stats" /> Statistics</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><span class="glyphicon glyphicon-log-out" /> Log-out</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
<div class="row">
<div class="rcpage">
<div class="row">
<h1>Test</h1>
<hr>
</div>
</div>
</div>
</div>
</div>
</div>
My css file (besides the bootstrap):
/* set a max-width for horizontal fluid layout and make it centered */
.container-fluid {
margin-right: auto;
margin-left: auto;
max-width: 970px;
}
body {
margin:0;
padding:0;
font-size:100%;
font-family:verdana,arial,'sans serif';
background-color:#3b607e;
color:#000000;
}
.navbar-rc {
margin-bottom:0px;
-moz-border-radius:0px;
border-radius:0px;
}
.navbar-nav > li > a, .navbar-brand {
padding-top:5px !important;
padding-bottom:0 !important;
height: 30px;
}
.navbar-nav > li:hover, .navbar-header:hover {
background-color:#e7e7e7;
}
.navbar {
min-height:30px !important;
}
.rcshadow {
-moz-box-shadow: 0px 0px 30px 10px #000;
-webkit-box-shadow: 0px 0px 30px 10px #000;
-khtml-box-shadow: 0px 0px 30px 10px #000;
box-shadow: 0px 0px 30px 10px #000;
-moz-border-radius: 0px 0px 15px 15px;
border-radius: 0px 0px 15px 15px;
}
.rcpage {
background-color:#FFF;
padding:0px 30px;
margin-left:auto;
margin-right:auto;
-moz-border-radius: 0px 0px 15px 15px;
border-radius: 0px 0px 15px 15px;
}
When you scroll to the right position, you can adjust the navbar to have position: fixed. However, this will take it out of position in the DOM, so you'll need to adjust the width and placement, and account for the lost space by adding a margin to the container element. You might want to account for a few other things like taking out the border-radius like bootstrap normally would with navbar-fixed-top. It might actually be better just to add that class instead of setting position:fixed, but I ran into some other issues in my example, so it will take a bit of tweaking.
There's a bit of extra javascript in here to check the banner height rather than hardcoding it based on what it should be. It only checks when the page is resized, but this might need to be further tweaked (e.g. if you dynamically add elements on the page it could affect the height of the bar, or if a scrollbar gets added to the page to account for overflow).
I added ids to key parts of the navbar, but you could get them other ways as well (e.g. if you will only have one navbar you could just use .navbar.
I added id="banner" to your banner, and id="navbar-container" to the div containing it (which also contains the navbar). I added id="navbar" to the element with the .navbar class.
Then, here's a simplified version of the javascript, just to show the important bits. I've optimized it a bit though, which you can see in the full code snippet. For some reason the width seemed to be off by a pixel; I didn't really dig into why that is, but just added it back in when setting the width.
$(document).on("scroll", function () {
var bannerHeight = $("#banner").outerHeight(true),
shouldBeSticky = $(window).scrollTop() > bannerHeight,
$container = $("#navbar-container"),
$navbar = $("#navbar");
if (shouldBeSticky) {
var margin = parseInt($container.css("margin-bottom"), 10);
$container.css("margin-bottom", margin + $navbar.outerHeight(true) + "px");
$navbar.css({
width: $navbar.outerWidth(true) + 1 + "px",
left: $navbar.offset().left + "px",
top: 0,
position: "fixed"
})
} else {
$container.css("margin-bottom", "");
$navbar.css({
width: "",
left: "",
top: "",
position: ""
});
}
}
}
});
jQuery(function($) {
var bannerHeight = 0,
navbarHeight = 0,
scrollTop = 0,
isSticky = false,
$container = $("#navbar-container"),
$navbar = $("#navbar"),
$banner = $("#banner"),
$window = $(window);
function setHeights() {
bannerHeight = $banner.outerHeight(true);
navbarHeight = $navbar.outerHeight(true);
}
function setScrollTop() {
scrollTop = $window.scrollTop();
}
function affixScrollbar() {
if (!bannerHeight || !navbarHeight) setHeights();
var shouldBeSticky = scrollTop > bannerHeight;
if (shouldBeSticky !== isSticky) {
if (isSticky = shouldBeSticky) {
var margin = parseInt($container.css("margin-bottom"), 10),
width = $navbar.outerWidth(true) + 1 + "px",
left = $navbar.offset().left + "px";
$container.css("margin-bottom", margin + navbarHeight + "px");
$navbar.css({ width: width, left: left, top: 0, position: "fixed" });
} else {
$container.css("margin-bottom", "");
$navbar.css({ width: "", left: "", top: "", position: "" });
}
}
}
$(window).on("resize", function() {
setHeights();
affixScrollbar();
});
$(document).on("scroll", function() {
setScrollTop();
affixScrollbar();
});
setHeights();
setScrollTop();
affixScrollbar();
});
/* set a max-width for horizontal fluid layout and make it centered */
.container-fluid {
margin-right: auto;
margin-left: auto;
max-width: 970px;
}
body {
margin: 0;
padding: 0;
font-size: 100%;
font-family: verdana, arial, 'sans serif';
background-color: #3b607e;
color: #000000;
}
.navbar-rc {
margin-bottom: 0px;
-moz-border-radius: 0px;
border-radius: 0px;
}
.navbar-nav > li > a,
.navbar-brand {
padding-top: 5px !important;
padding-bottom: 0 !important;
height: 30px;
}
.navbar-nav > li:hover,
.navbar-header:hover {
background-color: #e7e7e7;
}
.navbar {
min-height: 30px !important;
}
.rcshadow {
-moz-box-shadow: 0px 0px 30px 10px #000;
-webkit-box-shadow: 0px 0px 30px 10px #000;
-khtml-box-shadow: 0px 0px 30px 10px #000;
box-shadow: 0px 0px 30px 10px #000;
-moz-border-radius: 0px 0px 15px 15px;
border-radius: 0px 0px 15px 15px;
}
.rcpage {
background-color: #FFF;
padding: 0px 30px;
margin-left: auto;
margin-right: auto;
-moz-border-radius: 0px 0px 15px 15px;
border-radius: 0px 0px 15px 15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-md-10 col-md-offset-1 rcshadow">
<div class="row" id="navbar-container">
<img class="img-responsive center-block" src="../images/header.jpg" id="banner">
<!-- Fixed navbar -->
<div class="navbar navbar-default navbar-rc" id="navbar">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"><span class="glyphicon glyphicon-home" /></span> Roll Call</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li><span class="glyphicon glyphicon-user"> New</span> <span class="glyphicon">Contact</span>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-search" /> Search<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li>Contact Search
</li>
<li class="divider"></li>
<li class="dropdown-header">Quick Search</li>
<li>
<a href="#">
<form class="navbar-form" role="search">
<div class="input-group input-group-xs">
<input type="text" class="input" placeholder="Search by name..." name="srch-term" id="srch-term">
<div class="btn-group btn-group-xs">
<button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i>
</button>
</div>
</div>
</form>
</a>
</li>
</ul>
</li>
<li>
<span class="glyphicon glyphicon-list-alt" /> Attendance
</li>
<li>
<span class="glyphicon glyphicon-calendar" /> Planner
</li>
<li>
<span class="glyphicon glyphicon-stats" /> Statistics
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<span class="glyphicon glyphicon-log-out" /> Log-out
</li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</div>
<div class="row">
<div class="rcpage">
<div class="row">
<h1>Test</h1>
<hr>
</div>
<p>Retro iPhone four dollar toast, synth Schlitz Austin cray typewriter Thundercats fashion axe fixie aesthetic bicycle rights. Bicycle rights taxidermy pickled tattooed. Kogi shabby chic Tumblr, Helvetica American Apparel raw denim chia before
they sold out aesthetic bicycle rights taxidermy salvia paleo. Skateboard bespoke fanny pack, Godard hashtag put a bird on it yr vinyl sustainable tofu photo booth meh Thundercats forage. Street art 90's skateboard sartorial synth McSweeney's.
Cornhole High Life twee tote bag, polaroid Pinterest listicle literally organic sartorial YOLO blog master cleanse 8-bit. Small batch Banksy wolf yr.</p>
<p>Aesthetic cornhole lo-fi plaid, Odd Future seitan blog paleo. Ugh cronut aesthetic whatever photo booth PBR. Carles pop-up XOXO +1 readymade, meh health goth DIY. Chillwave Pitchfork kogi, Echo Park quinoa meggings fap asymmetrical Austin Wes
Anderson butcher Neutra hella PBR mixtape. IPhone Bushwick wayfarers, organic ennui ethical flexitarian. Blue Bottle 8-bit single-origin coffee hashtag, Marfa mlkshk meggings literally pug YOLO. Small batch cliche literally, lumbersexual try-hard
beard whatever Etsy umami skateboard tote bag Pitchfork.</p>
<p>Street art YOLO scenester, irony tousled shabby chic crucifix roof party bitters gastropub you probably haven't heard of them. Keffiyeh before they sold out wolf typewriter DIY Thundercats. Keffiyeh banh mi meditation pug, leggings 3 wolf moon
sriracha Blue Bottle. Swag ennui biodiesel, fashion axe mumblecore lomo sustainable lumbersexual American Apparel narwhal tofu Helvetica kitsch cred +1. Cronut squid keytar retro, chillwave ugh authentic synth semiotics distillery fingerstache
bespoke Williamsburg. Helvetica chambray tote bag semiotics Godard fingerstache. Actually slow-carb artisan blog, deep v tote bag keytar pickled lomo Pitchfork biodiesel.</p>
<p>Ethical Etsy drinking vinegar PBR&B Echo Park direct trade. Pug cronut ethical, you probably haven't heard of them Helvetica tousled mumblecore flexitarian before they sold out YOLO PBR&B dreamcatcher. XOXO craft beer sriracha, hella PBR&B polaroid
Brooklyn Williamsburg banh mi biodiesel cray bicycle rights typewriter. Brunch vegan tousled, leggings hella chia deep v cold-pressed VHS. Jean shorts post-ironic authentic literally Shoreditch squid. Polaroid mixtape freegan, food truck Blue
Bottle 90's umami. Craft beer Blue Bottle cliche, banjo polaroid keytar put a bird on it chillwave 90's.</p>
<p>Four loko keffiyeh migas, kitsch High Life DIY chillwave farm-to-table banh mi gluten-free mixtape locavore. Flexitarian paleo photo booth fap, scenester mustache High Life organic small batch. Pug raw denim organic, biodiesel bicycle rights
keytar mustache art party gentrify fingerstache ennui Williamsburg craft beer Brooklyn. Letterpress sustainable kogi squid cornhole. Semiotics disrupt pickled, kitsch XOXO scenester skateboard lumbersexual sustainable food truck pork belly
crucifix Godard raw denim fixie. Pour-over master cleanse forage VHS, Thundercats disrupt pug Truffaut vegan quinoa keffiyeh Echo Park cardigan you probably haven't heard of them. Mumblecore leggings iPhone Blue Bottle, fashion axe street
art chia Godard distillery shabby chic.</p>
<p>Flexitarian Kickstarter migas, distillery jean shorts roof party cold-pressed lumbersexual. Asymmetrical 3 wolf moon Truffaut, Wes Anderson lo-fi hashtag PBR&B tousled Schlitz hoodie pug Williamsburg. American Apparel skateboard chambray Wes
Anderson. Echo Park 90's Pitchfork Portland street art pop-up, flannel banh mi Godard VHS literally food truck. Semiotics butcher farm-to-table Etsy church-key Schlitz, tousled XOXO Vice street art selfies lumbersexual locavore. Cronut yr
post-ironic plaid pop-up tattooed DIY master cleanse paleo, normcore scenester quinoa slow-carb. Neutra wolf jean shorts umami banjo fashion axe.</p>
<p>Dreamcatcher migas artisan, iPhone tofu Austin food truck forage four dollar toast organic four loko gentrify Portland trust fund. Irony tilde mlkshk Neutra, gentrify organic pop-up next level photo booth. Austin Godard keffiyeh, plaid Tumblr
vinyl meditation freegan direct trade mustache. Actually yr polaroid flexitarian banh mi. Sartorial cray Blue Bottle cred. Gluten-free YOLO post-ironic, craft beer Kickstarter American Apparel freegan. Marfa meggings tote bag biodiesel, umami
migas kogi photo booth letterpress single-origin coffee Bushwick. Deep v organic readymade hashtag. Plaid mustache wolf irony leggings. Tilde occupy sustainable synth irony, readymade wolf normcore put a bird on it cliche. Cray fap cronut
butcher, trust fund umami banh mi. Street art retro next level forage. Kogi fashion axe sriracha, biodiesel ugh cray roof party. Kogi keffiyeh occupy paleo bicycle rights, drinking vinegar tousled Portland.</p>
<p>You probably haven't heard of them fanny pack street art pour-over, Wes Anderson 8-bit chambray mustache pork belly drinking vinegar. Bushwick health goth mumblecore keytar. 3 wolf moon direct trade put a bird on it twee tousled deep v, kogi
iPhone meditation dreamcatcher lo-fi jean shorts. Church-key bitters vinyl narwhal lomo synth. Chambray literally flexitarian Marfa craft beer tilde. Synth small batch Portland 8-bit, American Apparel church-key disrupt skateboard brunch health
goth listicle irony tofu artisan photo booth. Odd Future squid leggings paleo stumptown disrupt.</p>
<p>Neutra Marfa McSweeney's asymmetrical Intelligentsia, wolf Brooklyn stumptown street art. Cardigan Intelligentsia viral, pickled crucifix forage wolf listicle normcore synth. Dreamcatcher keffiyeh cronut 90's, readymade sriracha fap mustache
leggings Banksy Austin kale chips crucifix. Portland craft beer next level stumptown, heirloom four dollar toast tattooed McSweeney's hoodie shabby chic squid distillery before they sold out. Literally Tumblr ethical, cardigan 8-bit beard
put a bird on it gastropub pour-over cornhole ugh lo-fi. Post-ironic shabby chic chillwave, semiotics salvia before they sold out flexitarian retro locavore distillery meditation letterpress cliche. Blue Bottle pork belly roof party Thundercats
tattooed.
</p>
</div>
</div>
</div>
</div>
</div>