Register to get access to free programming courses with interactive exercises

Flexible elements CSS: Site Adaptability

In the first project, we created a page with a fixed width of 1000 pixels. This approach has a right to life. But what happens if we open such a layout on a device with a resolution less than 1000 pixels wide? In that case, horizontal scrolling appears, which annoys users.

No one wants to constantly move the content to read a line or see a sidebar. Besides the obvious inconvenience, the user may not see that the scroll exists. Then he might miss a piece of content.

Let's create a container with a width of 800 pixels and add some styles and text. Inside the container, we'll put three blocks with a width of 245 pixels:

See the Pen css_adaptive_percent_1 by Hexlet (@hexlet) on CodePen.

In this example, the blocks take up a total of 735 pixels. Imagine that the page size has changed to 700 pixels. In this case, all three blocks won't fit on the page. Instead, there will be horizontal scrolling:

See the Pen css_adaptive_percent_1 by Hexlet (@hexlet) on CodePen.

The oveflow-x property emulates browser behavior on the container, which will hide content and add a scrollbar. Think of the .container block as a browser.

When computers and the Internet first appeared, screens were almost standardized. Several basic resolutions were popular so developers could fix the size of elements.

With the advent of mobile devices, laptops, and tablets, we got a variety of resolutions. It became inconvenient to work with the size of entire blocks. Scroll bars constantly appeared. If vertical scrolling is perceived well, then horizontal scrolling infuriates users.

Now developers cannot rely on specific sizes, so they calculate them based on the screen size. We can use percentages for this. Instead of specifying a block size in pixels, we can count it as a percentage of the parent block or the device's current screen resolution.

This approach is called fluid layout.

Let's make the three blocks fluid. To do that, we'll remove the fixed width of 245 pixels and replace the value with 33%. In this way, the three blocks will take up almost all the available space:

See the Pen css_adaptive_percent_2 by Hexlet (@hexlet) on CodePen.

Changing the size of pages or blocks will also change their size in a fluid layout. This feature also helps to create an adaptive site:

See the Pen css_adaptive_percent_2 by Hexlet (@hexlet) on CodePen.

In addition to the pros, this concept has some cons. What happens if the screen resolution is too small? Then our blocks will continue to shrink as long as they can find free space. Then the text inside the blocks can no longer fit and will go beyond its parent. Devices with such low resolutions are hard to find. But working with relative units, we should remember that blocks are shrinking until the end:

See the Pen css_adaptive_percent_3 by Hexlet (@hexlet) on CodePen.

The min-width style is a perfect way to solve this problem. Using it, we can set a stopping point, after which the container and the elements inside will stop shrinking.

The same approach helps to limit content stretching for large widths. More often than not, a site only needs 1200-1300 pixels wide to read text comfortably.

At higher resolutions, we stretch the content too much and get long lines, which are hard to perceive. In that case, we set the stopping point using the max-width rule, which specifies a value in absolute units such as pixels.

Additional materials

  • min-width
  • max-width

Do it yourself

Create a page on your computer with two blocks: a sidebar and a main content block. Make a fluid layout, using relative units, the max-width and the min-width properties. Don't forget to observe the layout's behavior, Using the mobile view emulation in your browser.


Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY
Suggested learning programs
profession
Layout with the latest CSS standards
5 months
from scratch
under development
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.