{"id":14765,"date":"2020-04-17T12:36:58","date_gmt":"2020-04-17T19:36:58","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=14765"},"modified":"2023-12-01T02:41:06","modified_gmt":"2023-12-01T10:41:06","slug":"css-tables","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/css-tables\/","title":{"rendered":"How to Style HTML Tables with CSS"},"content":{"rendered":"\n<p>Early on in web development, HTML tables were very basic and lacked extensive styling options. Today, however, most tables are styled to create a more aesthetically pleasing and functional experience for users.<br><\/p>\n\n\n\n<p>CSS provides a number of attributes for styling tables. These attributes allow you to\u2014among other things\u2014separate cells in a table, specify table borders, and specify the width and height of a table.<br><\/p>\n\n\n\n<p>This tutorial will discuss, with examples, how to style a table using CSS. By the end of this tutorial, you\u2019ll be an expert at it.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">HTML Tables<\/h2>\n\n\n\n<p>HTML code defines the structure of tables. You use the &lt;table&gt; tag to define a table. The &lt;tr&gt;, &lt;th&gt;, and &lt;td&gt; tags specify rows, table headers, and content cells, respectively.&nbsp;<br><\/p>\n\n\n\n<p>Let\u2019s look at an example of an HTML table. In the below example, we have a table that lists the top five books on the <em>New York Times<\/em> best sellers list for the week of March 23, 2020:<br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"515\" height=\"150\" src=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-09.50.10-1.jpg\" alt=\"\" class=\"wp-image-14795\" srcset=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-09.50.10-1.jpg 515w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-09.50.10-1-385x112.png 385w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-09.50.10-1-20x6.png 20w\" sizes=\"auto, (max-width: 515px) 100vw, 515px\" \/><\/figure>\n\n\n\n<p>The code for our table is as follows:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>&lt;table&gt;\n  &lt;tr&gt;\n    &lt;th&gt;Book Name&lt;\/th&gt;\n    &lt;th&gt;Author&lt;\/th&gt;\n    &lt;th&gt;Weeks on List&lt;\/th&gt;\n  &lt;\/tr&gt;\n  &lt;tr&gt;\n    &lt;td&gt;The Mirror &amp; the Light&lt;\/td&gt;\n    &lt;td&gt;Hilary Mantel&lt;\/td&gt;\n    &lt;td&gt;New this Week&lt;\/td&gt;\n  &lt;\/tr&gt;\n  &lt;tr&gt;\n    &lt;td&gt;Journey of the Pharaohs&lt;\/td&gt;\n    &lt;td&gt;Clive Cussler and Graham Brown&lt;\/td&gt;\n    &lt;td&gt;New this Week&lt;\/td&gt;\n  &lt;\/tr&gt;\n  &lt;tr&gt;\n    &lt;td&gt;Where the Crawdads Sing&lt;\/td&gt;\n    &lt;td&gt;Delia Owens&lt;\/td&gt;\n    &lt;td&gt;79&lt;\/td&gt;\n  &lt;\/tr&gt;\n  &lt;tr&gt;\n    &lt;td&gt;American Dirt&lt;\/td&gt;\n    &lt;td&gt;Jeanine Cummings&lt;\/td&gt;\n    &lt;td&gt;8&lt;\/td&gt;\n  &lt;\/tr&gt;\n  &lt;tr&gt;\n    &lt;td&gt;Little Fires Everywhere&lt;\/td&gt;\n    &lt;td&gt;Celeste Ng&lt;\/td&gt;\n    &lt;td&gt;57&lt;\/td&gt;\n  &lt;\/tr&gt;\n&lt;\/table&gt;\n<\/pre><\/div>\n\n\n\n<p>This table includes three columns and six rows, including one header row.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">CSS Tables<\/h2>\n\n\n\n<p>CSS is used to style tables. While the table above displays data in an organized way, it is written in plain HTML (there are no styles present). By using CSS, you can make tables more aesthetically pleasing.<br><\/p>\n\n\n\n<p>There are many CSS functions you can use to style a table. Using CSS, you can:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>add borders<\/li>\n\n\n\n<li>collapse borders<\/li>\n\n\n\n<li>adjust border spacing<\/li>\n\n\n\n<li>adjust the width and height of a table<\/li>\n\n\n\n<li>add padding<\/li>\n\n\n\n<li>align text horizontally<\/li>\n\n\n\n<li>align text vertically<\/li>\n\n\n\n<li>add a mouse-over (hover) feature<\/li>\n\n\n\n<li>define cell colors<\/li>\n\n\n\n<li>define how empty cells will display<\/li>\n<\/ul>\n\n\n\n<p>We cover all of these topics in the discussion below.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Borders<\/h3>\n\n\n\n<p>Suppose we want to add borders around the table or around elements within it.&nbsp;<br><\/p>\n\n\n\n<p>To add borders, we can use the border property. Here\u2019s an example that uses the border property to add borders to a table, and its cells, including header cells:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>table, th, td {\n\tborder: 1px black solid;\n}\n<\/pre><\/div>\n\n\n\n<p>In our code, we define a solid, 1-pixel-wide, black border. Here\u2019s the result of our code:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"520\" height=\"168\" src=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-09.53.50.jpg\" alt=\"\" class=\"wp-image-14796\" srcset=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-09.53.50.jpg 520w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-09.53.50-385x124.png 385w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-09.53.50-20x6.png 20w\" sizes=\"auto, (max-width: 520px) 100vw, 520px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Notice that our table contains double borders. This is because we applied a border to the table itself (&lt;table&gt;), its headers (&lt;th&gt;), and its cells (&lt;td&gt;). To merge double borders into single borders, we can use the border-collapse property.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Border Collapse<\/h3>\n\n\n\n<p>The border-collapse property converts double borders in a table to single borders. The default value of the border-collapse property is collapsed. If the border-collapse property is assigned the value <code>collapse<\/code>, the borders around a table will be collapsed.<br><\/p>\n\n\n\n<p>Here\u2019s an example of the border-collapse property in action:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>table {\n\tborder-collapse: collapse;\n}\n<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"503\" height=\"143\" src=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-09.55.49.jpg\" alt=\"\" class=\"wp-image-14797\" srcset=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-09.55.49.jpg 503w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-09.55.49-385x109.png 385w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-09.55.49-20x6.png 20w\" sizes=\"auto, (max-width: 503px) 100vw, 503px\" \/><\/figure>\n\n\n\n<p>Our table and its contents now have a single border.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Border Spacing<\/h3>\n\n\n\n<p>You can use the border-spacing property to set the spacing between cells in a table. The border-spacing property defines the horizontal and vertical spacing between cells\u2014and it does so in that order.<br><\/p>\n\n\n\n<p>Here\u2019s an example that uses the border-spacing property on our initial table (the one without collapsed borders):<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>table {\n\tborder-spacing: 10px 10px;\n}\n<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"555\" height=\"221\" src=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-09.58.18.jpg\" alt=\"\" class=\"wp-image-14798\" srcset=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-09.58.18.jpg 555w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-09.58.18-20x8.png 20w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-09.58.18-385x153.png 385w\" sizes=\"auto, (max-width: 555px) 100vw, 555px\" \/><\/figure>\n\n\n\n<p>Each of our cells has a 10px spacing on both the horizontal and vertical edges.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Width and Height<\/h3>\n\n\n\n<p>You can specify the width and height of a table and its properties using the width and height attributes.<br><\/p>\n\n\n\n<p>Suppose we want the width of our above table\u2014the one with collapsed borders\u2014to be the width of the web page itself. And, let\u2019s suppose we want the height of each table header to be 30 pixels tall. We can do this using the following CSS code:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>table {\n\twidth: 100%;\n}\nth {\n\theight: 30px;\n}\n<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"737\" height=\"155\" src=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.00.26.jpg\" alt=\"\" class=\"wp-image-14799\" srcset=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.00.26.jpg 737w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.00.26-385x81.png 385w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.00.26-20x4.png 20w\" sizes=\"auto, (max-width: 737px) 100vw, 737px\" \/><\/figure>\n\n\n\n<p>As you can see, our table is now the width of the web page. In addition, the column headers in our table are 30 pixels tall.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Padding<\/h3>\n\n\n\n<p>You can use the padding property to add a certain amount of space between the borders of the cells in a table and the contents of those cells. The padding property can be used on the &lt;td&gt; and &lt;th&gt; tags.<br><\/p>\n\n\n\n<p>Suppose we want to add a 10 pixel padding around the contents of our table\u2019s cells\u2014including the header cells. We can do so using this code:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>th, td {\n\tpadding: 10px;\n}\n<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"622\" height=\"249\" src=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.08.03-1.jpg\" alt=\"\" class=\"wp-image-14805\" srcset=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.08.03-1.jpg 622w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.08.03-1-20x8.jpg 20w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.08.03-1-385x154.jpg 385w\" sizes=\"auto, (max-width: 622px) 100vw, 622px\" \/><\/figure>\n\n\n\n<p>The contents of each of our table rows and headers now has a 10px padding around all edges.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Horizontal Text Alignment<\/h3>\n\n\n\n<p>You can use the text-align property to horizontally align text stored in a &lt;th&gt; or &lt;td&gt; tag in a table. By default, &lt;th&gt; elements are center aligned and &lt;td&gt; elements are left aligned.<br><\/p>\n\n\n\n<p>The text-align attribute\u2019s most commonly used values are:<br><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>left, center, right (supported by all browsers except Microsoft Edge and Internet Explorer)<\/li>\n\n\n\n<li>start and end (supported by all browsers except Microsoft Edge and Internet Explorer)<\/li>\n<\/ul>\n\n\n\n<p>Suppose we want to center the &lt;td&gt; elements in our table and align the &lt;th&gt; elements to the left of each cell. We can do so using this code:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>th {\n\ttext-align: left;\n}\ntd {\n\ttext-align: center;\n}\n<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"736\" height=\"145\" src=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.03.04.jpg\" alt=\"\" class=\"wp-image-14801\" srcset=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.03.04.jpg 736w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.03.04-385x76.png 385w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.03.04-20x4.png 20w\" sizes=\"auto, (max-width: 736px) 100vw, 736px\" \/><\/figure>\n\n\n\n<p>In this example, we center-aligned non-header cell text and left-aligned header cell text.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Vertical Text Alignment<\/h3>\n\n\n\n<p>The CSS vertical-align property is used to specify the vertical alignment of content in &lt;th&gt; or &lt;td&gt; tags. By default, the value of the vertical-align property is set to <code>middle<\/code>, which means the content is vertically aligned to the middle of the cell.<br><\/p>\n\n\n\n<p>Let\u2019s suppose we want to align the text in our &lt;td&gt; cells to the bottom of the cells. We can do so using this code:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>td {\nheight: 40px;\nvertical-align: bottom;\n}\n<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"738\" height=\"255\" src=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.05.40.jpg\" alt=\"\" class=\"wp-image-14802\" srcset=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.05.40.jpg 738w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.05.40-385x133.png 385w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.05.40-20x7.png 20w\" sizes=\"auto, (max-width: 738px) 100vw, 738px\" \/><\/figure>\n\n\n\n<p>In this example, we set the height of each &lt;td&gt; cell to 40 pixels. Then we vertically aligned the contents of those cells to the bottom of the cells.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Horizontal Borders<\/h3>\n\n\n\n<p>When you\u2019re creating a table, you may decide that you only want borders to appear at the bottom of each cell. You can apply the border-bottom property to &lt;th&gt; and &lt;td&gt; cells to do so.<br><\/p>\n\n\n\n<p>Here\u2019s the code we can use to create a horizontal lower border for each cell in our table:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>th, td {\n\tborder-bottom: 1px solid black;\n}\n<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"651\" height=\"142\" src=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.13.34.jpg\" alt=\"\" class=\"wp-image-14803\" srcset=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.13.34.jpg 651w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.13.34-385x84.png 385w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.13.34-20x4.png 20w\" sizes=\"auto, (max-width: 651px) 100vw, 651px\" \/><\/figure>\n\n\n\n<p>In this example, we created a solid, black, 1-pixel-wide bottom border for each cell in our table.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mouse Over (:hover)<\/h3>\n\n\n\n<p>If you use the :hover selector in your code, the browser will highlight table rows when a user hovers over them with the mouse.&nbsp;<br><\/p>\n\n\n\n<p>The :hover selector is useful because it allows you to make your tables more interactive. It also helps users better visualize individual rows when they are looking at the table.<br><\/p>\n\n\n\n<p>Suppose we want the background color of the rows (&lt;tr&gt;) in our table to change to light gray when the user hovers over them. We can do so using this code:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>tr:hover {\nbackground-color: lightgray;\n}\n<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"651\" height=\"152\" src=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.17.24.jpg\" alt=\"\" class=\"wp-image-14804\" srcset=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.17.24.jpg 651w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.17.24-385x90.png 385w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.17.24-20x5.png 20w\" sizes=\"auto, (max-width: 651px) 100vw, 651px\" \/><\/figure>\n\n\n\n<p>When we hover over a table row, the color of the row changes to light gray. In this case, we hovered over the fourth row of our table, so the color of that row turned to light gray.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Table Colors<\/h3>\n\n\n\n<p>You can use the color property to specify the background and text colors of elements in a table. Suppose we want our table header cells to have a gray background and white text. We can use the following code to style the table that way:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>th {\n\tbackground-color: gray;\n\tcolor: white;\n}\n<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"650\" height=\"145\" src=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.19.46.jpg\" alt=\"\" class=\"wp-image-14806\" srcset=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.19.46.jpg 650w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.19.46-385x86.png 385w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.19.46-20x4.png 20w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/figure>\n\n\n\n<p>In this example, our table\u2019s header cells (&lt;th&gt;) have gray backgrounds. We also set the text in each header cell to white. We defined both of these characteristics using the CSS color property.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Striped Design<\/h3>\n\n\n\n<p>When you\u2019re designing a table, you may want to use the zebra-styled approach. In this style, row colors alternate between two colors. This creates a striped, zebra-like effect. To accomplish this task, we can use the nth-child() selector.<br><\/p>\n\n\n\n<p>Here\u2019s the code we will use to create a striped design wherein we set the background color of every other row to light gray:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>tr:nth-child(even) {\n\tbackground-color: lightgray;\n}\n<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"652\" height=\"144\" src=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.22.51.jpg\" alt=\"\" class=\"wp-image-14807\" srcset=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.22.51.jpg 652w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.22.51-385x85.png 385w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.22.51-20x4.png 20w\" sizes=\"auto, (max-width: 652px) 100vw, 652px\" \/><\/figure>\n\n\n\n<p>We set the background color of every even-numbered row in our table to light gray.&nbsp;<br><\/p>\n\n\n\n<p>If we want to set the background color of odd-numbered rows in our table to light gray, we can specify \u201codd\u201d instead of \u201ceven\u201d in the code above.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Empty Cells<\/h3>\n\n\n\n<p>We use the empty-cells property to specify whether cells should have any borders or background if they do not contain any text.&nbsp;<br><\/p>\n\n\n\n<p>The empty-cells property takes one of two values: hide or show. CSS does the following to empty cells when a user specifies one of these values:&nbsp;<br><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>hide<\/strong>: hides the background color and borders.<\/li>\n\n\n\n<li><strong>show<\/strong>: shows the background color and borders.<\/li>\n<\/ul>\n\n\n\n<p>The empty-cells CSS property only works if border-collapse is set to separate. This means that the borders are not collapsed.<br><\/p>\n\n\n\n<p>Suppose that we have an empty cell in our table above and want to hide the background and borders of that empty cell. We can use the following code to accomplish this task:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>table {\n\tempty-cells: hide;\n\tborder-collapse: separate;\n}\n<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"651\" height=\"168\" src=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.31.08.jpg\" alt=\"\" class=\"wp-image-14808\" srcset=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.31.08.jpg 651w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.31.08-385x99.png 385w, https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/Screen-Shot-2020-03-24-at-10.31.08-20x5.png 20w\" sizes=\"auto, (max-width: 651px) 100vw, 651px\" \/><\/figure>\n\n\n\n<p>In this example, we removed the <code>Weeks on List<\/code> value for the book <em>Where the Crawdads Sing<\/em>. Because that cell contains no value, when we specify the value hide for the empty-cells attribute, the table hides the border element that otherwise would have been applied to the cell.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>CSS offers a number of properties used to style tables. These include the padding property (adds space between the contents of a cell and its borders) and the text-align property (aligns the text inside a cell).<br><\/p>\n\n\n\n<p>This tutorial discussed, with examples, how to style a table using CSS. Now you\u2019re ready to start styling tables in CSS like a pro!<\/p>\n","protected":false},"excerpt":{"rendered":"Early on in web development, HTML tables were very basic and lacked extensive styling options. Today, however, most tables are styled to create a more aesthetically pleasing and functional experience for users. CSS provides a number of attributes for styling tables. These attributes allow you to\u2014among other things\u2014separate cells in a table, specify table borders,&hellip;","protected":false},"author":240,"featured_media":14766,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[17287],"tags":[],"class_list":{"0":"post-14765","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-css"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"CSS","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":"","is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to Style HTML Tables with CSS: A Step By Step Guide<\/title>\n<meta name=\"description\" content=\"CSS provides a number of attributes for styling tables. These attributes allow you to\u2014among other things.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/css-tables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Style HTML Tables with CSS\" \/>\n<meta property=\"og:description\" content=\"CSS provides a number of attributes for styling tables. These attributes allow you to\u2014among other things.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/css-tables\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-17T19:36:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T10:41:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/macbook-pro-macbook-computer-programming-60626.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"665\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/css-tables\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/css-tables\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"How to Style HTML Tables with CSS\",\"datePublished\":\"2020-04-17T19:36:58+00:00\",\"dateModified\":\"2023-12-01T10:41:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/css-tables\/\"},\"wordCount\":1610,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/css-tables\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/macbook-pro-macbook-computer-programming-60626.jpg\",\"articleSection\":[\"CSS\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/css-tables\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/css-tables\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/css-tables\/\",\"name\":\"How to Style HTML Tables with CSS: A Step By Step Guide\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/css-tables\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/css-tables\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/macbook-pro-macbook-computer-programming-60626.jpg\",\"datePublished\":\"2020-04-17T19:36:58+00:00\",\"dateModified\":\"2023-12-01T10:41:06+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"CSS provides a number of attributes for styling tables. These attributes allow you to\u2014among other things.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/css-tables\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/css-tables\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/css-tables\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/macbook-pro-macbook-computer-programming-60626.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/macbook-pro-macbook-computer-programming-60626.jpg\",\"width\":1000,\"height\":665},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/css-tables\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"CSS\",\"item\":\"https:\/\/careerkarma.com\/blog\/css\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to Style HTML Tables with CSS\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Style HTML Tables with CSS: A Step By Step Guide","description":"CSS provides a number of attributes for styling tables. These attributes allow you to\u2014among other things.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/careerkarma.com\/blog\/css-tables\/","og_locale":"en_US","og_type":"article","og_title":"How to Style HTML Tables with CSS","og_description":"CSS provides a number of attributes for styling tables. These attributes allow you to\u2014among other things.","og_url":"https:\/\/careerkarma.com\/blog\/css-tables\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-04-17T19:36:58+00:00","article_modified_time":"2023-12-01T10:41:06+00:00","og_image":[{"width":1000,"height":665,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/macbook-pro-macbook-computer-programming-60626.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/css-tables\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/css-tables\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"How to Style HTML Tables with CSS","datePublished":"2020-04-17T19:36:58+00:00","dateModified":"2023-12-01T10:41:06+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/css-tables\/"},"wordCount":1610,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/css-tables\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/macbook-pro-macbook-computer-programming-60626.jpg","articleSection":["CSS"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/css-tables\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/css-tables\/","url":"https:\/\/careerkarma.com\/blog\/css-tables\/","name":"How to Style HTML Tables with CSS: A Step By Step Guide","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/css-tables\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/css-tables\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/macbook-pro-macbook-computer-programming-60626.jpg","datePublished":"2020-04-17T19:36:58+00:00","dateModified":"2023-12-01T10:41:06+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"CSS provides a number of attributes for styling tables. These attributes allow you to\u2014among other things.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/css-tables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/css-tables\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/css-tables\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/macbook-pro-macbook-computer-programming-60626.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/macbook-pro-macbook-computer-programming-60626.jpg","width":1000,"height":665},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/css-tables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"CSS","item":"https:\/\/careerkarma.com\/blog\/css\/"},{"@type":"ListItem","position":3,"name":"How to Style HTML Tables with CSS"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/14765","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=14765"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/14765\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/14766"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=14765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=14765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=14765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}