【发布时间】:2015-04-22 11:56:35
【问题描述】:
当子元素文本换行时,如何使 HTML 父元素调整其宽度?我需要使用 javascript 还是可以使用 css 完成?
下面是一个例子。
ul {
align-items: stretch;
justify-content: center;
background: lightgrey;
display: flex;
flex-direction: row;
height: 80px;
justify-content: flex-start;
padding-top: 20px;
padding-bottom: 20px;
}
li {
background-color: #303E49;
display: flex;
list-style: none;
margin-right: 0.5em;
}
a {
color: white;
text-decoration: none;
margin: 0.5em 0.5em 0.5em 0.5em;
border: 1px solid white;
}
Resize this window horizontally to see what I am talking about.
<ul>
<li>
<a href="#">HowCanIMakeThisBoxShrink
WhenTheTextBreaksToANewLine?</a>
</li>
<li>
<a href="#">Text</a>
</li>
<li>
<a href="#">Text</a>
</li>
</ul>
我需要容器元素 <li></li> 的宽度来匹配其子元素 <a></a> 的文本宽度。在我当前的实现中,当文本换行时,<a></a> 的宽度会减小,但<li></li> 的宽度不会减小
谢谢
【问题讨论】: