【问题标题】:Flexbox issue - divs vertically overflow parent elementFlexbox 问题 - div 垂直溢出父元素
【发布时间】:2021-12-24 13:13:55
【问题描述】:

所以我正在为不存在的游戏做一个练习登陆页面,这就是我遇到的问题。 hero-row div 溢出其 header-box 容器,因为它本身已被其内容溢出 header-box 包含 2 行:nav-rowhero-row。它们显示为弹性项目,并且它们本身在内部使用弹性布局。该问题似乎与图像azamat-icon 的填充有关。请帮我把头绕过去,因为这会使hero-row 的内容低于必要的水平,并且header-box 大于屏幕高度,尽管heigth: 100vhScreenshot

这部分项目的代码:

.header-box {
    background-color: grey;
    color: black;
    margin: 0 auto;
    height: 100vh;
}

/************* Nav Bar **************/
.nav-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 2rem 6rem;
}
.nav-ul {
    font-family: 'bold';
    list-style: none;
    display: flex;
}
.nav-item {
    margin-left: 7rem;
    cursor: pointer;
}

/************* Nav Bar **************/


/************* Logo **************/

.logo {
    font-family: 'scbold';
    font-size: 2rem;
    cursor: pointer;
}

/************* Logo **************/

/******************* Hero Starts ************************/

.hero-row {
    font-family: 'bold';
    display: flex;
    width: 100%;
    overflow: hidden;
}
.hero-content {
    flex: 0 0 50%;
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    overflow: hidden;
    padding: 0 5%;
    max-height: inherit;
}
.hero-azamat {
    flex: 0 0 50%;
    overflow: hidden;
    padding: 10%;
    max-height: inherit;
}
.azamat-icon {
    width: 100%;
}
.hero-description {
    font-size: 1.5rem;
    padding-right: 25%;
    margin: 5% 0 10%;
    letter-spacing: 0.01rem;
}
<header>

    <div class="header-box">

      <nav class="nav-row">
        <h1 class="logo">LOGO</h1>
        <ul class="nav-ul">
          <li class="nav-item">About</li>
          <li class="nav-item">Merch</li>
          <li class="nav-item">Purchase</li>
        </ul>
      </nav>

      <div class="hero-row">

        <div class="hero-content">
          <h1 class="hero-heading">
            Start your adventure!
          </h1>
          <p class="hero-description">
            The game is so good and tasty mmm you should definitely try it out baby come on oh i love Freddie Mercury btw
          </p>
          <button class="hero-btn">Buy Now!</button>
        </div>
        
        <div class="hero-azamat">
          <img src="images\164-1648549_8-bit-mario-png-mario-8-bit-png.png" alt="" class="azamat-icon">
        </div>
  
      </div>
      
    </div>

  </header>

如您所见,我尝试了一些方法来解决 overflow: hidden 的问题,但没有奏效。

【问题讨论】:

    标签: html css layout flexbox overflow


    【解决方案1】:

    只是改变

      .header-box {
        height: 100%;
         min-height:100vh;
       } 
    

    HTML

     <header>
        
            <div class="header-box">
        
              <nav class="nav-row">
                <h1 class="logo">LOGO</h1>
                <ul class="nav-ul">
                  <li class="nav-item">About</li>
                  <li class="nav-item">Merch</li>
                  <li class="nav-item">Purchase</li>
                </ul>
              </nav>
        
              <div class="hero-row">
        
                <div class="hero-content">
                  <h1 class="hero-heading">
                    Start your adventure!
                  </h1>
                  <p class="hero-description">
                    The game is so good and tasty mmm you should definitely try it out baby come on oh i love Freddie Mercury btw
                  </p>
                  <button class="hero-btn">Buy Now!</button>
                </div>
                <div class="hero-azamat">    
                </div>
              </div>
            </div>
          </header>
    

    CSS

    .header-box {
        background-color: grey;
        color: black;
        margin: 0 auto;
        height: 100%;
    }
    
    /************* Nav Bar **************/
    .nav-row {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 2rem 6rem;
    }
    .nav-ul {
        font-family: 'bold';
        list-style: none;
        display: flex;
    }
    .nav-item {
        margin-left: 7rem;
        cursor: pointer;
    }
    
    /************* Nav Bar **************/
    
    
    /************* Logo **************/
    
    .logo {
        font-family: 'scbold';
        font-size: 2rem;
        cursor: pointer;
    }
    
    /************* Logo **************/
    
    /******************* Hero Starts ************************/
    
    .hero-row {
        font-family: 'bold';
        display: flex;
        width: 100%;
        overflow: hidden;
    }
    .hero-content {
        flex: 0 0 50%;
        width: 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        overflow: hidden;
        padding: 0 5%;
        max-height: inherit;
    }
    .hero-azamat {
        flex: 0 0 50%;
        overflow: hidden;
        padding: 10%;
        max-height: inherit;
        background:red;
    }
    .azamat-icon {
        width: 100%;
    }
    .hero-description {
        font-size: 1.5rem;
        padding-right: 25%;
        margin: 5% 0 10%;
        letter-spacing: 0.01rem;
    }
    

    jsfiddle

    【讨论】:

    • 但它会保持标题框与屏幕高度相同吗?
    • .header-box { 背景色:灰色;颜色:黑色;边距:0 自动;高度:100%;最小高度:100vh; }
    • 我试试看,谢谢关注
    • 我检查了它似乎工作合法
    猜你喜欢
    • 1970-01-01
    • 2019-01-14
    • 2020-07-08
    • 2016-06-03
    • 1970-01-01
    • 2022-08-08
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    相关资源
    最近更新 更多