我有一个动态填充项的列表,需要比其绝对定位的父元素更宽(这是自定义的
#testContainer
中使用 display: flex
替代 inline-flex
width: calc(50vw - 50%)
left:-50%
和 width: calc(100vw - 50%)
.testItem
中使用 flex: 1
替代 width: 200px
word-wrap: break-word
#wrapper {
position: relative;
border: 1px dashed black;
width: 300px;
margin: 0 auto;
}
#testContainer {
display: flex;
position: absolute;
right: 0;
background-color: fuchsia;
list-style: none;
padding: 0;
border: 5px dashed orange;
width: calc(50vw - 50%)
}
#testLabel {
width: 300px;
background-color: yellow;
}
.testItem {
flex: 1;
height: 50px;
background-color: #aaa;
word-wrap: break-word
}
@media (max-width: 800px) {
#testContainer {
right: auto;
left: -50%;
width: calc(100vw - 50%)
}
}
<div id="wrapper">
<div id="testLabel">label</div>
<ul id="testContainer">
<li class="testItem">aaaaaaaaaaaaaaaaaaaaaaaaaaapppppppppp</li>
<li class="testItem">www</li>
<li class="testItem">cccccccccccccccccc</li>
</ul>
</div>
#testContainer {
display: flex;
position: absolute;
right: 0;
background-color: fuchsia;
list-style: none;
overflow-x: auto; /* Added this here */
padding: 0;
border: 5px dashed orange;
width: calc(50vw - 50%)
}
<select>
元素中的一个或多个答案。 - montrealist让我们不使用 flex,像我们的祖先教导我们的那样工作!
#wrapper {
border: 1px dashed black;
margin: 0 auto;
position: relative;
width: 300px;
}
#testLabel {
background: yellow;
}
#testContainer {
background: fuchsia;
border: 5px dashed orange;
font-size: 0;
list-style: none;
padding: 0;
position: absolute;
right: 0;
white-space: nowrap;
}
.testItem {
background: #aaa;
display: inline-block;
font-size: 16px;
height: 50px;
min-width: 200px;
}
<div id="wrapper">
<div id="testLabel">label</div>
<ul id="testContainer">
<li class="testItem">aaaaaaaaaaaaaaaaaaaaaaaaaaapppppppppp</li>
<li class="testItem">www</li>
<li class="testItem">cccccccccccccccccc</li>
</ul>
</div>
li
项目并排显示。这是一个严格的要求。 - montrealist