Beautiful frames for html table. Change line background on hover
- 4.7 out of 5 based on 6 votes
When creating web pages, it is very often necessary to present some page content in the form of tables.
Sometimes tables are used to create page structure. This approach is not entirely correct, because tables were not originally designed to position page elements.
It is best to use CSS for this purpose. But in some cases, tables are irreplaceable and convenient for providing information.
The tag is responsible for creating tables in HTML.
In order to see how everything works in practice, let's create a table consisting of two rows and four cells. Our table code will be as follows:
1 - cell | 2 - cell |
3 - cell | 4 - cell |
In order to see what will come of it, create an html page using the code below. If you don't know how to create an HTML page, see the tutorial.
1 - cell | 2 - cell |
3 - cell | 4 - cell |
You should have something like this:
As you can see, our table doesn't look like a table at all. This is all because our tags
First, we will look at the attributes that are inherent in tags
bordercolor - the color of the table border, this attribute is not supported by all browsers, so you may not see the specified border color:
We set the border width to 2 pixels, blue, the table will look like this:
width - sets the width of the table in pixels or percent:
height - the height of the table in pixels or percent:
The table will be 250 pixels wide and 150 pixels high, the table will look like this:
align - table alignment;
align \u003d left - tables will be aligned to the left;
align \u003d right - the table will be right-aligned:
Our table should be right-aligned.
bgcolor - table background color, bgcolor \u003d # FFC000 - table background color will be yellow:
The table will look like this:
background - using this attribute, you can set an image that will be the background of the table.
As an example, save the small image that you see in brackets () to the folder where you have the page with the table, and to the tag
some of them are the same as tag attributes
Note that it is enough to set one cell height or width and all cells in this row or column will take the same size. Therefore, if you need to set, for example, a certain cell height, then it is enough to specify this parameter for one cell and all other cells in the row will become the same. align - aligns the contents of cells, has the following values: align \u003d "lef" - the contents of the cell will be aligned to the left; align \u003d "right" - content will be right aligned; align \u003d "center" - the content will be aligned to the center of the cell. Let's add these attributes and values \u200b\u200bto our code and align the contents of the 1st cell to the left (the contents are left-aligned by default, but in some cases this attribute is necessary), the contents of the 2nd cell are right-aligned, and the 4th in the center.
bgcolor - using this attribute, you can set the color of the cell. background - sets the image as the background of the cell. We have already met with these attributes, considering the attributes of the tag
This option is simpler than the first, but, unfortunately, it is not interpreted in the same way by Internet Explorer and Netscape. Box 3
If the two previous options display frames in different browsers, albeit minimally, but of different thicknesses, then the third example is interpreted by the common browsers Internet Explorer 4.x / 5.x / 6, Netscape 4.x / 6.x and Opera 6. x is the same in terms of border thickness. I find it appropriate to mention that Netscape 4.x is the most problematic for a web designer.
All cells, except for the 5th one, are painted over in black, the fifth - in white. The background of the table itself is not important in this case. Then the parameter is added to cells 1-3 and 7-9 height \u003d "1". Accordingly, cells 1, 3, 4, 6, 7, 9 are made one pixel wide ( width \u003d "1"). Middle cells 2, 5, 8 are made as wide as possible ( width \u003d "100%"). The trick is that the so-called "modified Malevich square" is added to the cells, which should be narrow or thin, or both, - a transparent gif file of 1x1 size, otherwise the desired effect will not be achieved. This option is undoubtedly too complex and cumbersome to create a thin border around a table with one cell, despite its compatibility with many browsers. It is mainly used for page layout with thin lines. You can also use style sheets to create frames, but more on that in one of the following workshops.
The CSS specification gives unlimited possibilities for table design. By default, the table and table cells do not have visible borders and backgrounds, and cells within the table are not adjacent to each other. The width of table cells is determined by the width of their contents, so the width of the table columns can be different. The height of all cells in a row is the same and is determined by the height of the tallest cell. Formatting tables1. Table borders borderThe table and cells inside it are displayed in the browser without visible borders by default. Table borders set by the border property: Table (border-collapse: collapse; / * remove empty spaces between cells * / border: 1px solid gray; / * set an outer border of 1px gray for the table * /) Header cell borders each column are set for the th element: Th (border: 1px solid gray;) Cell borders table bodies are set for the td element: Td (border: 1px solid gray;) The width of the borders of adjacent cells is not doubled, so you can set borders for the entire table in the following way: Th, td (border: 1px solid gray;) The outer border of the table can be selected by giving it an increased width: Table (border: 3px solid gray;) Borders can be set partially: / * set a 3px gray outer border for the table * / table (border-top: 3px solid gray;) / * set a 1px gray border for the table body cell * / td (border-bottom: 1px solid gray;) You can read more about the border property. 2. How to set the width and height of the tableDefault table width and height is determined by the contents of its cells. If no width is specified, it will be equal to the width of the widest row (row). Table and Column Width set using the width property. If table (width: 100%;) is specified for a table, then the width of the table will be equal to the width of the container block in which it is located. The width of the table and columns is usually specified in px or%, for example: Table (width: 600px;) th (width: 20%;) td: first-child (width: 30%;) Table height not set. The height of the rows tables can be manipulated by adding top and bottom padding to elements and | ... Fixing the height using the height property is not recommended. | Th, td (padding: 10px 15px;) 3. How to set the background of the tableDefault table background and the cells are transparent. If the page or block containing the table has a background, then it will shine through the table. If the background is set for both the table and the cells, then only the background of the cells will be visible in the places where the background of the table and cells overlap. The background for the table as a whole and its cells can be: 4. Table columnsThe CSS table model is focused mainly on strings (rows) formed using a tag using the tag You can set the background for any number of columns; using the table td: first-child, table td: last-child selector, you can style the first or last column of the table (excluding the first cell of the table header); using the table selector td: nth-child (column selection rule), you can style any columns in the table. You can read more about CSS selectors. 5. How to add a table titleYou can add a title to the table using the tag
6. How to remove the gap between cell framesBy default, the borders of table cells are separated by a small space. If we set border-collapse: collapse for the table, then the gap will be removed. The property is inherited. Syntax Table (border-collapse: collapse;) 7. How to increase the spacing between cell bordersUsing the border-spacing property, you can change the distance between cell borders. This property applies to the table as a whole. Inherited. Syntax Table (border-collapse: separate; border-spacing: 10px 20px;) table (border-collapse: separate; border-spacing: 10px;) 8. How to hide empty table cellsThe empty-cells property hides or shows empty cells. Only affects cells that do not contain any content. If a background is specified for a cell, and table (border-collapse: collapse;) is specified for a table, then the cell will not be hidden. Inherited.
9. Layout table layout using the table-layout propertyThe layout of a table layout is determined by one of two approaches: fixed layout or automatic layout. Layout in this case means how the width of the table is distributed between the width of the cells. The property is not inherited. Syntax Table (table-layout: fixed;) 10. Best Table Layouts1. Horizontal minimalismHorizontal tables are tables in which text is read horizontally. Each entity is a separate line. You can style tables like this in a minimalist style by placing a two-pixel border under the th heading.
With a large number of rows, this design of tables makes them difficult to read. To solve this problem, you can add a one pixel border under all td elements. Td (border-bottom: 1px solid #ccc; color: # 669; padding: 9px 8px; transition: .3s linear;) / * rest of the code - as in the example above * / Adding a: hover effect to the tr element makes tables that are minimalist in style easier to read. When you hover the mouse over a cell, the rest of the cells in the same row are selected at the same time, which simplifies the process of keeping track of information if tables have multiple columns. Th (font-weight: normal; color: # 039; padding: 10px 15px;) td (color: # 669; border-top: 1px solid # e8edff; padding: 10px 15px;) tr: hover td (background: # e8edff ;) 2. Vertical minimalismAlthough such tables are rarely used, vertically oriented tables are useful for categorizing or comparing descriptions of objects represented by a column. You can style them in a minimalist style by adding a space separating the columns. Th (font-weight: normal; border-bottom: 2px solid # 6678b1; border-right: 30px solid #fff; border-left: 30px solid #fff; color: # 039; padding: 8px 2px;) td (border- right: 30px solid #fff; border-left: 30px solid #fff; color: # 669; padding: 12px 2px;) 3. "Box" styleThe most reliable style for decorating tables of all types is the so-called "box" style. It is enough to choose a good color scheme and then set the background color for all cells. Remember to emphasize the difference between lines by setting borders as separators. Th (font-size: 13px; font-weight: normal; background: # b9c9fe; border-top: 4px solid #aabcfe; border-bottom: 1px solid #fff; color: # 039; padding: 8px;) td (background : # e8edff; border-bottom: 1px solid #fff; color: # 669; border-top: 1px solid transparent; padding: 8px;) tr: hover td (background: #ccddff;) The most difficult thing is to find the color scheme that will blend harmoniously with your site. If the site is loaded with graphics and design, then it will be quite difficult for you to use this style. Table (font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif; font-size: 14px; max-width: 70%; width: 70%; text-align: center; border-collapse: collapse ; border-top: 7px solid # 9baff1; border-bottom: 7px solid # 9baff1;) th (font-size: 13px; font-weight: normal; background: # e8edff; border-right: 1px solid # 9baff1; border- left: 1px solid # 9baff1; color: # 039; padding: 8px;) td (background: # e8edff; border-right: 1px solid #aabcfe; border-left: 1px solid #aabcfe; color: # 669; padding: 8px ;) 4. Horizontal zebraZebra table looks pretty attractive and handy. The complementary background color can serve as a visual cue for people to read the table. Th (font-weight: normal; color: # 039; padding: 10px 15px;) td (color: # 669; border-top: 1px solid # e8edff; padding: 10px 15px;) tr: nth-child (2n) ( background: # e8edff;) 5. Newspaper styleTo achieve the so-called newspaper effect, you can apply borders to table elements and play with the cells inside. A light, minimal newspaper style might look like this: play with the color scheme, add borders, padding, different backgrounds, and a: hover effect on hovering over a line. Table (border: 1px solid # 69c;) th (font-weight: normal; color: # 039; border-bottom: 1px dashed # 69c; padding: 12px 17px;) td (color: # 669; padding: 7px 17px; ) tr: hover td (background: #ccddff;) Table (border: 1px solid # 69c;) th (font-weight: normal; color: # 039; padding: 10px;) td (color: # 669; border-top: 1px dashed #fff; padding: 10px; background: #ccddff;) tr: hover td (background: # 99bcff;) Table (border: 1px solid # 6cf;) th (font-weight: normal; font-size: 13px; color: # 039; text-transform: uppercase; border-right: 1px solid # 0865c2; border-top: 1px solid # 0865c2; border-left: 1px solid # 0865c2; border-bottom: 1px solid #fff; padding: 20px;) td (color: # 669; border-right: 1px dashed # 6cf; padding: 10px 20px;) 6. Table backgroundIf you're looking for a quick and unique way to style your table, choose an attractive image or photo related to the table's theme and set it as the background of the table. Png ") 98% 86% no-repeat;) th (font-weight: normal; font-size: 14px; color: # 339; padding: 10px 12px; background: white;) td (color: # 669; border- top: 1px solid white; padding: 10px 12px; background: rgba (51, 51, 153, .2); transition: .3s;) tr: hover td (background: rgba (51, 51, 153, .1); ) To clearly separate the content of one cell from another, borders are added to the cells. The border parameter of the tag is responsible for their creation.
Frames created in this way differ slightly in their appearance in different browsers. Figure: 1. The frame obtained using the border parameter To get a border of the same type, it is recommended to apply the border style tag, applying it to the table cells (the or | ). However, here too there are pitfalls. Since a frame is created for each cell, a border of double thickness is obtained at the points of contact of the cells. There are several ways to solve this feature. The easiest is to use the border-collapse property set to collapse. His task is to track the contact of lines and, instead of a double border, depict a single one. We add this attribute of the datatkov to the tag |
The difference between the table borders when adding the border-collapse parameter, as well as without it, is shown in Fig. 2. a b Figure: 2. Table view when using the border-collapase attribute In fig. 2a shows the default table frame. Note that within the table, all lines are double the width. Adding the border-collapse parameter removes this feature, and the thickness of all lines becomes the same (Fig. 2b). To develop lines of the same type within the table, you can go the other way. Should be added for the selector frame, but cancel the right and bottom line by setting the corresponding attributes to none. Then, when the cells are joined, their borders will not overlap with each other, for the reason that there will be only one line. However, with this method of forming borders, there are no lines at the bottom and right of the table itself. Adding border-right and border-bottom parameters to the selector |
This method can have variations, for example, for the selector we add a border only to the right and bottom, and at |
|