代码之家  ›  专栏  ›  技术社区  ›  Danny Lo

如何让两个垂直元素的高度中的一个依赖于另一个?

  •  0
  • Danny Lo  · 技术社区  · 7 年前

    底部元素是一个图像,应该与视口底部对齐。它应按比例调整大小,最大值为视口宽度的90%,最大值为视口高度的70%。上面的元素是一个文本元素,我希望它填充剩余的垂直空间,并将该文本水平和垂直地与中心对齐。

    这就是我目前所拥有的。

    body { 
        background-image: url('https://i.imgur.com/sWfZ8nq.png');
        background-repeat: repeat;
    }
    
    .fg {
        margin: auto;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        max-height: 100%;
    }
    
    .fg div {
        display: flex;
        align-items: center;
        position: absolute;
        width: 100%;
        height: 100%;
        max-height: 30%;
    }
    
    .fg span {
        width: 100%;
        text-align: center;
        color: white;
        font-size: 10vmin;
        letter-spacing: 3px;
        text-shadow:  2px  2px 2px green,
                      2px -2px 2px green,
                     -2px  2px 2px green,
                     -2px -2px 2px green;
    }
    
    .fg img {
        position: absolute;
        bottom: 0;
        left: 50%;
        transform: translateX(-50%);
        max-height: 70%;
        max-width: 90%;
    }
    <html>
      <head>
        <title>HNY</title>
        <meta charset="utf-8">
      </head>
      <body>
        <div class="fg">
          <div>
            <span>Happy new year!</span>
          </div>
          <img src="https://i.imgur.com/Ql8A585.png"/>
        </div>
      </body>
    </html>

    对于底部元素高度=70vh,例如对于分辨率1920 x 1080,这一点很好。但如果我翻转它,即切换到1080 x 1920,上面的框只需要30vh,但我希望它填满空间。有什么建议吗?

    2 回复  |  直到 7 年前
        1
  •  0
  •   user2332995 user2332995    7 年前

    我做了 .fg flex-grow: 1;

    body { 
        margin: 0;
        background-image: url('https://i.imgur.com/sWfZ8nq.png');
        background-repeat: repeat;
        font-size: 0;
    }
    
    * {
        box-sizing: border-box;
    }
    
    .fg {
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 100vw;
        height: 100vh;
    }
    
    .fg .text {
        display: flex;
        align-items: center;
        flex-basis: 0;
        flex-grow: 1;
        flex-shrink: 1;
    }
    
    .fg span {
        color: white;
        font-size: 10vmin;
        letter-spacing: 3px;
        text-shadow:  2px  2px 2px green,
                      2px -2px 2px green,
                     -2px  2px 2px green,
                     -2px -2px 2px green;
    }
    
    .fg .img {
        flex-basis: 0;
        flex-grow: 1;
        flex-shrink: 0;
        max-height: 70%;
        max-width: 90%;
    }
    
    @media (orientation: landscape) {
        img {
            height: 100%;
        }
    }
    
    @media (orientation: portrait) {
        img {
            width: 100%;
        }
    }
    <html>
      <head>
        <title>HNY</title>
        <meta charset="utf-8">
      </head>
      <body>
        <div class="fg">
          <div class="text">
            <span>Happy new year!</span>
          </div>
          <div class="img">
              <img src="https://i.imgur.com/Ql8A585.png" />
          </div>
        </div>
      </body>
    </html>
        2
  •  0
  •   Stedman Dennis    7 年前

    我使用了网格css。我将.fg设置为100vh,将主体边距设置为0以避免溢出问题。因为您需要将上限重新绑定到img大小,所以我将网格行设置为: grid-template-rows: 1fr auto;

    body { 
        background-image: url('https://i.imgur.com/sWfZ8nq.png');
        background-repeat: repeat;
        margin: 0;
    }
    
    .fg {
        display: grid;
        grid-template-rows: 1fr auto;
        margin: auto;
        height: 100vh;
    }
    
    .fg div {
        display: flex;
        align-items: center;
        width: 100%;
        height: 100%;
    }
    
    .fg span {
        width: 100%;
        text-align: center;
        color: white;
        font-size: 10vmin;
        letter-spacing: 3px;
        text-shadow:  2px  2px 2px green,
                      2px -2px 2px green,
                     -2px  2px 2px green,
                     -2px -2px 2px green;
    }
    
    .fg img {
    	position: relative;
    	bottom: 0;
    	left: 50%;
    	transform: translateX(-50%);
    	max-height: 70vh;
    	max-width: 90vw;
    }
    <html>
      <head>
        <title>HNY</title>
        <meta charset="utf-8">
      </head>
      <body>
        <div class="fg">
          <div>
            <span>Happy new year!</span>
          </div>
          <img src="https://i.imgur.com/Ql8A585.png"/>
        </div>
      </body>
    </html>