We can determine which device is being used by using what we call the "viewport" width.
In Computer graphics, the viewport usually refers to the polygonal (usually rectangular) area that the user is currently looking at. When we talk about viewport in Web browser, we usually refer to a window in which content is visible and the user cannot view content outside the window.
There are basically two types of viewports.
The fixed viewport on which the browser draws the entire web page is called the layout viewport.
The portion of the layout viewport that is currently visible due to scaling or any other reason is called the visual viewport.
It basically refers to the physical width and height of the device. There are many devices with different screen sizes; depending on the screen size, the way users interact with web pages changes. So what is the relationship between screen size and viewport width.
Regardless of the device, users are more accustomed to vertical scrolling, which is why we use the width of the viewport to classify devices, because the maximum width a layout viewport can have is constrained by the device physics. width.
At this point, we know what "viewports" are and how they relate to screen size. If we want a responsive web page that changes style after a specific width, we have to set the viewport using the meta viewport tag.
This tag tells the browser how to control the size and scaling of the page. The meta viewport value width=device-width tells the page to adjust its width (in device-independent pixels) to match the screen width.
Pages can utilize the entire landscape width by adding the value initial-scale=1, which instructs the browser to establish a 1:1 relationship between CSS pixels and device-independent pixels regardless of device orientation.
Below is an example of setting the viewport using the device width and initial scaling of 1.
<!DOCTYPE html> <html lang="en"> <head> <title>Example of Viewport</title> </head> <body> <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Atque earum in iste non animi itaque debitis sint! Repellat vero aliquid ullam. Aliquid voluptates recusandae praesentium numquam reiciendis, ullam aliquam nostrum! </p> </body> </html>
We know that we can use viewport width to trigger style changes, now we will discuss media queries in CSS. You can use media queries to apply CSS styles based on the overall type of device (such as print vs. screen) or other details (such as screen resolution or browser viewport width). We use media queries to accomplish the following tasks −
Apply styles conditionally.
When we need to target any specific form of media
Or when we want to test or have to monitor media status
To use media queries, we must specify our target media type and our target functionality. We can also create very complex media queries using logical operators. We can also use not to reverse the meaning of a media query, which can be very convenient in some cases. Let's look at an example of a media query.
@media print { color: black; font-size: larger; }
The above media query will only be applied to print type media and will change the color to black and increase the font size.
An example of a complex media query is given below.
@media (min-width: 30em) and (orientation: landscape) {Color: black; Font-size: larger; }
The above query is applicable to media with a size of at least 30em and landscape orientation.
Media Rules −
The part where we specify media types and media characteristics is often called a media rule.
The following lists various logical operators that can be used in media rules -
no
besides
or
Given below is a sample code that uses media queries to solve the current problem.
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <style> body { background-color: rgb(223, 241, 223); font-size: 20px; } @media only screen and (max-width: 750px) { body { background-color: aliceblue; } } </style> </head> <body> <h1>Example of media query to change background color</h1> <p>Please resize the browser window to see a change in background color. </p> </body> </html>
To summarize, by using media queries in CSS you can change the background color on a specific wider viewport. You just specify the width of the viewport and use it in code to set different background colors for specific viewport sizes. This will allow you to create optimized website designs for every device type and screen size, which is crucial to delivering a great user experience.
The above is the detailed content of How to change the background color of a specific wider viewport in CSS?. For more information, please follow other related articles on the PHP Chinese website!