当子元素文本换行到新行时,如何使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;
}Run Code Online (Sandbox Code Playgroud)
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>Run Code Online (Sandbox Code Playgroud)
我需要容器元素<li></li>宽度来匹配其子元素的文本宽度<a></a> …