简体   繁体   English

为什么 flex-direction: row 仅在我将元素设置为 display: inline-block 时才起作用?

[英]Why does flex-direction: row only work when I set the element to display: inline-block?

The flex-direction:row is only working when I add display: inline-block inside nav li{} . flex-direction:row仅在我在nav li{}内添加display: inline-block时才有效。 I know it because -webkit-flex-direction: row-reverse;我知道是因为-webkit-flex-direction: row-reverse; is working.正在工作。 But other properties like justify-content: space-between;但其他属性如justify-content: space-between; are not working.不工作。

And the opposite;而相反; if I remove display: inline-block inside nav li{} , then only flex-direction:column is working.如果我在nav li{}删除display: inline-block ,那么只有flex-direction:column正在工作。 What is the problem?问题是什么?

 nav{ background: grey; display:-webkit-flex; -webkit-flex-direction: row; } nav li{ list-style: none; }
 <nav> <ul> <li><a href="#about">About</a><li> <li><a href="#skills">Skills</a></li> <li><a href="#education">Education</a></li> <li><a href="#experience">Experience</a></li> <li><a href="#portfolio">Portfolio</a></li> </ul> </nav>

It only gives you the illusion that it works.它只会给你一种它有效的错觉。

Flex set at the <nav> level makes the <ul> a flex item, and doesn't affect <li> elements.<nav>级别设置的 Flex 使<ul>成为一个弹性项目,并且不会影响<li>元素。

You want the Flex container to be <ul> .您希望 Flex 容器为<ul>

在 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.

相关问题 显示设置为内联块的元素在MVC for循环中不起作用 - Element with display set to inline-block does not work in MVC for loop 在显示中设置时,Flex-basis不作为宽度:flex设置为flex-direction:row - Flex-basis is not acting as width when set within display: flex set to flex-direction: row 当我将 flex-direction 设置为行时,列中的自定义图标和文本? - Custom icon and text in column when I set flex-direction as row? 为什么内联块以这种方式工作? - Why does inline-block work that way? 为什么在使用display:inline-block;时div的顺序会更改? - Why does the order of my divs change when I use display: inline-block;? CSS:什么时候使用display flex和inline-block? - CSS : When to use display flex and display inline-block? 在 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 为什么图像使用显示:内联但行为类似于内联块元素 - Why does image use display: inline but behaves like an inline-block element 如何在 JavaScript 中应用 display: flex 和 flex-direction: row? - How to apply display: flex and flex-direction: row in JavaScript? 在 flex-direction: column 中显示一行中的 flex 项 - Display flex items in a row in flex-direction: column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM