How to create a responsive blog layout with CSS?


A blog layout consists of a header. That header consists of a logo, then the menu, and after that the blog heading, content, etc. With that, the link for some other blogs can also be seen. In the bottom, you have a footer. The footer should have a copyright text. Let us see how to create a blog layout.

Create the header

The header of a blog consists of a logo on the left and then the website name −

<div class="header">
   <h1>Logo ↶</h1>
   <h2>Website Name</h2>
</div>

Position the header

Here, we will position the logo and website name −

.header {
   padding: 40px;	
   background: #7b1abc;
   color: white;
}
.header h1 {
   font-size: 40px;
}
.header h2 {
   text-align: center;
   font-size: 30px;
}

Create the navigation menu

Use the <nav> to create the navigation menu −

<nav>
   <a class="links selected" href="#">Home</a>
   <a class="links" href="#">Login</a>
   <a class="links" href="#">Register</a>
   <a class="links" href="#">Contact Us</a>
   <a class="links" href="#">More Info</a>
</nav>

Position the navigation menu

The overflow and height for the property is to be set to auto. Also, the width is 100% for the navigation menu −

nav {
   width: 100%;
   background-color: rgb(39, 39, 39);
   overflow: auto;
   height: auto;
}

Create the blog header and body

First, the blog header is set and then comes the body of the blog. The body comes under the <p> element −

<div class="post">
   <div class="blogHeader">
      <h1>Blog Heading</h1>
   </div>
   <div class="blog-body">
      <p>
         Lorem ipsum dolor sit, amet consectetur adipisicing elit. Deserunt non neque eos eaque veritatis praesentium.
      </p>
   </div>
</div>

Position the blog header and body

The div for the blog header and body is positioned on the left using the float property with the value left

.blog {
   margin-top: 20px;
   width: 75%;
   margin-left: auto;
   margin-right: auto;
   height: 400px;
}
.post {
   margin-top: 20px;
   float: left;
}
.blogHeader {
   font-size: 36px;
   margin-bottom: 20px;
}

Define the footer

The footer for the blog layout i.e., the HTML web page is set using the <footer> element −

<footer>
   Copyright ©
</footer>

Position the footer

The position property is used with the value fixed to fix the footer. To position the footer in the bottom, the bottom property is used −

footer {
   color: white;
   position: fixed;
   width: 100%;
   bottom: 0;
   margin-left: auto;
   margin-right: auto;
   font-size: 30px;
   height: 100px;
   padding: 20px;
   background-color: rgb(9, 141, 101);
   text-align: center;
}

Example

To create a blog layout with CSS, the code is as follows −

<!DOCTYPE html>
<html>
<head>
   <title>My Website</title>
   <style>
      * {
         box-sizing: border-box;
      }
      body {
         font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
         margin: 0;
      }
      .header {
         padding: 40px;	
         background: #7b1abc;
         color: white;
      }
      .header h1 {
         font-size: 40px;
      }
      .header h2 {
         text-align: center;
         font-size: 30px;
      }
      nav {
         width: 100%;
         background-color: rgb(39, 39, 39);
         overflow: auto;
         height: auto;
      }
      .links {
         display: inline-block;
         text-align: center;
         padding: 14px;
         color: rgb(178, 137, 253);
         text-decoration: none;
         font-size: 17px;
      }
      .links:hover {
         background-color: rgb(100, 100, 100);
      }
   
      .selected {
         background-color: rgb(0, 18, 43);
      }
      .blog {
         margin-top: 20px;
         width: 75%;
         margin-left: auto;
         margin-right: auto;
         height: 400px;
      }
      .post {
         margin-top: 20px;
         float: left;
      }
      .blogHeader {
         font-size: 36px;
         margin-bottom: 20px;
      }

      .footer {
         padding: 20px;
         text-align: center;
         color: white;
         background: rgb(255, 0, 0);
      }
      @media screen and (max-width: 830px) {
         div, main {
            width: 100%;
            text-align: center;
         }
         .links {
            display: block;
         }
      }
   </style>
</head>
<body>
   <div class="header">
      <h1>Logo ↶</h1>
      <h2>Website Name</h2>
   </div>
   <nav>
      <a class="links selected" href="#">Home</a>
      <a class="links" href="#">Login</a>
      <a class="links" href="#">Register</a>
      <a class="links" href="#">Contact Us</a>
      <a class="links" href="#">More Info</a>
   </nav>
   <div class="blog">
      <div class="post">
         <div class="blogHeader">
            <h1>Blog Heading</h1>
         </div>
         <div class="blog-body">
            <p>
               Lorem ipsum dolor sit, amet consectetur adipisicing elit. Deserunt non neque eos eaque veritatis praesentium.
            </p>
         </div>
      </div>
   </div>
   <div class="footer">
      <h2>Footer ↷</h2>
   </div>
</body>
</html>

Updated on: 08-Dec-2023

780 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements