Open In App

Responsive Web Design Using Media Queries

Responsive Design is a web design approach that aims to adapt web page layout to any screen from maximum screen sizes like TVs, monitors, etc. to minimum screen sizes like mobiles, etc. with good usability. Media Queries is a CSS3 Feature that makes a website page adapt its layout to different screen sizes and media types. Media queries are CSS rules that apply specific styles based on the characteristics of the user’s device or viewport. These characteristics can include screen width, height, orientation, resolution, and more.

Syntax:




@media medieType and (condition) {
  /* CSS rules that applies if condition is true */
}

The @media rule is used to target media queries and then the media type which is optional depending on the device type and then the condition also called a breakpoint, if the condition is true then the CSS rules are applied to the web page from the parenthesis.



Media Types

Media Types describe the category of device type. Media type is optional and by default, it is considered as `all` type. Media Types are categorized into four types.

  1. all – for all device types.
  2. print – for printers
  3. screen — for smartphones, tablets, TVs and computer screens
  4. speech — for screen readers that “read” the page out loud

Why to use Media Queries?

Media Queries helps the developers to create a consistent and optimized user experience across all devices. Without responsive design, websites may appear unstable and broken on certain screens which results in poor user experience. With a better user experience, it will be easy to use the product.



Benefits of Responsive Design

Elements of a Responsive Design

How Does Responsive Design Work?

Example of the Main section of a Landing Page using HTML and CSS:




<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Responsive Design Example</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <nav>
      <div class="menu-toggle">
        <div class="bar"></div>
        <div class="bar"></div>
        <div class="bar"></div>
      </div>
      <ul class="menu">
        <li><a href="https://www.geeksforgeeks.org/">Home</a></li>
        <li><a href="https://www.geeksforgeeks.org/about/">About</a></li>
        <li>
          <a href="https://www.geeksforgeeks.org/press-release/">Services</a>
        </li>
        <li>
          <a href="https://www.geeksforgeeks.org/about/contact-us/">Contact</a>
        </li>
      </ul>
    </nav>
    <div class="header-tip">
      <a href="https://www.geeksforgeeks.org/" class="latest">
        ???? New: Courses, Tutorials, Jobs, Contests and more →</a>
    </div>
    <main>
      <section>
        <h2>Responsive Design Example Website!</h2>
        <p>
          Lorem ipsum dolor sit amet consectetur adipisicing elit. Saepe amet
          beatae illo fuga cupiditate suscipit, animi quasi
        </p>
        <button>Get started</button>
      </section>
      <section id="img-section">
        <img
          src=
          alt="Responsive Design Example"
          loading="lazy"
        />
      </section>
    </main>
    <footer>
      <div>© 2023 Your GFG Website</div>
    </footer>
    <script>
      document.querySelector(".menu-toggle").addEventListener("click", function () {
          const menu = document.querySelector(".menu");
          menu.classList.toggle("active");
        });
    </script>
  </body>
</html>




/* styles.css */
body {
  font-family: Arial, sans-serif;
  margin: 0;
  padding: 0;
  height: 100vh;
}
nav {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #32cd32;
}
main {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 30px;
  height: 80vh;
  padding: 64px;
}
button {
  background-color: #32cd32;
  border: none;
  padding: 12px 24px;
  border-radius: 4px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  width: 200px;
  color: white;
}
section {
  max-width: 50%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 18px;
}
img {
  width: 500px;
  height: 100%;
}
h2 {
  font-size: 64px;
  font-weight: 600;
  margin-bottom: 16px;
  text-align: left;
}
 
nav ul {
  list-style-type: none;
  padding: 24px;
  margin: 0;
}
 
nav li {
  display: inline;
  margin-right: 20px;
  padding: 24px;
}
 
nav a {
  color: #fff;
  text-decoration: none;
  color: black;
  padding: 2px;
}
 
footer {
  padding: 12px;
  text-align: center;
  font-size: 12px;
  color: black;
}
.header-tip {
  padding: 64px 0px 0px 64px;
}
.latest {
  text-decoration: none;
  color: black;
  border: 1px solid rgb(218, 218, 218);
  padding: 8px 12px;
  border-radius: 100px;
  font-size: x-small;
  top: 64px;
  left: 64px;
}
 
.menu-toggle {
  display: none;
  flex-direction: column;
  cursor: pointer;
  padding: 10px;
}
 
.menu-toggle .bar {
  width: 25px;
  height: 3px;
  background-color: white;
  margin: 3px 0;
  transition: 0.4s;
}
 
.menu {
  list-style-type: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: row;
}
 
.menu li {
  display: inline;
  margin-right: 20px;
}
 
.menu a {
  color: #fff;
  text-decoration: none;
}
 
/* Mobile Styles (320px — 480px) */
@media (max-width: 480px) {
  body {
    font-size: 14px;
    background-color: black;
    color: #fff;
  }
  main {
    flex-direction: column;
    padding: 24px;
  }
  .header-tip {
    padding: 24px 0px 0px 24px;
  }
  .latest {
    color: white;
  }
  section {
    width: 100%;
    padding: 0px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  h2 {
    font-size: 32px;
    margin-bottom: 12px;
    text-align: center;
  }
  p {
    text-align: center;
  }
  .menu {
    display: none;
    flex-direction: column;
    background-color: #32cd32;
    position: absolute;
    top: 60px;
    right: 0;
    width: 100%;
    z-index: 1;
  }
 
  .menu.active {
    display: flex;
  }
 
  .menu-toggle {
    display: flex;
  }
 
  nav {
    flex-direction: row-reverse;
    justify-content: space-between;
    background-color: #000;
  }
  #img-section {
    display: none;
  }
  button {
    width: 150px;
  }
  footer {
    color: white;
  }
}
 
/* Tablet Styles (481px — 768px) */
@media (min-width: 481px) and (max-width: 768px) {
  main {
    flex-direction: column;
    padding: 40px;
    height: auto;
  }
  section {
    width: 100%;
  }
  header-tip {
    padding: 40px 0px 0px 40px;
  }
  img {
    width: 100%;
    height: 100%;
  }
  h2 {
    font-size: 40px;
    margin-bottom: 14px;
    text-align: center;
  }
  p {
    text-align: center;
  }
  footer {
    color: white;
  }
  button {
    width: 100%;
  }
}
 
/* Laptop Styles (769px — 1024px) */
@media (min-width: 769px) and (max-width: 1024px) {
  main {
    padding: 64px;
  }
  h2 {
    font-size: 44px;
    margin-bottom: 16px;
  }
  img {
    width: 384px;
    height: 100%;
  }
}

Output: Click here to see the live output

Responsive Design Using CSS Media Queries Example

What Practices to Avoid?

Conclusion

CSS media queries are indispensable tool for creating responsive web designs. They allow websites to adapt gracefully to different devices, providing a consistent and user-friendly experience. By mastering media queries and following best practices, web developers can ensure their websites are ready for the diverse landscape of the modern digital world.


Article Tags :