Stylish html css table
In the recent past, the popularity of tables has been incredibly high, they were used not only to present tabular data, but also were the main means of forming the structure of a document (despite a number of disadvantages). Currently, the DIV block element is increasingly used for document layout.
The table remains the main element HTML language, which is indispensable for creating complex structures and for presenting tabular data, where block elements are powerless and do not have such variability.
Table base
The table is a complex structure and is formed from several HTML tag ov. At the first stage, it is built from block element
, which represents the foundation, the basis of the table. The next stage in construction is table rows, which are formed using paired tags
:
The final stage of table construction is creating cells. There are two categories of tags for this purpose:
- the paired tag creates a cell that serves as a column header, this tag is optional; - a mandatory paired tag creates cells - the basis of the table;
Heading 1 | Heading 2 | Heading 3 | Heading 4 | Heading 5 |
---|---|---|---|---|
Cell 1,1 | Cell 1,2 | Cell 1.3 | Cell 1.4 | Cell 1.5 |
Cell 2.1 | Cell 2,2 | Cell 2,3 | Cell 2.4 | Cell 2.5 |
Outcome 1 | Outcome 2 | Outcome 3 | Outcome 4 | Outcome 5 |
Fig 1. Table
As you can see from the code above, the table contains four rows (
Pay attention to the structure of the table; it has a strictly nested hierarchical structure; if you break it and rearrange the tags in a different order, the table will either not be displayed on the page, or will be displayed, but not correctly. Basic rules when designing a table:
- Paired tag
the base of the table, all other tags are nested within it;
- Paired tag
the second level of the table or its row must be nested in a tag ;
- Paired tags
And third level of the table or its cells must be nested in a tag and contain tabular data; - If you place any other tag or text outside the matching tag
, then this data will be ignored and will not be displayed on the web page; Table title
The table provides the ability to add a title using a paired tag
. In principle, it can be inserted anywhere in the table; this is not an error. By default, the header text appears above the table.
Table Header Heading 1 Heading 2 Heading 3 Heading 4 Heading 5 Cell 1,1 Cell 1,2 Cell 1.3 Cell 1.4 Cell 1.5 Cell 2.1 Cell 2,2 Cell 2,3 Cell 2.4 Cell 2.5 Outcome 1 Outcome 2 Outcome 3 Outcome 4 Outcome 5 Rice. 2. Table with title.
Table Sections
Using sections, which are paired tags, the table can be divided into logical parts. They are not displayed by the browser on the page, but you can set a different representation for each of them. There are the following three logical parts:
-
- Column headers section;
- - Table base section;
- - Summary section, summarizing the entire table;
,
, , are placed inside the table, and must contain tags
: ), use the ROWSPAN attribute with a value of 2:Table Header Heading 1 Heading 2 Heading 3 Heading 4 Heading 5 Cell 1,1 Cell 1,2 Cell 1.3 Cell 1.4 Cell 1.5 Cell 2.1 Cell 2,2 Cell 2,3 Cell 2.4 Cell 2.5 Outcome 1 Outcome 2 Outcome 3 Outcome 4 Outcome 5 Merging cells
Above we looked at a table with a simple structure, but what if you need to complicate it, for example, combine several cells into one. There are special attributes for this case COLSPAN And ROWSPAN tags
And . The first is used to combine horizontally, the second vertically. The value for the attribute is the number of cells to be merged: 1. To merge horizontally first four cells last line table, required by the COLSPAN attribute of the tag
(which contains the text “Total 1”) assign the value 4: Total (1+2+3+4) 2. To merge cells vertically the main part of the document (part
Cell (1,1+2,1) Cell (1,2+2,2) Cell (1.3+2.3) Cell (1.4+2.4) Please note that if you combine cells with the COLSPAN or ROWSPAN attributes, you must remove the merged cells from the code, except for the one with which the merge begins, their number will depend on the value of the COLSPAN, ROWSPAN attribute.
That is, in example 1, four cells are merged, starting with the first one, which is assigned the attribute COLSPAN=4 (since the merge begins with it), the remaining three must be deleted (since they are considered merged). Only the fifth cell will remain in the code (since it is not included in the union).
The same principle will apply to vertical unification. In the second example, the first four cells of the row are assigned the ROWSPAN=2 attribute, as a result the first four lower cells are removed from the code, because they are considered united. Except for the last one, which is not included in the association.
Table Header Heading 1 Heading 2 Heading 3 Heading 4 Heading 5 Cell (1,1+2,1) Cell (1,2+2,2) Cell (1.3+2.3) Cell (1.4+2.4) Cell 1.5 Cell 2.5 Total 1+ Total 2+ Total 3+ Total 4 Outcome 5 Rice. 4. Merging table cells using the COLSPAN, ROWSPAN attributes.
Decorating Tables with CSS Properties
After creating the table using HTML, move on to the next stage, designing the table using CSS styles that apply to tags
,
, , . Cell frames
You can draw a frame around the cells using using CSS properties border, with the following syntax:
Border: “line thickness” “style type” “color”;
- line thickness- can be specified in pixels (px), as well as using special reserved words: thin (2 px), medium (4 px) and thick (6 px);
- style type- attribute values are special words: dotted (dotted line), dashed (dotted line), solid (line), double (double line), etc.
Fig 5. Applying the border property to table cells to frame each cell with a frame.
If you want the frame to be only between the cells, and not frame each one separately, you need to use the style property border-collapse, taking the value:
- collapse- there is only one frame between the cells;
- separate- each cell is framed by its own frame;
Figure 6. Applying the border-collapse property to a table. Adjacent cells are separated by one frame.
Cell sizes
By default, the browser forms cell sizes depending on their content, if necessary, set static form, use the style property width, height. In this case, if the contents of the cell do not fit on one line, then it is transferred to the next one.
Fig 7. Using the width and height properties to set the width and height of cells.
Cell padding
There are two types of indents, external and internal, the first type of indent is set between the borders of adjacent cells, the second from the borders of cells to their contents. The CSS properties that create indentation are listed below:
- padding- padding, the value is set in pixels (px);
- border-spacing - margin, the value is also set in pixels (px). This parameter should not be used if (border-collapse:collapse), because the indentation is set between the borders of adjacent cells, and if the specified property is present, adjacent cells are separated by only one frame;
Rice. 8. Applying the padding property to the table to set indents.
Header Location
By default, the table header is located above it, but the position can be changed using the style property caption-side, which takes the following values:
- top- the title is located above the table;
- bottom- the title is located below the table;
Cell color
To color a cell a certain color you need to use the property background-color:
Rice. 10. Aligning the contents of cells with the text-align and vertical-align properties.
Styling cell contents
Content formatting refers to the formatting of the text contained in the cells. Basics CSS property, used for this purpose is font:
Rice. 11. Decorating the cell contents with the font property.
All CSS styles applied to the table
Designing tables using CSS is an interesting and responsible activity. You need to approach this matter competently, with knowledge of all the possibilities of styles. In addition, you need to have a sense of beauty so as not to scare off site visitors with your creativity.
Almost anything can be transformed in tables. Beautiful design CSS tables involves the use of border design, table background, cell background, space between them and much more. Let's look at the most basic things.
Table Border
CSS table styling always involves playing with the border (border). All tables are not surrounded by a border by default. That is, it is equal to 0 pixels. But this can be fixed using the border property.
You can specify outer frame for the entire table:
table ( border: 3px solid black; )
Thanks to this line, all tables on the site that use this style will have a black border. Note that the border is only around the edges, not inside the table. For cells and rows, the frame is set differently.
th, td (border: 3px solid black;)
You can specify any thickness and color. Keep in mind that borders are not doubled when cells are joined.
The word solid means continuous design. You can specify other values.
Most often, a solid frame is used because it looks more attractive and does not distract attention from the main content of the site.
The border property can also be specified in directions. The border can only be set for the top, bottom, left or right part. Because in some cases the option with a frame for the entire table at once is not suitable.
table (border-top: 1px solid red; )
This way you can set a border only for the top part of the table. Similarly for any other sides, just instead of top we write: right, left or bottom.
Table title
The table title can be specified using a tag
. This tag can have many properties in CSS for more fine tuning. IN CSS styling The good thing about tables is that you can maneuver the elements the way you want. This heading is displayed in the same way as standard in books (for example, "Table 1").
You can also specify the location of this title using the caption-side property (top or bottom). Left or right alignment is specified by the text-align property.
Table background
The background of the table can be any color or pattern. The color is specified by the background-color property. The names of the properties fully correspond to those used in speech. This makes memorization much easier.
Color can be indicated either by name or different encodings. In addition, you can specify the following:
- Transparent - element transparency.
- Inherit - the color is the same as parent element.
- Initial - default value.
The transparency option can be used in cases where all tables in the text in the CSS file are designed in the same color, but in in this case there is no such need.
In addition, the background can be an image. To do this, the background-image property is specified in the style. The path is indicated like this:
The path to the file can be either relative or absolute.
A more complex fill can be done with a gradient:
- linear-gradient();
- radial-gradient();
- repeating-linear-gradient() and repeating-radial-gradient() - repeating the gradient.
Cell background
In addition to the background as a whole, you can set an alternating background in columns or rows. This property is used very often for design, since the visual separation of lines makes the information easier to read.
In addition to alternation, you can also specify the number of a specific column or row. For example like this:
- tr:nth-child (even) ( ... ) - indication of line alternation;
- tr:nth-child (1) ( ... ) - indicating the property of a specific row;
- td:nth-child (even) ( ... ) - indicating the alternation of columns;
- td:nth-child (1) ( ... ) - indicating the property of a specific column.
In addition to alternation and numbers, you can specify the first (td:first-child) or last (td:last-child).
Space between cells
In CSS, table styling allows you to remove spaces between cells. By default they are. For example, if you set a frame in a table without setting the distance between the borders, you will get the following result.
Agree, it does not look very nice and is not convenient for reading. Users will have a ripple in their eyes because of this. You can remove these gaps by writing the following line in the table style:
border-collapse: collapse
But it also happens that the distance, on the contrary, needs to be increased. Moreover, the size of the gaps can be specified both between columns and between rows. To do this, specify the following value (instead of collapse):
border-collapse:separate
But this action will indicate that the cells need to be divided. How exactly to separate them is indicated by an additional property:
border-spacing: 20px.
If you need to specify different distance between rows and columns, then two values are indicated:
border-spacing:10px20px.
Browser difference
Keep in mind that table styling in CSS may look different depending on the browser. The situation is especially bad with older versions, which do not support innovations in CSS.
Above is an example of border thickness for digital values.
Frame styles also vary greatly.
Therefore, when developing, always look at the result in different browsers.
Very advanced developers can connect completely different CSS files. And someone does checks in each or any specific style (class).
The most problems arise with shadows.
CSS: table design, examples
The design can be very diverse. It all depends on the site as a whole and its design. Everything should be combined and not full of colors. The taste of the developer also plays a big role. Everyone's sense of beauty is different.
Here are examples of various tables. The picture above shows the use of tilt and playing with and boundaries.
Many will be interested in an example of a beautiful, neat design that will not hurt the eyes of users. This option is appropriate in almost any situation.
The edges can be made rounded. It looks quite nice.
Conclusion
As you can see, for registration appearance tables in CSS exist great amount tools. Each parameter also has a huge number of possible values. If you use all this at once, you can create masterpieces. Especially if you do it for all browsers.
The main thing when designing is not to overdo it with effects. Everything needs to be done in moderation. At first, layout designers love to experiment and use all their knowledge at once. As a result, the tables end up being oversaturated with properties. Try to avoid these mistakes.
Moreover, some parameters may interfere with each other. For example, there is no need to specify the table background color if the background picture, which will overlap the specified color.
- - Table base section;
- Paired tag