How to set up smartphones and PCs. Informational portal

Html table column size.

Vlad Merzhevich

When layout using tables, you need to know some of the features that are inherent in tables. This is necessary in order to understand how to build a layout in a particular case, create an effective code and prevent outright blunders in work. It is believed that tabular layout is easier than other methods, such as using layers. However, tables are also fraught with many tricks that, one way or another, affect the display of the page.

Table width

The width of the table, if it is not explicitly specified, is automatically set by the browser based on the contents of the cells. When a table is used to create a reference grid on a page, this approach is not desirable because it depends on variable data. Therefore, the width of the table is always specified - as a percentage if a "fluid" layout is used, or in pixels for a fixed-width layout.

Cell width

The width of the cells is determined by the width attribute of the tag , and setting this value is required if the cell width must be specified.

Consider, for example, the case when the cell size needs to be specified. Suppose we want to create two columns on a web page, one of them should have a size of 200 pixels, and the second should take up the remaining space. We create a table with two cells and for the left cell we set the parameter width="200" (example 1). You do not need to set units of measurement, the browser itself understands that if a number is given, it means that pixels are used.

Example 1: Cell Width

table

Left columnRight column

Since the table width is set to 100% in this example, the table will take up the full width of the browser window. In this case, the left column takes 200 pixels for itself, and the right column takes the remaining free space.

The width of the cell can sometimes change, despite the fact that it is specified rigidly. This happens, for example, if a picture is added to the cell, the size of which exceeds the width of the cell. To fit the image, the cell will be forced to expand. The size of a cell can also be affected by its text content, which contains a very long word.

