【问题标题】:keeping a div centered within a BG image in CSS在 CSS 中保持 div 居中在 BG 图像中
【发布时间】:2018-11-26 03:19:27
【问题描述】:

我遇到的问题是我正在努力保持我的防暴游戏文本流畅,当我调整浏览器的大小时,它似乎卡在了它所在的位置。无论是左侧还是右侧,通常右侧从 S 的末端到窗口的一侧都有更多的空间。

// The showcase is just the container 
// containing the background image. 

    .showcase {
      position: relative;
      width: 100%;
      height: 35.76em;
      text-align: center;
      background: url(https://www.riotgames.com/darkroom/original/d6120739b8bf25995d5c43e69b9ce85d:bf411966145dd8b6b0f224e372aa27e5/annie.jpg);
      background-repeat: no-repeat;
      background-position: center;
      background-size: cover;
    }
    
// Info Text is The Main Text 
// I'm trying to keep centered and fluid **

    .info-text {
      color:white;
      position: absolute;
      text-transform: uppercase;
      letter-spacing: 1px;
      font-family: "Montserrat", sans-serif;
      font-size: 6.5em;
      font-weight: bold;
      margin: 0;
      top: 15%;
      left: 25%;
    }

// I just ended up adding in the flex-box properties hoping to fix it, 
// but im not too sure if it made things worse or actually helped out.

    @media only screen and (max-width: 768px) {
      .grid {
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        align-content: center;
      }
      
      .info-text {
        display: flex;
        flex-direction: row;
        flex-wrap: nowrap;
        align-content: center;
        height: 25vh;
        margin: 0;
        text-transform: uppercase;
        letter-spacing: 1px;
        font-family: "Montserrat", sans-serif;
        font-size: 3.5em;
        font-weight: bold;
        top: 38%;
        left: 10%;
        margin: 0;
      }
<section class="showcase">
    <div class="welcome-headline">
        <h6>Welcome</h6>
    </div>
    <div class="info-text">
        <p>
            Riot Games
        </p>
    </div>
</section>

【问题讨论】:

    标签: html css text fluid-layout


    【解决方案1】:

    您可以在容器上使用具有此属性的 flexbox(在您的情况下为背景图像):

    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    align-items: center;
    justify-content: center;
    

    例子:

    .showcase {
        position: relative;
        width: 100%;
        height: 35.76em;
        text-align: center;
        background: url(https://www.riotgames.com/darkroom/original/d6120739b8bf25995d5c43e69b9ce85d:bf411966145dd8b6b0f224e372aa27e5/annie.jpg);
        background-repeat: no-repeat;
        background-position: center;
        background-size: cover;
        display: -webkit-box;
        display: -moz-box;
        display: -ms-flexbox;
        display: -webkit-flex;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    
    .info-text {
        color: white;
        position: absolute;
        text-transform: uppercase;
        letter-spacing: 1px;
        font-family: "Montserrat", sans-serif;
        font-size: 6.5em;
        font-weight: bold;
    }
    
    
    @media only screen and (max-width: 768px) {
    
        .info-text {
            font-size: 3.5em;
        }
    }
    <section class="showcase">
        <div class="welcome-headline">
            <h6>Welcome</h6>
        </div>
        <div class="info-text">
            <p>
                Riot Games
            </p>
        </div>
    </section>

    【讨论】:

    • 这太棒了!我试图找出那些 Display: 值除了 flex 之外还有什么作用,如果你不介意的话,你能启发我吗?我正在努力在谷歌上找到关于它们的任何答案。不过感谢您的帮助!
    • 嘿,迪伦,没问题!当然。显示:-webkit-box;显示:-moz-box;显示:-ms-flexbox;显示:-webkit-flex;是跨浏览器兼容性的属性(使 flexbox 工作)-> ptb2.me/flexbox,align-items 是告诉将所有子元素居中居中,而 justify-content 定义子元素的对齐方式-> css-tricks.com/almanac/properties/j/justify-content
    • 明白了!我还找到了另一种解决问题的方法,告诉信息文本向左推 50%,然后使用 transform: translatex(-50%);最终也工作了。 :D
    猜你喜欢
    • 2015-02-21
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 2018-12-19
    • 2014-09-30
    • 2011-04-08
    相关资源
    最近更新 更多