I am using Bootstrap and I want to create the grid layout shown on the following image.
I would like to have a sidebar with a fixed with of 330px, no matter which screen sizes and keep everything inside #page-content
re sizable depending on the resolution of my visitor's screen.
How can I achieve the layout shown on the image? If I do the following:
<div class="col-md-10" id="page-content"> <div class="col-md-2">...</div> <div class="col-md-10">...</div> </div> <div class="col-md-2" id="sidebar"> ... </div>
and try to fix the width of #sidebar
with width="330px";
for smaller screen sizes, the content will shift to the right, where it can't be seen. Since Bootstrap assigns width: 16.66666667%;
to .col-md-2
, it seems like I would have to get rid the grid system for the higher level divs, #page-content
and #sidebar
. But then, how can I make sure #page-conten
t fills the remaining space at the left of #sidebar
.
The width of the columns i.e. td in a table can be fixed very easily. This can be done by adding the width attribute in the <td> tag. If the width is not specified, the width of the column changes according to the change in the content. The specifications of width for the columns can be in pixels, or percentage.
This is done by adding the class “col-SIZE-SIZE_TO_OCCUPPY”. For example, . col-md-4 which means take 4 columns on the medium-sized screens. If we stack multiple column classes on a single element we can define how we want the layout to look like on other screens and change the columns to rows as we want.
You can nest columns once you have a container, row, and column set up. To do this, add a new row <div> within the parent column's code, then add your nested columns. It will function as if the area inside the new row is its own grid system.
container class is a responsive, fixed-width container, meaning its max-width changes at each breakpoint.
Bootstrap 4.0 beta:
HTML:
<div class="container-fluid"> <div class="row"> <div class="col my-sidebar"> <h2>LEFT</h2> <h6>(FIXED 230px COLUMN)</h6> </div> <div class="col text-center"> <h1>CENTER (FLUID COLUMN)</h1> </div> <div class="col my-sidebar"> <h2>RIGHT</h2> <h6>(FIXED 230px COLUMN)</h6> </div> </div> </div>
CSS:
.my-sidebar { -ms-flex: 0 0 230px; flex: 0 0 230px; background-color: greenyellow; } @media (max-width: 690px) { .my-sidebar { display: none; } }
Live example here: https://www.codeply.com/view/PHYTWFDFRU
In this example fixed columns will disappear if the browser window width becomes less than 690px. If you want them to be always visible remove the second css-block with @media entirely.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With