所以我想将多个div对齐(水平居中)。如果有更多n个div,div所在的容器应该在x方向上滚动,就像一个简单的滚动条一样,得到这样的东西:
注意:有人会在图片之前添加(!)
吗?
但是 - 到目前为止我无法使用此代码:
#container {
position: relative;
width: 80%;
height: 40%;
overflow-x: scroll;
background: gray;
}
.item {
position: absolute;
width: 10%;
height: 80%;
background-image: url("http://www.icemeltmanufacturers.com/wp-content/uploads/2016/09/head-icon-png-5.png");
background-repeat: no-repeat;
background-position: 50% 50%;
margin: auto;
top: 0;
bottom: 0;
background-size: contain;
}
#bigContainer {
position: absolute;
width: 100%;
height: 50%;
background: white;
bottom: 0%;
border-radius: 15px 15px 0px 0px
}

<div id="bigContainer">
<div id="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</div>
&#13;
注意:看起来Stackoverflow目前已经破产了,所以有测试代码的小提琴:https://jsfiddle.net/nfdgyx73/13/
注意:提供一个解决方案,我不需要对容器进行大量更改(特别是不要更改其位置属性),这很酷。
任何帮助都将非常感谢,提前感谢一百万!
答案 0 :(得分:0)
只需显示内联项(display: inline-block
),并确保没有包装容器,因为它使用%来计算宽度(white-space: nowrap
),同时确保没有溢出对于y轴上的容器(overflow-y: hidden
)
根据您的评论,我还在容器ID中添加了text-align: center
,因此如果不需要滚动的项目较少,它们将会居中。
#container {
position: relative;
width: 80%;
height: 40%;
overflow-x: scroll;
background: gray;
overflow-y: hidden;
white-space: nowrap;
text-align: center;
}
.item {
display: inline-block;
width: 10%;
height: 80%;
background-image: url("http://www.icemeltmanufacturers.com/wp-content/uploads/2016/09/head-icon-png-5.png");
background-repeat: no-repeat;
background-position: 50% 50%;
margin: 4px;
top: 0;
bottom: 0;
background-size: contain;
}
#bigContainer {
position: absolute;
width: 100%;
height: 50%;
background: white;
bottom: 0%;
border-radius: 15px 15px 0px 0px
}
<div id="bigContainer">
<div id="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</div>
答案 1 :(得分:0)
我的解决方案使用弹性框根据需要对齐所有项目。如果有多个项目而不是空间,则添加一个滚动条(取消注释额外的div以查看它的实际效果)
https://jsfiddle.net/nfdgyx73/58/
#container {
position: relative;
width: 80%;
height: 40%;
overflow-x: scroll;
background: gray;
display: inline-flex;
flex-direction: row;
align-items: center;
}
#container::before, #container::after {
content: '';
margin: auto;
}
.item {
width: 10%;
height: 80%;
background: url("http://www.icemeltmanufacturers.com/wp-content/uploads/2016/09/head-icon-png-5.png");
background-repeat: no-repeat;
background-position: 50% 50%;
background-size: contain;
margin: 0 5px;
flex-shrink: 0;
}
#bigContainer {
position: absolute;
width: 100%;
height: 50%;
background: white;
bottom: 0%;
border-radius: 15px 15px 0px 0px
}
&#13;
<div id="bigContainer">
<div id="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<!-- <div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div> -->
</div>
</div>
&#13;
答案 2 :(得分:0)
如果position: absolute;
不是必需的话,可以使用flexboxes来完成
#container {
display: flex;
justify-content: center;
width: -webkit-fit-available;
padding: 20px;
background: gray;
}
#container .item {
margin-right: 10px;
}
#container .item:last-child {
margin-right: 0;
}
.item {
width: 25px;
height: 25px;
background-image: url("http://www.icemeltmanufacturers.com/wp-content/uploads/2016/09/head-icon-png-5.png");
background-repeat: no-repeat;
background-position: 50% 50%;
background-size: contain;
}
#bigContainer {
display: flex;
width: 80%;
background: white;
overflow-x: auto;
border-radius: 15px 15px 0px 0px;
margin-bottom: 20px; /* This is just to help differentiate the two bigContainers */
}
&#13;
<div id="bigContainer">
<div id="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</div>
<div id="bigContainer">
<div id="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</div>
&#13;