skip to Main Content

Using the CSS Grid system, I have two columns; first is 40% and second 60% using grid-template-column. The first column is made of text, the second of an img; how to make both columns the same height?

.container
{
    max-width: 1130px;
    margin: 0 auto;
}

section
{
    padding: 30px;
}

#sec-1
{
    padding: 0;
}

.sec-1 img
{
    width: 100%;
    display: block;
}

#sec-1 .wrapper
{
    padding: 50px;
    background-image: linear-gradient(to bottom right, #a454fd, #2b05df);
    color: white;
}

section[id^="sec"] h1
{
    font-size: 34px;
    font-weight: 900;
}

#sec-1 a 
{
    text-decoration: none;
    color: white;
    border: 1px solid white;
    padding: 15px 30px;
    border-radius: 25px;
    margin: 50px auto;
    display: block;
    width: 115px;
}

#sec-1 a:hover
{
    color: #0000cc;
    cursor: pointer;
}

/* Small devices (portrait tablets and large phones, 600px and up) */
@media only screen and (min-width: 600px) {
}

/* Medium devices (landscape tablets, 768px and up) */
@media only screen and (min-width: 768px) {
}

/* Large devices (laptops/desktops, 992px and up) */
@media only screen and (min-width: 992px) {
    #sec-1
    {
        display: grid;
        grid-template-columns: 40% 60%;
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Elements</title>
    <link rel="stylesheet" href="styles.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.min.css">
    <link rel="stylesheet" href="fonts/webfontkit-roboto/stylesheet.css">
</head>
<body>
    <div class="container">
        <section id="sec-1">
            <div class="wrapper">
                <h1>DISCOVER SOMETHING BRAND NEW</h1>  
                <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Repellendus veniam, atque totam rerum repellat velit quae omnis consequatur debitis harum laudantium at laborum iure! Veritatis accusantium assumenda mollitia quas consequuntur!</p>
                <div>
                    <a href="#"> CONTACT US </a>
                </div>
            </div>
            <img src="https://placehold.jp/375x271.png" alt="p-2">
        </section>
    </div>
</body>
</html>

2

Answers


  1. Consider applying height: 100% to the <img> element:

    .container
    {
        max-width: 1130px;
        margin: 0 auto;
    }
    
    section
    {
        padding: 30px;
    }
    
    #sec-1
    {
        padding: 0;
    }
    
    .sec-1 img
    {
        width: 100%;
        display: block;
    }
    
    #sec-1 .wrapper
    {
        padding: 50px;
        background-image: linear-gradient(to bottom right, #a454fd, #2b05df);
        color: white;
    
    }
    
    section[id^="sec"] h1
    {
        font-size: 34px;
        font-weight: 900;
    }
    
    #sec-1 a 
    {
        text-decoration: none;
        color: white;
        border: 1px solid white;
        padding: 15px 30px;
        border-radius: 25px;
        margin: 50px auto;
        display: block;
        width: 115px;
    }
    
    #sec-1 a:hover
    {
        color: #0000cc;
        cursor: pointer;
    }
    
    
    /* Small devices (portrait tablets and large phones, 600px and up) */
    @media only screen and (min-width: 600px) {
    
    
    
    }
    
    /* Medium devices (landscape tablets, 768px and up) */
    @media only screen and (min-width: 768px) {
    
    
    
    
    }
    
    /* Large devices (laptops/desktops, 992px and up) */
    @media only screen and (min-width: 992px) {
    
    
        #sec-1
        {
            display: grid;
            grid-template-columns: 40% 60%;
        }
    
    
        img
        {
            height: 100%;
        }
    
    }
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Elements</title>
        <link rel="stylesheet" href="styles.css">
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.min.css">
        <link rel="stylesheet" href="fonts/webfontkit-roboto/stylesheet.css">
    </head>
    <body>
        <div class="container">
            <section id="sec-1">
                <div class="wrapper">
                    <h1>DISCOVER SOMETHING BRAND NEW</h1>  
                    <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Repellendus veniam, atque totam rerum repellat velit quae omnis consequatur debitis harum laudantium at laborum iure! Veritatis accusantium assumenda mollitia quas consequuntur!</p>
                    <div>
                        <a href="#"> CONTACT US </a>
                    </div>
                </div>
                <img src="https://placehold.jp/375x271.png" alt="p-2">
            </section>
        </div>
    </body>
    </html>
    Login or Signup to reply.
  2. According to provided code, to implement required functionality of making height of content column and image column same in grid page, the following CSS code can be used:

    @media only screen and (min-width: 992px) {
        #sec-1 img
        {
            width: 100%;
            display: block;
            height: 100%;
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search