有没有一种方法可以根据中心对齐这些项目?

时间:2020-07-16 21:43:04

标签: html css

我想根据得分将所有内容垂直对齐并居中对齐,但是我找不到一种方法,我主要使用flexbox,但是我还不那么先进事情,这是我要执行的操作的示例,后面是我的代码:

.clanwars {
  display: flex;
  flex-wrap: nowrap;
  flex-direction: column-reverse;
  margin: auto;
  color: #fff;
}

.clanwars .cw-row {
  display: flex;
  flex-wrap: nowrap;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: #22242B;
  border-radius: 4px;
}

.clanwars .cw-row:not(:last-of-type) {
  margin-bottom: 5px;
}

.clanwars .cw-row .cw-infos {
  display: flex;
  flex-wrap: nowrap;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  font-size: 1.5em;
}

.clanwars .cw-row .cw-infos .cw-infos-side {
  display: flex;
  flex-wrap: nowrap;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  text-overflow: ellipsis;
}

.clanwars .cw-row .cw-infos .cw-infos-side:last-child {
  flex-direction: row-reverse;
}

.clanwars .cw-row .cw-infos .cw-infos-side .cw-infos-side-logo {
  display: flex;
  flex-wrap: nowrap;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  width: 50px;
  height: 50px;
  margin: 0 10px;
  background: #1c1e25;
  border-radius: 100px;
}

.clanwars .cw-row .cw-infos .cw-infos-side .cw-infos-side-logo img {
  width: 32px;
  height: 32px;
  border-radius: 100px;
  object-fit: contain;
}

.clanwars .cw-row .cw-infos .cw-infos-score {
  margin: 0 auto;
}

.clanwars .cw-row .cw-infos .cw-infos-score .cw-infos-score-box {
  display: flex;
  flex-wrap: nowrap;
  justify-content: center;
  align-items: center;
  padding: 13px 15px;
  text-align: center;
  text-overflow: ellipsis;
  font-size: 18px;
  color: rgba(255, 255, 255, 0.8);
  background: transparent;
  border-radius: 100px;
}

.clanwars .cw-row .cw-infos .cw-infos-score .cw-infos-score-box span {
  padding: 0 5px;
}

.clanwars .cw-row .cw-details {
  display: flex;
  flex-wrap: nowrap;
  justify-content: center;
  align-items: center;
  margin: 5px 0 15px 0;
}

.clanwars .cw-row .cw-details div {
  margin: 0 10px;
}

.clanwars .cw-row .cw-details span {
  opacity: 0.6;
}

.clanwars .cw-row .cw-details div i {
  font-size: 1.1em;
  opacity: 0.6;
  padding: 0 3px;
}
<div class="clanwars">

  <div id="cw-results">

    <!-- CW BOX -->
    <div class='total won cw-row'>

      <div class='cw-infos'>

        <div class='cw-infos-side'>
          <h5><span>Team Name</span></h5>
          <div class='cw-infos-side-logo'>
            <img style='filter: drop-shadow(0 0 0.5px #FF8900);' src='https://1000logos.net/wp-content/uploads/2017/05/Pepsi-Logo.png'>
          </div>
        </div>

        <div class='cw-infos-score'>
          <div class='cw-infos-score-box won'>85<span>:</span>55</div>
        </div>

        <div class='cw-infos-side'>
          <h5><span>First Opponent</span></h5>
          <div class='cw-infos-side-logo'>
            <img style='filter: drop-shadow(0 0 1px #FFFFFF);' src='https://1000logos.net/wp-content/uploads/2017/05/Pepsi-Logo.png'></div>
        </div>
      </div>

      <!-- TYPE & DATE -->
      <div class='cw-details'>
        <div id='cw-type'><span>WFF</span></div>
        <span>-</span>
        <div id='cw-date'><span>19.06.2020</span></div>
      </div>

    </div>

  </div>

</div>

希望有人能给我解决方案,加油!

2 个答案:

答案 0 :(得分:1)

查看以下解决方案: https://jsfiddle.net/espmxL0a/2/

