【问题标题】:How can I align this text to be centered within my CSS grid?如何将此文本对齐在我的 CSS 网格中居中?
【发布时间】:2021-09-27 00:13:59
【问题描述】:

我正在尝试将 FreeCodeCamp 的 Product Landing Page 重新创建为个人项目。

对于“开始”按钮下方和视频上方的内容,我使用了网格来尝试正确对齐它们。但是,我无法在行中垂直对齐文本(标题和段落)。我该怎么做呢?他们似乎坐在他们行的“底部”。

HTML 代码:

body {
    font-family: Lato, sans-serif;
    background-color: #ececec;
}

#top {
    margin-top: 100px;
}

#top h1 {
    font-size: 1.5em;
    font-weight: 600;
    margin-bottom: 0;
}

#nav-bar {
    position: fixed;
    width: 100%;
    height: 80px;
    background-color: #ececec;
    display: flex;
    justify-content: space-between;
    align-items: center;
    top: 0;
}

#header-img {
    height: 35px;
    left: 0;
    margin-left: 20px;
}

#top {
    text-align: center;
}

#navbar li {
    display: inline;
    right: 0;
}

#navlinks {
    display: flex;
    list-style-type: none;
}

#navlinks li {
    padding: 5px;
    text-align: center;
    margin-right: 50px;
}

#submit {
    width: 150px;
    height: 30px;
    margin-top: 15px;
    font-weight: 900;
    font-size: 1em;
    background-color: #f3c807;
    border: none;
}

#submit:hover {
    background-color: orange;
    transition: background-color 1s;
    cursor: pointer;
}


#email {
    height: 23px;
    padding-left: 7px;
    width: 275px;
}

i.fa, i.fas {
    color: rgb(255, 136, 0);
    text-align: center;
}

#grid {
    display: grid;
    grid-template-columns: 100px auto;
    grid-template-rows: 50px 50px 50px;
    grid-column-gap: 50px;
    grid-row-gap: 80px;
    margin-left: 50px;
    align-content: center;
    margin-top: 100px;
}

.icon {
    text-align: center;
}

.middlecontent h1 {
    font-size: 1.5em;
    margin-bottom: 0;
}

.middlecontent p {
    margin-top: 0;
}
    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,100;0,300;0,400;1,100;1,300&display=swap" rel="stylesheet">
    <link href="./css/landingpagestyles.css" rel="stylesheet">
    <script src="https://kit.fontawesome.com/2b4114baf6.js" crossorigin="anonymous"></script>
    <title>Original Trombones</title>

</head>

<body>
   <header id="header">
       <nav id="nav-bar">
        <!-- Picture logo in header -->
        <img id="header-img" src="https://cdn.freecodecamp.org/testable-projects-fcc/images/product-landing-page-logo.png" alt="Original Trombones">
        <ul id="navlinks">
            <li>
                Features
            </li>
            <li>
                How It Works
            </li>
            <li>
                Pricing
            </li>
        </ul>
       </nav>
   </header>
    <main>
        <div id="top">
            <h1>Handcrafted, home-made masterpieces</h1>
            <br>
            <form id="form" action="https://www.freecodecamp.com/email-submit">
            <label for="email"></label>
            <input type="email" name="email" id="email" placeholder="Enter your email address">
            <br>
            <input type="submit" id="submit" value="GET STARTED">
            </form>
        </div>

        <div id="middle">

            <div id="grid">
                <div class="icon">
                <i class="fa fa-3x fa-fire"></i>
                </div>
                <div class="middlecontent">
                <h1>
                    Premium Materials
                </h1>
                <p>
                    Our trombones use the shiniest brass which is sourced locally. This will increase the longevity of your purchase.
                </p>
                </div>
                <div class="icon">
                <i class="fa fa-3x fa-truck"></i>
                </div>
                <div class="middlecontent">
                    <h1>
                        Fast Shipping
                    </h1>
                    <p>
                        We make sure you recieve your trombone as soon as we have finished making it. We also provide free returns if you are not satisfied.
                    </p>
                </div>
                <div class="icon">
                <i class="fa fa-3x fa-battery-full" aria-hidden="true"></i>
                </div>
                <div class="middlecontent">
                    <h1>
                        Quality Assurance
                    </h1>
                    <p>
                        For every purchase you make, we will ensure there are no damages or faults and we will check and test the pitch of your instrument.
                    </p>
                </div>
            </div>

        </div>
    </main>

</body>
</html>

