We set that our table occupies 100% of the parent element, the table cells occupy 25% of the parent element and have a solid border of 1 pixel green, the height of the header and data cells is 45px . We removed the gap between cells using the property border-collapse with meaning .
And so, using pseudo-element ::after add content after each element on hover. Pseudo element ::after must be used together with the property content, thanks to which we insert a block element that has a background color forestgreen and has absolute positioning.
Absolute positioning here is necessary to offset the element relative to the given edge of its ancestor, while the ancestor must have the value position different from the default - static , otherwise the count will be relative to the specified edge of the browser window, for this reason we have set for the table relative positioning(relative ).
We set the property for our generated block top, which specifies the offset direction of the positioned element from the top edge, and the property bottom A that specifies the direction in which the positioned element is offset from the bottom edge. Both properties were set to 0 , so the block will completely occupy the element from the bottom and top of the table, the width of this block was set to 25%, this value corresponds to the value of the width of the cell itself.
And the final property we set for this block: z-index with a value of "-1" it determines the order in which positioned elements are arranged Z axis. This property is necessary so that the text is in front of this block, and not behind it, if you do not set a value less than zero, then the block will close the text.
The result of our example:
If at this stage of the study you do not understand the process of positioning elements, then do not be discouraged, this is a difficult topic to understand, which we also did not consider, but we will definitely consider it in the next article of the tutorial " Positioning elements in CSS ".
Questions and tasks on the topic
Before moving on to the next topic, complete the practical task:
![](https://i2.wp.com/basicweb.ru/css/primer/practice_31.png) If you are having difficulty doing practical task, You can always open the example in a separate window and inspect the page to see what CSS was used.
2016-2020 Denis Bolshakov, you can send comments and suggestions on the site to [email protected]
The tables themselves look rather "poor", besides, browsers display some characteristics of tables in their own way, in particular, frames. However, these shortcomings are easy to correct using the power of styles. At the same time, the tools for designing tables are greatly expanded, which allows you to successfully fit tables into the site design and present tabular data more clearly.
Cell background color
The background color of all table cells at the same time is set through the background property, which is applied to the TABLE selector. At the same time, you should remember the rules for using styles, in particular, the inheritance of element properties. Although the background property is not inherited, the default background value for cells is transparent , i.e. transparency, so the effect of filling the background is also obtained for the cells. If you specify a color for the selector TD or TH at the same time as TABLE, then this color will be set as the background of the cell (example 2.3).
Example 2.3. Background color
tables
Heading 1 | Heading 2 |
Cell 3 | Cell 4 |
In this example, we get a blue background color for cells (tag | ) and red at the title (tag | ). This is because the style for the TH selector is not defined, so the background of the parent, in this case the TABLE selector, is "shown through". And the color for the TD selector is specified explicitly, so the cells are “filled” with blue.
The result of this example is shown in Fig. 2.4.
Rice. 2.4. Changing the background color
Margins inside cells
The margin is the distance between the edge of the cell's content and its border. Usually, the cellpadding attribute of the tag is used for this purpose. . It defines the margin value in pixels on all sides of the cell. You can use the padding style property by adding it to the TD selector, as shown in Example 2.4.
Example 2.4. Fields in tables
XHTML 1.0 CSS 2.1 IE Cr Op Sa Fx
tables
Heading 1 | Heading 2 |
Cell 3 | Cell 4 |
In this example, by grouping selectors, the fields are set simultaneously for the selector TD and TH . The result of the example is shown in Fig. 2.5.
![](https://i2.wp.com/htmlbook.ru/files/images/layout2/2-05.png)
Rice. 2.5. Fields in cells
If the padding style property is applied to table cells, then the effect of the cellpadding attribute of the tag ignored.
Distance between cells
To change the spacing between cells, use the cellspacing attribute of the tag . The effect of this attribute is clearly visible when using borders around cells or when filling cells with a color that stands out against the background of the page. Cellspacing is replaced by the border-spacing style property, which sets the distance between cell borders. One value sets both the vertical and horizontal distance between cell borders. If this property has two values, then the first defines the horizontal distance (i.e., to the left and right of the cell), and the second determines the vertical (top and bottom).
The border-spacing property only has an effect if the TABLE selector does not have border-collapse set to collapse (Example 2-5).
Example 2.5. Distance between cell borders
XHTML 1.0 CSS 2.1 IE 7 IE 8+ Cr Op Sa Fx
Replacing cellspacing
Leonardo | 5 | 8 |
Raphael | 4 | 11 |
Michelangelo | 24 | 9 |
Donatello | 2 | 13 |
The result of this example is shown in Fig. 2.6.
![](https://i2.wp.com/htmlbook.ru/files/images/layout2/2-06.png)
Rice. 2.6. Table appearance when using border-spacing
Internet browser Explorer up to and including version 7 does not support the border-spacing property, so this browser will use the default cellspacing value for tables (usually 2px).
When you add a border-collapse property with a collapse value to the TABLE selector, the cellspacing attribute is ignored and the border-spacing value is set to zero.
Borders and Frames
By default, there is no border in the table initially, and it is added using the border attribute of the tag . Browsers display such a border differently, so it's better not to specify this attribute at all, and to assign border drawing to styles. Let's look at two methods directly related to styles.
Using the cellspacing attribute
It is known that the cellspacing attribute of the tag sets the distance between table cells. If you use a different background color for the table and cells, then a grid of lines will appear between the cells, the color of which matches the color of the table, and the thickness is equal to the value of the cellspacing attribute in pixels. Example 2.3 above shows this effect, so I won't repeat it.
Note that this is not a very convenient way to create borders, since it has a limited scope. This way you can get only a single-color grid, and not vertical or horizontal lines in the right places.
Applying the border property
The border style property simultaneously sets the color of the border, its style, and the width around the element. When you want to create separate lines on different sides, it is better to use derivatives - border-left , border-right , border-top and border-bottom , these properties respectively define the border on the left, right, top and bottom.
By applying the border property to the TABLE selector, we add a border around the table as a whole, and to the TD or TH selector, we add a border around the cells (Example 2-6).
Example 2.6. Adding a double border
XHTML 1.0 CSS 2.1 IE Cr Op Sa Fx
tables
Heading 1 | Heading 2 |
Cell 3 | Cell 4 |
This example uses a black double border around the table itself and a solid white border around each cell. The result of the example is shown in Fig. 2.7.
![](https://i2.wp.com/htmlbook.ru/files/images/layout2/2-07.png)
Rice. 2.7. Border around table and cells
Note that double lines are formed where the cells join. They are obtained again due to the action of the cellspacing attribute of the tag . Although this attribute does not appear anywhere in the example code, the browser uses it by default. If you set , then we get not double, but single lines, but of doubled thickness. To change this feature, apply the border-collapse style property with the value collapse , which is added to the TABLE selector (Example 2-7).
Example 2.7. Creating a Single Frame
XHTML 1.0 CSS 2.1 IE Cr Op Sa Fx
tables
Heading 1 | Heading 2 |
Cell 3 | Cell 4 |
This example creates a solid green line between cells and a black line around the table. All borders within a table have the same thickness. The result of the example is shown in Fig. 2.8.
![](https://i2.wp.com/htmlbook.ru/files/images/layout2/2-08.png)
Rice. 2.8. Border around the table
Alignment of cell contents
By default, text in a table cell is left-aligned. The exception to this rule is the tag , it defines a heading that is center-aligned. To change the alignment method apply style text-align property(example 2.8).
Example 2.8. Align cell contents horizontally
XHTML 1.0 CSS 2.1 IE Cr Op Sa Fx
tables
Heading 1 | Cell 1 | Cell 2 |
Heading 2 | Cell 3 | Cell 4 |
In this example, the content of the tag | is left-aligned, and the contents of the tag | - in the center. The result of the example is shown below (Figure 2.9).
![](https://i0.wp.com/htmlbook.ru/files/images/layout2/2-09.png)
Rice. 2.9. Aligning text in cells
Vertical alignment in a cell is always centered, unless otherwise specified. This is not always convenient, especially for tables whose cell contents vary in height. In this case, the alignment is set to the top edge of the cell using the vertical-align property, as shown in Example 2-9.
Example 2.9. Align cell contents vertically
XHTML 1.0 CSS 2.1 IE Cr Op Sa Fx
tables
Heading 1 | Heading 2 |
Cell 1 | Cell 2 |
This example sets the header height | like 40 pixels and text alignment is bottom. The result of the example is shown in Fig. 2.10.
![](https://i0.wp.com/htmlbook.ru/files/images/layout2/2-10.png)
Rice. 2.10. Aligning text in cells
Empty cells
Browsers differently display a cell that has nothing inside. “Nothing” in this case means that neither a picture nor text was added inside the cell, and the space is not taken into account. Naturally, the appearance of cells differs only if a border is set around them. When using an invisible border, the appearance of the cells, whether they have anything in them or not, is the same.
Older browsers did not display the background color of empty view cells | | , so in the case when it was required to leave the cell without content, but display the background color, an unseparated space () was added inside the cell. A space is not always appropriate, especially when you need to set the cell height to 1-2 pixels, which is why the one-pixel transparent drawing has become widespread. Indeed, such a picture can be scaled at your discretion, but it is not displayed on the web page.
Fortunately, the era of single-pixel drawings and all kinds of spacers based on them has passed. Browsers work quite correctly with tables without the presence of the contents of the cells.
To control the appearance of empty cells, the empty-cells property is used; when set to hide, the border and background in empty cells are not displayed. If all cells in a row are empty, then the entire row is hidden. A cell is considered empty in the following cases:
- there are no symbols at all;
- the cell contains only a newline, a tab character, or a space;
- the visibility value is set to hidden .
Adding a non-breaking space is treated as visible content, i.e. the cell will no longer be empty (example 2.10).
Example 2.10. Empty cells
XHTML 1.0 CSS 2.1 IE Cr Op Sa Fx
Using empty-cells
Leonardo | 5 | 8 |
Raphael | | 11 |
Michelangelo | 24 | |
Donatello | | 13 |
The view of the table in the Safari browser is shown in fig. 2.11a. The same table in IE7 is shown in Fig. 2.11b.
![](https://i0.wp.com/htmlbook.ru/files/images/layout2/2-11a.png)
a. In Safari, Firefox, Opera, IE8, IE9 browser
![](https://i1.wp.com/htmlbook.ru/files/images/layout2/2-11b.png)
b. In IE7 browser
Rice. 2.11. Table view with empty cells
| |