“Flex-grow 不会改变元素的状态”

huangapple go评论62阅读模式
英文:

Flex-grow does not change the state of an element

问题

flex-grow: 1 在这里为什么不起作用?我希望元素共享整个空间,它们之间没有间隔。它们应该是导航按钮,如果需要,可以添加一个。如果可能,请使用 flex-box,我想理解它。

这些框保持在一个距离上,但没有变得更大。

英文:

Why does flex-grow: 1 not work here? I want the elements to share the whole space and there are no spaces between them. They should be navigation buttons to which you can add one if you need one more. If possible please use flex-box, I would like to understand it.

http://jsfiddle.net/vqL95o0s/1/

CSS

header{
    background-color: #1D3557;
    height: 300px;
    background-repeat: repeat-x;
}

ul.navlist {
    width: auto;
    display: flex;
    justify-content: space-between;

    list-style-type: none;
    align-items: flex-end;
    padding: 0;
  }


a.button {
    height: 75px;
    display: flex;
    align-items: center;
    flex-grow: 1;
    background-color: black;
}

HTML

        <header style="background-image: url('img/headerStripes.png')">
            <img src="img/webdev_logo.png" alt="Logo">
            <ul class="navlist">
                <li><a class="button" href="#">Example</a></li>
                <li><a class="button" href="#">Example 2</a></li>
                <li><a class="button" href="#">Example 3</a></li>
                <li><a class="button" href="#">Example 4</a></li>
                <li><a class="button" href="#">Example 5</a></li>
                <li><a class="button active">Example 6</a></li>
            </ul>
        </header>

The boxes stayed in one place at a distance, but did not get any bigger.

答案1

得分: 2

您需要为li元素设置flex-grow属性,因为它们是navlist的直接子元素,您希望它们能够伸展。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

    header{
        background-color: #1D3557;
        height: 300px;
        background-repeat: repeat-x;
    }

    ul.navlist {
        width: auto;
        display: flex;
        justify-content: space-between;

        list-style-type: none;
        align-items: flex-end;
        padding: 0;
      }

    .navlist li{
      flex-grow:1;
    }

    a.button {
        height: 75px;
        display: flex;
        align-items: center;
        background-color: black;
    }

<!-- language: lang-html -->

            <header style="background-image: url('img/headerStripes.png')">
                <img src="img/webdev_logo.png" alt="Logo">
                <ul class="navlist">
                    <li><a class="button" href="#">示例</a></li>
                    <li><a class="button" href="#">示例2</a></li>
                    <li><a class="button" href="#">示例3</a></li>
                    <li><a class="button" href="#">示例4</a></li>
                    <li><a class="button" href="#">示例5</a></li>
                    <li><a class="button active">示例6</a></li>
                </ul>
            </header>

<!-- end snippet -->
英文:

You need to set the flex-grow property for the li elements as they are the direct children of the navlist in which you want them to grow.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

header{
    background-color: #1D3557;
    height: 300px;
    background-repeat: repeat-x;
}

ul.navlist {
    width: auto;
    display: flex;
    justify-content: space-between;

    list-style-type: none;
    align-items: flex-end;
    padding: 0;
  }

.navlist li{
  flex-grow:1;
}

a.button {
    height: 75px;
    display: flex;
    align-items: center;
    background-color: black;
}

