Enter media queries | The Fundamentals of Responsive Web Design

Understanding Media Queries

Media queries are a powerful tool in CSS that allow you to apply different styles based on specific conditions, such as screen size, orientation, resolution, and more. They are essential for creating responsive web designs that adapt to various devices and screen sizes.

Basic Structure of a Media Query

A media query consists of two main parts:

  1. Media type: Specifies the type of media the query targets (e.g., screen, print).
  2. Conditions: Defines the conditions under which the styles should be applied.

Common Conditions

  • width and height: Specify the viewport width and height.
  • min-width and max-width: Set minimum and maximum width values.
  • min-height and max-height: Set minimum and maximum height values.
  • orientation: Specify the orientation of the device (portrait or landscape).
  • resolution: Specify the screen resolution.

Using Media Queries

  1. Create a media query block: Enclose your styles within a media query block using curly braces.
  2. Specify the conditions: Inside the block, define the conditions under which the styles should be applied.
  3. Apply styles: Add the CSS rules you want to apply when the conditions are met.

Example:

@media only screen and (max-width: 600px) {

  /* Styles for screens narrower than 600px */

  .header {

    text-align: center;

  }

  .nav {

    display: none;

  }

}

Tips for Using Media Queries

  • Start with a mobile-first approach: Design for smaller screens first and progressively enhance for larger screens.
  • Use logical units: Consider using em or rem for font sizes and spacing to create more flexible layouts.
  • Test thoroughly: Test your website on various devices and screen sizes to ensure that the media queries are working as expected.
  • Be mindful of performance: Avoid excessive media queries to prevent performance issues.

By effectively using media queries, you can create responsive websites that adapt seamlessly to different screen sizes and provide a great user experience across all devices.

Understanding Responsive Web Design | The Fundamentals of Responsive Web Design
Writing HTML Markup

Get industry recognized certification – Contact us

keyboard_arrow_up
Open chat
Need help?
Hello 👋
Can we help you?