Html table cell background. Attributes and properties and


This chapter goes into detail about the principles of using tables in HTML markup. This is a tabular organization of text, and a tabular coordinate grid, and a graph organized in tables.

HTML table description facilities

With the development of the WWW, it became clear that the means that are embedded in the NTML are not enough for high-quality display of various types of documents. The disadvantage of НТМL was the lack of table display facilities in its composition. For this purpose, preformatted text (tag

), in which the table was outlined with ASCII symbols. But this form of presentation of tables was not of high quality and knocked out of the general style of the document. With the introduction of tables into HTML, Webmasters have not just a tool for placing text and numbers, but a powerful design tool for placing graphics and text in the right place on the screen.

Creating tables in HTML

The tag is used to describe tables<ТАВLЕ>... Tag<ТАВLЕ>like many others, it automatically translates the line before and after the table.

Create table row - tag<ТR>

Tag<ТR> (Table Row, table row) creates a table row. All text, other tags and attributes that need to be placed on one line must be placed between the lt; TR\u003e tags.

Defining Table Cells - Tag<ТD>

Cells with data are usually placed inside a table row. Each cell containing text or image must be surrounded by tags<ТD>... Number of tags<ТD> per line defines the number of cells

Table

If a table has two TR tags, then it has two rows.
If there are three TD tags in a line, then in it three columns.

Table Column Headers - Tag<ТН>

Headers for table columns and rows are set using the header tag<ТН> (Table Neader, table heading). These tags are like<ТD>... The difference is that the text between the tags<ТН>, is automatically written in bold and is located in the middle of the cell by default. You can undo centering and align text to the left or right. If you use<ТD> with tag<В> and attribute<АLIGN=center>, the text will also look like a title. However, keep in mind that not all browsers support bold text in tables, so it is best to set table titles using<ТН>.

The title is centered by default Header can combine columns
Header can be placed before columns Text or data Text or data
Header can concatenate lines Text or data Text or data
Text or data Text or data
Text or data Text or data

Using table headers - tag<САРТIОN>

Tag allows you to create table headers. By default, titles are centered and positioned either above (<САРТION АLIGN=top>), or under the table (<САРТION ALIGN=bottom>). The title can be any text and images. The text will be split into lines that match the width of the table. Sometimes the tag<САРТION> used for caption under the picture. To do this, it is enough to describe the table without borders.

Heading above the table
Text or data Text or data Text or data Text or data
Title below the table
Text or data Text or data Text or data

NOWRAP Attribute

Typically, any text that does not fit in one line of a table cell is moved to the next line. However, when using the NOWRAP attribute with tags<ТН> or<ТD> the cell length expands so that the enclosed text fits on one line.

COLSPAN Attribute

Tags<ТD> and<ТН> modified using the COLSPAN (Column Span) attribute. If you want to make a cell wider than the top or bottom, you can use the COLSPAN attribute to stretch it over any number of regular cells.

If you want to make a cell wider than the top or bottom, you can use the attribute COLSPAN \u003d 2,
to stretch it over any number of regular cells.

ROWSPAN attribute

ROWSPAN attribute used in tags<ТD> and<ТН>, is similar to the COLSPAN \u003d attribute, except that it specifies the number of rows to span the cell. If you specified a number greater than one in the ROWSPAN \u003d s attribute, then the corresponding number of rows must be below the stretched cell. It cannot be placed at the bottom of the table.

WIDTH Attribute

The WIDTH attribute is used in two ways. You can put it in a tag<ТАВLЕ>to give the width of the entire table, or can be used in tags<ТD> or<ТН>to set the width of a cell or group of cells. The width can be specified in pixels or as a percentage. For example, if you set in the tag<ТАВLЕ> WIDTH \u003d 250, you get a table that's 250 pixels wide regardless of the page size on your monitor. When setting WIDTH \u003d 50% in the tag<ТАВLЕ> the table will take up half the page width for any image size on the screen. So when specifying a table width as a percentage, keep in mind that if the user has a narrow viewport, your page might look a little strange. If you are using pixels and the table is wider than the viewport, a scroll bar appears at the bottom to move right and left on the page. Depending on the task at hand, both methods of setting the table width can be useful.