<!-- language: lang-html -->

        &lt;header style=&quot;background-image: url(&#39;img/headerStripes.png&#39;)&quot;&gt;
            &lt;img src=&quot;img/webdev_logo.png&quot; alt=&quot;Logo&quot;&gt;
            &lt;ul class=&quot;navlist&quot;&gt;
                &lt;li&gt;&lt;a class=&quot;button&quot; href=&quot;#&quot;&gt;Example&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a class=&quot;button&quot; href=&quot;#&quot;&gt;Example 2&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a class=&quot;button&quot; href=&quot;#&quot;&gt;Example 3&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a class=&quot;button&quot; href=&quot;#&quot;&gt;Example 4&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a class=&quot;button&quot; href=&quot;#&quot;&gt;Example 5&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a class=&quot;button active&quot;&gt;Example 6&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/header&gt;

<!-- end snippet -->

答案2

得分: 1

问题在于,您正在将 flex-grow 属性应用于 "a 元素"。您应该将 flex-grow 应用于 flexbox 的直接子元素,即 li 项目。以下是更新后的代码:

header {
    background-color: #1D3557;
    height: 300px;
    background-repeat: repeat-x;
}

ul.navlist {
    width: auto;
    display: flex;
    justify-content: space-between;
    list-style-type: none;
    align-items: flex-end;
    padding: 0;
}

ul.navlist > li:nth-child(1) {
    flex-grow: 1;
}

a.button {
    height: 75px;
    display: flex;
    align-items: center;
    background-color: black;
}

在这里,我将 flex-grow 应用于第一个 li 项目

英文:

The problem is that, you are applying flex-grow property on "a element". You should apply the flex-grow on the direct child of flexbox that is li items. Here is the updated code:-

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

header{
    background-color: #1D3557;
    height: 300px;
    background-repeat: repeat-x;
}

ul.navlist {
    width: auto;
    display: flex;
    justify-content: space-between;

    list-style-type: none;
    align-items: flex-end;
    padding: 0;
  }

ul.navlist &gt; li:nth-child(1){
  flex-grow: 1;
}

a.button {
    height: 75px;
    display: flex;
    align-items: center;
    background-color: black;
}

<!-- language: lang-html -->

        &lt;header style=&quot;background-image: url(&#39;img/headerStripes.png&#39;)&quot;&gt;
            &lt;img src=&quot;img/webdev_logo.png&quot; alt=&quot;Logo&quot;&gt;
            &lt;ul class=&quot;navlist&quot;&gt;
                &lt;li&gt;&lt;a class=&quot;button&quot; href=&quot;#&quot;&gt;Example 1&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a class=&quot;button&quot; href=&quot;#&quot;&gt;Example 2&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a class=&quot;button&quot; href=&quot;#&quot;&gt;Example 3&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a class=&quot;button&quot; href=&quot;#&quot;&gt;Example 4&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a class=&quot;button&quot; href=&quot;#&quot;&gt;Example 5&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a class=&quot;button active&quot;&gt;Example 6&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/header&gt;

<!-- end snippet -->

Here I applied flex-grow to the first li item.

答案3

得分: 1

a 标签位于 li 元素内部。这里应该填充空间:

li {
    flex:auto;
}

你不需要使用 flex-grow: 1;

参考链接:https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content

对齐操作在长度和自动边距应用之后进行,这意味着,在 Flexbox 布局中如果至少有一个具有非零的 flex-grow 值的可伸缩元素,它将不会生效,因为没有可用的空间。

英文:

You a tag is inside the li element. This one should fill the space :

li {
    flex:auto;
}

http://jsfiddle.net/dL7nz9xw/

And you don't need the flex-grow: 1;

https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content

> The alignment is done after the lengths and auto margins are applied,
> meaning that, if in a Flexbox layout there is at least one flexible
> element, with flex-grow different from 0, it will have no effect as
> there won't be any available space.

答案4

得分: 0

删除 .navlist 上的 justify-content: space-between(或将其设置回 normal),并将 flex-grow: 1; 设置在你的 li 元素上,而不是你的锚点上。你的 a 不是弹性项,因为它们不是你的 ul 的直接子元素。希望能有所帮助。

英文:

Remove the justify-content: space-between on your .navlist (or set it back to normal) and set the flex-grow: 1; on your li elements rather than your anchors. Your a are not flex items because they are not the direct children of your ul. Hope it helps.

huangapple
  • 本文由 发表于 2023年5月22日 02:44:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定