Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS vertical align puzzle with float right and variable line height

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.

like image 291
daniel.sedlacek Avatar asked May 21 '15 13:05

daniel.sedlacek


People also ask

How do you vertically align floated elements?

By creating a wrapper around the content you want floated, you can then use the ::after or ::before pseudo selectors to vertically align your content within the wrapper. You can adjust the size of that content all you want without it affecting the alignment.

How do you vertically align items in CSS?

The vertical-align property in CSS controls how elements set next to each other on a line are lined up. In order for this to work, the elements need to be set along a baseline. As in, inline (e.g. <span> , <img> ) or inline-block (e.g. as set by the display property) elements.

How do I vertically align a div in CSS?

you need to set the outer div to be displayed as a table and the inner div to be displayed as a table-cell — which can then be vertically centered. For Internet Explorer, you need to position the inner div absolutely within the outer div and then specify the top as 50%.


1 Answers

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>
like image 96
Selven Avatar answered Sep 22 '22 01:09

Selven