Home > Blockchain >  How to align this text to be centered within my CSS grid?
How to align this text to be centered within my CSS grid?

Time:09-27

I'm fairly new to coding and to Stack Overflow. I couldn't find this answer, so bear with me if it's frequently answered or something.

I'm trying to recreate FreeCodeCamp's Product Landing Page as a personal project. (I understand they want you to do your own thing, but I'm starting with a recreation before I do that so that I may get more of an understanding of HTML and CSS before I try designing my own thing.)

For the content below the Get Started button and above the video, I used a grid to try to align them properly. However, I'm having trouble aligning the text (the heads and paragraphs) vertically in their rows. How may I go about doing this? They seem to be sitting at the "bottom" of their rows.

HTML code:

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>

Thank you very much for any insight/help.

CodePudding user response:

I figured it out, and I figured I would leave this up for anyone else who had the same question. Mods, feel free to delete if this is against this community's rules, of course.

What I did was I made the div surrounding the h1 and p tags into a flexbox

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

And that seemed to work. It was really as simple as that (because of course it was). Haha

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>

CodePudding user response:

According to my experience, If we want to align items(vertically or horizontally) in a grid we can use flex property inside that child element. The following example will help to get an idea.

To vertically align:

align-items:{position}

To horizontally align:

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>

  • Related