【问题标题】:Make a child HTML element fill it's parent container, both vertically and horizontally - (CSS & Flexbox) [duplicate]使子 HTML 元素垂直和水平填充它的父容器-(CSS 和 Flexbox)[重复]
【发布时间】:2021-10-20 14:53:55
【问题描述】:

我不知道如何让子元素 (#child3) 垂直和水平填充它的父容器。

我已经尝试了 100 种使用和不使用 Flexbox 的不同方法,但我不知道如何做到这一点。有谁知道这是怎么实现的?

#container{
        
  height: 50vh;
  width: 50%;
  min-width: 500px;
}
    
#container nav{
  height: 5rem;
}

#container nav ul{
  height: 100%;
  padding-left: 1rem;
  display: flex;
}

#container nav ul li{
  display: flex;
  align-items: center;
  padding: 0rem 2rem;
  height: 100%;
}

#container nav ul li:first-child{
  margin-left: -1rem;
}

#container nav ul li:last-child{
  margin-left: auto;
}
    
#child3{}
<section id="container">
    <nav>
        <ul>
            <li id="child1">Settings</li>
            <li id="child2">Account</li>
            <li><span id="child3" class="material-icons">close</span></li>
        </ul>
    </nav>
</section>

【问题讨论】:

  • 你的意思是“填满整个parent”还是“填满parent的剩余空间”
  • 我不确定我是否理解其中的区别。我希望字体图标与它的父元素的确切大小相匹配,这样我就可以使它成为一个链接,如果单击容器中的任何位置,则该链接将处于活动状态,因此光标不必直接位于图标上方。希望这能澄清我正在尝试做的更多事情。

标签: css flexbox


【解决方案1】:

试试

#child3 {
  width: 100%;
  height: 100%;
}

百分比是相对于父元素的。

【讨论】:

  • 是的,这是我尝试的第一件事。垂直高度有效,但宽度无效。此外,当应用 height: 100% - align-items: center 中断 css 格式。
【解决方案2】:

您只需将#child3 父级的宽度设置为 100%。您可以在代码块中看到它。

#container{
        
  height: 50vh;
  width: 50%;
  min-width: 500px;
}
    
#container nav{
  height: 5rem;
}

#container nav ul{
  height: 100%;
  padding-left: 1rem;
  display: flex;
}

#container nav ul li{
  display: flex;
  align-items: center;
  padding: 0rem 2rem;
  height: 100%;
}

#container nav ul li:first-child{
  margin-left: -1rem;
}

#container nav ul li:last-child{
  margin-left: auto;
}
    
#child3{
  width: 100%;
  height: 100%;
  background: red;
}

.w-100 {
  width: 100%;
}
<section id="container">
    <nav>
        <ul>
            <li id="child1">Settings</li>
            <li id="child2">Account</li>
            <li class="w-100"><span id="child3" class="material-icons">close</span></li>
        </ul>
    </nav>
</section>

推荐你玩这个游戏来学习more about flexboxes.

【讨论】:

  • 这与对伪类“last-child”应用 100% 宽度是一样的,这反过来又会破坏 margin-left: auto 样式。这不好。幸运的是,弗朗西斯有上述正确的解决方案。
【解决方案3】:

如果您只想填充父级,只需添加 #child3 的样式

width: 100%;
height: 100%;

但是如果你想填充父级的整个空间,你应该首先删除ul的最后一个子级的填充,在这里添加一些东西:

#container nav ul li:last-child{
      margin-left: auto;
      padding 0;
}

然后在#child3 中添加 100% 的高度和宽度。但这会改变#child3 父级的宽度,因此您向#child3 添加填充

您的代码将如下所示

#container{
  height: 50vh;
  width: 50%;
  min-width: 500px;
}
    
#container nav{
  height: 5rem;
}

#container nav ul{
  height: 100%;
  padding-left: 1rem;
  display: flex;
}

#container nav ul li{
  display: flex;
  align-items: center;
  padding: 0rem 2rem;
  height: 100%;
  
  /* just to view the size of the element */
  background: black;
  opacity: 50%;
}

#container nav ul li:first-child{
  margin-left: -1rem;
}

#container nav ul li:last-child{
  margin-left: auto;
  padding: 0;
}
    
#child3{
  height: 100%;
  width: 100%;
  padding: 0rem 2rem;
  
  /* just to view the size of the element */
  background: red;
  opacity: 50%;
}
<section id="container">
    <nav>
        <ul>
            <li id="child1">Settings</li>
            <li id="child2">Account</li>
            <li><span id="child3" class="material-icons">close</span></li>
        </ul>
    </nav>
</section>

【讨论】:

  • 哦,哇,非常感谢。这就像你说的那样有效。所以 height 和 width 100% 指的是考虑 padding 后的 100% 的大小。这听起来对吗?
  • 是的,但我的回答并不完美,span中文本的垂直对齐方式发生了变化,但很容易解决,所以干杯。
  • 是的,我添加了: display: flex 和 align-items: center;到使图标重新居中的 SPAN 元素。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-01
  • 2015-03-07
  • 2019-07-19
相关资源
最近更新 更多