【问题标题】:My figures won't center in my div我的数字不会在我的 div 中居中
【发布时间】:2018-04-04 23:41:28
【问题描述】:

对于一个学校项目,我需要制作一个网站,作为我们项目的一部分(制作一个可以洗牌和发牌的乐高机器人)。在我网站的商品部分,我的数字排列成一排,但不知何故它们没有居中(参见商品标签:http://i385436.hera.fhict.nl/theshufflr/index.html),我已经尝试了所有方法。

这些应该居中:

有人可以帮我解决这个问题吗?以下是与商品标签配合的部分代码:

.et-slide {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
  position: relative;
  background: black;
  text-align: center;
  padding: 0;
  margin: 0;
  width: 100%;
}

.introback {
  position: relative;
  height: 100vh;
  width: 100%;
}

.introback img {
  text-align: center;
  position: relative;
  z-index: 0;
  width: 100%;
}

.introoverlay {
  position: absolute;
  top: 20vh;
  left: 30%;
  height: 100% width: 60%;
  color: white;
  z-index: 1;
  text-align: center;
  vertical-align: center;
  padding: 0;
  margin: 0;
}

.merchoverlay {
  position: absolute;
  top: 10vh;
  left: 30%;
  height: 100% width: 60%;
  color: white;
  z-index: 1;
  text-align: center;
  vertical-align: center;
  padding: 0;
  margin: 0;
}

.et-slide-merch {
  display: flex;
  height: 40vh;
  text-align: center;
  padding: 0;
  left: 30%;
  width: 100%;
}

.et-slide-merch figure {
  display: inline-block;
  margin: 20px;
}

.et-slide-merch figure img {
  display: inline-block;
  vertical-align: top;
  border-radius: 50%;
  width: 200px;
  height: 200px;
}

.et-slide-merch figure figcaption {
  border: 0px;
  text-align: center;
  color: white;
}

.et-slide-merch figure figcaption h {
  font-size: 1.5rem;
  font-weight: bold;
  letter-spacing: 0.2rem;
  color: white;
}

.et-slide-merch figure figcaption p {
  font-size: 1.1rem;
  letter-spacing: 0.1rem;
  color: white;
}
<!-- Merch page -->
<section class="et-slide" id="tab-shop">
  <div class="introback">
    <img src="merch/background.jpg">
    <div class="merchoverlay">
      <h1>MERCHANDISE</h1>
      <h2>Get everything you need right here.</h2><br><br><br>
      <section class="et-slide-merch">
        <figure>
          <img src="merch/unisexhoodie.png">
          <figcaption><br>
            <h>Everyday I'm<br>Shufflin' hoodie</h>
            <br>
            <p>Unisex - XS, S, M, L, XL, XXL</p>
          </figcaption>
        </figure>
        <figure>
          <img src="merch/unisexhoodie.png">
          <figcaption><br>
            <h>Everyday I'm<br>Shufflin' hoodie</h>
            <br>
            <p>Unisex - XS, S, M, L, XL, XXL</p>
          </figcaption>
        </figure>
        <figure>
          <img src="merch/unisexhoodie.png">
          <figcaption><br>
            <h>Everyday I'm<br>Shufflin' hoodie</h>
            <br>
            <p>Unisex - XS, S, M, L, XL, XXL</p>
          </figcaption>
        </figure>
        <figure>
          <img src="merch/unisexhoodie.png">
          <figcaption><br>
            <h>Everyday I'm<br>Shufflin' hoodie</h>
            <br>
            <p>Unisex - XS, S, M, L, XL, XXL</p>
          </figcaption>
        </figure>
      </section>
    </div>
  </div>
</section>

任何帮助都非常感谢,在此先感谢!

【问题讨论】:

  • 我已经添加了一个屏幕截图,我很确定你会得到什么。如果我错了,请告诉我(编辑)
  • 那是因为你使用 left:30% simple use text-align center to center 定位....
  • &lt;br&gt; 现在已弃用,不应使用,请使用边距或填充到空间元素而不是 &lt;br&gt; developer.mozilla.org/en-US/docs/Web/HTML/Element/br

标签: html image center


【解决方案1】:

使用以下代码更改您的 CSS 元素。

删除 /* 左:30%; */ 来自“merchoverlay”并创建 flex 带有 justify 中心的环境将解决此问题。

 .introback {
    display: flex;
    justify-content: center;
}
.merchoverlay {
    position: absolute;
    top: 10vh;
    /* left: 30%; */
    height: 100% width: 60%;
    color: white;
    z-index: 1;
    text-align: center;
    vertical-align: center;
    padding: 0;
    justify-content: center;
    margin: 0;
}

如果您添加以下更改,则文本 (h1 anf h2) 内容也会居中。

.et-hero-tabs h2, .et-slide h2 {
    font-size: 3rem;
    margin: 0;
    letter-spacing: 0.6rem;
    color: white;
    /* width: 60%; */
}

.et-hero-tabs h1, .et-slide h1 {
    color: white;
    font-size: 1.5rem;
    /* width: 60%; */
}

希望这对您有所帮助。 干杯。

【讨论】:

  • 很高兴 :) (Y)
