How can I make a div's width be contained between two other divs with 'space-around' spacing?

huangapple go评论64阅读模式
英文:

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:
How can I make a div's width be contained between two other divs with 'space-around' spacing?

But ending up with this:

How can I make a div's width be contained between two other divs with 'space-around' spacing?

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 -->

&lt;div class=&quot;duel&quot;&gt;
    &lt;img class=&quot;opponent center&quot; src=&quot;https://picsum.photos/100?asdf=1&quot; draggable=&quot;false&quot; /&gt;
    &lt;div class=&quot;verticalLine center&quot; &gt;&lt;/div&gt;
    &lt;div class=&quot;horizontalLine&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;verticalLineWrapper&quot;&gt;
        &lt;div class=&quot;verticalLine&quot;&gt;&lt;/div&gt;
        &lt;div class=&quot;verticalLine&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;opponets&quot;&gt;
        &lt;img class=&quot;opponent&quot; src=&quot;https://picsum.photos/100?asdf=2&quot; draggable=&quot;false&quot; /&gt;
        &lt;img class=&quot;opponent&quot; src=&quot;https://picsum.photos/100?asdf=3&quot; draggable=&quot;false&quot; /&gt;
    &lt;/div&gt;
  &lt;/div&gt;

<!-- 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:

&lt;div class=&quot;line-container&quot;&gt;
  &lt;div class=&quot;verticalLine&quot;/&gt;
  &lt;div class=&quot;arc&quot;/&gt;
&lt;/div&gt;

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 -->

&lt;div class=&quot;duel&quot;&gt;
  &lt;img class=&quot;opponent center&quot; src=&quot;https://robohash.org/player1&quot; /&gt;

  &lt;div class=&quot;line-container&quot;&gt;
    &lt;div class=&quot;verticalLine&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;arc&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class=&quot;opponets&quot;&gt;
    &lt;img class=&quot;opponent&quot; src=&quot;https://robohash.org/player2&quot; /&gt;
    &lt;img class=&quot;opponent&quot; src=&quot;https://robohash.org/player1&quot; /&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月26日 16:19:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定