Bring css into normal form online. How to catch a lion in the desert


Wide development various platforms and devices forces developers to make for them special versions sites, which is quite labor-intensive and problematic. However, times and needs change, and creating a website for different devices is inevitable and necessary link its development. With this in mind, CSS introduced the concept of a media type, where a style is applied only to a specific device. In table 4.1 lists some media types.

CSS uses the @import and @media commands to specify media types, which can be used to style elements depending on whether the document is being output to the screen or to a printer.

The @media and @import keywords refer to et-rules. This name comes from the name of the @ symbol - “et”, with which these keywords begin. In RuNet, the established term “dog” is used to denote the @ symbol. It’s just that I don’t dare use the expression “dog rule”.

When importing a style using the @import command, the media type is specified after the file address. In this case, it is possible to specify several types at once, mentioning them separated by commas, as shown in example 4.1.

Example 4.1. Import style file

HTML5 CSS 2.1 IE Cr Op Sa Fx

Importing a style

IN in this example two files are imported - main.css is intended for changing the appearance of the document when viewing it on a monitor screen, and smart.css - when printing the page and displaying it on a smartphone.

Browser Internet Explorer up to the seventh version inclusive does not support media types when importing a style file.

The @media command allows you to specify the media type for global or linked styles and generally has the following syntax.

@media media type 1 (Style description for media type 1) @media media type 2 (Style description for media type 2)

After keyword@media is one or more media types listed in the table. 4.1, if there is more than one, then they are separated by a comma. This is followed by mandatory braces, inside of which there is a usual description of style rules. Example 4.2 shows how to set a different style for printing and display on the monitor.

Example 4.2. Styles for different types carriers

HTML5 CSS 2.1 IE Cr Op Sa Fx

Media types

How to catch a lion in the desert

Random number method

Let's divide the desert into a number of elementary rectangles, the size of which coincides with the size of the lion's cage. Then we sort through the resulting rectangles, each time choosing a given area randomly. If there is a lion in this area, we will catch it by covering it with a cage.

In this example, two styles are introduced - one to change the appearance of elements when they are normally displayed in the browser, and the second when the page is printed. At the same time, the appearance of a document for different media can vary greatly, for example, as shown in Fig. 4.1 and fig. 4.2.

You can view a document that has CSS set to print type by printing a specific page or by using the browser preview ( File > Preview ). Or use a trick and temporarily replace print with screen to display the result in the browser. This is how the rice was obtained. 4.2.

The @media command is used mainly to generate one style file, which is divided into blocks by device type. Sometimes it makes sense to create several different CSS files - one for printing, one for display in the browser - and connect them to the document as needed. In such a case, you should use the tag with the media attribute, the value of which is all the same types listed in table. 4.1.

Example 4.3. Connecting styles for different media

HTML5 CSS 2.1 IE Cr Op Sa Fx

Various media

This example uses two linked style sheets, one for display in the browser, and one for printing the document and viewing it on a smartphone. Although two are loaded onto the page at the same time different styles, they apply only to certain devices.

Similarly, you can use global styles by adding the media attribute to the tag

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diem nonummy nibh euismod tincidunt ut lacreet dolore magna aliguam erat volutpat.

In this example, the width for handheld devices is limited to 320 pixels.

Questions to check

1. Pasha decided to make a printable version for his website. Which line should he use in the code?

  1. @import "palm.css" print;
  2. @import url("printer.css") printer;
  3. @media "palm.css" print;