To avoid this situation, several means are used.

  • Do not add to a fixed-width cell those images whose size exceeds the width of the cell. The method, of course, sounds trite, however, knowing the features of the cells, you can avoid trouble with their display.
  • For Tag use the table-layout style property with the value fixed . Using this property allows you to crop the picture if it does not fit entirely in the cell (example 2).

    Example 2: The table-layout property

    table

    ...

    The result of this example is shown in Fig. one.

    Modern browsers (Firefox 3+, Internet Explorer 8+, Chrome and Safari) show the table a little differently (Fig. 2).

    Rice. 2. Display a picture in the Safari browser

    • Use the style property overflow with the value scroll . This property adds scrollbars to the content, but because it applies to block elements, it cannot be used on the tag . Therefore, you have to put a tag inside the cell
      and set a style property for it (example 3). This is certainly not entirely correct and convenient, but it can give the desired result.

    Example 3: Scroll bars in cells

    table

    ...

    The result of this example is shown in Fig. 3.

    Cell content

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

    Merging cells

    Let's assume that we need to use a union of some cells in a table, as shown below. Moreover, the height of the orange and gray cells is hard-coded and equal to 30 pixels.

    Cell 1 Cell 2
    Cell 3
    Cell 4

    For clarity, the code of this table is shown in example 4.

    Example 4: Table with merged cells

    table

    Cell 1 Cell 2
    Cell 3

    Cell 4

    Although the height of the orange cell seems to be fixed, it can change arbitrarily depending on the amount of information in other cells. A similar nuisance is seen in some browsers.

    As the complexity of the table increases by increasing the number of cells and their associations, the likelihood and increase in errors when displaying the document increases. Due to this, vertical merging of cells is used quite rarely and in the case when the height of the cells does not significantly affect the page layout.

    To still get the desired result, one table is divided into several small tables or the table is nested inside the cell of another table. Nested tables have become widespread precisely for the reason that they give a stable and uniform result.

    Table loading speed

    Until the table is fully loaded, its contents will not be displayed. The fact is that the browser, before showing the contents of the table, must calculate the required cell sizes, their width and height. And for this you need to know what is in these cells. Therefore, the browser waits until everything that is in the cells is loaded, and only then displays the table.

    Based on this fact, tables are not used to store large information (from 100 kB). And in order to speed up the loading of a table layout, it is divided into separate tables or the table-layout property is used, the use of which allows you to slightly increase the speed of displaying the contents of the table.

    1. What are HTML tables and why are they needed?

    html tables- this is one of the most basic components of each html page and, accordingly, each site. For example, a site site is all made up of tables: pictures, texts, links, registration forms - everything is placed relative to each other using html tables (in fact, the entire site is also located in one large table).

    2. What tags do html tables have?

    tags, defining the html table are written like this:

    But since we know that all tables consist of rows and columns (cells), so you need to set tags for them:

    - line html table
    - column (cell) html table.

    And now let's try to combine all the tags into one table. To do this, we will create an html table that consists of two rows and three columns:

    This html table is very easy to make. First, let's put the tags of the table itself, inside which we put the required number of lines (in our example, 2 lines):




    Then we break each row into cells using column tags:



    - cell 1.1
    - cell 1.2
    - cell 1.3


    - cell 2.1
    - cell 2.2
    - cell 2.3

    In fact, the first digit in the inscriptions is the row number, and the second is the cell number (1x2 - the first row, the second cell, etc.).

    3. How to set the background color of html tables?

    And now let's see how the background of the table or each cell is set separately.

    So, to set the background, the attribute is used bgcolor="background_color".

    For example, if we need to set one color for the entire table, then this is done like this:

    bgcolor="background_color">

    - cell 1.1
    - cell 1.2
    - cell 1.3


    - cell 2.1
    - cell 2.2
    - cell 2.3

    For example, if it is necessary to make a yellow plate, then we write:

    If needed set a background color for a row html table, then the attribute bgcolor="background_color" apply to tag :

    bgcolor="#FFFF00">
    bgcolor="#0000FF">









    1.1 1.2 1.3
    2.1 2.2 2.3
    1.1 1.2 1.3
    2.1 2.2 2.3

    In this case, the attribute cell number sets the white color to the text inside the tag.

    In the same way, it is also color of each cell separately. For example, if you want to change the color of cell 1.2 to blue, then inside the opening tag attribute bgcolor="background_color":

    4. How to set width and height size of html table cells?

    So, in order to manually set the width and height of cells, use the height and width attributes. They can be set for the entire table, for one row, for a cell (column). Height and width can be set in pixels or percentages. In our case, we will set the width and height in pixels for the columns (cells).

    height- attribute that sets the height of the cell

    width- an attribute that sets the width of the cell

    And now for an example:

    Now I will explain why we set the height of the cells only once in each row.

    If in a row you set a height for any cell greater than for others, then, despite this, all cells (columns) of your row will become equal in height to the largest. And the width of each row is best set separately!

    You can set the height and width for the entire table. In this case, all cells (columns) and rows will share the space given to them equally, if you do not set these parameters personally for them (as a percentage of the total width (height) of the table or pixels).

    Also, very often you have to set the height height and width width in percents:

    cell content

    If we set a width of 40 percent for a cell, as in our example, then this means that we still have 60 percent left for the rest of the cells, i.e. the sum of their widths must not exceed 60 percent. Everything is very simple: in total we have only 100%. We consider: 100 - 40 = 60.

    5. How to align text inside html table?

    And now let's figure out how to align the text in our html table to the left, right and center.

    This is done using the attribute:

    align="left"- aligns the text inside the html table to the left

    align="center"- aligns the text of the html table to the center

    align="right"- aligns the text inside the html table to the right

    Accordingly, the align attribute can also take on the values ​​right and left. Meaning of justify when used with a tag the align attribute cannot accept.

    For example, the html code for a table with text aligned to the center looks like this:

    align="center">










    align="left"> 1x1 align="center">1.2 align="right"> 1x3
    align="left">2.1 align="center"> 2x2 align="right">2.3

    By writing an attribute align="center" in opening

    , we have aligned the html table to the center of the browser page.

    This is the result you get in the browser:

    1x1 1.2 1x3
    2.1 2x2 2.3

    And now let's look at an example of how to align the text inside the html table to the top or bottom, that is, how to make the contents of the cell not only be located exactly in the middle of it (as by default), but also at the top or bottom.

    Vertical alignment is set by the attribute:

    align="top"- the content will be placed at the top of the cell

    align="middle"- the content will be centered in the cell

    align="bottom"- the content will be placed at the bottom of the cell

    Let's look at these attributes in our example:












    align="top"> 1x1 1.2 align="top"> 1x3
    align="bottom">2.1 2x2 align="bottom">2.3

    Here is what we get if we look at the result of the attributes in the browser align And valign:

    1x1 1.2 1x3
    2.1 2x2 2.3

    6. How to merge cells and columns of html table?

    In this part of our article, we will talk about attributes. colspan And row span.

    colspan- determines the number of columns that the given cell spans

    row span- determines the number of rows that this cell covers

    Values colspan And row span can take a value of 2 or more, i.e. a cell can span two or more columns (rows).

    So, now in the example we will stretch a 1x1 cell into two columns (cells). To do this, we will use the colspan="2" attribute, setting it for a 1x1 cell. The code will look like this:

    As you can see, a 1x1 cell spans the length of two cells. Accordingly, its length is equal to the summed length of these two cells (160 pixels). We didn't set the width attribute for the 1x1 cell, but if we did, we would set the width to 160 pixels. And yet, note that in our example there is no 1x3 cell, i.e. there are only two cells in the first row, why - we have already discussed this - a 1x1 cell is equal to two cells, thanks to the colspan attribute.

    The colspan and rowspan attributes must be handled very carefully. An error can cause your site to go down.

    And now that we have dealt with the colspan parameter, let's understand the rowspan parameter. The principle of operation is the same here:










    colspan="2"> 1.1 1.2
    2.1 2.2
    1.1 1.2
    2.1 2.2

    Thus, we have learned to merge cells in rows and columns of html tables.

    7. How to remove, add or change indents and how to set an html frame for a table?

    So, earlier we created an html table, between the cells of which the indents are clearly visible. To make them even more visible, let's add a border to our table. This is done using attributes:

    border="width of border in pixels"- sets the border width

    bordercolor="border color"- sets the border color

    Let's, for example, make our html table a black frame with a width of 1 pixel.

    For this tag

    add attributes:

    Now we can clearly see the padding between the cells of the html table and the padding from the inner edges of the cells to the text. Therefore, we can manage these indents without any problems. This is what the attributes are for:

    cellspacing="padding width in pixels"- spacing between cells

    cellpadding="padding width in pixels"- indent inside the cell (from cell edge to text, image, link...)

    For example, let's make an indent between cells of 10 pixels, and increase the indent from the edge of the cell to the text to 20 pixels. It is done like this:

    cellpadding="20"cellspacing="10">








    1.1 1.2
    2.1 2.2

    If we look at the result in the browser, we can clearly see that the distance between the cells ( cellspacing) of our html table has increased to 10 pixels, and the distance between the text and the inner edge of the cell has increased by 20 pixels.

    1.1 1.2
    2.1 2.2

    If you need to remove the indents altogether, then simply put cellpadding="0" And cellspacing="0".

    8. How to insert one html table inside another?

    We are approaching the final stage of studying html tables. Now we will learn how to insert one table inside another.

    In the example, inside cell 1.2 we will insert a new table consisting of one row and two columns. In the inner html table, we will set a red border with a width of 2 pixels and a cell spacing of 0 pixels. Also, we will place the internal table at the top of the cell.

    This is done very simply:










    1.1






    3.13.2

    2.1 2.2

    This is what happened as a result of the new transformations of our table (red cells are an internal table located inside a large table):

    1.1
    3.1 3.2
    2.1 2.2

    At this stage, we will finish this article. I think it turned out to be successful and not difficult to understand, and I hope you will have no more problems with the tables.

    Became popular, web designers mainly used the tabular layout method and got a very good result.


    Tags used to build a table in html

    table- obligatory tag that opens and closes the table
    caption- optional tag denoting the title of the table
    th- an optional tag, in the opening and closing tags of which the name of the column fits. Usually in bold
    tr- obligatory tag from which the line is opened and closed
    td- a required tag denoting the opening and closing of a cell in a row

    Simple table code example



    html table





    Table name

    Column 1

    Stobets 2

    Text in first cell

    Text in second cell



    The browser will display

    Assigning tables in html

    The lesson on tables is very important! Thanks to tables, you can arrange not only text, but also images, links, and much more. The table can be set background(or its color), indent, width, border And other options which will give it a nice look. Table - Your first step to understand web design! Previously, many sites were completely laid out as tables, that is, everything that the user saw (side menu, top menu, content) was the content of the cells of a large table.
    On that note, let's move on to the attributes that make a table beautiful...

    Properties and parameters of html tables: padding, width, background color, border (frame)

    At table tag has the following attributes:

    width- table width. can be in pixels or % of screen width.
    bgcolor- background color of table cells
    background- fills the background of the table with a pattern
    border- frame and borders in the table. Thickness is specified in pixels
    cell padding- padding in pixels between the contents of the cell and its inner border
    As before, the attribute value is written in quotation marks.

    Consider the use of these attributes on an example. To do this, create a table (but already without the rarely used caption and th tags) and set the attribute parameter border (border), width (width of table, row or cell) And bgcolor (cell color)



    html table







    As a result, the following table will be displayed in the browser

    frame- an attribute denoting a frame around the table. There are the following values:

    Void - no frame,
    above - only the top frame,
    below - only the bottom frame,
    hsides - top and bottom frames only,
    vsides - left and right borders only,
    lhs - left frame only,
    rhs - right frame only,
    box - all four parts of the frame.

    rules- an attribute denoting the borders within the table, between cells. There are the following values:

    None - there are no borders between cells,
    groups - borders only between row groups and column groups (will be discussed a little later),
    rows - borders between rows only,
    cols - borders between columns only,
    all - show all borders.

    Consider code example



    html table


    Column 1

    Stobets 2









    Result

    Now let's try to create a beautiful table. To do this, let's start using table alignment. To do this, there are the following already familiar parameters:

    align- table alignment. It can be placed on the left (left), right (right), center (center)
    cellspacing- distance between table cells. Specified in pixels (default 0 pixels)
    cell padding- padding in pixels between the contents of the cell and its inner border (default 0 pixels)
    Consider an example



    html table


    Column 1

    Stobets 2

    Text in the first cell of the first column

    Text in the second cell of the second column







    The browser will display a center-aligned table that looks like this:

    tr rows and td cells in HTML tables

    Let me remind you again that tables are formed line by line (tr). The rows (tr) already contain cells (td). If you set a parameter for a string (tr), then it will be valid for all cells(td) in this line, if for a specific cell, then only for it. In the examples above, I specified the color for the row, this parameter was distributed accordingly for all cells.





    For tr and td tags there are the following

    align- text alignment inside the cell. Left (left), right (right), center (center)
    valign- alignment of the text inside the cell vertically. Up (top), down (bottom), center (middle)
    bgcolor- sets the line color
    width- column width (all cells below will accept this parameter) is specified in pixels or as a percentage, where 100% is the width of the entire table
    height- cell height (all cells in the row will accept this parameter)

    Consider the code where the contents of the cells (td) are aligned on different edges: in the first one on the left, in the second one on the right:



    html table


    Column 1

    Stobets 2

    Text in the first cell of the first column

    Text in the second cell of the second column

    Column 1

    Stobets 2







    Result

    With the help of tables, you can make up a very good page. Do not forget that you can insert not only text into cells, but also images, links, and more!)

    Thanks for attention! I hope the material was helpful!

    The CSS specification gives unlimited possibilities for the design of tables. By default, a table and table cells do not have visible borders or backgrounds, and cells within a table do not sit close to each other.

    The width of the table cells is determined by the width of their content, 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.

    Table Formatting

    1. Table borders

    The table and cells inside it are displayed by default in the browser without visible borders. Table borders are set by the border property:

    Table ( border-collapse: collapse; /*remove empty spaces between cells*/ border: 1px solid gray; /*set the table to a 1px gray outer border*/ )

    Header cell borders each column are set for the th element:

    Th (border: 1px solid grey;)

    Cell borders table bodies are specified for the td element:

    Td (border: 1px solid grey;)

    The thickness of the borders of adjacent cells is not doubled, so you can set the borders for the entire table in the following way:

    Th, td (border: 1px solid grey;)

    The outer border of a table can be highlighted by giving it an increased width:

    Table (border: 3px solid grey;)

    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 determined by the contents of its cells. If the width is not set, then it will be equal to the width of the widest row (line).

    Table and column widths set using the width property. If the table is set to table (width: 100%;) , 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 set in px or % , for example:

    Table (width: 600px;) th (width: 20%;) td:first-child (width: 30%;)

    Table height not set. row height tables can be manipulated by adding top and bottom padding to elements

    . In practice, there are times when special column formatting is needed, 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 set styles for 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 set styles for any columns of the table.

    You can read more about CSS selectors.

    5. How to add a title to a table

    You can add a header to a table using the tag

    However, we want to note that it is not so often necessary to change the sizes of cells individually. As a rule, this is done in situations where it is necessary to set a specific width for the columns of a table. In this case, the task is easier to solve. You just need to specify the width of the cells of the first row.


    Column 1

    Stobets 2

    Text in the first cell of the first column

    Text in the second cell of the second column
    And . Fixing the height with the height property is not recommended.

    Th, td (padding: 10px 15px;)

    3. How to set the table background

    Default table background and cells transparent. If the page or block containing the table has a background, it will show through the table. If the background is set both for the table and for the cells, then in the places where the background of the table and cells overlap, only the background of the cells will be visible. The background for the table as a whole and its cells can be:
    filling ,
    ,
    .

    4. Table columns

    The table CSS model is focused mainly on rows (rows) formed using the tag

    , and using the caption-side property, it can be placed before or below the table. The text-align property is used to align the header text horizontally. Inherited.

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

    6. How to remove the gap between cell frames

    Table cell borders are separated by a small gap by default. If you set border-collapse: collapse for the table, then the gap will be removed. The property is inherited.

    Syntax

    Table (border-collapse: collapse;)
    Rice. 3. An example of tables with merging and split cell borders

    7. How to increase the spacing between cell borders

    Using the border-spacing property, you can change the spacing 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;) Rice. 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. Affects only cells that do not contain any content. If the cell is set to a background and the table is set to table (border-collapse: collapse;) , 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;) Rice. 5. An example of hiding an empty table cell

    9. Table layout layout with 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 refers to 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 whose text reads horizontally. Each entity is a separate line. You can style these tables in a minimalist style by placing a two-pixel border under the th heading.

    employeeSalaryBonusSupervisor
    Stephen C. Cox$300$50Bob
    Josephine 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 table design makes them difficult to read. To solve this problem, you can add a one-pixel border below all td elements.

    Td ( border-bottom: 1px solid #ccc; color: #669; padding: 9px 8px; transition: .3s linear; )/*other code as above*/

    Adding a :hover effect to the tr element will make it easier to read minimalist tables. When you hover over a cell with the mouse, the rest of the cells in the same row are selected at the same time, making it easier to keep 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 nevertheless useful for categorizing or comparing descriptions of the objects represented by a column. You can style them in a minimalist style by adding a space to separate 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. "Boxed" style

    The most reliable style for designing tables of all types is the so-called "boxed" style. It is enough to choose a good color scheme, and then set the background color for all cells. Don't forget to emphasize the difference between the lines by setting the boundaries as the delimiter.

    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 right 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 quite attractive and comfortable. The optional 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 the table elements and play with the cells inside. A light, minimalistic newspaper style might look like this: play with the color scheme, add borders, padding, different backgrounds, and a hover effect on the 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're looking for a quick and unique way to design a table, choose an eye-catching image or photo related to the table's theme and set it as the table's background.

    Jpg") no-repeat; background-position: 100% 55px; ) th ( font-weight: normal; color: #339; padding: 10px 12px; ) td ( background: url("https://website/images/ back..png"); background: transparent; )

    The dimensions of the table, its height and width, are automatically determined depending on what the cells contain inside. The more filling in the cells, the larger the size of the table and vice versa.

    However, the power of CSS allows you to resize the table in HTML, specifying exactly the width and height that are needed. In fairness, it should be noted that the table attributes also allow you to set the desired dimensions. However, for now, we'll only talk about CSS.

    It must be emphasized that each table has its own minimum width and minimum height, which are affected by its content. And even if you set the width and height values ​​even smaller, nothing will happen. The sides of the table will not cross their minimum.

    To specify the width of the table, you must use the CSS property width, and to set the height, you need to use the property height.

    In an example, it looks like this.

    Table ( width: 500px; height: 100px; )

    Result in browser:

    Full code:

    Dimensional table

    Dimensional table
    1 2 3 4 5
    1 2 3 4 5
    1 2 3 4 5

    Table width and height values ​​can be specified both as an absolute value (in pixels) and relative - as a percentage. For example, 20px and 20% respectively.

    If you use percentages to set the dimensions of the table, then they will be calculated based on the dimensions of the parent element. If we talk about our example, then we are talking about the browser window.

    There is one more value - auto. And it is special, because with its help, the calculation of the width and height of the table occurs automatically, by default. In practice, specifying this value looks like this.

    Width: auto;

    height: auto;

    We would like to draw your attention to another important point. It is better not to indicate the height as a percentage, because, as a rule, in this case they will not work.

    Specifying individual cell and column sizes

    If you think that the browser did not correctly set the width of the columns based on the contents of the cells, then you have the right to specify the sizes of the cells and columns yourself. And now we will talk about how to resize a cell in an HTML table.

    Cell resizing is done using the same CSS properties as table sizes, namely: width And height.

    To add styles to cells, you can use one of two options:

    1. Give cells individual class names. It will look like this: class="cell-50px"

      And after that, you should apply the styles for these classes.

    2. Enable Attribute style, inside which to register the necessary CSS code.

    In practice, the second option would look like this:

    ...
    Dimensional table
    1 2 3 4 5
    1 2 3 4 5
    1 2 3 4 5

    Result in browser:

    How to change the font size in a table

    Often there is a need to change the size of the text in the table. For example, in header cells. This can be done with the CSS property font-size. Let's set the font size for the elements :

    Th ( font-size: 30px; )

    This is where this lesson ends. I suggest you think carefully about your homework. Just study it carefully and discuss it. All the best!

Top Related Articles