CSS Grid Example

CSS Grid

The use of grid layout is one of the main web design methodologies. Plenty of tools help Front-End Developers build grid-based websites (for instance Bootstrap, Foundation, or gridle {.scss}), with the introduction of CSS Grid Layout, we realize that until now we were never really able to implement grid on our websites.

Grid Layout excels at dividing a page into major regions or defining the relationship in terms of size, position, and layer, between parts of a control built from HTML primitives.

Example of Grid

HTML :
<div class="grid-container">
    <main>
        <h1>“... food is not simply organic fuel to keep body and soul together, it is a perishable art that must be savoured at the peak of perfection.”</h1>
    </main>
    <nav>
        <ul>
            <li>Home</li>
            <li>About Us</li>
            <li>Services</li>
            <li>Products</li>
            <li>Contact Us</li>
        </ul>
    </nav>
    <section class="nom">
        <h2>Green Vibes</h2>
    </section>
    <section class="yum">
        <h2>Don't Panic it's Organic</h2>
    </section>
    <header>
        <h4>ONCE UPON A TIME ALL FOOD WAS ORGANIC - BELIEVE ME </h4>
    </header>
    <div class="plant">
    </div>
</div>
CSS:
body {
    margin: 0;
    padding: 0;
}
.grid-container {
    display: grid;
    grid-template-columns: 1fr 1.2fr 1.5fr;
    grid-template-rows: 1fr 4fr 3fr 2fr;
    height: 100vh;
}

main {
    /* grid-column-start: 1;
    grid-column-end: 3;
    grid-row-start: 1;
    grid-row-end: 3;

    grid-row: 1 / 3;
    grid-column: 1 / 3; */

    grid-area: 1 / 1 / 3 / 3;
    
    background-color: #FFFAE1;
}
main h1 {
    padding: 2em;
    font-family: monospace;
    color: #303b17;
    font-size: 2.5em;
}


nav {
    grid-column: 3 / 4;
    grid-row: 1 / 2;
    background-color: #ffffff;
}
nav ul li {
    list-style: none;
    float: left;
    margin-left: 2em;
    font-family: monospace;
    font-size: 1.3em;
    font-weight: 700;
    text-transform: uppercase;
    padding: 10px 0;
    color: #30381a;
}


.nom {
    /* grid-area: 3 / 1 / 4 / 2; */
    grid-column: 2 / 3;
    grid-row: 3 / 4; 
    background-color: #343334;
}
.nom h2 {
    text-align: center;
    margin: 17% 0;
    font-size: 3em;
    color: #fffae1;
    font-family: monospace;
}


.yum {
    grid-column: 1 / 2;
    grid-row: 3 / 4;
    background-color: #C4B25A;
    text-align: center;
    justify-items: center;
}
.yum h2 {
    text-align: center;
    margin: 18% 0;
    font-size: 3em;
    color: #fffae1;
    font-family: monospace;
}



.plant {
    grid-column: 3 / 4;
    grid-row: 2 / 5;
    background: url(plant.jpg);
    background-size: cover;
}



header {
    /* grid-area: 3 / 1 / 4 / 2; */
    grid-column: 1 / 3;
    grid-row: 4 / 5; 
    background: black
}
header h4 {
    color: #2f3b17;
    font-size: 2em;
    margin: 2em;
    font-family: monospace;
}

Grid Properties for Column and row :

grid-column-start: 1;      // grid column starting from 1st point
grid-column-end: 2;      // grid column ending to 2nd point
grid-row-start: 1;      // grid row starting from 1st point
grid-row-end: 3;     // grid row ending to 3rd point

We will right in short way like below:

grid-row: 1 / 3;     // grid row cover from 1st point to 3rd point
grid-column: 1 / 2;     // grid column cover from 1st point to 2nd point

Final we will right like:

grid-area: 1 / 1 / 2 / 3;     // grid column cover from 1st point to 2nd point & grid row cover from 1st point to 3rd point

 

Output :

 

You May Also Like

SASS

CSS Grid Properties

Image Responsive

Angular Components

Leave a Reply

Your email address will not be published. Required fields are marked *