w3resource

CSS Properties: How to set the table layout algorithm

Go to Exercise page

Solution:

HTML Code :

<!DOCTYPE html><!-- Declares the document type and version of HTML -->
<html><!-- Begins the HTML document -->
<head><!-- Contains metadata and links to external resources -->
<title>How to set the table layout algorithm</title><!-- Sets the title of the document -->
<style type="text/css"> /* Starts CSS styling */
table { /* Styles all table elements */
    border-collapse: separate; /* Specifies the border-collapse property to separate */
    width: 100%; /* Sets the width of the table to 100% */
    border: 2px solid #CC63FF; /* Sets the border of the table */
} 

td { /* Styles all table cell elements */
    border: 2px solid #FF36CC; /* Sets the border of the table cells */
}

table.w3r1 { /* Styles tables with the class "w3r1" */
    table-layout: auto; /* Sets the table layout algorithm to auto */
}

table.w3r2 { /* Styles tables with the class "w3r2" */
    table-layout: fixed; /* Sets the table layout algorithm to fixed */
}
</style><!-- Ends CSS styling -->
</head><!-- Ends the head section -->
<body><!-- Begins the body of the document -->
<p><strong>w3resource Tutorial</strong></p><!-- Displays a paragraph with bold text "w3resource Tutorial" -->
<p>table-layout: auto</p><!-- Displays a paragraph with the text "table-layout: auto" -->
<table class="w3r1"><!-- Begins a table with the class "w3r1" -->
<tr><!-- Begins a table row -->
<td width="5%">w3resource web development tutorial</td><!-- Defines a table cell with 5% width and text content -->
<td width="95%">JavaScript Tutorial</td><!-- Defines a table cell with 95% width and text content -->
</tr><!-- Ends the table row -->
</table><!-- Ends the table -->

<p>table-layout: fixed</p><!-- Displays a paragraph with the text "table-layout: fixed" -->
<table class="w3r2"><!-- Begins a table with the class "w3r2" -->
<tr><!-- Begins a table row -->
<td width="5%">w3resource web development tutorial</td><!-- Defines a table cell with 5% width and text content -->
<td width="95%">JavaScript Tutorial</td><!-- Defines a table cell with 95% width and text content -->
</tr><!-- Ends the table row -->
</table><!-- Ends the table -->
</body><!-- Ends the body section -->
</html><!-- Ends the HTML document -->

Explanation:

  • This HTML document demonstrates how to set different table layout algorithms using CSS.
  • CSS comments are added to explain each section of the code.
  • The table selector sets properties for all tables, such as border-collapse and width.
  • The td selector sets properties for all table cells, such as border.
  • Two table classes are defined:
    • .w3r1 sets the table layout algorithm to auto.
    • .w3r2 sets the table layout algorithm to fixed.
  • The width attribute in the td elements specifies the width of each table cell.
  • The first table has its layout algorithm set to auto, and the second table has its layout algorithm set to fixed.

Live Demo :

See the Pen table-layout-answer by w3resource (@w3resource) on CodePen.


See the solution in the browser

Supported browser

Firefox logo Chrome logo Opera logo Safari logo Internet Explorer logo
YesYesYesYesYes

Go to Exercise page

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.