[英]span element doesn't increase in width when using display: flex;
I want to center a span-element in an container div. 我想将span元素居中放在容器div中。 The span-element is preceeded by another div.
span元素位于另一个div之前。
here is my code: 这是我的代码:
<div id="container">
<div id="a">A</div>
<span id="b">B</span>
</div>
#container {
display:flex;
background-color:#0AA;
text-align:center;
}
#a {
width:70%;
background-color:#AA0;
}
and on jsfiddle: http://jsfiddle.net/pbek7av3/1/ 并在jsfiddle上: http : //jsfiddle.net/pbek7av3/1/
I tried it with text-align:center and margin:auto, but sadly this doesn't work. 我尝试使用text-align:center和margin:auto进行尝试,但遗憾的是这不起作用。
edit: This would be another approach: http://jsfiddle.net/pbek7av3/2/ Is there something like width:auto; 编辑:这将是另一种方法: http : //jsfiddle.net/pbek7av3/2/是否有width:auto;之类的东西? to let div b take the remaining space fo the parent container?
让div b占用父容器的剩余空间?
You are using display: flex;
您正在使用
display: flex;
for the parent, so span
being an inline element, use flex: 1;
对于父对象,因此
span
是一个内联元素,请使用flex: 1;
which is nothing but grow for the flex item like 对于弹性项目来说,这不过是什么而已
#b {
flex: 1; /* Or flex-grow: 1; */
}
Demo (Missed out the declaration in previous demo #3 so updated again) 演示 (在先前的演示3中遗漏了声明,因此再次进行了更新)
Note that flex
is not supported in older versions of IE as well as other popular browsers so if you are interested in the browser support than take a look at CanIUse : Flexbox for more details. 请注意,在较旧版本的IE以及其他流行的浏览器中不支持
flex
因此,如果您对浏览器支持感兴趣,那么可以查看CanIUse:Flexbox了解更多详细信息。
Test case without flex: 1;
没有
flex: 1;
测试用例flex: 1;
or flex-grow: 1;
或
flex-grow: 1;
With flex: 1;
使用
flex: 1;
or flex-grow: 1;
或
flex-grow: 1;
If you know that there are only two element which is getting entire width ie one your div width:70%
, then you can easily give the remaining width:30%
to the span
like this 如果您知道只有两个元素会获得整个宽度,即一个元素的div
width:70%
,那么您可以像这样轻松地将剩余的width:30%
赋予span
#b {
text-align:center;
width:30%;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.