Text or data - 100% width
or
Text or data - 50% width
or
Text or data - 200 pixels wide
or
Text or data - 100 pixels wide

Applying blank cells

If a cell contains no data, it will have no borders. If you want a cell to have borders but no content, you need to put something in it that won't be visible when viewed. You can use an empty line<ВR>... You can even define empty columns by specifying their width in pixels or relative units and not entering any data into the resulting cells. This tool can be useful when placing text and graphics on a page.

CELLPADDING Attribute

This attribute defines the width of the white space between the contents of the cell and its borders, that is, it sets the margins within the cell.

Text or data Text or data Text or data
Text or data Text or data Text or data

Text or data Text or data Text or data
Text or data Text or data Text or data

The ALIGN and VALIGN Attributes

Tags<ТR>, <ТD> and<ТН> can be modified using the ALIGN and VALIGN attributes. The ALIGN attribute determines the horizontal alignment of text and graphics, that is, to the left or right, or to the center. Horizontal alignment can be specified in several ways:

ALIGN \u003d bleedleft presses the contents of the cell flush against the left edge.

ALIGN \u003d left aligns the contents of the cell to the left with the padding specified by the CELLPADDING attribute

ALIGN \u003d center centers the contents of the cell.

ALIGN \u003d right right-aligns the contents of the cell with the padding specified by the CELLPADDING attribute.

Text or data Text or data Text or data
Text or data Text or data Text or data
Text or data Text or data Text or data
Text or data Text or data Text or data
Text or data Text or data Text or data

The VALIGN attribute vertically aligns text and graphics within a cell. Vertical alignment can be specified in several ways:

VALIGN \u003d top aligns the contents of a cell to its top border.

VALIGN \u003d middle centers the cell contents vertically.

VALIGN \u003d bottom aligns the contents of a cell to its bottom border.

The VALIGN attribute vertically aligns text and graphics within a cell. top, middle, bottom.
VALIGN \u003d top Aligns the contents of the cell to its top border. top, top, top.
VALIGN \u003d middle Centers the content of the cell vertically. middle, middle, middle.
VALIGN \u003d bottom Aligns the contents of the cell to its bottom border. bottom, bottom, bottom.

BORDER attribute

In the tag<ТАВLЕ> often determine how the frames will look, that is, the lines that surround the table cells and the table itself. If you do not specify a border, you will get a table without lines, but there will be space for them. The same result can be achieved by setting<ТАВLЕ ВОRDER=0>... Sometimes you want to make the border thicker to make it stand out better. You can set exceptionally bold borders to draw attention to a picture or text. When creating nested tables, you have to make borders of different thickness for different tables to make it easier to distinguish them.

CELLSPACING Attribute

The CELLSPACING attribute specifies the width of the spaces between cells in pixels. If this attribute is not specified, the default is two pixels. The CELLSPACING \u003d attribute allows you to place text and graphics wherever you want. If you want to leave a blank space, you can enter a space in the cell.

Text or data Text or data Text or data
Text or data Text or data Text or data
Text or data Text or data Text or data
Text or data Text or data Text or data
Text or data Text or data Text or data
Text or data Text or data

BGCOLOR Attribute

This attribute allows you to set the background color. Depending on which tag (TABLE, TR, TD) it is applied with, the background color can be set for the entire table, for a row, or for a single cell. The value of this attribute is an RGB code or a standard color name.

Text or data Text or data Text or data
Text or data Text or data Text or data

BACKGROUND Attribute

This attribute sets the background image for tables. Applicable to TABLE and TD tags. Its value is the URL of the background image file. The use of this attribute is discussed below.

Using tables in page design

The good thing about tables is that you can make their borders invisible if you want. This allows using the tag<ТАВLЕ> beautifully place text and graphics on the page. While the tag<ТАВLЕ> remains the only powerful formatting tool in HTML. Web page designers now have almost the same freedom to use "white space" as the creators of printed pages. Tables are the best way to move away from the hierarchical placement of text on Web pages.

