CSS vertical align puzzle with float right and variable line height
Asked Answered
T

10

8

I have a table where numbers are aligned to the right and texts can have more than 1 line.

http://jsbin.com/qelosicono/1/edit?html,css,output

The vertical-align:middle does not work with float:right unless I set the line-height which I can not set because some texts will wrap to multiple lines while other will stay single line so I don't know the line height upfront.

How do I vertically align the number to the middle of the text?

EDIT I am looking for solution that does not use tables - they behave too differently in too many cases to be fully substitutable for other elements.

Trapan answered 21/5, 2015 at 13:51 Comment(2)
Why do you use float instead of a table or display table/table-cell for tabular datas (because it looks like a table) ?Simmon
this is just an example. CSS should be capable of formatting this without a tableTrapan
S
0

I upgraded your code, works fine with any number of rows (without tables).

http://jsbin.com/dufesexabu/1/edit?html,css,output

Sillsby answered 31/5, 2015 at 13:10 Comment(0)
B
4

You could use a table, or make the divs act as a table.

http://jsbin.com/bobupetefu/2/edit?html,css,output

.column {
  background-color : #aaaaff;
  width : 300px;
   display: table;
}

.row {
  border-top-color: #eeeeee;
  border-top-width: 1px;
  border-top-style: solid;
  display: table-row;
}

.text {
  padding: 10px;
  width : 150px;
  display: table-cell;
  vertical-align: middle;
}

.sum {
  padding: 10px;
  vertical-align: middle;
  display: table-cell;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<div class="column">
  <div class="row">
    <span class="text">Lorem yposum dolor sit amet</span><span class="sum">1,000,000,000</span>
  </div>
  <div class="row">
    <span class="text">Hello World</span><span class="sum">10,000</span>
  </div>
   <div class="row">
    <span class="text">Very long amount of text, Very long amount of text, Very long amount of text</span>
    <span class="sum">10,000</span>
  </div>
</div>
</body>
</html>
Batiste answered 21/5, 2015 at 14:35 Comment(0)
W
3

Make .row a flexbox. Like this:

.row {
  display: flex;
  align-items: center;
}

http://jsbin.com/kanucuqidu/2/edit

Edit: With .sum aligned right.

Add

.text {
    flex: 0 0 150px;
}
.sum {
    flex: 1;
}

http://jsbin.com/kanucuqidu/3/edit

Wage answered 27/5, 2015 at 14:33 Comment(3)
I am sorry but the sum is still not aligned to the right in my chromeTrapan
It is supported from Chrome 31 version. Use vendor prefix like -webkit-flex: 0 0 150px; and -webkit-align-items: center;Wage
@Trapan add justify-content: space-between; to .row. css-tricks.com/snippets/css/a-guide-to-flexboxArkhangelsk
H
0

I entered margin-top: -5%; into

.sum {
    float : right;
    vertical-align: middle; 
    line-height: 40px;
    margin-top: -5%;
}

which seemed to work.

Hulett answered 21/5, 2015 at 13:59 Comment(0)
H
0

another version using css transform:

css

.row {
  position: relative;
  padding : 10px;
  border-top-color: #eeeeee;
  border-top-width: 1px;
  border-top-style: solid;
  vertical-align: middle;
}

.text {
  width : 150px;
  display: inline-block;
}

.sum {
  position: absolute;
  top: 50%;
  transform: translate(0, -50%);
  right: 10px;
}

result

enter image description here

Hoodwink answered 30/5, 2015 at 20:45 Comment(0)
S
0

I upgraded your code, works fine with any number of rows (without tables).

http://jsbin.com/dufesexabu/1/edit?html,css,output

Sillsby answered 31/5, 2015 at 13:10 Comment(0)
S
0

You can do it by making your row class a flexbox.

.row {
   display: flex;
  display: -webkit-flex;
  -webkit-align-items: center;
  align-items: center;
  padding : 10px;
  border-top-color: #eeeeee;
  border-top-width: 1px;
  border-top-style: solid;
}
.text {
    flex: 0 0 150px;
  display: inline-block;
}
Syncom answered 31/5, 2015 at 16:37 Comment(0)
W
0

http://jsbin.com/taqirojupi/2/edit

Here's my solution. It unfortunetly only works as long as your .sum stays on one row (or, remains the same size). I set its position to absolute and 50% minus half of its own height, which always places it in the middle.

.sum {
  position:absolute;
  right:10px;
  top:calc(50% - 8px);
}
Weinert answered 1/6, 2015 at 11:4 Comment(0)
V
0

My solution uses absolute positioning on .sum and relative on .row.

Because you know the line-height, you can move .sum down by 50% of the container and up by 50% of the line-height.

http://jsfiddle.net/z02fgs4n/ (tested in Chrome)

The important additions below:

.row {
  position: relative;
}

.sum {
  position: absolute;
  right: 10px;
  top: 50%;
  line-height: 40px;
  margin-top: -20px;
}
Varro answered 2/6, 2015 at 21:12 Comment(1)
Sorry, OP, did you say the text on the right or the left can flow to more lines so you don't want to sent line-height?Varro
R
0

Daniel, I solved this using tags in html. Please try this code.

MyHTML.html:-

  <!DOCTYPE html>
  <html>
  <head>
  <style>
     .column {

       width : 300px;
       display: table;
             }

    .row {

       border-top-width: 1px;
       border-top-style: solid;
       display: table-row;
         }

   .text {
       padding: 10px;
       width : 150px;
       display: table-cell;
       vertical-align: middle;
         }

   .sum {
      padding: 10px;
      vertical-align: middle;
      display: table-cell;
        }
 </style>
 </head>
 <body>

    <div class="column">
      <div class="row">
         <span class="text">150000000  USD       =</span><span 
                         class="sum">Rs.9587972846</span>
       </div>
        <div class="row">
           <span class="text">15000000 GBP        =</span><span
                                 class="sum">Rs.1471043671</span>
       </div>

         </div>

         </body>
         </html>


     http://www.w3schools.com/css/tryit.asp?filename=trycss_align_float

           150000000 USD = Rs.9587972846
           15000000 GBP  = Rs.1471043671
Ridotto answered 3/6, 2015 at 7:31 Comment(1)
Sorry Lee I am on my phone, can you maybe update and share the jsbin?Trapan
S
0

You can try this....this may help you solve your problem....

.row{
width: 220px;
height: 50px;
border: 1px solid black;
display: -webkit-flex;
display: flex;
float:right;

-webkit-align-items:center; }

Shyster answered 3/6, 2015 at 11:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.