英文:
How can I make a div's width be contained between two other divs with 'space-around' spacing?
问题
问题在于当我将horizontalLine
的宽度设置为100%时,它被设置为verticalLineWrapper
的整个宽度,而不是其两个元素之间的宽度。有没有一种简洁的方法来设置horizontalLine
的宽度,使其看起来像第一张图片中的样子?
要实现您的设计,您可以使用Flexbox来布局元素,并将horizontalLine
的宽度设置为适当的值,以使其在两个verticalLine
之间居中。以下是更新后的CSS代码:
.duel {
display: flex;
flex-direction: column;
}
.verticalLine {
background-color: black;
width: 5px;
height: 50px;
}
.center {
align-self: center;
}
.horizontalLine {
background-color: black;
width: 60%; /* 设置为适当的宽度,以使其在两个verticalLine之间居中 */
height: 5px;
align-self: center;
}
.verticalLineWrapper {
display: flex;
justify-content: space-around;
width: 100%;
}
.opponent {
width: 100px;
}
.opponents {
display: flex;
justify-content: space-around;
gap: 4px;
}
通过将horizontalLine
的宽度设置为60%(或您认为合适的值),它将在两个verticalLine
之间居中,从而实现您所期望的效果。
英文:
I am trying to make a div whose width will be contained between two other divs that are spaced with space-around instead of the entire width of the containing div.
I am trying to reach a design that looks like a tournament. This would be the goal:
But ending up with this:
Here is my code:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.duel {
display: flex;
flex-direction: column;
}
.verticalLine {
background-color: black;
width: 5px;
height: 50px;
}
.center {
align-self: center;
}
.horizontalLine {
background-color: black;
width: 100%;
height: 5px;
align-self: center;
}
.verticalLineWrapper {
display: flex;
justify-content: space-around;
width: 100%;
}
.opponent {
width: 100px;
}
.opponets {
display: flex;
justify-content: space-around;
gap: 4px;
}
<!-- language: lang-html -->
<div class="duel">
<img class="opponent center" src="https://picsum.photos/100?asdf=1" draggable="false" />
<div class="verticalLine center" ></div>
<div class="horizontalLine"></div>
<div class="verticalLineWrapper">
<div class="verticalLine"></div>
<div class="verticalLine"></div>
</div>
<div class="opponets">
<img class="opponent" src="https://picsum.photos/100?asdf=2" draggable="false" />
<img class="opponent" src="https://picsum.photos/100?asdf=3" draggable="false" />
</div>
</div>
<!-- end snippet -->
The problem is that when I set horizontalLine's width to 100% it is set to the full width of verticalLineWrapper instead of the width between its two elements.
Is there an elegant way to set the width of horizontalLine to look like in the first image?
答案1
得分: 0
你可以只使用一个带有左、右和上边框的 <div>
来绘制整个弧线:
<div class="line-container">
<div class="verticalLine"></div>
<div class="arc"></div>
</div>
和
.line-container {
display: flex;
align-items: center;
flex-direction: column;
}
.arc {
width: 50%;
border-width: 5px 5px 0;
border-style: solid;
border-color: black;
height: 50px;
}
在片段中查看效果:
<div class="duel">
<img class="opponent center" src="https://robohash.org/player1" />
<div class="line-container">
<div class="verticalLine"></div>
<div class="arc"></div>
</div>
<div class="opponents">
<img class="opponent" src="https://robohash.org/player2" />
<img class="opponent" src="https://robohash.org/player1" />
</div>
</div>
英文:
You could use just one div with border left, right and top to draw the whole arc in one:
<div class="line-container">
<div class="verticalLine"/>
<div class="arc"/>
</div>
and
.line-container{
display: flex;
align-items: center;
flex-direction: column;
}
.arc{
width: 50%;
border-width: 5px 5px 0;
border-style: solid;
border-color: black;
height: 50px;
}
See how it looks in the snippet:
<!-- begin snippet: js hide: true console: false babel: false -->
<!-- language: lang-css -->
.duel {
display: flex;
flex-direction: column;
}
.verticalLine {
background-color: black;
width: 5px;
height: 50px;
}
.center {
align-self: center;
}
.opponent {
width: 100px;
}
.opponets {
display: flex;
justify-content: space-around;
gap: 4px;
}
.line-container {
display: flex;
align-items: center;
flex-direction: column;
}
.arc {
width: 50%;
border-width: 5px 5px 0;
border-style: solid;
border-color: black;
height: 50px;
}
<!-- language: lang-html -->
<div class="duel">
<img class="opponent center" src="https://robohash.org/player1" />
<div class="line-container">
<div class="verticalLine"></div>
<div class="arc"></div>
</div>
<div class="opponets">
<img class="opponent" src="https://robohash.org/player2" />
<img class="opponent" src="https://robohash.org/player1" />
</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论