【解决方案2】:

您在 css 代码中遇到语法错误。在此处获取更新的样式:

.introoverlay {
  position: absolute;
  top: 20vh;
  left: 20%;
  height: 100%;
  width: 60%;
  color: white;
  z-index: 1;
  text-align: center;
  vertical-align: center;
  padding: 0;
  margin: 0;
}

【讨论】:

    【解决方案3】:

    添加以下 CSS:

    .merchoverlay {
        left: unset;
        width: 100%;
    }
    
    .et-slide-merch {
        width: fit-content;
        margin-left: auto;
        margin-right: auto;
    }
    
    .merchoverlay h1, .merchoverlay h2 {
        margin-left: auto;
        margin-right: auto;
    }
    

    【讨论】:

      【解决方案4】:

      首先您需要将flex-wrap: wrap;justify-content: center; 添加到.et-slide-merch 以及下面的一些小的css 调整

      .merchoverlay {
          /* remove left: 30%; */
          width: 100%;
      }
      
      .et-slide-merch {
          flex-wrap: wrap; 
          /* remove align-items: center; */
          justify-content: center;
          /* remove height: 40vh; */
      }
      

      示例:

      .et-slide {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        height: 100vh;
        position: relative;
        background: black;
        text-align: center;
        padding: 0;
        margin: 0;
        width: 100%;
      }
      
      .introback {
        position: relative;
        height: 100vh;
        width: 100%;
      }
      
      .introback img {
        text-align: center;
        position: relative;
        z-index: 0;
        width: 100%;
      }
      
      .introoverlay {
        position: absolute;
        top: 20vh;
        left: 30%;
        height: 100% width: 60%;
        color: white;
        z-index: 1;
        text-align: center;
        vertical-align: center;
        padding: 0;
        margin: 0;
      }
      
      .merchoverlay {
        position: absolute;
        top: 10vh;
         /* left: 30%; remove*/
        height: 100% width: 60%;
        color: white;
        z-index: 1;
        text-align: center;
        vertical-align: center;
        padding: 0;
        margin: 0;
        width: 100%;
      }
      
      
      .et-slide-merch {
        display: flex;
        /* height: 40vh; remove*/
        text-align: center;
        padding: 0;
        left: 30%;
        width: 100%;
        
        justify-content: center;
        flex-wrap: wrap;
           
         
         
          
          
      }
      
      .et-slide-merch figure {
        display: inline-block;
        margin: 20px;
      }
      
      .et-slide-merch figure img {
        display: inline-block;
        vertical-align: top;
        border-radius: 50%;
        width: 200px;
        height: 200px;
      }
      
      .et-slide-merch figure figcaption {
        border: 0px;
        text-align: center;
        color: white;
      }
      
      .et-slide-merch figure figcaption h {
        font-size: 1.5rem;
        font-weight: bold;
        letter-spacing: 0.2rem;
        color: white;
      }
      
      .et-slide-merch figure figcaption p {
        font-size: 1.1rem;
        letter-spacing: 0.1rem;
        color: white;
      }
      
      
      
      .merchoverlay {
          /* left: 30%; */
          width: 100%;
      }
      
      .et-slide-merch {
          flex-wrap: wrap; */
          justify-content: baseline;
          /* align-items: center; */
          justify-content: center;
          /* height: 40vh; */
      }
      <!-- Merch page -->
      <section class="et-slide" id="tab-shop">
        <div class="introback">
          <img src="http://i385436.hera.fhict.nl/theshufflr/merch/background.jpg">
          <div class="merchoverlay">
            <h1>MERCHANDISE</h1>
            <h2>Get everything you need right here.</h2><br><br><br>
            <section class="et-slide-merch">
              <figure>
                <img src="http://i385436.hera.fhict.nl/theshufflr/merch/unisexhoodie.png">
                <figcaption><br>
                  <h>Everyday I'm<br>Shufflin' hoodie</h>
                  <br>
                  <p>Unisex - XS, S, M, L, XL, XXL</p>
                </figcaption>
              </figure>
              <figure>
                <img src="http://i385436.hera.fhict.nl/theshufflr/merch/unisexhoodie.png">
                <figcaption><br>
                  <h>Everyday I'm<br>Shufflin' hoodie</h>
                  <br>
                  <p>Unisex - XS, S, M, L, XL, XXL</p>
                </figcaption>
              </figure>
              <figure>
                <img src="http://i385436.hera.fhict.nl/theshufflr/merch/unisexhoodie.png">
                <figcaption><br>
                  <h>Everyday I'm<br>Shufflin' hoodie</h>
                  <br>
                  <p>Unisex - XS, S, M, L, XL, XXL</p>
                </figcaption>
              </figure>
              <figure>
                <img src="http://i385436.hera.fhict.nl/theshufflr/merch/unisexhoodie.png">
                <figcaption><br>
                  <h>Everyday I'm<br>Shufflin' hoodie</h>
                  <br>
                  <p>Unisex - XS, S, M, L, XL, XXL</p>
                </figcaption>
              </figure>
            </section>
          </div>
        </div>
      </section>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-04
        • 2011-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-04
        • 1970-01-01
        相关资源
        最近更新 更多