Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change bootstrap col width dynamically according to other columns?

Let's say we've created a row into bootstrap and then added two columns, one for the blog content, on the left side (col-sm-7) and another one for the sidebar (col-sm-5), on the right side.

is there a way to make the content in col-sm-7 expands to col-sm-12 when the height of col-sm-5 is less than the height of col-sm-7 (or in other words: when there's no content on the sidebar?: let's say a user adds only one widget. The problem is, the sidebar will occupy all the right side, which is not good looking.)

Here's an image to show what I mean:

bootstrap grid, dynamic width question

like image 682
Dexter Avatar asked Apr 10 '17 15:04

Dexter


People also ask

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

In general you want to search for an existing column width (say col-sm-3 ) and copy over all the styles that apply to it, including generic ones, over to your custom stylesheet where you define new column widths. Save this answer.

How do I change the width of a column in Bootstrap 4?

Add . w-auto class to the table element to set an auto width to the table column. The width of the columns will automatically adjust to the content of the column.

How do you change the space between columns in Bootstrap?

To add space between columns in Bootstrap use gutter classes. With gutters you can add horizontal or vertical space or even specify how big space should be on different screen size.

What does Col-SM-4 mean?

col-sm-4 applies to small, medium, large, and extra large devices, but not the first xs breakpoint). You can use predefined grid classes (like . col-4 ) or Sass mixins for more semantic markup.


1 Answers

As mentioned, there's no way to do this with the standard Bootstrap grid. However, with a few CSS tweaks it's possible, by adhering to these "rules"..

Put the sidebar first, and float it right..

<div class="col-sm-5 pull-right">
            ..
</div>

Make the col-sm-7 width:auto, and un-float it..

.float-none {
    float: none;
    width: auto;
}

Finally, any child DIV's inside the col-sm-7 need to be overflow: auto...

Demo: http://codeply.com/go/njEg31ZG33

AFAIK, flexbox would not work well for this.

like image 153
Zim Avatar answered Oct 13 '22 00:10

Zim