gpt4 book ai didi

html - 我怎样才能将我的 flex 元素展开到超过容器的宽度?

转载 作者:太空狗 更新时间:2023-10-29 12:29:55 26 4
gpt4 key购买 nike

我想使用 flex 通过 CSS 创建类似的东西:

Click to see the image in fullscreen


到目前为止我试过的是这段代码:

#dot-container {
position: absolute;
width: 30vw;
background: black;
height: 8vw;
justify-content: center;
align-items: center;
display: flex;
}

.dot {
border-radius: 100%;
width: 2vw;
height: 2vw;
margin: 3.2%; /*(30-2*7) / (7-2)*/
background: green;
}
<div id="dot-container">
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
</div>

但我无法真正让它工作,使得最左边和最右边的点恰好位于左 Angular 和右 Angular ,就像你在我上面的图片中看到的那样。

注意:还有 justify-content: space-between; 似乎不起作用,因为点本身最左边但不是点中心!

最佳答案

这可以使用 justify-content: space-between 和使用负的 margin 来完成。

#dot-container {
position: absolute;
width: 30vw;
background: black;
height: 8vw;
justify-content: space-between;
align-items: center;
display: flex;
}

.dot {
border-radius: 100%;
width: 2vw;
height: 2vw;
margin: -3.2%; /*(30-2*7) / (7-2)*/
background: green;
}
<div id="dot-container">
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
</div>

或者,如果您不想计算边距,您可以添加一个具有绝对定位的子元素,并根据需要更改 div.dot 元素的数量:

#dot-container {
position: relative;
width: 30vw;
height: 6vw;
justify-content: space-between;
align-items: center;
display: flex;
}

#dot-container-inner {
position: absolute;
left: 3%;
top: 0;
z-index: -1;
height: 100%;
width: 95%;
background: black;
}

.dot {
border-radius: 100%;
width: 2vw;
height: 2vw;
background: green;
}
<div id="dot-container">
<div id="dot-container-inner">
</div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
</div>

关于html - 我怎样才能将我的 flex 元素展开到超过容器的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56209669/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com