Home Web Front-end CSS Tutorial Responsive Web Design with Media Queries

Responsive Web Design with Media Queries

Sep 05, 2024 am 06:00 AM

Responsive Web Design with Media Queries

Lecture 6: Responsive Web Design with Media Queries

In this lecture, we’ll dive into responsive web design, a crucial skill for creating websites that look great on all devices, from desktops to smartphones. The key to responsive design is using media queries, which allow you to apply different styles based on the screen size or device characteristics.


Understanding Responsive Web Design

Responsive web design ensures that your website adapts to various screen sizes, providing an optimal viewing experience for users regardless of the device they’re using. This approach eliminates the need for separate mobile and desktop sites, streamlining your design process.

1. Why Responsive Design Matters
  • User Experience: Enhances usability by ensuring your content is accessible and easy to navigate on any device.
  • SEO: Google and other search engines prioritize mobile-friendly websites in search rankings.
  • Cost-Efficiency: Saves time and resources by maintaining a single site that works across all platforms.

Introduction to Media Queries

Media queries are the backbone of responsive design. They allow you to apply CSS rules only when certain conditions are met, such as when the screen width falls below a certain threshold.

1. Basic Syntax

A media query consists of a media type and one or more expressions that check for conditions, such as screen width.

  • Example:
  @media screen and (max-width: 768px) {
    body {
      background-color: lightblue;
    }
  }
Copy after login

This media query changes the background color to light blue on screens that are 768 pixels wide or smaller.

2. Combining Media Queries

You can combine multiple conditions to target specific scenarios.

  • Example:
  @media screen and (min-width: 600px) and (max-width: 1200px) {
    .container {
      padding: 20px;
    }
  }
Copy after login

This targets screens between 600px and 1200px wide, applying padding to the .container class.

3. Common Breakpoints

Breakpoints are the points at which your website’s layout changes based on the screen size.

  • Common breakpoints:
    • 320px: Small devices (smartphones in portrait mode)
    • 768px: Tablets
    • 1024px: Small desktops or tablets in landscape mode
    • 1200px: Large desktops

Creating a Responsive Layout

Let’s create a simple responsive layout that adjusts based on the screen size.

HTML:

<div class="container">
  <header>Header</header>
  <nav>Navigation</nav>
  <main>Main Content</main>
  <aside>Sidebar</aside>
  <footer>Footer</footer>
</div>
Copy after login

CSS:

body {
  font-family: Arial, sans-serif;
  margin: 0;
  padding: 0;
}

.container {
  display: grid;
  grid-template-columns: 1fr 3fr;
  grid-gap: 10px;
}

header, nav, main, aside, footer {
  padding: 20px;
  background-color: #f4f4f4;
  border: 1px solid #ddd;
}

/* Media Query for Tablets and Smaller Devices */
@media screen and (max-width: 768px) {
  .container {
    grid-template-columns: 1fr;
  }

  nav, aside {
    display: none; /* Hide navigation and sidebar on smaller screens */
  }
}
Copy after login

In this example:

  • The layout uses CSS Grid to create a two-column layout on larger screens, with the sidebar alongside the main content.
  • A media query at 768px is used to switch to a single-column layout, hiding the navigation and sidebar on smaller screens.

Responsive Images

In addition to responsive layouts, you should also ensure your images scale appropriately on different devices. Use the max-width property to make images responsive.

  • Example:
  img {
    max-width: 100%;
    height: auto;
  }
Copy after login

This ensures that images never exceed the width of their container and maintain their aspect ratio.

Practice Exercise

  1. Create a webpage with a multi-column layout using CSS Grid.
  2. Use media queries to adjust the layout for different screen sizes (e.g., hiding elements or changing column numbers).
  3. Make sure all images on the page are responsive by applying the max-width property.

Next Up: In the next lecture, we’ll explore CSS Transitions and Animations, where you’ll learn how to add dynamic effects to your website, making it more interactive and engaging. Stay tuned!


follow me on LinkedIn-

Ridoy Hasan

The above is the detailed content of Responsive Web Design with Media Queries. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

See all articles