Create Layout using CSS grid

Create Layout using CSS grid

CSS Grid help you to create a responsive website. Keeps everything laid out correctly on different screen resolutions.

To create grid layout you need to set display: grid on your element or class in css.

css-grid layout

Yellow - sidebar

red - footer

blue - header

pink - content


HTML CODE


<body>
    <div class="header">
        Header
    </div>
    <div class="drawer">
        Sidebar
    </div>
    <div class="content">
        Content
    </div>
    <div class="footer">
        Footer
    </div>
</body>



CSS CODE

  
body {
   displaygrid;
   margin0;
   padding0;
   height100vh;
   grid-template-columnsrepeat(41fr);
   grid-template-rowsrepeat(101fr);
   /* grid-template-areas:
    "header header header header"
    "drawer content content content"
    "drawer content content content"
    "drawer content content content"
    "drawer content content content"
    "drawer content content content"
    "drawer content content content"
    "drawer content content content"
    "drawer content content content"
    "footer footer footer footer" */
   grid-template-areas:
    "header header header header"
    "content content content drawer"
    "content content content drawer"
    "content content content drawer"
    "content content content drawer"
    "content content content drawer"
    "content content content drawer"
    "content content content drawer"
    "content content content drawer"
    "footer footer footer footer"
  }


Grid will have 4 columns and 10 Rows and each column and row will be 1 fraction of the available space.


.header {
   grid-area: header;
   min-height3rem;
   background-colorblue;
 }

.drawer {
   grid-area: drawer;
   background-coloryellow;
   min-width10rem;
 }

.footer {
   grid-area: footer;
   min-height3rem;
   background-colorred;
 }

 .content {
   grid-areacontent;
   background-colorpink;
 }


If you want drawer on left side than set drawer as first place in template-areas.
Already commented code for left side drawer.

Hope you like it











Previous Post Next Post