2

I have a text-image component, and i need to vertical-align text middle to the floated image , if the text is less (condition one) (for larger screens). if the text is more then let it wrap around the floated image (condition two) (again for larger screens). How can i do this in CSS or do we need Javascript for this? Here is fiddle. Both my conditions one and two should work.

 .clearfix { clear: both; }
.text-img { padding-left: 15px; padding-right: 15px; }
.text-img .info-box .info--body p { max-width: none; }
.text-img .info-box { text-align: justify; }
.text-img .stock-img { width: 100%; }

@media (min-width: 992px) {
  .text-img.text-right .stock-img { width: 50%; float: left; }
  .text-img.text-right  .stock-img { padding-right: 15px; padding-bottom: 15px; }

  .text-img.text-left .stock-img { width: 50%; float: right; }
  .text-img.text-left  .stock-img { padding-left: 15px; padding-bottom: 15px; }
}
<div class="clearfix text-img text-left">
 <img src="https://cdn0.vox-cdn.com/thumbor/gvDQZLtlEM7U99rmTEdMoUtLRJU=/0x96:2039x1243/1600x900/cdn0.vox-cdn.com/uploads/chorus_image/image/50319283/ipad1_2040.0.0.jpg" alt="iPad" class="img-responsive stock-img" />
 <div class="info-box">
  <header class="info--header">
   <h3 class="h3">The science of today is the technology of tomorrow.</h3>
  </header>
  <div class="info--body">
   <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc semper urna nec lectus malesuada tincidunt. Aenean faucibus, nulla sed luctus tempus, purus libero vestibulum velit, et blandit odio nunc ac quam. Donec tellus tellus, venenatis ac diam nec, sodales viverra orci.</p>
  </div>
 </div>
</div>

I want the final output to be like this, which satisfys both my condition:

enter image description here

Well the answers given are right, this cannot be solved just by CSS alone, so i had to come up with jQuery solution. For those looking for solution for such scenarios, here is jQuery code that solved my problem:

$(document).ready(function() {

    $(".text-img").each( function() {
        var cH = parseInt( $( this ).height() );
        var tH = parseInt( $( this ).find('.info-box').height() );

        if( tH < cH ) {
            var pt = ( cH - tH ) / 2;

            $( this ).find('.info-box').css({
                "padding-top" : pt + "px"
            });
        }
    });

});
VishwaKumar
  • 3,433
  • 8
  • 44
  • 72
  • Vishwa, kindly check if my solution works, and let me know your feedback... `:D` – Praveen Kumar Purushothaman Jan 04 '17 at 13:02
  • You cannot vertically align a floated element: http://stackoverflow.com/questions/12557897/how-to-vertically-middle-align-floating-elements-of-unknown-heights – Praveen Kumar Purushothaman Jan 04 '17 at 13:19
  • From the [Spec](https://www.w3.org/TR/CSS21/visuren.html#floats): A float is a box that is shifted to the left or right on the current line. The most interesting characteristic of a float (or "floated" or "floating" box) is that content may flow along its side (or be prohibited from doing so by the 'clear' property). Content flows down the right side of a left-floated box and down the left side of a right-floated box. The following is an introduction to float positioning and content flow; the exact rules governing float behavior are given in the description of the 'float' property. – Praveen Kumar Purushothaman Jan 04 '17 at 13:20

2 Answers2

3

Use a flex box layout when you are in the smaller screens.

@media (min-width: 992px) {

  .text-left {
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .text-left img {
    max-width: 50%;
    height: auto;
    margin-right: 10px;
  }

}

Preview

preview

preview

Output: http://jsbin.com/lulecudaji/edit?html,css,output

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • but if the text is more , then your code does not wrap around the image – VishwaKumar Jan 04 '17 at 13:03
  • 1
    @VishwaKumar Do you really need to float the image? If you float it, you cannot vertically align. – Praveen Kumar Purushothaman Jan 04 '17 at 13:09
  • Yes that is also a condition :), i want the image to be floated – VishwaKumar Jan 04 '17 at 13:13
  • @VishwaKumar Not possible boss. You cannot vertically align a floated element. – Praveen Kumar Purushothaman Jan 04 '17 at 13:18
  • @VishwaKumar http://stackoverflow.com/questions/12557897/how-to-vertically-middle-align-floating-elements-of-unknown-heights – Praveen Kumar Purushothaman Jan 04 '17 at 13:19
  • @VishwaKumar From the [Spec](https://www.w3.org/TR/CSS21/visuren.html#floats): A float is a box that is shifted to the left or right on the current line. The most interesting characteristic of a float (or "floated" or "floating" box) is that content may flow along its side (or be prohibited from doing so by the clear property). Content flows down the right side of a left-floated box and down the left side of a right-floated box. The following is an introduction to float positioning and content flow; the exact rules governing float behavior are given in the description of the 'float' property. – Praveen Kumar Purushothaman Jan 04 '17 at 13:21
1

I would recommend you better/older than flex-box tick for centring elements.

For horizontal centring use simply text-align: center; on container div

For vertical centring uses propoerty of display inline-block elements which aligned in to the middle to center all display inline-block in the line.

enter image description here

Making it bigger i'll move other elements to the center enter image description here

Making it 100% height causes the othere elements centers to the middle. enter image description here

You simply need to create ghost (not visible) - red element to center content - blue and green elements.

For ghost element use for it before or after of conteiner div:

.continer:before {
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}

And display inline-block your content:

.content{
  display: inline-block;
}

Of course delete position:absolute etc.

Last tweak will be to get rid of that small spaces between elements (especially between red and others) use one of the tricks from here: https://css-tricks.com/fighting-the-space-between-inline-block-elements/ Probably you will need to set font size to zero.

More about ghost elements: https://css-tricks.com/centering-in-the-unknown/

Kinga
  • 386
  • 1
  • 8
  • Sorry but this is not what i need , please read my question once again, thank you for your reply. – VishwaKumar Jan 04 '17 at 13:08
  • It is the closest thing you can get with css only. You can not align float element becuse their are... floats. Floating element is taking it from the "scope" and moving it to left or right edge and "the top of the current line box" https://www.w3.org/TR/CSS21/visuren.html#floats. – Kinga Jan 04 '17 at 15:40