<div class="box">
  <div class="box__item row">
    <div class="row__item">Team Name</div>
    <div class="row__item score">
      <div class="score__number">100:200</div>
      <div class="score__opponents">ZZZ - WWW</div>
    </div>
    <div class="row__item">First Opponent</div>
  </div>
  
   <div class="box__item row">
    <div class="row__item">Team Name xxxxxxxxx</div>
    <div class="row__item score">
      <div class="score__number">100:200</div>
      <div class="score__opponents">ZZZ - WWW</div>
    </div>
    <div class="row__item">First Opponent</div>
  </div>
  
   <div class="box__item row">
    <div class="row__item">Team Name</div>
    <div class="row__item score">
      <div class="score__number">100:200</div>
      <div class="score__opponents">ZZZ - WWW</div>
    </div>
    <div class="row__item">First Opponent</div>
  </div>
  <div class="box__item row">
    <div class="row__item">Team Name</div>
    <div class="row__item score">
      <div class="score__number">100:200</div>
      <div class="score__opponents">ZZZ - WWW</div>
    </div>
    <div class="row__item">First Opponent yyyyyyy</div>
  </div>
  <div class="box__item row">
    <div class="row__item">Team Name</div>
    <div class="row__item">100:200</div>
    <div class="row__item">First Opponent</div>
  </div>
</div>

scss:

.box {
  background: black;
  height: 100vh;
}

.row {
 background: #bbb;
 border: 1px solid black;
 display: grid;
 grid-template-columns: 1fr auto 1fr;
 
 &__item {
   display: flex;
   padding: 10px;
   align-items: center;
   margin: 2px 0;
   
   &:first-child {
     justify-content: flex-end;
   }
     &:last-child {
     justify-content: flex-start;
   }
 }
}

.score {
  display: flex;
  flex-direction: column;
}

答案 1 :(得分:0)

我没有更改flex布局,而是更新了CSS。我在添加样式的地方添加了注释。您已经很接近了,但是您缺少在flexcw-infos-side元素上设置cw-infos-scores属性。

.clanwars {
  display: flex;
  flex-wrap: nowrap;
  flex-direction: column-reverse;
  margin: auto;
  color: #fff;
}

.clanwars .cw-row {
  display: flex;
  flex-wrap: nowrap;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: #22242B;
  border-radius: 4px;
}

.clanwars .cw-row:not(:last-of-type) {
  margin-bottom: 5px;
}

.clanwars .cw-row .cw-infos {
  display: flex;
  flex-wrap: nowrap;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  font-size: 1.5em;
  /* added this */
  width: 80%
}

.clanwars .cw-row .cw-infos .cw-infos-side {
  display: flex;
  flex-wrap: nowrap;
  flex-direction: row;
  /* Changed to flex-end */
  justify-content: flex-end;
  align-items: center;
  text-overflow: ellipsis;
  /* Added this */
  flex: 0 0 45%;
}

/* added this */
.clanwars .cw-row .cw-infos div:first-child h5 {
  text-align: right;
}

.clanwars .cw-row .cw-infos div:last-child {
  flex-direction: row-reverse;
}

.clanwars .cw-row .cw-infos .cw-infos-side .cw-infos-side-logo {
  display: flex;
  flex-wrap: nowrap;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  width: 50px;
  height: 50px;
  margin: 0 10px;
  background: #1c1e25;
  border-radius: 100px;
  /* Added this */
  flex: 0 0 50px;
}

.clanwars .cw-row .cw-infos .cw-infos-side .cw-infos-side-logo img {
  width: 32px;
  height: 32px;
  border-radius: 100px;
  object-fit: contain;
}

.clanwars .cw-row .cw-infos .cw-infos-score {
  /* Added this */
  flex: 0 0 10%;
}

.clanwars .cw-row .cw-infos .cw-infos-score .cw-infos-score-box {
  display: flex;
  flex-wrap: nowrap;
  justify-content: center;
  align-items: center;
  padding: 13px 15px;
  text-align: center;
  text-overflow: ellipsis;
  font-size: 18px;
  color: rgba(255, 255, 255, 0.8);
  background: transparent;
  border-radius: 100px;
}

.clanwars .cw-row .cw-infos .cw-infos-score .cw-infos-score-box span {
  padding: 0 5px;
}

.clanwars .cw-row .cw-details {
  display: flex;
  flex-wrap: nowrap;
  justify-content: center;
  align-items: center;
  margin: 5px 0 15px 0;
}

.clanwars .cw-row .cw-details div {
  margin: 0 10px;
}

.clanwars .cw-row .cw-details span {
  opacity: 0.6;
}

.clanwars .cw-row .cw-details div i {
  font-size: 1.1em;
  opacity: 0.6;
  padding: 0 3px;
}
<div class="clanwars">

  <div id="cw-results">

    <!-- CW BOX -->
    <div class='total won cw-row'>

      <div class='cw-infos'>

        <div class='cw-infos-side'>
          <h5><span>Team Name Really Long</span></h5>
          <div class='cw-infos-side-logo'>
            <img style='filter: drop-shadow(0 0 0.5px #FF8900);' src='https://1000logos.net/wp-content/uploads/2017/05/Pepsi-Logo.png'>
          </div>
        </div>

        <div class='cw-infos-score'>
          <div class='cw-infos-score-box won'>85<span>:</span>55</div>
        </div>

        <div class='cw-infos-side'>
          <h5><span>First Opponent</span></h5>
          <div class='cw-infos-side-logo'>
            <img style='filter: drop-shadow(0 0 1px #FFFFFF);' src='https://1000logos.net/wp-content/uploads/2017/05/Pepsi-Logo.png'></div>
        </div>
      </div>

      <!-- TYPE & DATE -->
      <div class='cw-details'>
        <div id='cw-type'><span>WFF</span></div>
        <span>-</span>
        <div id='cw-date'><span>19.06.2020</span></div>
      </div>

    </div>
    <!-- /won -->
    <!-- CW BOX -->
    <div class='total won cw-row'>

      <div class='cw-infos'>

        <div class='cw-infos-side'>
          <h5><span>Team Name</span></h5>
          <div class='cw-infos-side-logo'>
            <img style='filter: drop-shadow(0 0 0.5px #FF8900);' src='https://1000logos.net/wp-content/uploads/2017/05/Pepsi-Logo.png'>
          </div>
        </div>

        <div class='cw-infos-score'>
          <div class='cw-infos-score-box won'>185<span>:</span>55</div>
        </div>

        <div class='cw-infos-side'>
          <h5><span>First Opponent Really Long</span></h5>
          <div class='cw-infos-side-logo'>
            <img style='filter: drop-shadow(0 0 1px #FFFFFF);' src='https://1000logos.net/wp-content/uploads/2017/05/Pepsi-Logo.png'></div>
        </div>
      </div>

      <!-- TYPE & DATE -->
      <div class='cw-details'>
        <div id='cw-type'><span>WFF</span></div>
        <span>-</span>
        <div id='cw-date'><span>19.06.2020</span></div>
      </div>

    </div>
    <!-- /won -->
    <!-- CW BOX -->
    <div class='total won cw-row'>

      <div class='cw-infos'>

        <div class='cw-infos-side'>
          <h5><span>Team Name Another One</span></h5>
          <div class='cw-infos-side-logo'>
            <img style='filter: drop-shadow(0 0 0.5px #FF8900);' src='https://1000logos.net/wp-content/uploads/2017/05/Pepsi-Logo.png'>
          </div>
        </div>

        <div class='cw-infos-score'>
          <div class='cw-infos-score-box won'>85<span>:</span>155</div>
        </div>

        <div class='cw-infos-side'>
          <h5><span>First Opponent And Another</span></h5>
          <div class='cw-infos-side-logo'>
            <img style='filter: drop-shadow(0 0 1px #FFFFFF);' src='https://1000logos.net/wp-content/uploads/2017/05/Pepsi-Logo.png'></div>
        </div>
      </div>

      <!-- TYPE & DATE -->
      <div class='cw-details'>
        <div id='cw-type'><span>WFF</span></div>
        <span>-</span>
        <div id='cw-date'><span>19.06.2020</span></div>
      </div>

    </div>
    <!-- /won -->

  </div>

</div>