How To Set The Width Of The Table Column - W3docs

w3docs logo Books Learn HTML Learn CSS Learn Git Learn Javascript Learn PHP Learn python Learn Java Exercises HTML JavaScript Git CSS PHP Courses Quizzes Snippets Tools General Tools
  • Password Generator
  • HTML Editor
  • HTML Encoder
  • Base 64
  • Code Diff
  • JSON Beautifier
  • CSS Beautifier
  • Markdown Convertor
  • Find the Closest Tailwind CSS Color
  • Phrase encrypt / decrypt
  • Browser Feature Detection
  • Number convertor
CSS Maker
  • CSS Maker
  • CSS Maker text shadow
  • CSS Maker Text Rotation
  • CSS Maker Out Line
  • CSS Maker RGB Shadow
  • CSS Maker Transform
  • CSS Maker Font Face
Color Tools
  • Color Picker
  • Colors CMYK
  • Colors HWB
  • Colors HSL
  • Color Hex
  • Color mixer
  • Color Converter
  • Colors RGB
  • Color Contrast Analyzer
  • Color Gradient
String Tools
  • String Length Calculator
  • MD5 Hash Generator
  • Sha256 Hash Generator
  • String Reverse
  • URL Encoder
  • URL Decoder
  • Base 64 Encoder
  • Base 64 Decoder
  • Extra Spaces Remover
  • String to Lowercase
  • String to Uppercase
  • Word Count Calculator
  • Empty Lines Remover
  • HTML Tags Remover
  • Binary to Hex
  • Hex to Binary
  • Rot13 Transform on a String
  • String to Binary
  • Duplicate Lines Remover
Change theme
  • Dark
  • Light
  • System
  • Books
    • Learn HTML
    • Learn CSS
    • Learn Git
    • Learn Javascript
    • Learn PHP
    • Learn python
    • Learn Java
  • How To
    • How To NodeJs
    • How To Linux
    • How To AngularJs
    • How To PHP
    • How To HTML
    • How To CSS
    • How To Symfony
    • How To Git
    • How To Apache
    • How To JavaScript
    • How To Java
    • How To Vue.js
    • How To Python
  1. Snippets
  2. CSS
  3. How to Set the Width of the Table Column
How to Set the Width of the Table Column

Solution with the CSS width property

If you want to set the width of the table column, you can use some CSS. You need to use the width property.

In the example below, we set the width of the the <table> element to 100%. Then, in the HTML part, we distribute the table width of 100% among <col> elements having span attributes. But you’re free to decide to specify the properties in HTML, or CSS.

Here, we use the :first-child, :nth-child, and :last-child pseudo-classes on <td> elements and style them with background-color.

Example of the setting the width of the table column:

<!DOCTYPE html> <html> <head> <title>Title of the document</title> <style> table { width: 100%; } td:first-child { background-color: #32a852; } td:nth-child(2) { background-color: #aaaaaa; } td:last-child { background-color: #5696c7; } </style> </head> <body> <table> <colgroup> <col span="1" style="width: 20%;"> <col span="1" style="width: 50%;"> <col span="1" style="width: 30%;"> </colgroup> <tbody> <tr> <td>20%</td> <td>50%</td> <td>30%</td> </tr> </tbody> </table> </body> </html> Try it Yourself »

Result

20% 50% 30%

How to expand the last column in the table without expanding the table?

To expand the width of the last column in the table without changing the width of the entire table, you can set the width property of the last column using a percentage value that is larger than the current width.

<!DOCTYPE html> <html> <head> <title>Title of the document</title> <style> table { width: 100%; } td:first-child { background-color: #32a852; } td:nth-child(2) { background-color: #aaaaaa; } td:last-child { background-color: #5696c7; width: 60%; /* set width of last column to 60% */ } </style> </head> <body> <table> <colgroup> <col span="1" /> <col span="1" /> <col span="1" /> </colgroup> <tbody> <tr> <td>A</td> <td>C</td> <td>C</td> </tr> </tbody> </table> </body> </html> Try it Yourself » Copy Tags column html css table width Sorry about that How can we improve it? Submit Thanks for your feedback! Thanks for your feedback! Do you find this helpful? Yes No Quizzes
  • PHP basics
  • HTML Basics
  • Javascript Basics
  • CSS Basics
  • ES6 Basics
  • TypeScript Basics
  • React Basics
  • Angular Basics
  • Sass Basics
  • Git Basics
  • Vue.js Basics
  • SQL Basics
  • Python Basics
  • Java Basics
  • NodeJS Basics

Từ khóa » Html Change Column Sizes