Search code examples
csstwitter-bootstrapresponsive-designscale

Div not properly wrapping around contents (image) - Includes margin, possible float/Bootstrap issues


How to size/wrap a div container around an image inside It? Where float: right and margin-left: auto are potentially causing issues.

I'm struggling to get a div to be sized by wrapping properly around the image inside it. Please have a look at the example I'm referring to here:

Link to Example

(Might be worth playing around with the window size to help explain my problem)

I'm practicing with Bootstrap for the first time. The red blocks on each side are grid blocks 1 and 12, with the blue, and green sections filling the remaining 10. The big orange rectangles are responsive images that I want to be kept central spaced 20px apart at all times.

Using Chrome's "Inspect Element" (or similar) - If you inspect the orange rectangle on the right hand side, and have a look at the container div (class="container-img-r") - This div is wrapping around the orange image exactly how I wanted (albeit including the invisible border). But I'm not having much luck achieving the same result with the div container for the orange image on the left side (it still fills the blue parent element)

I've played around with different options for float/margins/position but can't seem to crack it.

Here's the CSS I have for the relevent content:

.container-img-l {
    /* float:right; ??? Nothing I tried here seemed to make a difference */
}

.container-img-r {
    float:left;
}

.item-pos-l {
    margin-left:auto;
    border-right:10px solid transparent; /* Margins just collapsed when resizing window */
    height:323px;
    width:510px;
}

.item-pos-r {
    float:left;
    border-left:10px solid transparent;
    height:323px;
    width:510px;    
}

The reason for me wanting the div to accurately wrap around the responsive images is that I want to overlay some more CSS content over the images, scaling/re-positioning automatically as the window/device size changes (Click here and you'll clearly see where I'm hoping to implement this responsive style).

Maybe there are clashes with the Bootstrap CSS at play but I'm out of ideas.


Solution

  • Your first link doesn't remotely look like the html you want to make responsive. It would be best to learn responsive and fluid (no pixels heights or widths if possible) css before attempting to modify a framework you are unfamiliar with. Also, you have an error in your html - validate it to make sure you've closed all your elements. Also indent and comment all your code and avoid the use of inline styles.

    Demo: http://jsbin.com/wazanu/2/

    http://jsbin.com/wazanu/2/edit -- edit link

    CSS:

    body {background:#eee}
    
    .header {padding:20px;}
    
    .portfolio-grid .col-sm-6 {
        margin-bottom: 30px
    }
    
    .img-wrapper .title {
         text-align:center;
    }
    
    @media (min-width:768px) { 
        .img-wrapper {
            position: relative;
            overflow: hidden;
        }
      
        .img-wrapper img {width:100%;}
      
        .img-wrapper .title {
            position: absolute;
            text-align:left;
            bottom: -90px;
            padding: 0 20px 20px 20px;
            height: 150px;
            background: red;
            transition: all .5s ease-in-out;
        }
        .img-wrapper .title h3 {
            margin: 0;
            height: 60px;
            line-height: 60px;
        }
        .img-wrapper:hover .title {
            bottom: 0
        }
    }
    

    HTML:

    <header class="header text-center">
       My header
    </header>
    <div class="container">
       <div class="row portfolio-grid">
          <div class="col-sm-6">
             <div class="img-wrapper">
                <img src="http://placekitten.com/g/400/300" class="img-responsive center-block" alt="">
                <div class="title">
                   <h3>Title of Project</h3>
                   <p>Content about project goes here</p>
                </div>
             </div>
          </div>
          <!--/.col-sm-6 -->
          <div class="col-sm-6">
             <div class="img-wrapper">
                <img src="http://placebear.com/g/400/300" class="img-responsive center-block" alt="">
             </div>
          </div>
          <!--/.col-sm-6 -->
          <div class="clearfix visible-sm"></div>
          <div class="col-sm-6">
             <div class="img-wrapper">
                <img src="http://placekitten.com/g/400/300" class="img-responsive center-block" alt="">
             </div>
          </div>
          <!--/.col-sm-6 -->
          <div class="col-sm-6">
             <div class="img-wrapper">
                <img src="http://placekitten.com/g/400/300" class="img-responsive center-block" alt="">
             </div>
          </div>
          <!--/.col-sm-6 -->
       </div>
       <!--/.row-->
    </div>
    <!--/.container-->