If the browser supports tables, it usually displays the most interesting effects obtained with them correctly.

Ural State Pedagogical University

Welcome!

Training course "Basics of WEB-mastering"

Creating colorful tables

Some browsers allow displaying colors. There are several ways to color the table, they mostly depend on the browser used.

Colored borders in Netscape Navigator. You can not only surround the table with a nice frame, but you can also set a color for it that is different from the colors of the text and background. Create a simple gray GIF (or whatever GIF you would like to have as a background) and define it in the tag<ВODY> as the background of the page. Then set the background color of the page. As a result, your tag<ВОDY> will look something like this:

You have created a double background - GIF and a given color. As a result, the background color will be visible on all table borders and horizontal lines (<НR>). Regardless of whether your background GIF is gray or not, the colored lines and borders of the tables will stand out noticeably. If the background GIF is not too complex, the page load time will only increase slightly.

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 merge 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 the 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:

... In practice, there are times when you need special column formatting, which is possible in the following ways:

using the tag

You can set the background for any number of columns;

using the selector table td: first-child, table td: last-child, you can style the first or last column of the table (except for 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 title

You can add a title to the table using the tag

1
2
3
4
5
6

Page
Top left cell Top right cell
Bottom left cell Bottom right cell


Result in the browser:

Now let's add borders for each cell. To do this, just add the styles:

Result in the browser:

How to remove padding between cells in HTML table

Agree that a border defined using CSS doesn't look the way you would like. Undoubtedly, from the point of view of aesthetics, there is work to do. On the browser page, you can see that by default it displays the borders of tables and cells separately. An example clearly demonstrates this.

However, it is quite possible to get rid of such borders, which are called double, if we use the border-collapse CSS property. In practice, it looks like this:

Table (border: solid 1px blue; border-collapse: collapse;) ...

As a result, the distance between the cells is removed:

The collapse value assigned to the border attribute removes double borders. As you can see, the result is a collapse of adjacent cell borders, as well as the cell borders and the outer border of the table. As for the latter, it can be removed altogether. And if there is a need to display it, you need to increase its width. Thus, we got rid of the separators in the table. And in the next lesson we'll talk about how you can set vertical and horizontal borders. Good luck to all!

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 tables

1. Table borders border

The 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 table

Default table width and height is determined by the contents of its cells. If no width is specified, then 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 table

Default 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:
fill,
,
.

4. Table columns

The CSS table model focuses mainly on strings (rows) formed using the tag

, and with the caption-side property it can be placed in front of or below the table. The text-align property is used to horizontally align the title text. Inherited.

...
Table No. 1
Company Q1 Q2 Q3 Q4
caption (caption-side: bottom; text-align: right; padding: 10px 0; font-size: 14px;) Figure: 2. An example of displaying the header under the table

6. How to remove the gap between cell frames

By 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;)
Figure: 3. An example of tables with merging and split cell borders

7. How to increase the spacing between cell frames

Using 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;)
Figure: 4. An example of tables with increased gaps between cell frames

8. How to hide empty table cells

The 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.

Company Q1 Q2 Q3
Microsoft 20.3 30.5
Google 50.2 40.63 45.23
table (border: 1px solid # 69c; border-collapse: separate; empty-cells: hide;) th, td (border: 2px solid # 69c;) Figure: 5. An example of hiding an empty table cell

9. Layout table layout using the table-layout property

The 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 Layouts

1. Horizontal minimalism

Horizontal 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.

EmployeeSalaryBonusSupervisor
Stephen C. Cox$300$50Bob
Josephin tan$150-Annie
Joyce ming$200$35Andy
James A. Pentel$175$25Annie
table (font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif; font-size: 14px; background: white; max-width: 70%; width: 70%; border-collapse: collapse; text -align: left;) th (font-weight: normal; color: # 039; border-bottom: 2px solid # 6678b1; padding: 10px 8px;) td (color: # 669; padding: 9px 8px; transition: .3s linear;) tr: hover td (color: # 6699ff;)

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 of 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 minimalism

Although 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" style

The 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 zebra

Zebra 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 style

To 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 background

If you are 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); )