【问题讨论】:

    标签: html css css-grid


    【解决方案1】:

    根据我的经验,如果我们想在网格中对齐项目(垂直或水平),我们可以在该子元素内使用 flex 属性。下面的例子将有助于理解。

    垂直对齐:

    align-items:{position}
    

    水平对齐:

    justify-content:{position}
    

     body{
            color: #ffff;
        }
        .content{
            display: grid;
            grid-gap: 10px;
            max-width: 960px;
            margin: auto;
            text-align: center;
            font-weight: bold;
            font-size: 50px;
            font-family: sans-serif;
            grid-template-columns: repeat(3,1fr);
            grid-auto-rows:200px;
        }
    
        .content div{
            background: gray;
            padding: 30px;
        }
    
        .content div:nth-child(even){
            background: green;
        }
        
        .one{
          display:flex;
          align-items: start;
          justify-content: start;
        }
        
        .two{
          display:flex;
          align-items: center;
          justify-content: center;
        }
        
        .three{
          display:flex;
          align-items: end;
          justify-content: end;
        }
        
        .four{
          display:flex;
          align-items: start;
          justify-content: center;
        }
        
        .five{
          display:flex;
          align-items: end;
          justify-content: center;
        }
        
        .six{
          display:flex;
          align-items: start;
          justify-content: end;
        }
        
    <html>
    <body>
    <div class="content">
    
            <div class="one">1</div>
            <div class="two">2</div>
            <div class="three">3</div>
            <div class="four">4</div>
            <div class="five">5</div>
            <div class="six">6</div>
    
        </div>
    </body>
    </html>

    【讨论】:

      【解决方案2】:

      我所做的是将 h1 和 p 标签周围的 div 放入弹性框

      .middlecontent {
          display: flex;
          justify-content: center;
          flex-direction: column;
      }
      

      这似乎奏效了。就这么简单。

      body {
          font-family: Lato, sans-serif;
          background-color: #ececec;
      }
      
      h1 {
          margin-bottom: 0;
      }
      
      p {
          margin-top: 0;
      }
      
      #top {
          margin-top: 100px;
      }
      
      #top h1 {
          font-size: 1.5em;
          font-weight: 600;
          margin-bottom: 0;
      }
      
      #nav-bar {
          position: fixed;
          width: 100%;
          height: 80px;
          background-color: #ececec;
          display: flex;
          justify-content: space-between;
          align-items: center;
          top: 0;
      }
      
      #header-img {
          height: 35px;
          left: 0;
          margin-left: 20px;
      }
      
      #top {
          text-align: center;
      }
      
      #navbar li {
          display: inline;
          right: 0;
      }
      
      #navlinks {
          display: flex;
          list-style-type: none;
      }
      
      #navlinks li {
          padding: 5px;
          text-align: center;
          margin-right: 50px;
      }
      
      #submit {
          width: 150px;
          height: 30px;
          margin-top: 15px;
          font-weight: 900;
          font-size: 1em;
          background-color: #f3c807;
          border: none;
      }
      
      #submit:hover {
          background-color: orange;
          transition: background-color 1s;
          cursor: pointer;
      }
      
      
      #email {
          height: 23px;
          padding-left: 7px;
          width: 275px;
      }
      
      i.fa, i.fas {
          color: rgb(255, 136, 0);
          text-align: center;
      }
      
      #grid {
          display: grid;
          grid-template-columns: 100px auto;
          grid-template-rows: 50px 50px 50px;
          grid-column-gap: 50px;
          grid-row-gap: 80px;
          margin-left: 50px;
          align-content: center;
          margin-top: 100px;
      }
      
      .icon {
          text-align: center;
      }
      
      .middlecontent h1 {
          font-size: 1.5em;
          margin-bottom: 0;
      }
      
      .middlecontent p {
          margin-top: 0;
      }
      
      .middlecontent {
          display: flex;
          justify-content: center;
          flex-direction: column;
      }
      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <link rel="preconnect" href="https://fonts.googleapis.com">
          <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
          <link href="https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,100;0,300;0,400;1,100;1,300&display=swap" rel="stylesheet">
          <link href="./css/landingpagestyles.css" rel="stylesheet">
          <script src="https://kit.fontawesome.com/2b4114baf6.js" crossorigin="anonymous"></script>
          <title>Original Trombones</title>
      
      </head>
      
      <body>
         <header id="header">
             <nav id="nav-bar">
              <!-- Picture logo in header -->
              <img id="header-img" src="https://cdn.freecodecamp.org/testable-projects-fcc/images/product-landing-page-logo.png" alt="Original Trombones">
              <ul id="navlinks">
                  <li>
                      Features
                  </li>
                  <li>
                      How It Works
                  </li>
                  <li>
                      Pricing
                  </li>
              </ul>
             </nav>
         </header>
          <main>
              <div id="top">
                  <h1>Handcrafted, home-made masterpieces</h1>
                  <br>
                  <form id="form" action="https://www.freecodecamp.com/email-submit">
                  <label for="email"></label>
                  <input type="email" name="email" id="email" placeholder="Enter your email address">
                  <br>
                  <input type="submit" id="submit" value="GET STARTED">
                  </form>
              </div>
      
              <div id="middle">
      
                  <div id="grid">
                      <div class="icon">
                      <i class="fa fa-3x fa-fire"></i>
                      </div>
                      <div class="middlecontent">
                      <h1>
                          Premium Materials
                      </h1>
                      <p>
                          Our trombones use the shiniest brass which is sourced locally. This will increase the longevity of your purchase.
                      </p>
                      </div>
                      <div class="icon">
                      <i class="fa fa-3x fa-truck"></i>
                      </div>
                      <div class="middlecontent">
                          <h1>
                              Fast Shipping
                          </h1>
                          <p>
                              We make sure you recieve your trombone as soon as we have finished making it. We also provide free returns if you are not satisfied.
                          </p>
                      </div>
                      <div class="icon">
                      <i class="fa fa-3x fa-battery-full" aria-hidden="true"></i>
                      </div>
                      <div class="middlecontent">
                          <h1>
                              Quality Assurance
                          </h1>
                          <p>
                              For every purchase you make, we will ensure there are no damages or faults and we will check and test the pitch of your instrument.
                          </p>
                      </div>
                  </div>
      
              </div>
          </main>
      
      </body>
      </html>

      【讨论】:

        猜你喜欢
        • 2013-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多