简体   繁体   English

在显示中设置时,Flex-basis不作为宽度:flex设置为flex-direction:row

[英]Flex-basis is not acting as width when set within display: flex set to flex-direction: row

Hi guys here is the codepen for more details including html: [ https://codepen.io/Onsopen/pen/BmvNJe?editors=1100][1] 嗨,大家好,这里有更多详细信息,包括html:[ https://codepen.io/Onsopen/pen/BmvNJe?editors=1100] [1 ]

body {
  display: flex;
  margin: 0;
  flex-direction: column; // so here i am using body tag as overall container
  min-height: 100vh;
  background: #2c3e50;
}

header {
  display: flex;
  justify-content: space-between;
}

main {
  display: flex; // <-here the main html tag is being set as display flex and by default is row direction. 
  flex-basis: 200px; // <-THE PROBLEM is the 200px are set as height??
}

article {
  flex-basis: 60%; // <- over here i am setting the tags within main tag individually
  background: #ecf0f1;
}

nav {
  flex-basis: 20%; <- over here i am setting the tags within main tag individually
  background: #95a5a6;
  order: -1;
}

aside {
  flex-basis: 20%; <- over here i am setting the tags within main tag individually
  background: #3498db; 
}

header, footer {
  background: #1abc9c;
  flex-basis: 100px;
}

I am trying to make holy grail layout with flexbox and I am facing a strange behavior from flex-basis within the main tag which is set as display: flex with direction as row .So the items all line up as a row. 我试图让圣杯布局Flexbox的,我面临着一个奇怪的行为flex-basis被设置为主要标签中display: flex与方向row 。所以项目都排队作为行。

When trying to set the flex-basis instead of acting on or as the width is actually just acting as height which is strange considering that the flex-basis stretch is dependent on the direction and main axis which within the main tag is set to row with display: flex as default. 当试图设置flex-basis而不是作用时或width实际上只是作为height而奇怪,考虑到flex-basis拉伸取决于主标签内的方向和主轴设置为row的方向display: flex默认为display: flex

Is it because the <body> tag set to flex-direction:column is influencing the <main> tag item thus when setting the main tag item as flex-basis: 200px is actually responding as an individual item within a column, like the other items within tag (nav, aside, article) stretch on the side because of being under flex row direction within main. 是因为<body>标签设置为flex-direction:column正在影响<main>标签项,因此在将主标签项设置为flex-basis: 200px实际上是作为列中的单个项响应,就像另一个标签内的项目(导航,旁边,文章)由于在主要内部的弯曲行方向下而在侧面伸展。 Was thinking that would be the cause. 认为这将是原因。

However after some research I read at csstricks that items set to display: flex are not influenced by the other parent above them. 然而,经过一些研究后,我在csstricks上读到设置为display: flex项目不会受到其他父项的影响。 So kind of when Individual items that are child become parent they are not anymore influenced by the previous parent in terms of direction. 因此,当孩子的个别物品成为父母时,他们在方向方面不再受前一父母的影响。 Just a guess but would love some help. 只是一个猜测,但会喜欢一些帮助。 I figure out to solve this with body tag set to row and it worked well as their are different ways to solve it but on this specific situation seems strange. 我想通过将body标记设置为row来解决这个问题并且它运行良好,因为它们有不同的解决方法,但在这种特定情况下看起来很奇怪。

Any reason to why so? 有什么理由为什么这样?

The flex-basis: 200px; flex-basis: 200px; set in the main is affecting the height of itself, which is what it's suppose to. 设置在main是影响自己的height ,这是它的假设。

And why, because the flex-basis is a flex item property and whether it affect height or width is based on its parent, the flex container's, flex-direction , which in your case is the body having column . 为什么,因为flex-basis是一个flex项属性,它是否影响高度宽度是基于它的父级,flex容器的flex-direction ,在你的情况下是具有columnbody

在此输入图像描述


If to control the main 's width, you need to use just that, width : 如果要控制main宽度,则需要使用width

main {
  display: flex;
  flex-basis: 200px;         /*  height  */
  width: 500px;              /*  width  */
}

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

相关问题 当flex-direction为&#39;column&#39;时,CSS flex-basis无法正常工作 - CSS flex-basis not working when flex-direction is 'column' 为什么 flex-direction: row 仅在我将元素设置为 display: inline-block 时才起作用? - Why does flex-direction: row only work when I set the element to display: inline-block? 当 flex-basis 和 flex-grow 都被设置时,弹性项目之间的空间是如何分配的? - How is space divided amongst flex items when flex-basis and flex-grow both are set? 如果未设置flex-basis或width,flexbox如何计算flex-item的宽度? - How flexbox calculates flex-item's width if no flex-basis or width are set? 如何为设置弹性方向 <td> 分子 - How to set flex-direction for <td> elements 如何在 JavaScript 中应用 display: flex 和 flex-direction: row? - How to apply display: flex and flex-direction: row in JavaScript? 如何使用display = flex和flex-direction = row将div水平居中; - How to horizontally center div with display = flex and flex-direction = row; 在 flex-direction: column 中显示一行中的 flex 项 - Display flex items in a row in flex-direction: column 在 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 最小宽度渲染在flex-direction:行和flex-direction:列上有所不同 - min-width rendering differently in flex-direction: row and flex-direction: column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM