This question already has answers here:
How to create a fixed sidebar layout with Bootstrap 4?
(5 answers)
Closed 3 years ago.
I have a left div with id sideNav and a right main div like this
#sideNav {
background-color: #012d20;
position: fixed;
z-index: 1;
top: 0;
left: 0;
overflow: hidden;
border-right-style: groove;
height: 100vh;
}
<div class="row">
<!-- navigation sidebar -->
<div class="col-2 pt-5" id="sideNav">
...
</div>
<!-- main content area -->
<main class="col-10">
...
</main>
</div>
The code works but the left part of main is now inside behind the sideNav. When I remove the position property of the sideNav css, my divs are displayed correctly again but sideNav is no longer fixed and scrolls with the page.
How do I keep sideNav fixed and my divs properly displayed?
The reason this isn't working for you, is because row has display type flex, so that all the cols below fit into that row.
Your cols are then blocks spaced via their given value e.g. col-3 col-4 etc
By changing the column display type (to fixed in your example) your removing it from the flex spacing, and so your main nav will move left because you haven't offset it.
To fix this, don't add padding as others have suggested, BootStrap has classes that handle this. Instead, add offset-2 to your main nav, and leave everything else as is.
Example Snippet
#sideNav {
background-color: #012d20;
position: fixed;
z-index: 1;
top: 0;
left: 0;
overflow: hidden;
border-right-style: groove;
height: 100vh;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<div class="row">
<!-- navigation sidebar -->
<div class="col-2 pt-5" id="sideNav">
...
</div>
<!-- main content area -->
<main class="col-10 offset-2">
...
</main>
</div>
Thats because fixed gets "ignored" by other containers. It is handelt as if it was absolute. You can give your sidebar a fixed width and your main a padding.
#sideNav {
background-color: #012d20;
position: fixed;
z-index: 1;
top: 0;
left: 0;
overflow: hidden;
border-right-style: groove;
height: 100vh;
width: 350px;
}
main {
padding-left: 350px;
}
Codepen
Lets try this,
iam adding width for sideNav about 40px;the same padding-left iam put #main-content.
html
<div class="row">
<!-- navigation sidebar -->
<div class="col-2 pt-5" id="sideNav">
<p>paragraph...</p>
</div>
<!-- main content area -->
<main class="col-10" id="main-content">
<p>paragraph...</p>
<p>paragraph...</p>
</main>
</div>
css
#sideNav {
background-color: #012d20;
position: fixed;
z-index: 1;
top: 0;
left: 0;
overflow: hidden;
border-right-style: groove;
height: 100%;
width:40px;
}
#main-content{
width:100%;
float:left;
padding:0 0 0 40px;
background:yellow;
}
I saw you are using bootstrap grid, so to keep the grid working I recommend putting your fixed container in the .col-2 div
#sideNav {
background-color: #012d20;
position: fixed;
z-index: 1;
top: 0;
left: 0;
overflow: hidden;
border-right-style: groove;
height: 100vh;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">
<!-- navigation sidebar -->
<div class="col-2 pt-5">
<div id="sideNav">
...
</div>
</div>
<!-- main content area -->
<main class="col-10">
...
</main>
</div>
Related
I am trying to place the content of my homepage below my navabr which is located at the center of the page.
I was able to place the content below the navbar, but it changes when I change the window's height.
How do I make it stick below the navbar?
this is the css:
.content {
position: relative;
height: 100vh;
transition: 0.3s;
}
.homeNav {
transition: 0.3s;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100%;
}
.homeContent {
position: absolute;
transition: 0.3s;
top: 65%;
}
This is my html:
<body>
<div class="container content">
<nav class="homeNav" id="all">
<img src="/images/400x200.png" class="img-fluid mx-auto d-block" />
<div class="row bg-info">
NAVIGATION BAR HERE
</div>
</nav>
<div id="main-page" class="homeContent">
</div>
</div>
</body>
Please note that this is a single-page application, and the content of the main-page is obtained from a separate HTML file.
You can add css to the body element
body{
margin-top: 200px;
}
That's it.
Alternatively, you can wrap the header and bottom content in a div and then give margin-top to that div element.
<div class="wrapper">
<nav>Header Area</nav>
<!-- Everything else goes here -->
</div>
CSS for wrapper class:
.wrapper{
margin-top: 200px;
}
I'm having trouble setting up a "master-detail" view using Bootstrap 3. I'm attaching a picture of the specific layout I'm trying to get:
I've created a div with .row spanning the 12 columns for the header, and then another div.row with a div.col-md-4 and div.col-md-8 for the two columns. But obviously this doesn't achieve what I'm looking for. I've tried playing with the position attribute, but it seems to conflict with the Bootstrap CSS and I always just get jumbled text.
Thank you very much in advance and let me know if you'd like me to provide more details on what I'm looking for. Hopefully the pic is clear.
You are looking for
position: fixed;
This ensures that the element is always static relative to the viewport. You will then use the relative positioning properties to place it where you want it to go.
For the top element (say, your navbar), you can set:
top: 0;
For the bottom element, you can set:
bottom: 0;
So, something like this:
.row, .col-md-4, .col-md-8 {
border: 2px solid pink; /* For showing the div borders */
}
.row.main-content {
background-color: blue;
height: 300px;
margin-top: 20px; /* Needs to match the height of .row.top */
}
.row.top {
position: fixed;
top: 0;
height: 20px;
width: 100%;
background-color: red;
z-index: 9999;
}
.row.bottom {
position: fixed;
bottom: 0;
height: 20px;
width: 100%;
background-color: green;
z-index: 9999;
}
.scrollable {
overflow-y: scroll;
height: 300px; /* May need to be set somehow */
}
<div class="container-fluid"><!-- To get it to take up the whole width -->
<div class="row top">
</div><!-- row -->
<div class="row main-content">
<div class="col-md-4">
<div class="sub-header">
</div>
<div class="scrollable">
<!-- menu items here -->
</div>
<div class="sub-footer">
</div>
</div>
<div class="col-md-8 scrollable">
<!-- Product detail goes here -->
</div>
</div><!-- row -->
<div class="row bottom">
</div><!-- row bottom -->
</div><!-- container-fluid -->
Hope that helps.
I need to lay out a web page with two images that must stretch to fill the available browser window, but with text content that is centred in the page. So far, the only ways I seem to be able to do this are, a) combine the two images into one, and set this as a background image for body, then carefully vertically position the middle band of content to fit the gap between images, or, b) to nest a container (fixed) div inside a container-fluid holding the image and the fixed holding the text content. I have, however, seen dire warnings and scorn poured on those who advocate nesting bootstrap containers.
This image may help convey what I need:
'Image 1' must stretch across the entire window, with the content remaining centered, the same with 'Image 2', with a plain band of white vertically between the images, and a plain band of grey right across the screen at the bottom.
You can try this.
Note that you have to apply some custom css as required by your design needs. Follow the example:
.container-fluid {
padding: 0;
}
.container-fluid.wrapper {
padding-top: 100px;
}
.ht33 {
height: 200px;
}
.container.content {
width: 970px;
position: absolute;
height: 800px;
background: gray;
color: #fff;
top: 0;
left: 0;
right: 0;
margin-right: auto;
margin-left: auto;
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<div class="container-fluid wrapper">
<div class="container content"></div>
<div class="container-fluid bg-primary ht33"></div>
<div class="container-fluid bg-warning ht33"></div>
<div class="container-fluid bg-danger ht33"></div>
</div>
I used row class to the BGs to have a Grid feel on them and also eliminate the padding.
HTML
<div class="container-fluid wrapper">
<div class="container content bg-primary"></div>
<div class="row bg-success bg"></div>
<div class="row bg-warning bg"></div>
<div class="row bg-danger bg"></div>
</div>
CSS
.container-fluid.wrapper {
padding-top: 100px;
}
.bg {
height: 200px;
}
.container.content {
width: 970px;
position: absolute;
height: 800px;
color: #fff;
top: 0;
left: 0;
right: 0;
margin: 0 auto;
}
You could try to do it like this:
http://jsfiddle.net/kqoonr24/1/
You may have to adapt it to fit your needs, but this is the basic idea. :-)
Basically, you'll want to have a div that acts as a background and houses the individual divs which make up each background element.
Then, after that you'll want to have a foreground div which houses the actual content.
html,
body,
.wrapper,
.foreground,
.background {
height: 100%;
width: 100%;
}
.foreground {
background-color: green;
max-width: 400px;
margin: 0 auto;
}
.background {
position: absolute;
z-index: -1;
}
.background > * {
height: 33%;
content: '';
}
.background-element-1 {
background-color: blue;
}
.background-element-2 {
background-color: red;
}
.background-element-3 {
background-color: orange;
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<body>
<div class="wrapper">
<div class="background">
<div class="background-element-1"></div>
<div class="background-element-2"></div>
<div class="background-element-3"></div>
</div>
<div class="foreground">
<div class="foreground-inner">
I am your text!
</div>
</div>
</div>
</body>
You will likely need to adjust it as I made quite a few assumptions—let me know if you need any more help.
The simpliest example that I might think of, I assumed you don't know the size of your background images, that's why I lay them inside background div that's not affecting page flow, while on top there's natural content
Demo
<div class="images-wrapper">
<img src="..." alt="">
<img src="..." alt="">
</div>
<div class="container">
.......
</div> <!-- /container -->
body {
background:#ccc;
}
.images-wrapper {
position:absolute;
top:0;
left:0;
right:0;
background:#fff;
z-index:0;
}
.images-wrapper img {
display:block;
width:100%;
height:auto;
margin-top:30px;
}
.container {
z-index:2;
position:relative;
background:#fff;
}
Why is my header not fixed to the column.
<div class="container-fluid full-ht">
<div class="row full-ht">
<div class="col-sm-4 full-ht">
<div class="patient-column full-ht">
<div class="fixed-header">
Fixed Header
</div>
Some content follows
</div>
</div>
</div>
</div>
I want my header to be fixed inside the column when the vertical-scrolling happens.
http://www.bootply.com/tducyvW5u1
you should use
position:fixed.
for fixed header,to achieve fixed header while scrolling
CSS
.fixed-header {
position: fixed; // changed position:absolute to fixed
width: 100%;
top: 0;
padding: 15px;
}
In your CSS I would add !important and make the absolute a fixed as well as make it a priority.
.fixed-header {
position: fixed; !important
width: 100%;
top: 0;
padding: 15px;
}
Using Bootstrap for a responsively designed web-app, I am looking to replicate the typical mobile-app layout:
Fixed Navbar + Overflowing Body + Fixed Footer
Check out the Smartphone preview (right-side icon) of: http://www.bootply.com/124373
I have the following CSS, which is close, but the fixed footer cuts-off the body - instead of the body scrolling:
html {
position: relative;
min-height: 100%;
}
body {
/* Margin bottom by footer height */
margin-bottom: -60px;
}
#footer {
position: absolute;
bottom: 0;
width: 100%;
/* Set the fixed height of the footer here */
height: 60px;
background-color: #f5f5f5;
}
body > .container {
padding: 60px 15px 0;
}
.boxes {
margin-bottom: 22px;
}
.middle {
margin-top: 4%;
overflow: auto;
}
And here's the HTML:
<!-- Fixed navbar -->
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">Mobile App Format Replication</a>
</div>
</div>
</div>
<!-- Begin page content -->
<div class="container middle">
<div class="row boxes">
<div class="col-xs-6 col-md-6"><img src="http://placehold.it/125"></div>
<div class="col-xs-6 col-md-6"><img src="http://placehold.it/125"></div>
</div>
...
<div class="row boxes">
<div class="col-xs-6 col-md-6"><img src="http://placehold.it/125"></div>
<div class="col-xs-6 col-md-6"><img src="http://placehold.it/125"></div>
</div>
</div>
<!-- Fixed Footer -->
<div id="footer">
<div class="container">
<p class="text-muted">Fixed Footer Content</p>
</div>
</div>
What about using position: fixed on the footer element, and removing the negative margin on the body (instead, use 60px bottom padding instead):
#footer {
position: fixed;
bottom: 0;
width: 100%;
height: 60px;
background-color: #f5f5f5;
}
body > .container {
padding: 60px 15px;
}
See fork here: Bootply
If you are trying to replicate the mobile app layout, you should try Ratchet:
http://goratchet.com/
And here are some of the examples: http://goratchet.com/examples/
You can use "Tab bar" to have a fixed footer on there. Hope this helps
Just add .bar-nav~.content{padding-top: 44px;padding-bottom: 44px;} in your styles.
It's completely done!