【问题标题】:how to adjust the inner elements based on parent height/width changes?如何根据父级高度/宽度变化调整内部元素?
【发布时间】:2016-08-09 11:22:16
【问题描述】:

我在水平对齐的div 中有ulli。我使用了flex 显示。这里 div 是父容器,所有样式,如背景、高度、宽度都将在这里应用。 li 元素有一些内部内容,如 div、span 可能有文本/图标。 每当我调整父 div 的宽度/高度时,都应该调整内部内容。图标/文本(li 的子跨度)需要居中。填充将需要调整。我在下面的 plunker 中编辑了我的需求

plunker link

您能否检查一下并让我知道任何选项。感谢您的任何建议。

这是我的css代码

 .table_ul {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  width: 100%;
  height:100%;
  flex-wrap: wrap;
  list-style: none;
  padding:0px;
}
.cells_li {
  flex: 1 1 auto;
  border: 1px solid red;
  border-right:none;
  padding: .5em;
  text-align: center;
}

.cells_li:last-child {
  border-right: 1px solid red;
}
.main_div {
  background:gray;
  width:400px;
  height:20px;
}

下面是我的html页面

<div id="group" class="main_div">
        <ul class="table_ul">
            <li class="cells_li"><div><span>desktop</span></div></li>
            <li class="cells_li"><div><span>laptop</span></div></li>
            <li class="cells_li"><div><span>work</span></div></li>
        </ul>
    </div>

【问题讨论】:

  • 你已经限制了.main_div的高度,你需要删除它才能开始。
  • 或许您可以提供图片以便我们了解应该的外观?

标签: html css styles


【解决方案1】:

您可以将float:left 分配给LI 和height:100% 以使它们获得父高度。

/* Styles go here */
.table_ul {
  width: 100%;
  height:100%;
  list-style: none;
  padding:0px;

}
.cells_li {
  border: 1px solid red;
  border-right:none;
  padding: 0.5em;
  text-align: center;
  width:calc(33% - 1em);
  float:left;display: inline;
  height:calc(100% - 1em);
}

.cells_li:last-child {
  border-right: 1px solid red;
}
.main_div {
  background:gray;
  width:400px;
  height:150px;
}

【讨论】:

  • 您的代码看起来不错。但我的需要是我必须对 li 应用填充。在这种情况下,孩子的填充也需要根据父高度和宽度进行更新。它应该是响应式的
  • 这能解决您的问题吗?
  • 是的,我正在检查。在其他情况下面临问题
  • 嘘,我还有一些其他对齐问题,现在工作正常。根据您的建议,我已经完成了对齐。谢谢
猜你喜欢
  • 1970-01-01
  • 2013-02-07
  • 1970-01-01
  • 2012-02-27
  • 2014-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多