我有一个表格,其中数字向右对齐,文本可以多于 1 行。
http://jsbin.com/qelosicono/1/edit?html,css,输出
vertical-align:middle不能与float:right一起使用,除非我设置了我无法设置的行高,因为有些文本会换行成多行,而其他文本会保持单行,所以我不知道预先的行高。
如何将数字垂直对齐到文本的中间?
编辑我正在寻找不使用表格的解决方案 - 它们在太多情况下的行为差异太大而无法完全替代其他元素。
我有一个表格,其中数字向右对齐,文本可以多于 1 行。
http://jsbin.com/qelosicono/1/edit?html,css,输出
vertical-align:middle不能与float:right一起使用,除非我设置了我无法设置的行高,因为有些文本会换行成多行,而其他文本会保持单行,所以我不知道预先的行高。
如何将数字垂直对齐到文本的中间?
编辑我正在寻找不使用表格的解决方案 - 它们在太多情况下的行为差异太大而无法完全替代其他元素。
您可以使用表格,或将 div 用作表格。
http://jsbin.com/bobupetefu/2/edit?html,css,输出
.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>
制作.row
一个弹性盒子。像这样:
.row {
display: flex;
align-items: center;
}
http://jsbin.com/kanucuqidu/2/edit
编辑:.sum
右对齐
。
添加
.text {
flex: 0 0 150px;
}
.sum {
flex: 1;
}
我输入了margin-top:-5%;进入
.sum {
float : right;
vertical-align: middle;
line-height: 40px;
margin-top: -5%;
}
这似乎奏效了。
使用 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;
}
结果
我升级了您的代码,适用于任意数量的行(没有表格)。
你可以通过让你的行类成为一个 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;
}
http://jsbin.com/taqirojupi/2/edit
这是我的解决方案。不幸的是,它只在您.sum
停留在一排(或保持相同大小)时才有效。我将它的位置设置为绝对和 50% 减去它自身高度的一半,它总是将它放在中间。
.sum {
position:absolute;
right:10px;
top:calc(50% - 8px);
}
我的解决方案使用绝对定位.sum
和相对定位.row
。
因为你知道行高,所以你可以.sum
向下移动容器的 50%,向上移动 50% 的行高。
http://jsfiddle.net/z02fgs4n/(在 Chrome 中测试)
以下重要补充:
.row {
position: relative;
}
.sum {
position: absolute;
right: 10px;
top: 50%;
line-height: 40px;
margin-top: -20px;
}
丹尼尔,我使用 html 中的标签解决了这个问题。请尝试此代码。
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
你可以试试这个......这可能会帮助你解决你的问题......
.row{
width: 220px;
height: 50px;
border: 1px solid black;
display: -webkit-flex;
display: flex;
float:right;
-webkit-align-items:中心;}