DEV Community

Cover image for Media Attribute in HTML
aryan015
aryan015

Posted on • Edited on

Media Attribute in HTML

Syntax

  1. This will include styles.css only if the screen width is 600 pixels or less.
<link rel="stylesheet" href="styles.css" media="screen and (max-width: 600px)">
Enter fullscreen mode Exit fullscreen mode

Possible Use-case

<link rel="stylesheet" href="desktop-styles.css" media="screen and (min-width: 1024px)">
<link rel="stylesheet" href="mobile-styles.css" media="screen and (max-width: 600px)">
Enter fullscreen mode Exit fullscreen mode
<link rel="stylesheet" href="mobile.css" media="screen and (min-width:200px)">
<link rel="stylesheet" href="desktop.css" media="screen and (min-width:800px)">
Enter fullscreen mode Exit fullscreen mode

Order matters if you swap positions of links👶 then it wont work since mobile.css always load(true) no matter screen size.

It will help organize your code little bit.

Top comments (0)