The units are vw and vh. Absolute units

One article is not necessary.

1px (pixel)= 1/96 inch but not in CSS. 1 px in CSS is a dot on the user's screen, physical quantity which depends on the resolution of the device and on the distance from which a person looks at its surface ( mobile phone or TV). In whatever units of measurement we would not write the code, it is reduced to px.

1 em= to the parent's font-size value. Note that in the polygon below the yellow and brown blocks have different font-sizes, but the font size is the same.

16px * 1 = 16px // yellow 16px * 0.5 = 8px // green 8px * 2 = 16px // brown

1 rem= value of font-size (the root element of the document).

16px * 1 = 16px // yellow 16px * 0.5 = 8px // green 16px * 2 = 32px // brown

1 percent (1%)— value relative to the property value of the parent tag.

16px * 100% = 16px // yellow 16px * 50% = 8px // green 8px * 200% = 16px // brown

Decreasing the width of the parent also reduces the width of the element, but not its font.

1vw= 1% of window width. Decreasing the width of the window reduces the width, height, and font of the element. The font will not be scaled when pressing Ctrl + or Ctrl - .

1vh= 1% of window height. Decreasing the height of the window reduces the width, height, font of the element. The font will not be scaled when pressing Ctrl + or Ctrl - .

1vmin= 1vw or 1vh. The one that is smaller is chosen.

1vmax= 1vw or 1vh. The one that is larger is chosen.



font-size: 16px 100% 1em 1vw 1vh 1vmin 1vmax 1rem ;

font-size: 8px 50% .5em .5vw .5vh .5vmin .5vmax .5rem ;

font-size: 32px 200% 2em 2vw 2vh 2vmin 2vmax 2rem ;

font-size: 16px 100% 1em 1vw 1vh 1vmin 1vmax 1rem;
font-size: 8px 50% .5em .5vw .5vh .5vmin .5vmax .5rem;
font-size: 32px 200% 2em 2vw 2vh 2vmin 2vmax 2rem;

calc() function

Have you ever wanted to subtract pixels or ems from percentages? For example, in order to . Now it's possible. The calc() function allows you to implement mathematical expressions

Addition (character separated by spaces on both sides) - subtraction (character separated by spaces on both sides) * multiplication / division

Here's another good example, .

  • 1
  • 2
  • 3
  • 4

One of the most confusing aspects css is the application font-size attribute for text scaling. Using CSS, you can change the text size in the browser with four different units measurements. Which of these four units is best for the web? This is a question that has generated a variety of discussions and criticism. Finding a definitive answer is difficult because the question itself is complex.

Meet - units

1. "Ems" (em): "em" is a scalable unit that is used in web documents. "em" is equal to the current font-size, for example, if the document's font-size is 12pt, 1em is 12pt. "em" is inherently scalable, so 2em would be equal to 24pt, 0.5em would be equal to 6pt, and so on. The use of "em" is becoming more and more popular in web documents due to its scalability and ability to be usefully applied in mobile devices Oh.
2. Pixels (px): "px" have fixed size units that are used on screens (for example, for reading on a computer screen). One pixel equals one dot on a computer screen (the smallest element of your screen resolution). Many web designers use px in web documents in order to achieve a pixel-perfect representation of their site when displayed in a browser. One of the problems with using px is that these units don't allow zooming for visually impaired readers or mobile devices.
3. Points (pt): "pt", traditionally used in print media (anything that needs to be printed on paper, etc.). One "pt" is equal to 1/72 of an inch. "pt", like "px", have a fixed unit size and cannot be scaled.
4. Percents (%): Units of measure in % are similar to "em", except for a few fundamental differences. First, the current font-size is 100% (i.e. 12pt = 100%). By using "%", your text becomes fully scalable for mobile devices and user-friendliness (accessibility).

So what's the difference?

It will be easy to understand the difference between font-size units when you see them in action. As a general rule, 1em = 12pt = 16px = 100%. With these font sizes, let's see what happens when you increase the base font size (with using CSS selector body) from 100% to 120%.

Change Font-size from 100% to 120%.

As you can see, "em" and "%" increased the font size, while "px" and "pt" did not. Setting an absolute size for your text can be a simple matter, but it's much better for your visitors to use scalable text that can be displayed on any device or machine. For this reason, the units "em" and "%" are preferred to be used for web document text.

"em" vs "%"

We found out that the units "px" and "pt" are not in the best way are suitable for web documents, which forces us to use "em" and "%". In theory, the units "em" and "%" are identical, but in practice they have minor differences that are important to consider.

In the example above, we used percentages as the base font-size unit (in the body tag). If you change your font-size base unit from "%" to "em" (i.e. body (font-size: 1em;)) you, should not notice the difference. Let's see what happens when "1em" is our base unit and when the client changes the "Font Size" in their browser settings (many browsers such as Internet Explorer have this option).


The font size when the client resizes the text in the browser.

When the text size is set to "medium" in the client browser, there is no noticeable difference between "em" and "%". However, if the parameter is changed, the difference becomes very large. When set to "Smallest", "em" is much smaller than "%", and when set to "Largest", vice versa, "em" is displayed much larger than "%". While many claim that the units in "em" scale as intended, in practice the text in "em" is scaled too sharply, with the smallest text becoming illegible on some machines.

Verdict

In theory, "em" units are the new and upcoming font-size standard on the web, but in practice, "%" units allow text to be displayed more consistently and comfortably to users. When changing client parameters, the text in "%" changed in reasonable proportions, which allows designers to maintain readability, accessibility, and design.

Winner: percent (%).

New ones have been added to CSS3 relative units measurements such as vh , vw , vmin , vmax . These units are calculated relative to the size of the browser window. For desktop computers browser window width more width viewport (the width of the scrollbar is added), so if you set the width of the element to 100vw , it will go outside the html.



Rice. 1. Full screen background image with width 100vw

Browser Support

IE: 9.0 other than vmax, use vm instead of vmin
edge: 12.0 except vmax, use vm instead of vmin
Firefox: 19.0
Chrome: 26.0
opera: 15.0
safari: 6.1
iOS Safari: 8.0
android: 4.4
Chrome for Android: 55.0

1. vh and vw units

tricks responsive web design based on the use of percentages. But the percentages are far from the best solution for each case, since they are calculated relative to the dimensions of the nearest parent element. So if you want to use the height and width of the browser window, it's better to use vh and vw units. For example, if the height of the browser window is 900px , then 1vh will be equal to 9px . Similarly, if the width of the browser window is 1600px , then 1vw will be equal to 16px .

1.1. Responsive fullscreen background image

Since the width of the element specified with 100vw is greater than the width of the viewport, it is better to use a width of 100% to create full screen background images, which will be equal to the width of the root html element.

Fullscreen-bg ( background: url(image.jpg); background-position: center; background-size: cover; width: 100%; height: 100vh; ) Rice. 2. Responsive fullscreen background image

1.2. full page slide effect

To make the block fill the entire height of the browser window, you need to set height: 100%; for three elements - html, body and directly for the block itself:

Html, body ( height: 100%; ) section ( height: 100%; )

Since percentage sizes are calculated relative to the values ​​of the parent elements, it is necessary to set the appropriate values ​​for each DOM element. The vh unit does not need to be chained, as its value is calculated directly relative to the browser window:

Section (height: 100vh; )

The effect of flipping slides when clicking on the arrow is implemented using small script jQuery:

jQuery(document).ready(function($) ( $("nav").on("click", function() ( if ($(this).hasClass("down")) ( var movePos = $(window ).scrollTop() + $(window).height(); ) if ($(this).hasClass("top")) ( var movePos = $(window).scrollTop() - $(window).height( ); ) $("html, body").animate(( scrollTop: movePos), 600); )); ));

The height given with vh can also be used to align an element to the center of the page.

From the author: several years have passed since the first showing of viewport units in CSS. These are really adaptive length units, their value changes according to the size of the browser window. If you have heard of them but never went into the details, this article is for you.

Units of measurement and their meaning

There are 4 types of viewport units in CSS: vh, vw, vmin and vmax.

Viewport height (vh) - Based on the height of the viewport. The 1vh value is equal to 1% of the viewport height.

Viewport width (vw) - Based on the viewport width. The value of 1vw is 1% of the viewport width.

Viewport minimum (vmin) - based on the minimum side of the viewport. If the viewport's height is less than its width, 1vmin will be equal to 1% of the height. Similarly, if the width is less than the height, then 1vmin will be equal to 1% of the viewport width.

Viewport maximum (vmax) - based on the large side of the viewport. If the viewport height is greater than the width, then the value of 1vmax will be equal to 1% of the viewport height. If the viewport's width is greater than its height, then 1vmax will be equal to 1% of the width.

Let's see what values ​​we get in different situations:

If the viewport is 1200px wide and 1000px high, then 10vw will be 120px and 10vh will be 100px. The viewport's width is greater than its height, so 10vmax will be 120px and 10vmin will be 100px.

If we rotate the device so that the width is 1000px and the height is 1200px, then 10vh will be 120px and 10vw will be 100px. Interestingly, 10vmax will still be 120px, because now the value is determined by the height of the viewport. The 10vmin value will also remain 100px.

If we shrink the browser window to 1000px wide and 800px high, then 10vh will be 80px and 10vw will be 100px. Similarly, 10vmax will become 100px, and 10vmin will become 80px.

On this moment viewport units for you may not differ much from percentages, but the difference is large. In the case of percentages, the width and height of child elements depend on the parent block. Example:

The demo shows how the width of the first child element occupies 80% of the width from the parent. The second child element has a width of 80vw, making it wider than the parent.

Applying Viewport Units

These units are based on the size of the viewport, so they are very useful in situations where the width, height, or dimensions of elements need to change depending on the size of the viewport.

Full screen background images and sections

On the web, it is quite common to see background images on elements that take up the entire screen. You can do the same in website design so that a separate section about a product or service takes up the entire screen. In such cases, you can set the width of the elements to 100% and the height to 100vh.

Let's analyze the following HTML example:

a

< div class = "fullscreen a" >

< p >a< p >

< / div >

The CSS below will stretch the background image section to its full width:

Fullscreen ( width: 100%; height: 100vh; padding: 40vh; ) .a ( background: url("path/to/image.jpg") center/cover; )

