Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a fixed column width with a Bootstrap grid and keep the other ones fluid

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. grid

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-content fills the remaining space at the left of #sidebar.

like image 939
LaGuille Avatar asked Jan 28 '17 21:01

LaGuille


People also ask

How do I keep my column width fixed in HTML?

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.

How do I change the width of a col in Bootstrap?

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.

Is nested column possible with Bootstrap grid system?

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.

Which class in Bootstrap is used to provide a responsive fixed-width container?

container class is a responsive, fixed-width container, meaning its max-width changes at each breakpoint.


1 Answers

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.

like image 71
Sergei Avatar answered Sep 28 '22 02:09

Sergei