DEV Community

vidhya murali
vidhya murali

Posted on

CSS Media Queries

Responsive Web Design - Media Queries

  • CSS media queries allow you to apply styles based on the characteristics of a device or the environment displaying the web page.
  • CSS media queries are essential for creating responsive web pages.
  • The CSS @media rule is used to add media queries to your style sheet.

Use Mediaqueries to Add a Breakpoint

Example :

@media (min-width: 600px) {
.header {grid-area: 1 / span 6;}
.menu {grid-area: 2 / span 1;}
.content {grid-area: 2 / span 4;}
.facts {grid-area: 2 / span 1;}
.footer {grid-area: 3 / span 6;}
}

Typical Device Breakpoints

There are tons of screens and devices with different heights and widths, so it is hard to create an exact breakpoint for each device. To keep things simple you could target five groups:

Media Queries for Screen Orientation

Media queries can also be used to change the layout of a page depending on the orientation of the screen.

Here, we change the background-color of the body, if the screen orientation is in landscape mode:

Hide Elements With Media Queries
Here, we use media queries to hide an element on small screens

Change Font Size With Media Queries

Here, we use media queries to change the font size of an element on different viewport widths

Media Queries for User Preferences

Some users have motion sensitivity and prefer websites with less animation.

The prefers-reduced-motion media feature lets you check if a user has asked to reduce motion, such as animations or transitions. Use this feature to turn off animations and transitions for the users who has activated this setting on their computer

Top comments (0)