Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Vertical align does not work with float

How can I use the vertical-align as well as float in the div properties? The vertical-align works fine if I do not use the float. But if I use the float, then it does not work. It is important for me to use the float:right for the last div.

I am trying following, if you remove the float from all div's then it would work fine:

<div class="wrap">     <div class="left">First div, float left,  has more text.</div>     <div class="left2">Second div, float left </div>     <div class="right">Third div, float right</div> </div> 

CSS:

.wrap{     width: 500px;     overflow:hidden;         background: pink; }  .left {     width: 150px;            margin-right: 10px;     background: yellow;       float:left;     vertical-align: middle;       display:inline-block  }  .left2 {     width: 150px;         margin-right: 10px;     background: aqua;     float:left;     vertical-align: middle;        display:inline-block }  .right{     width: 150px;     background: orange;     float:right;     vertical-align: middle;     display:inline-block } 

JSFiddle

like image 952
user1355300 Avatar asked Jul 30 '12 08:07

user1355300


People also ask

Can you do float Center in CSS?

The CSS float property is used to set or return the horizontal alignment of elements. But this property allows an element to float only right or left side of the parent body with rest of the elements wrapped around it. There is no way to float center in CSS layout.

How do I align vertically in CSS?

CSS Demo: vertical-align The vertical-align property can be used in two contexts: To vertically align an inline element's box inside its containing line box. For example, it could be used to vertically position an image in a line of text. To vertically align the content of a cell in a table.

Is vertical-align deprecated?

Deprecated as an attribute Occasionally you will see “valign” used on table cells to accomplish vertical alignment. e.g. <td valign=top> . It should be noted that this attribute is deprecated and should not be used.

Is float opposite of text align?

Float allows other HTML elements to flow around the floating element. Align simply shifts an element to the left or right. Align - You use align to align text and other items rather it be left, right, centered, or justified. Align DOES NOT remove the item from the document flow.


2 Answers

You need to set line-height.

<div style="border: 1px solid red;"> <span style="font-size: 38px; vertical-align:middle; float:left; line-height: 38px">Hejsan</span> <span style="font-size: 13px; vertical-align:middle; float:right; line-height: 38px">svejsan</span> <div style="clear: both;"></div> 

http://jsfiddle.net/VBR5J/

like image 198
Anders B Avatar answered Sep 29 '22 10:09

Anders B


Edited:

The vertical-align CSS property specifies the vertical alignment of an inline, inline-block or table-cell element.

Read this article for Understanding vertical-align

like image 26
Ahsan Khurshid Avatar answered Sep 29 '22 11:09

Ahsan Khurshid