Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize bootstrap div to fit content

I am using twitter bootstrap to make 2 divs placed side-by-side. Each div has medium and large images inside it, arranged using jquery isotope. The problem is that when there are not enough images in a div, the div width remains same and there's a lot of extra space left. Like this : enter image description here

I want the divs to align centrally and also reduce in width to fit the images inside it. Like this: enter image description here

My HTML code is this :

<div id="container" class="container-fluid">
    <div class="row-fluid">
        <div class="legend span8">Section1</div>
        <div class="legend span4">Section2</div>
    </div>
    <div class="row-fluid">
        <div id="section1" class="span8">
            <div class="small"><img src="images/1.jpg" /></div>
            <div class="small"><img src="images/2.jpg" /></div>
            <div class="big"><img src="images/3.jpg" /></div>
        </div>
        <div id="section2" class="span4">
            <div class="small"><img src="images/1.jpg" /></div>
            <div class="small"><img src="images/2.jpg" /></div>
            <div class="big"><img src="images/3.jpg" /></div>
        </div>
    </div>
</div>

My CSS looks like this :

#section1 .small{
    width:150px;
    height:200px;
    overflow:hidden;
}
#section1 .big{
    width:320px;
    height:420px;
    overflow:hidden;
}
#section2 .small{
    width:80px;
    height:100px;
    overflow:hidden;
}
#section2 .big{
    width:170px;
    height:220px;
    overflow:hidden;
}

Other styles are bootstrap's.

like image 342
soundswaste Avatar asked Dec 13 '12 12:12

soundswaste


People also ask

How do I fit an image in a div in Bootstrap 4?

Just add a bit of padding to the image and it resizes itself to fit within the div. Solved WITH Bootstrap rather than custom CSS.

How do I change the size of an element in Bootstrap?

Easily make an element as wide or as tall (relative to its parent) with our width and height utilities. Width and height utilities are generated from the $sizes Sass map in _variables.scss . Includes support for 25% , 50% , 75% , and 100% by default. Modify those values as you need to generate different utilities here.

How do I make Bootstrap container full width?

Choose from a responsive, fixed-width container (meaning its max-width changes at each breakpoint) or fluid-width (meaning it's 100% wide all the time). container-fluid class can be used to get full width container. container-fluid class provides a full-width container which spans the entire width of the viewport.

How do I reduce the size of a text box in Bootstrap?

We can also adjust the size of input elements in bootstrap by the use of classes like . input-lg and . input-sm for adjusting heights and . col-lg* and .


2 Answers

I also use bootstrap with isotope, but what I do first is make sure the width's and height's respect my own design.

just change the ones you use, normally I end up using

.width2, .width3, .width4, .width8 and .height2, .height4

then, make sure your container has the correct size and apply to isotope.

after that, all your design will be set and fall correctly with your sizes.

by the way, you should end up having something like this:

<div id="container">
    <div class="weather item width2 height8" data-id="1"></div>
    <div class="country item width2 height4" data-id="2"></div>
    <div class="country item width2 height4" data-id="3"></div>
    <div class="country item width2 height4" data-id="4"></div>
    <div class="country item width2 height4" data-id="5"></div>
</div>

and inside each div, I personally use JsRender to render the templates, but you should have the bootstrap layout, inside each .item

like image 125
balexandre Avatar answered Sep 30 '22 01:09

balexandre


I completely forgot about this question I had asked. I resolved the above situation by :

1) Setting equal left and right margins on the main container.

2) Setting the isotope in Section1 to right-to-left. Mine isnt really an Arabic site, but I am using images mostly so no issues.

Also, remove fixed-width specifications for all sections. Let the margins on container, and bootstrap's fluid design take care of centered-optimized design.

like image 44
soundswaste Avatar answered Sep 30 '22 01:09

soundswaste