The most important feature of style sheet is how a document is to be presented in diffeent media.
Media query is a w3c recommendation .
Media rule makes you to set different rules for media types.
Media queries also used to check :
View point (Width and Height)
Device (Width and Height)
Orientation mode(Landscape and portable)
resolution
using these media queries techniques you can set a tailored style sheet to all type of devices.
Syntax : Media Queries
@media not|only mediatype and (expressions) {
CSS-Code;
}
The corresponding stylesheet or style rules are applied when the media query is true.
Unless you use the not or only operators, the media type is optional.
The following is a list of media features for media queries
Feature |
Value |
Min/Max |
Description |
color |
integer |
yes |
Used to specifies the number of bits per color component. |
color-index |
integer |
yes |
Used to specifies the number of entries in the color lookup table. |
device-aspect-ratio |
integer |
yes |
Used to specifies the aspect ratio of the device. |
device-height |
length |
yes |
Used to specifies the height of the output device. |
device-width |
length |
yes |
Used to specifies the width of the output device. |
grid |
integer |
no |
Used to is true for a grid-based device. |
height |
length |
yes |
Used to specifies the height of the rendering surface. |
monochrome |
integer |
yes |
Used to specifies the number of bits per pixel in a monochrome frame buffer. |
resolution |
resolution (dpi or dpcm) |
yes |
Used to specifies the resolution of the display screen. |
scan |
progressive or interlaced |
no |
Used to specifies scanning process of "tv" media types. |
width |
length |
yes |
Used to specifies the width of the rendering surface. |
Example : Media Queries
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
box-sizing: border-box;
}
/* Style the top navigation bar */
.toplink {
overflow: hidden;
background-color: rgb(2, 8, 63);
}
/* Style the links */
.toplink a {
float: left;
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
/* Change color on hover */
.toplink a:hover {
background-color: rgb(16, 80, 218);
}
/* On screens that are 600px wide or less,
make the menu links stack on top */
@media screen and (max-width: 600px) {
.toplink a {
float: none;
width: 100%;
}
}
</style>
</head>
<body>
<h2>Responsive Menu</h2>
<p>Media Queries techniques used to set a tailored style sheet to all
type of devices.</p>
<div class="toplink">
<a href="/html/html.html">HTML</a>
<a href="/css/css.html ">CSS</a>
<a href="/javascript/javascript.html ">Java Script </a>
</div>
</body>
</html>
Example : Media Queries
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
/* browser's width is between 600 and 900px OR above 1100px, Change the DIV. */
@media screen and (max-width: 900px) and (min-width: 600px), (min-width: 1100px) {
div.example{
font-size: 50px;
padding: 50px;
border: 6px solid red;
background: rgb(2, 8, 63);
color:white;
}
}
</style>
</head>
<body>
<h2>Change the DIV on different screen sizes</h2>
<div class="example">codealways.com</div>
<p>Media Queries techniques used to set a tailored style sheet to all
type of devices.</p>
</body>
</html>