CSS3 Media Query For All Devices
CSS3 Media Query For All Devices
HTML CSS JavaScript jQuery PHP Bootstrap NodeJS ReactJS AngularJS ExpressJS
Media queries can be used to check many things like the following
A media query consist of a media type that can contain one or more
expression which can be either true or false. The result of the query is
true if the specified media matches the type of device where the
document is displayed on. If the media query is true then the style is
applied. In simple words, it uses the @media rule to add the block of
CSS properties, based on certain conditions.
Syntax:
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy
Got It !
▲ HIDE AD
1 de 9 24/01/23, 05:22
CSS3 Media query for all devices - GeeksforGeeks https://www.geeksforgeeks.org/css3-media-query-for-all...
We can add the breakpoint to see the screen-width along with the width
and height of the viewport for the different devices. A breakpoint is a
point or key that determines when to change the layout by reshaping &
adding the new rules inside the media queries. There are some common
breakpoints, not a standard resolution, that can be used for the
different widths & heights of devices:
2 de 9 24/01/23, 05:22
CSS3 Media query for all devices - GeeksforGeeks https://www.geeksforgeeks.org/css3-media-query-for-all...
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>GeeksforGeeks CSS Media Query</title>
<meta name="description"
content="CSS Media Query for all devices
like mobile, tablet, desktop etc.">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<link rel="stylesheet" href="gfg-style.css">
</head>
<body>
<div class="gfg-div">GeeksforGeeks</div>
</body>
</html>
CSS Code: The following CSS code specifies the media queries with
different styling properties based on certain conditions that will be
displayed according to the device size.
CSS
* {
margin: 0;
padding: 0;
}
/* Default Design */
.gfg-div {
cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
We use
/* To make that all elements
you have centerour*/
read and understood Cookie Policy & Privacy Policy
display: flex; Got It !
justify-content: center; ▲ HIDE AD
3 de 9 24/01/23, 05:22
CSS3 Media query for all devices - GeeksforGeeks https://www.geeksforgeeks.org/css3-media-query-for-all...
align-items: center;
/* Default Styling */
margin: 20px auto;
padding: 30px;
font-size: 30px;
width: 300px;
height: 300px;
background-color: darkseagreen;
color: black;
}
/* For Desktop View */
@media screen and (min-width: 1024px) {
.gfg-div {
background-color: #63c971;
color: #fff;
}
}
/* For Tablet View */
@media screen and (min-device-width: 768px)
and (max-device-width: 1024px) {
.gfg-div {
width: 400px;
height: 400px;
background-color: orange;
color: black;
}
}
/* For Mobile Portrait View */
@media screen and (max-device-width: 480px)
and (orientation: portrait) {
.gfg-div {
width: 200px;
height: 200px;
background-color: red;
color: #fff;
}
}
cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
We use
/* For Mobile Landscape
that you have read andView */ our Cookie Policy & Privacy Policy
understood
@media screen and (max-device-width: 640px)
Got It !
and (orientation: landscape) { ▲ HIDE AD
4 de 9 24/01/23, 05:22
CSS3 Media query for all devices - GeeksforGeeks https://www.geeksforgeeks.org/css3-media-query-for-all...
.gfg-div {
width: 400px;
height: 200px;
background-color: cyan;
color: black;
}
}
/* For Mobile Phones Portrait or Landscape View */
@media screen and (max-device-width: 640px) {
.gfg-div {
width: 400px;
height: 200px;
background-color: chartreuse;
color: black;
}
}
/* For iPhone 4 Portrait or Landscape View */
@media screen and (min-device-width: 320px)
and (-webkit-min-device-pixel-ratio: 2) {
.gfg-div {
width: 400px;
height: 400px;
background-color: brown;
color: black;
}
}
/* For iPhone 5 Portrait or Landscape View */
@media (device-height: 568px) and (device-width: 320px)
and (-webkit-min-device-pixel-ratio: 2) {
.gfg-div {
width: 400px;
height: 400px;
background-color: cornflowerblue;
color: black;
}
}
/* For iPhone 6 and 6 plus Portrait or Landscape View */
@media
We use cookies to(min-device-height: 667px)
ensure you have the best browsing and
experience (min-device-width:
on our 375px)
website. By using our site, you acknowledge
and (-webkit-min-device-pixel-ratio: 3)Policy
that you have read and understood our Cookie { & Privacy Policy
.gfg-div {
Got It !
width: 400px; ▲ HIDE AD
5 de 9 24/01/23, 05:22
CSS3 Media query for all devices - GeeksforGeeks https://www.geeksforgeeks.org/css3-media-query-for-all...
height: 400px;
background-color: darkgoldenrod;
color: black;
}
}
Output:
Supported Browsers:
• Google Chrome
• Firefox
• Microsoft Edge
• Internet Explorer
• Opera
• Safari
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy
Got It !
▲ HIDE AD
6 de 9 24/01/23, 05:22
CSS3 Media query for all devices - GeeksforGeeks https://www.geeksforgeeks.org/css3-media-query-for-all...
Like 9
Next
Related Articles
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy
Got It !
Article Contributed By : ▲ HIDE AD
7 de 9 24/01/23, 05:22
CSS3 Media query for all devices - GeeksforGeeks https://www.geeksforgeeks.org/css3-media-query-for-all...
SohelRaja
@SohelRaja
Company Learn
About Us DSA
We use cookies to ensure you have the best browsing experience on our website. By using
Careers our site, you acknowledge
Algorithms
that you have read and understood our Cookie Policy & Privacy Policy
In Media Data Structures
Got It !
Contact Us ▲ SDE Cheat Sheet HIDE AD
8 de 9 24/01/23, 05:22
CSS3 Media query for all devices - GeeksforGeeks https://www.geeksforgeeks.org/css3-media-query-for-all...
Courses
News Languages
Top News
Python
Technology
Java
Work & Career
CPP
Business
Golang
Finance
C#
Lifestyle
SQL
Knowledge
Kotlin
Bootstrap Internships
NodeJS
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy
Got It !
▲ HIDE AD
9 de 9 24/01/23, 05:22