full screen (

width: 100%

height : 100vh ;

padding : 40vh ;

background : url ("path/to/image.jpg" ) center / cover ;

Perfect headlines

You could hear or even use jQuery plugin Fit Text. Headers can be scaled to fit the full width of the parent element with this plugin. As I said before, the value of the viewport units directly depends on the size of the viewport. That is, if you specify the font-size of the titles in viewport units, then they will ideally fit under each screen. If the viewport width changes, the browser will automatically change the title. It is only necessary to determine the correct initial value for font-size.

the main problem with font-size and viewport units is that the size of the text will vary greatly depending on the viewport. For example, a font-size of 8vw would make the title 96px for a 1200px wide viewport, 33px for a 400px wide viewport, and 154px for a 1920px wide viewport. The font can be either too large or too small for comfortable reading. Read more about correct installation text sizes using units and calc functions() can be found in a wonderful article about typography on viewport units.

Easy element centering

Viewport units can be of great help when it comes to placing an element exactly in the center of the user's screen. If the height of the element is known, then you just need to set the top and bottom values margin properties in [(100 - height)/2]vh.

Centered (

width : 60vw ;

height : 70vh ;

margin : 15vh auto ;

Things to remember

If you decide to use the unit viewport in your projects, there are a few things you need to keep in mind.

When working with CCS, it is very easy to get stuck at some point, because sometimes even working according to a familiar method, it happens that new problems appear.

As the Internet grows, so does the demand for new solutions. And since you and I are web designers and front-end developers, we have no other choice but to be able to work with various options and be able to work well.

This means that we need to be able to work well even with those specific tools that are usually used very rarely, but there are situations when they are needed.

Today we would like to tell you about some CSS tools. Each of these tools is like a unit of measure, like a pixel or ems, but you probably didn't know about them.

Let's start with what looks like something you're probably already familiar with. The em unit is defined as the current font-size. So if you, for example, set the font-size on the body element, then the em value of any minor element within the body will be equal to that font-size.

test


body ( font-size: 14px; ) div ( font-size: 1.2em; // calculated at 14px * 1.2, or 16.8px )

Here we have written that the div will have a font-size of 1.2em. This is 1.2 times the font-size it inherited from, which was 14px. So the result is 16.8px.

However, what happens if you cascade the font-sizes defined by ems inside each other? In the following passage, we apply the same CCS as above. Each div inherits the font-size from its parent, giving us progressively larger font sizes.

test
test
test


While this may be appropriate in some cases, we often want to just rely on a single metric to measure. In this case, we must use rem. The "r" in rem stands for "root"; it is equal to the font size set in the root element; in most cases it is an HTML element.

Html ( font-size: 14px; ) div ( font-size: 1.2rem; )

In all three divs from the previous example, the font can be up to 16.8px.

5 points, Grid

Rems are useful for more than just font sizing. For example, we could base the entire grid system or UI library on the use of size HTML font root rem, and use their calculation in certain places. This would give us more predictable font sizing and calculations.

Container ( width: 70rem; // 70 * 14px = 980px )

The concept of the idea is to allow our interface to be commensurate with the size of the content. Although it is not necessary that in every case taken it has the same meaning.

vh and vw

Responsive web design techniques rely heavily on the percentage rule. However, CSS percentage is not always the best solution for every problem. The CSS width is relative to the nearest element that contains the parent. What if we want to use viewport width or height instead of width original element? This is exactly what vh and vw units help to do.

The vh element is equal to 1/100 of the height of the viewport. For example, if the browser's height is 900px, 1vh can go up to 9px. Also if the viewport width is 750px, 1vw can reach 7.5px.

It seems that this rule can be used indefinitely. For example, a very simple way to achieve with a single css strings full height, or approximately full height of the slides, you can:

Slide (height: 100vh; )

Let's say you want to make a title that fills the full width of the screen. To do this you need to set font-size to vw. This size will be commensurate with the width of the browser.

vmin and vmax

While vh and vm are always related to the height and width of the viewport, vmin and vmax are also related to the maximum and minimum of that height and width, whichever is smaller and which is larger. For example, if the browser is 1100px wide and 700px high, 1vmin would be 7px and 1vmax would be 11px. However, if the width is set to 800px and the height to 1080px, then vmin will be 8px while vmax will be set to 10.8px.

In what cases can you use these values?

Imagine that you want an element that always remains visible on the screen. Using height and width, set by value vmin below 100 will allow you to achieve this. For example, a square element that touches at least two parts of the screen can be defined like this:

Box ( height: 100vmin; width: 100vmin; )

If you want a square that will cover the entire visible area of ​​the viewport, use the same rules, except for vmax.

Box ( height: 100vmax; width: 100vmax; )

The combination of these rules will allow you to use the size of your viewport very flexibly and with maximum productivity.

ex and ch

The units ex and ch, like em and rem, refer to the current font and font size. However, because they are based on font-specific measures, ex and ch also map to the font-family, unlike em and rem.

The ch unit, or character unit, is defined as an "improved measure" of the width of a 0 character. This notion has caused a lot of controversy, but the basic idea is that in a fixed-width font, a square with N character width units, such as width: 40ch; can always contain a sequence of 40 characters in that particular font. While common use Of this particular rule is the braille layout, the scope for creativity here, of course, extends beyond these simple statements.

The ex unit is defined as "the x-height of the current font OR one-two of the em". Thex-height given font- height lowercase letters x of that font. Most often, this is approximately in the middle mark of the font.

There are many areas where such units can be used. Most often in print. For example, the sup element, which acts as a superscript, can be added to a string using the appropriate position and a final value of 1ex. Similarly, you can reset the bottom element. Browser errors are fixed by the superscript- and subscript-specific vertical-align rules, but if you want more control you can try the following:

Sup ( position: relative; bottom: 1ex; ) sub ( position: relative; bottom: -1ex; )

Conclusion

It's important to keep up with the development and spread of CSS, as well as learning about new tools and adding them to your list of skills. You will most likely encounter problems that can be solved with these units. Take the time to read the specification. Subscribe to updates from cssweekly. And of course, don't forget to subscribe for weekly updates and free tutorial videos on Tuts+!