简体   繁体   English

使用display时,span元素的宽度不会增加。

[英]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%;
}

Js Fiddle Demo Js小提琴演示

在 a 中设置“width: auto”<div> 用“显示:弯曲; flex-direction:行”不起作用</div><div id="text_translate"><p>我正在尝试将一堆 div 并排放置,从左到右。 所以我使用 flexbox 方向设置为row 。</p><p> 我希望这些 div 中的每一个都从里面的内容中导出它们的高度和宽度,因此我将它们的高度和宽度保留为默认值。</p><p> 最后,我想使用 svg 作为背景。 为此,我使用了一个网格容器并将 svg 和 div 放置在同一列和同一行中,如此<a href="https://stackoverflow.com/a/51949049/9613633" rel="nofollow noreferrer">stackoverflow 帖子</a>中所建议的那样。 我将 svg 的高度和宽度设置为100% ,以确保它填充整个背景。</p><p> 但是,当我将所有内容组合在一起时,div 的宽度设置不正确。 div 的高度似乎是由里面的内容设置的,但宽度似乎总是默认为300px 。 对于我的一生,我无法弄清楚这个宽度是如何计算的。 如果内容比300px宽,那么一切都按预期工作。 但是如果内容小于300px ,则容器永远不会缩小以适应。</p><p> 如何确保 div 始终缩小以适应? 请参阅下面的代码示例: </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-css lang-css prettyprint-override"> .top { padding: 20px; display: flex; flex-direction: row; }.grid-container { background-color: red; display: grid; }.svg { grid-column: 1; grid-row: 1; width: 100%; height: 100%; }.content { grid-column: 1; grid-row: 1; font-size: 100px; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> This div has calculated width 300px for some reason &lt;div class="top"&gt; &lt;div class="grid-container" key="1"&gt; &lt;svg class="svg" viewBox="-10 -10 20 20" preserveAspectRatio="none"&gt; &lt;polygon strokeWidth="0" fill="yellow" points="0,-10 10,0 0,10 -10,0" /&gt; &lt;/svg&gt; &lt;div class="content"&gt; ab &lt;/div&gt; &lt;/div&gt; &lt;,-- more grid-containers --&gt; &lt;/div&gt; This works as expected &lt;div class="top"&gt; &lt;div class="grid-container"&gt; &lt;svg class="svg" viewBox="-10 -10 20 20" preserveAspectRatio="none"&gt; &lt;polygon strokeWidth="0" fill="yellow" points="0,-10 10,0 0,10 -10,0" /&gt; &lt;/svg&gt; &lt;div class="content"&gt; abcdefghijkl &lt;/div&gt; &lt;/div&gt; &lt;!-- more grid-containers --&gt; &lt;/div&gt;</pre></div></div><p></p></div> - Setting “width: auto” inside a <div> with “display: flex; flex-direction: row” doesn't work

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么将动态内容添加到外部容器时内部元素的宽度不会增加 - Why inner element width doesn't increase when dynamic content is added to outer container flex-grow 不全宽显示容器中的项目 - flex-grow doesn't display items in container in full width 一个 `display: flex` 的 `button` 不占用容器的全宽 - A `button` that is `display: flex` doesn't take full width of container 跨度的元素内容未完全显示 - Span's element content doesn't display entirely 当flex项目包含可变宽度的图像时,flex容器的宽度不会扩展 - Width of flex-container doesn't expand when flex item contains a variable width image 当我使用 % 时,弹性项目的宽度不会改变 - Width of a flex item doesn't change when I use % 在 a 中设置“width: auto”<div> 用“显示:弯曲; flex-direction:行”不起作用</div><div id="text_translate"><p>我正在尝试将一堆 div 并排放置,从左到右。 所以我使用 flexbox 方向设置为row 。</p><p> 我希望这些 div 中的每一个都从里面的内容中导出它们的高度和宽度,因此我将它们的高度和宽度保留为默认值。</p><p> 最后,我想使用 svg 作为背景。 为此,我使用了一个网格容器并将 svg 和 div 放置在同一列和同一行中,如此<a href="https://stackoverflow.com/a/51949049/9613633" rel="nofollow noreferrer">stackoverflow 帖子</a>中所建议的那样。 我将 svg 的高度和宽度设置为100% ,以确保它填充整个背景。</p><p> 但是,当我将所有内容组合在一起时,div 的宽度设置不正确。 div 的高度似乎是由里面的内容设置的,但宽度似乎总是默认为300px 。 对于我的一生,我无法弄清楚这个宽度是如何计算的。 如果内容比300px宽,那么一切都按预期工作。 但是如果内容小于300px ,则容器永远不会缩小以适应。</p><p> 如何确保 div 始终缩小以适应? 请参阅下面的代码示例: </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-css lang-css prettyprint-override"> .top { padding: 20px; display: flex; flex-direction: row; }.grid-container { background-color: red; display: grid; }.svg { grid-column: 1; grid-row: 1; width: 100%; height: 100%; }.content { grid-column: 1; grid-row: 1; font-size: 100px; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> This div has calculated width 300px for some reason &lt;div class="top"&gt; &lt;div class="grid-container" key="1"&gt; &lt;svg class="svg" viewBox="-10 -10 20 20" preserveAspectRatio="none"&gt; &lt;polygon strokeWidth="0" fill="yellow" points="0,-10 10,0 0,10 -10,0" /&gt; &lt;/svg&gt; &lt;div class="content"&gt; ab &lt;/div&gt; &lt;/div&gt; &lt;,-- more grid-containers --&gt; &lt;/div&gt; This works as expected &lt;div class="top"&gt; &lt;div class="grid-container"&gt; &lt;svg class="svg" viewBox="-10 -10 20 20" preserveAspectRatio="none"&gt; &lt;polygon strokeWidth="0" fill="yellow" points="0,-10 10,0 0,10 -10,0" /&gt; &lt;/svg&gt; &lt;div class="content"&gt; abcdefghijkl &lt;/div&gt; &lt;/div&gt; &lt;!-- more grid-containers --&gt; &lt;/div&gt;</pre></div></div><p></p></div> - Setting “width: auto” inside a <div> with “display: flex; flex-direction: row” doesn't work 使用Display Flex时的最小宽度问题 - Issue with min-width when using display flex 单击跨度元素时,模态不会关闭 - When clicking span element, modal doesn't close display flex - 保持最后一个元素全宽 - display flex - keeping last element in full width
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM