【问题标题】:Flex container moves off pageFlex 容器移出页面
【发布时间】:2020-10-03 22:28:55
【问题描述】:

我的 flex 容器有问题,基本上只是离开页面并像这样:

我尝试将 display 设置为 table 并使用 <td> 元素将我的东西放入,但它没有显示在框架之外。我希望它是一个 flex 容器,其中包含第一个 <div><header>s。

HTML:

<div class="presen">

        <div class="tisane"> <!--overlap d'images-->
            <img id="img1" src="images/herbal-tea-cup-png-images-35.png" alt="Tasse d'infusion de millepertuis" />
            <img id="img2" src="images/noircontourthe.png" alt="" />
        </div> 

        <header>

        </header>

    </div> 

相关元素的CSS:

      .tisane {
      display: inline-flex;

  }

  .tisane img#img1 {
      display: block;
      margin-left: auto;
      margin-right: auto;
      width: 300px;
      height: 220px;
      position: absolute;
      top: 300px;
      left: 140px;
      z-index: 2;
  }

  .tisane img#img2 {
      display: block;
      margin-left: auto;
      margin-right: auto;
      width: 330px;
      height: 230px;
      position: absolute;
      top: 298px;
      left: 130px;
      z-index: 1;
  }

  header h1 {
      text-align: center;
  }

  .presen {
      display: flex;
      flex-direction: column;
      top: 298px;
      padding: 0;
      
  }

谢谢。

【问题讨论】:

    标签: html css flexbox containers


    【解决方案1】:

    您需要设置bodypresen 类的高度。

    .tisane {
      display: inline-flex;
    }
    
    .tisane img#img1 {
      display: block;
      margin: 0 auto;
      width: 300px;
      height: 220px;
      position: absolute;
      top: 300px;
      left: 140px;
      z-index: 2;
    }
    
    .tisane img#img2 {
      display: block;
      margin-left: auto;
      margin-right: auto;
      width: 330px;
      height: 230px;
      position: absolute;
      top: 600px;
      left: 130px;
      z-index: 1;
    }
    
    header h1 {
      text-align: center;
    }
    
    .presen {
      display: flex;
      flex-direction: column;
      top: 298px;
      padding: 0;
      height: 100%; //The height of a flexbox can be also set to a percentage
    }
    
    body {
      height: 100vh;
    }
    <div class="presen">
    
        <div class="tisane"> <!--overlap d'images-->
            <img id="img1" src="https://via.placeholder.com/250" alt="Tasse d'infusion de millepertuis" />
            <img id="img2" src="https://via.placeholder.com/250" alt="" />
        </div> 
    
        <header>
    
        </header>
    
    </div>

    【讨论】:

    • 我这样做了。谢谢!虽然现在问题不是在 .presen 内部,而是我的图像在它下面?
    • 你的 CSS 的问题是你的两个图像都被定位为 absolute 并且你的两个图像都设置为 top: 300 这使得 #img2 被放置在第一个之下。我更新了我的帖子来证明这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 2021-04-18
    • 2019-01-09
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多