[英]A way to center a span inside of div relatively to contents?
I am trying to make a fancy button. 我试图做一个花哨的按钮。
<div class="button" data-button='closed'>
<div class="button-default">
<span class="button-text">Click</span>
</div>
<div class="button-overflow">
<span class="button-overflow-text">Confirm!</span>
</div>
</div>
The idea is that by default it displays 'Click' and when clicked it switches to 'Confirm'. 这个想法是,默认情况下,它显示“单击”,并且在单击时切换到“确认”。
The problem is that I don't know how to make 'Click' and 'Confirm!' 问题是我不知道如何进行“点击”和“确认!” be always centered.
始终居中。 I want it so no matter what text I put there - it still will be centered.
我想要它,所以无论我在此处放什么文字-仍然会居中。
Just add 只需添加
text-align: center;
to .button
. 到
.button
。
forked fiddle -> http://jsfiddle.net/Wd5vP/ 分叉的小提琴-> http://jsfiddle.net/Wd5vP/
You can simplify the CSS slightly, as such: 您可以稍微简化CSS,例如:
.button {
border: 1px solid black;
background: #EEE;
width: 100px;
height: 28px;
overflow:hidden;
cursor: pointer;
}
body {
font-family:Arial;
}
.button-text {
position:absolute;
left:40px;
}
.button-overflow {
width:100%;
position:relative;
background:green;
width: 100px;
height: 28px;
right: -100px;
}
.button-default, .button-overflow {
text-align: center;
}
Add text-align: center;
添加
text-align: center;
to your css class .button
到您的CSS类
.button
It would look like this now: 现在看起来像这样:
.button {
border: 1px solid black;
background: #EEE;
width: 100px;
height: 28px;
overflow:hidden;
cursor: pointer;
text-align: center;
line-height: 28px;
} }
You also need to update your css class .button-overflow-text
to display: inline
and remove text-align: left
and vertical-align:middle
: 您还需要更新css类
.button-overflow-text
以display: inline
并删除text-align: left
和vertical-align:middle
:
.button-overflow-text{
display: inline;
} }
just add text-align:center to .button-overflow 只需将text-align:center添加到.button-overflow
.button-overflow {
width:100%;
position:relative;
display:table-cell;
background:green;
width: 100px;
height: 28px;
right: -100px;
text-align:center;
}
This modified CSS worked for me: 修改后的CSS对我有用:
.button {
border: 1px solid black;
background: #EEE;
width: 100px;
height: 28px;
overflow:hidden;
cursor: pointer;
text-align:center;
}
.button-default{text-align:Center;}
body {
font-family:Arial;
}
.button-text {
position:absolute;
left:40px;
}
.button-overflow {
width:100%;
position:relative;
display:table-cell;
background:green;
width: 100px;
text-align:center;
height: 28px;
right: -100px;
}
.button-overflow-text{
text-align:Center;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.