[英]How to display 4 div elements next to each other while position: fixed?
So I'm trying to make 4 boxes fixed in the bottom middle of the page. 因此,我试图在页面的底部中间固定4个框。 But When I apply
position: Fixed;
但是当我申请
position: Fixed;
to them, they just become one stacked box. 对他们来说,它们只是变成一个堆叠的盒子。
.g{ height: 70px; width: 70px; background-color: black; margin: 5px; position: fixed; bottom: 15%; }
<div class="groups"> <div class="g g1"></div> <div class="g g2"></div> <div class="g g3"></div> <div class="g g4"></div> </div>
Make outer div fixed
then inner 4 div element will show next to each other. 使外部div
fixed
然后内部4 div元素将彼此相邻显示。
.groups { display: flex; display: -webkit-flex; position: fixed; margin: 0 auto; left: 0; right: 0; justify-content: space-around; max-width: 500px; } .g{ height: 70px; width: 70px; background-color: black; margin: 5px; }
<div class="groups"> <div class="g g1"></div> <div class="g g2"></div> <div class="g g3"></div> <div class="g g4"></div> </div>
Otherwise you can do it with flex: 否则,您可以使用flex来做到这一点:
.g{ height: 70px; width: 70px; background-color: black; margin: 5px; } .groups { display: flex; justify-content: space-between; width: 400px }
<div class="groups"> <div class="g g1"></div> <div class="g g2"></div> <div class="g g3"></div> <div class="g g4"></div> </div>
Try this fiddle 试试这个小提琴
.groups{
position: fixed;
bottom: 15%;
display:flex;
justify-content:center;
width:100%;
}
.g{
height: 70px;
width: 70px;
background-color: black;
margin: 5px;
}
Just add float:left;
只需添加
float:left;
to the css class .g Of course you MUST remove position: fixed;
当然,您必须删除
position: fixed;
You can also use position: fixed;
您还可以使用
position: fixed;
but you must provide different values for position for each div. 但您必须为每个div的排名提供不同的值。
<div class="groups">
<div class="g g1"></div>
<div class="g g2"></div>
<div class="g g3"></div>
<div class="g g4"></div>
</div>
<div class="groups">
<div class="h h1"></div>
<div class="h h2"></div>
<div class="h h3"></div>
<div class="h h4"></div>
</div>
.g {
height: 70px;
width: 70px;
background-color: black;
margin: 5px;
float:left; /* add this */
border: 1px solid red;
bottom: 15%;
}
.h {
height: 70px;
width: 70px;
background-color: blue;
margin: 5px;
position:fixed;
}
.h1 {
bottom: 50px;
right: 20px;
}
.h2 {
bottom: 50px;
right: 100px;
}
.h3 {
bottom: 50px;
right: 180px;
}
.h4 {
bottom: 50px;
right: 260px;
}
As position is fixed, you may need to sepcify the positions of class g1, g2 and g3 like below. 由于位置是固定的,因此您可能需要像下面这样区分类g1,g2和g3的位置。
.g1{
left: 0px;
}
.g2{
left: 100px;
}
.g3{
left: 200px;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.