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.

daniel.sedlacek
  • 8,129
  • 9
  • 46
  • 77

10 Answers10

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>
Selven
  • 276
  • 4
  • 12
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

Arun Prakash
  • 160
  • 1
  • 9
0

I entered margin-top: -5%; into

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

which seemed to work.

Donagh McCarthy
  • 141
  • 1
  • 3
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

Nikos M.
  • 8,033
  • 4
  • 36
  • 43
0

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

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

Mark
  • 563
  • 4
  • 18
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;
}
sonam gupta
  • 775
  • 6
  • 17
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);
}
Okku
  • 7,468
  • 4
  • 30
  • 43
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;
}
goldins
  • 1,306
  • 1
  • 11
  • 14
  • 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? – goldins Jun 02 '15 at 21:19
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
Lee001
  • 19
  • 4
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; }

Kamran Khan
  • 1,042
  • 10
  • 21