CSS3 Media Query
- Mainly used for Responsive Web Designing
<meta>
tag inside <head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
- turned_in_notMedia Query property
- @media
- @import
@media
Media rules are been declared inside it, later on this media will execute automatically only when the media query statement is true
@media not|only mediatype and (media feature)
Media Query statement
If not or only operators are not used in media query, then by default all will be used
- turned_in_not@media syntax
@media not|only mediatype and (media feature) { /*CSS-Code;*/ }
OR, You can also have different external CSS for different media
<link rel="stylesheet" media="mediatype and|not|only (media feature)" href="extrnalstyle.css">
Types | Description |
---|---|
all | Used for all media type devices |
Used for printers | |
screen | Used for computer screens, tablets, smart-phones etc. |
speech | Used for screen readers that "reads" the page out loud |
Features | Min/Max | Description |
---|---|---|
aspect-ratio | done | Specifies the ratio of width and height of the viewport |
color | done | Specifies the number of bits per color component for the output device |
color-index | done | Specifies the number of colors the device can show |
grid | X | Whether the device is a grid or bitmap |
width | done | Specifies the viewport width |
height | done | Specifies the viewport height |
monochrome | done | Specifies the number of bits per pixel on a monochrome (greyscale) device |
orientation | done | Specifies the orientation of the viewport (landscape or portrait) |
resolution | done | Specifies the resolution of the output device, using dpi or dpcm |
hover | X | Does the primary input mechanism allow the user to hover over elements? |
any-hover | X | Does any available input mechanism allow the user to hover over elements? |
inverted-colors | X | Is the browser or underlying OS inverting colors? |
light-level | X | Light level of the environment |
pointer | X | Is the primary input mechanism a pointing device, and if so, how accurate is it? |
any-pointer | X | Is any available input mechanism a pointing device, and if so, how accurate is it? |
scan | X | Specifies the scanning process of the output device |
scripting | X | Is scripting (e.g. JavaScript) available? |
update | X | How quickly can the output device modify the appearance of the content |
overflow-block | X | How does the output device handle content that overflows the viewport along the block axis |
overflow-inline | X | Can content that overflows the viewport along the inline axis be scrolled |
Resize your browser
to see change
box3
@media screen and (min-width: 480px) {
#box3 {
background-color: lightgreen;
color:black;
}
}
@media screen and (min-width: 768px) {
#box3 {
background-color: teal;
color:white;
}
}
<!DOCTYPE html>
<html>
<head>
<title>Full CSS Code</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
#box3 {
background-color: #e95f73;
width:80%;
height:100px;
text-align:center;
padding:10px;
margin:auto;
}
@media screen and (min-width: 480px) {
#box3 {
background-color: lightgreen;
color:black;
}
}
@media screen and (min-width: 768px) {
#box3 {
background-color: teal;
color:white;
}
}
</style>
</head>
<body>
<div id="box3">Resize your browser<br>to see change<br>box3</div>
</body>
</html>
- turned_in_notConditional Media features
- label_outlineUsing
"and"
operator@media screen and (max-width: 768px) and (min-width: 320px){ body{ background-color: lightgreen; } }
Above Media Query statement specifies that the body background-color becomes lightgreen when browser width is in between 768px & 320px
- label_outlineUsing
","
[act as "or" operator]@media screen and (min-width: 1024px) , (max-width: 468px){ body{ background-color: lightgreen; } }
Above Media Query statement specifies that the body background-color becomes lightgreen when browser width is either at max 468px or minimum 1024px
Here, Any media feature statement becomes true then CSS code inside it will execute
@import
Used to import external style sheet into another style sheet either from same or different domain
Syntax
@import url|string;
/*Example*/
@import url('myCssFilePath.css');
@import "myCssFilePath.css";
Syntax - For media type
@import url|string mediatype and (media feature);
/*Example*/
@import url('myCssFilePath.css') screen and (min-width:1024px);
@import "myCssFilePath.css" print;
- Note! If using then it must be the top statement of any style sheet either in external or internal css
Hello World !
<style>
@import url('cssFilePath.css'); /* must be the top
statement of style sheet */
/*Other css properties*/
#box {
background-color: #e95f73;
........
}
</style>
<!DOCTYPE html>
<html>
<head>
<title>Full CSS Code</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
@import url('external.css');
#box {
background-color: #e95f73;
width:80%;
padding:10px;
text-align:center;
margin:auto;
}
</style>
</head>
<body>
<h1 id="box">Hello World !</h1>
</body>
</html>