The purpose of the lesson: Familiarity with table properties and CSS table layout principles
Let's take a look at the basic CSS table properties
border
A property is considered in one and includes several properties at the same time:
- BORDER-STYLE
- BORDER-WIDTH
- BORDER-COLOR
There is also a collective rule:
table.collapse (border-collapse: collapse;) table.separate (border-collapse: separate;)
Result:
width and height
(table height and width)
Values:
Example:
text-align
(horizontal alignment)
Values:
- center (centered)
- left (left-aligned)
- right (right-aligned)
- justify (width)
vertical-align
(vertical alignment)
Values:
- baseline (baseline)
- sub (as subindex)
- super (as superscript)
- top (top)
- middle (middle)
- bottom (bottom edge)
- % (of the height of the line spacing)
Example:
padding
(table padding)
background-color (background)
color (text color)
CSS table layout
Due to the large number of table properties and variations in their design, tables long time were some of the standard for layout of web pages. If you make the borders of the table invisible, then you can use its individual cells as separate blocks of the page: header, menu, footer, etc.
But this is not entirely correct, because each tag has its own purpose, and tables were not supposed to serve for page layout. However, the lack of an alternative prompted the designers to use this type of layout method.
Now there is another way - using layers, which gradually replaced tables in this kind of working with a web page. However, even nowadays, some designers successfully use table layout.
Two-column spreadsheet layout
One of the most common layout methods is two columns, i.e. the page is divided into two parts.
Example: set the main frame of the page in two columns: the first - with a fixed size, the second - for the rest of the browser area. Execute the task using CSS styles ()
Performance:
</ head> <body> <table id = "maket" cellspacing = "0"> <tr> <td id = "left"> 1</ td> <td id = "right"> 2</ td> </ tr> </ table> ... |
1 | 2 |
Result:
Example: set a different background of cells (to separate two columns from each other) and set the distance between columns (separator)
Performance:
Let's add new style properties:
/ * for the left cell * / td # left (width: 200px; background: #ccc; border: 1px solid black; / * temporarily denote the borders * /) / * for the right cell * / td # right (background: # fc3; border: 1px solid black; / * temporarily denote borders * /) / * for separator * / #razdel (width: 10px; / * Spacing between columns * /)
Together:
</ head> <body> <table id = "maket" cellspacing = "0"> <tr> <td id = "left"> 1</ td> <td id = "razdel"> td > <td id = "right"> 2</ td> </ tr> </ table> |
1 | 2 |
A new cell has been added for the separator.
Result:
Example: make separator between table columns using dotted line borders of adjacent cells
Performance:
Let's add new border properties for cells:
/ * for left cell * / td # left (width: 200px; background: #ccc; / * Left column background color * / / * new * / border-right: 1px dashed # 000; / * Right dashed border options * / )
Together:
</ head> <body> <table id = "maket" cellspacing = "0"> <tr> <td id = "left"> 1</ td> <td id = "right"> 2</ td> </ tr> </ table> |
1 | 2 |
Result:
Three-column spreadsheet layout
There is a concept of a fixed or "fluid" layout.
Fixed layout CSS
- Using fixed layout the width of the entire table is set in pixels, and then, regardless of the resolution of the monitor and browser window, the table will always have the same width.
- In this case the width of the remaining columns should also be fixed.
- It is possible not to specify the width of one cell, then it will be calculated automatically based on the dimensions of the remaining cells and the entire table.
Example: create a page template with three columns. Use fixed table layout:
- left column - 150 pixels;
- middle column - 400 pixels;
Performance:
</ head> <body> <table id = "maket" cellspacing = "0"> <tr> <td id = "left"> 1</ td> <td id = "central"> 2</ td> <td id = "right"> 3</ td> </ tr> </ table> |
1 | 2 | 3 |
Result:
Rubber mockup
- Table Width when using the "rubber" design set in% of the width of the browser window... That. when the browser window changes, the size of the table also changes.
- Width of all cells can be installed in percents.
- The second option is when width of some cells is established in percents, a some in pixels.
Important: The sum of the widths of all columns should be 100%, regardless of the width of the table.
Example:
- left column - 20%;
- middle column - 40%;
- right column - 40%;
Set the background for the columns and visually separate the columns with a border.
Performance:
</ head> <body> <table id = "maket" cellspacing = "0"> <tr> <td id = "left"> 1</ td> <td id = "central"> 2</ td> <td id = "right"> 3</ td> </ tr> </ table> |
1 | 2 | 3 |
Result:
Consider the second option, when the width of the center column is automatically selected by the browser; an example is the picture:
Example: create a page template with three columns. Use a fluid table layout:
- left column - 150 pixels;
- middle column - 40%;
- right column - 200 pixels;
Set the background for the columns and visually separate the columns with a border.
Performance:
</ head> <body> <table id = "maket" cellspacing = "0"> <tr> <td id = "left"> 1</ td> <td id = "central"> 2</ td> <td id = "right"> 3</ td> </ tr> </ table> |
1 | 2 | 3 |
Result:
The result will be approximately the same, only the "stretching" will occur due to the central column.
Using a nested table in a fluid layout
If the width of two columns is set in percentages and the third in pixels, you won't be able to get by with one table. So, if the width of the entire table is 100 percent, the first column is 200 pixels, and the remaining columns are 20 percent, then a simple calculation shows that the size of the first column is 60 percent. In this case, the browser will not accept the specified value in pixels, and the size will be set in percent.
- The original table is created with two cells. Table width is set as a percentage.
- For the left cell(first column) width is set in pixels.
- Right cell width(base for other speakers) not specified... A second table is inserted inside this cell, also consisting of two cells.
- The width of the cells of the nested table is set as a percentage..
- Inner table width must be set to 100 percent so that this table takes up all the free space in the outer table.
- Center and right column widths are calculated relative to the width of the cell, not the outer table as a whole.
Example: create a page template with three columns. Use fluid layout with nested table:
- left column - 150 pixels;
- middle column - 60%;
- right column - 40%;
Set the background for the columns.
Performance:
The tag attributes cellpadding and cellspacing are required here, so that there is no "gap" between the tables. So, we have studied with you the most simple actions that can be implemented with table borders. And now the table looks much more aesthetically pleasing. However, the filling of the cells directly rests on the boundaries. You can easily fix this by just indenting the cells in the HTML table. And then the text inside the frame, in the cell, will be more readable. In order to make indents in a cell, use the attribute cellpadding for tag In this case, the indents are set using the property padding... With its help, it will not be difficult to indent where necessary, that is, top, right, bottom or left, using, respectively, one of these properties: padding-top, padding-right, padding-bottom and padding-left. You can set how many pixels should be indented. Here's an example where the bottom margin will be 20
pixels, and all the rest will be 10
... Such CSS-the code will look like this: Td (padding: 10px; padding-bottom: 20px;) And the complete code of styles at this stage: Table (border: solid 1px blue; border-collapse: collapse;) td (border: solid 1px blue; padding: 10px; padding-bottom: 20px;) Result in the browser: Typically, tasks associated with creating tables can be solved using tags, attributes and properties that allow you to create borders, indents in cells, and also set the color background of the cells themselves. Indents in tables are not only inside cells. They can also be present between the cells themselves. There are two ways to indent between cells: It must be emphasized that border-spacing is written for the table as a whole, while the property padding prescribes directly for the cells. Let's take a look at an example: Table (border: solid 1px blue; border-collapse: separate; border-spacing: 5px;) td (border: solid 1px blue; padding: 10px; padding-bottom: 20px;) And the resulting result: Let's stipulate right away that you shouldn't try to make such indents using border-collapse: collapse... Indeed, when using this option, the cells "stick" to each other. And to keep them separate from each other, you should use border-collapse: separate... It is important to understand that this value is the default. And it is used only in order to clearly show how this problem is being solved. If we delete this line, then the result in the form of cells located separately from each other will be saved. CSS allows you to set not only the border style of the table, but also the style of the borders of individual cells. Since each cell has its own borders, the border between adjacent cells is doubled. But it is possible to combine the borders of neighboring cells into one. There is a border-collapse property for this. It takes on the values: border-collapse: separate - each cell has its own border (default) border-collapse: collapse - the general border border-collapse: inherit - the value is taken from parent element For example, let's create a table and set a frame for the cells of all tables that will be on the page. First, let's not change anything to see how the table will look: Style: 1 After adding frames to table cells, it became noticeable that the contents of the cells are too close to the edges. You can use the padding property to add white space between the edges of the cells and their contents: Th, td (padding: 7px;) Try it " The size of the table depends on its contents, but situations often arise when the table is too narrow and it becomes necessary to stretch it. The width and height of the table can be changed using the width and height properties, setting the desired dimensions either to the table itself or to the cells: Table (width: 70%;) th (height: 50px;) Try it " By default, text in table header cells is center-aligned, and text in normal cells is left-aligned, using the text-align property you can control the horizontal alignment of text. The vertical-align CSS property allows you to control the vertical alignment of text content. By default, text is vertically aligned to the center of the cells. Vertical alignment can be overridden with one of the values for the vertical-align property: When scanning large tables containing many rows with big amount information, it can be difficult to keep track of which data belongs to a particular row. To help users navigate, you can use two different background colors alternately. To create the described effect, you can use the class selector, adding it to every second row of the table:
Adding a class attribute to every second line is a tedious task. The: nth-child pseudo-class has been added in CSS3 to solve this problem alternative way... The alternation effect can now be achieved exclusively by means of CSS without having to change the HTML markup of the document. With the pseudo-class: nth-child it is possible to select all odd or even rows of a table using one of keywords: even (even) or odd (odd): Tr: nth-child (odd) (background-color: # EAF2D3;) Try it " Another way to improve the readability of tabular data is to change background color lines when you hover over it with the mouse cursor. This will help highlight the desired table content and enhance the visual perception of the data. Implementing this effect is very simple, for this you need to add the: hover pseudo-class to the table row selector and set the desired background color: Tr: hover (background-color: # E0E0FF;) Try it " Aligning an HTML table to the center is possible only if the width of the table is less than the width of its parent element. To align the table in the center, you need to use the margin property, setting it at least two values: the first value will be responsible for the outer margin of the table at the top and bottom, and the second for automatic center alignment: Table (margin: 10px auto;) Try it " If you need different margins at the top and bottom of the table, then you can set the margin property to three values: the first will be responsible for the top margin, the second for the horizontal alignment, and the third for the bottom margin: Table (margin: 10px auto 30px;) We have considered many methods of styling elements on the page such as text information, links, images, headings, but all this is not enough yet. In my articles, I often use HTML elements such as tables, because in most cases they are help systematize important information and make it easier to serve. In this article, I will introduce you to the intricacies of styling HTML tables, and you will learn new CSS properties designed to achieve these goals. Hypertext Markup Language HTML provided us with a large number of opportunities to bind CSS styles to ten unique tags designed for working with tables, I suggest repeating them before further study: In this example, we: The result of our example: After the above example, you may have noticed that we have a gap between all the cells in the table. Let's look at how to remove the gap between table cells, or increase it. To set the distance between the borders of adjacent cells, you must use the CSS property - border-spacing. In this example, we: I draw your attention to the fact that if only one length value is specified in the border-spacing property, then it indicates the spacing, both horizontally and vertically, and if two length values are specified, then the first determines the horizontal distance, and the second vertical. The distance between the borders of adjacent cells can be specified in CSS units (px, cm, em, etc.). Negative values are not allowed. The result of our example: You can say: - so, we removed the spacing between the cells using the border-spacing property with a value of 0, but why do we now have the borders of the cells intersecting? Double borders are formed due to the fact that the lower border of one cell is added to the upper border of the cell that is below it, a similar situation occurs on the sides of the cells and this is logical from the point of view of displaying the table, but fortunately there is a way to tell the browser that we are we don't want to see such cheeky behavior of cell borders. To do this, you need to use the CSS border-collapse property. Oddly enough, using the border-collapse property with the collapse value is the best way how you can remove the gap between the cells and not get double borders between them. Consider a comparison of border behavior when using the border-spacing property with a value of 0 and the border-collapse property with a value of collapse: In this example, we: The result of our example: Using CSS you can set whether to display borders and backgrounds for empty cells in a table or not. The empty-cells property is responsible for this behavior, which by default, as you may have noticed from the previous examples, displays empty cells. Let's move on to an example: To understand how the empty-cells property works from this example is very simple, if it is set to hide, then empty cells and the background in them will be hidden, if set to show (the default), then they will be displayed. Let's look at another simple property for styling tables - caption-side, which sets the position of the table caption (above or below the table). By default, the caption-side property is set to top, which places the caption above the table. In order to place the title under the table, you need to use the property with the bottom value. Let's take a look at an example of using this property: In this example, we have created two classes that control the position of the table header. First grade ( .topCaption) puts the table title above it, we applied it to the first table, and the second class ( .bottomCaption) places the table title below it, we applied it to the second table. Class .topCaption has a default caption-side property and was created for demonstration purposes. The result of our example: In most cases, when working with tables, you will need to adjust the alignment of content within header and data cells. The text-align property is used for horizontal alignment similar to any text information. We discussed the use of this property for text earlier in the article "". To set alignment for content in cells, you need to use special keywords with the text-align property. Let's consider the application of the keywords of this property in the following example. In this example, we have created four classes, which set different horizontal alignment in cells and applied them in order to the rows of the table. The value in the cell matches the value of the text-align property The result of our example: In addition to horizontal alignment, you can also define vertical alignment in table cells using the vertical-align property. Please note that when working with table cells, only the following values * of this property are used: *
- The values sub, super, text-top, text-bottom, length and% applied to the table cell will behave as if using the baseline value. Let's look at an example of use: In this example, we have created four classes, which set different vertical alignment in cells and applied them in order to the table rows. The value in the cell matches the value of the vertical-align property that was applied to that row. CSS uses the browser's automatic table layout algorithm by default. In this case the column width is set by the widest non-breaking content of the cell. This algorithm can be slow in some cases, as the browser must read all of the content in the table before determining its final layout. To change the layout type of the table by the browser with automatic on fixed, you must use the table-layout CSS property with the fixed value. In this case, the horizontal placement depends only on from the width of the table and the width of the columns, and not from the contents of the cells. The browser starts displaying the table immediately after the first row is received. As a consequence, the fixed algorithm allows you to create the layout of such a table faster than using the automatic version. When working with large tables, you can use a fixed algorithm to improve performance. Let's look at the application of this property with the following example: In this example, we: We have already partially touched upon the methods of styling the rows and columns of the table in the article "". In this article, we looked at using a group pseudo-class that allows you to alternate row styles in tables using values even (honest) and odd (odd), or by elementary mathematical formula. Let's revisit the previous techniques and explore new ways to style rows and columns in tables. Let's move on to examples. In this example, we: The result of our example: Let's move on to the next example, in which we'll look at the options for styling table rows. In this example, we: The result of our example: Rice. 153 Example of Styling Rows in Tables The following example shows how to apply corner rounding to table cells (property). In this example, we: The result of our example: The following example touches on the use of HTML elements In this example, we: The result of our example: Well, and the final example, which is rather difficult to understand and requires advanced knowledge in CSS, as it touches on future topics planned for detailed study within the framework of this course. In the previous example, we realized that HTML element In this example, we: 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". Complete the Practice Activity before moving on to the next topic: If you are having difficulty completing the practice, 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 your comments and suggestions on the work of the site to the [email protected] |