"I am Saqib Jahangir. A passionate vlogger, software engineer, trainer and avid traveler with a deep love for exploring the hidden gems of our beautiful planet. With a strong foundation in Application Development, Application Architecture & Database Design and Product Management, I bring over a decade of hands-on experience building secure, scalable, and resilient web applications for a diverse range of industries."

Media Queries and Responsive Design

With the vast variety of devices and screen sizes today, creating websites that look great on all of them is essential. Responsive design ensures your site adapts fluidly to different screen widths, orientations, and resolutions. The key to this adaptability lies in CSS Media Queries.

What Are Media Queries?

Media queries allow you to apply different CSS rules based on the characteristics of the user’s device or viewport. This means you can change the layout, font size, colors, and more depending on whether someone is using a phone, tablet, desktop, or even printing the page.

Basic Syntax

@media (condition) {
  /* CSS rules here */
}

For example:

@media (max-width: 600px) {
  body {
    background-color: lightblue;
  }
}

This changes the body’s background color to light blue when the screen width is 600px or less.

Common Media Features

·         width / max-width / min-width: Target screen widths.

·         height / max-height / min-height: Target screen heights.

·         orientation: Detect if device is in portrait or landscape.

·         resolution: Target device pixel density.

·         aspect-ratio: Match specific screen proportions.

Responsive Design Techniques

·         Fluid Grids: Use relative units like percentages or fr in grids instead of fixed pixels.

·         Flexible Images: Make images scale with max-width set to 100%.

·         Breakpoints: Set media queries at specific screen widths to adjust layout and typography.

·         Mobile-First Approach: Write styles for small screens first, then add media queries for larger screens.

Example: Responsive Layout

.container {
  display: flex;
  flex-direction: row;
}
 
@media (max-width: 768px) {
  .container {
    flex-direction: column;
  }
}

This switches a horizontal layout to a vertical one on tablets and smaller devices.


Popular Posts

Operators (Arithmetic, Comparison, Logical)

Functions (Built-in & User-defined)