Media queries for reactive design: a complete guide (2021)

Welcome to CSS media query novice guide. This article will describe in detail what media query is, how it works, and how to use it correctly. Different from the public trust, they go beyond reactive design (even the content focused here). Finally, we want you to know how to implement your own media query and customize the way the website works. What is CSS media query? By default, media queries are the way to create conditional CSS. This means that CSS annotations are rendered only when the browser meets certain conditions. The most common is reactive design, which instructs browsers to change the display of site elements when they are larger or smaller than a specific screen size. However, as shown below, different methods can be used. The
How to create a media query? So what is the right way to use media queries? In CSS, they are part of at rules, in this case @media. Then add the functions \/ conditions required to apply the target media type and query@ For example, the following code snippet is for devices with screens larger than 320 pixels: media[media-type] ([media feature]) {\/\/custom css}. All contents in brackets in the media query are only output in the browser of the device whose screen meets the conditions. This is also called a breakpoint, and refers to a point where the design changes significantly to accommodate different screen sizes. The
In the operator CSS media query of @media screen and (min width: 320px) {\/\/custom css} media query, operators such as and, or can be used without the following conditions:@ In the example on media screen and (min width: 320px) and (max width: 786px) {\/\/custom css}, we modified the original query to have not only the applicable minimum screen size, but also the applicable maximum screen size. Also, if the only style matches the entire query, it can be applied. It is also often used to improve compatibility with older browsers and media queries. For example, old browsers can only interpret @media screen and (min width:320px) as @media screen, which is applied in the wrong way. @Media only screen and (min width:320px). This operation will not affect the latest negative browser. The
The available media types screen is the most common media type to query, but there are other options. All- for all devices. You can also list only one or more media types, i.e. devices that read content (such as voice screen readers), to locate two or more media types for documents viewed in print print preview or media that display content in a print intended manner@ In the context of pre reactive design of media query for media screen and speech{\/\/custom css} media functions \/ conditions, the most common media functions include width, min width and max width. But there are more options: The
It is almost the same as the height width, but equivalent to the height of the device. In addition, min height and Max height are required to define the range. Aspect ratio the aspect ratio for the viewport. Orientation – queries can be applied depending on whether the device is in portrait mode or landscape mode. Hover
-Introduce conditional CSS on the device, which has a variety of ways to escort it to elements, such as the mouse and touch screen. The following features are also important for accessibility: Prevent reduce motion for example, you can disable animation on the site for use by people who have this setting in their browsers. Prevent territory – this media query allows you to define different color configurations for users who set their preferences to low or high. But there are no supported browsers. More features, such as device resolution or color depth. You can find the complete list here. The
Important: if you do not discuss viewport meta tags viewport meta tags, the guide to CSS media queries will not be completed. Located in the male section of the website, as follows: What does this do? Reset the viewport size in the mobile browser. This is necessary because mobile browsers usually set a wider display area than the actual device. It’s designed to allow them to match sites that don’t look scary or responsive. By default, a wider viewport is simulated so that the design still fits. The
Male image source: entering Google viewport meta tag will not happen, making media query work as expected. Media query examples now that we have seen how media queries typically work, let’s look at a few practical examples to bring new knowledge home. To define breakpoints for various devices, we will start with the classic example. Among them, change the font size of the site according to the site you see on the desktop, tablet or mobile phone. Body {font size: 20px;}@ Media only screen and (max width: 768px) {body {font size: 18px;}}@ Media only screen and (max width: 450px) {body {font size: 16px;}} This allows you to read the text on a small screen without overwhelming it. The
As with androg orientation, the following is an example of changing the action depending on whether the site is displayed in portrait or landscape mode@ Media only screen and (orientation: portrait) {div {margin left: 0px; margin right: 0px;}}@ Media only screen and (orientation: landscape) {div {margin left: 10px; margin right: 10px;}} In vertical mode, no margins are added to the sides of div elements. In contrast, in landscape mode with more free space, the media query adds 10 pixels to each face. The
You can also combine multiple queries, separated by commas. Commas work the same way as the or operator@ Media screen and (orientation: portrait), screen and (max width: 320px) {div {margin left: 0px; margin right: 0px;}} In this case, if the device is in portrait mode or the width is less than 320 pixels, the browser will not apply margins. One condition is enough. If you do not pay special attention to the target location of devices with screens, you can also simplify the query of @media (orientation:graph) and @max-width:320px. The
HTML and JavaScript media query, but media query is not just a CSS problem. This is the most common application, but it can also be used in HTML and JavaScript. For example, HT below
Ml snipet loads different stylesheets on different screen sizes. In order to maintain other designs more easily, you can perform this task. Instead of editing a media query in a large style sheet, you can edit a separate small style sheet. The
You can also use media queries in JavaScript, but here, the first is window, not this guide. The conditions to be met must be defined through matchmedia(). Examples can be found here. You do not always have to use media queries to reflect alternative elements of media queries. Some of them can do that in a box. CSS technologies like flexbox and grid serve this purpose. This allows you to create HTML elements that automatically fit into viewports in a variety of ways that control behavior. Male also mentioned in the introduction to reactive design that another important thing to turn a topic or website into reactive is to use relative measurements instead of absolute measurements. That is, it is recommended that you specify a relative size (32.5%) instead of defining the sidebar as 320px wide. This displays based on the available space, rather than forcing the element to a specific width of the unacceptable screen, regardless of the screen type displayed. Therefore, it is important to learn about media query, but it is also important to understand other modern methods to make the website suitable for a smaller screen size. Please use the tutorial for beginners linked above to learn more. You have learned how to create breakpoints through media query breakpoints 2021 media query, so now please answer the question where to place the breakpoints. In the past, when the number of devices was small, special optimization could be carried out for different devices. But today, we need to deal with more types of devices, screen sizes, resolutions, and so on, which is no longer possible. So, \

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *