How to set the table cell widths to minimum except last column using CSS ? Last Updated : 30 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Tables are widely used for showing the data and sometimes we get into a scenario where we need some special kind of styling over the rows or the cells. We will explore one case where we must make the last column dynamic using CSS and the rest taking the required width.These are the following approaches:Table of Content Styling using classesStyling using child selectorsStyling using classesIn this approach, A table with five columns named Name, Age, Gender, Contact, and City. Each cell in the first four columns has a class "w-min" to set its width to a minimum value, while the cells in the last column have a class "w-last" to allow them to expand to fit the remaining width of the table. Example: The example below shows the table cell widths to a minimum except last column using CSS classes HTML <!DOCTYPE html> <html> <head> <title> Table Demo </title> <link href="style.css" rel="stylesheet" /> </head> <body> <table border="1"> <thead> <tr> <th> Name </th> <th> Age </th> <th> Gender </th> <th> Contact </th> <th> City </th> </tr> </thead> <tbody> <tr> <td class="w-min"> Pradhyumn </td> <td class="w-min"> 25 </td> <td class="w-min"> M </td> <td class="w-min"> 90576xxxxx </td> <td class="w-last"> Bangalore </td> </tr> <tr> <td class="w-min"> Akash </td> <td class="w-min"> 23 </td> <td class="w-min"> M </td> <td class="w-min"> 87654xxxxx </td> <td class="w-last"> Hyderabad </td> </tr> <tr> <td class="w-min"> Manasvi </td> <td class="w-min"> 22 </td> <td class="w-min"> F </td> <td class="w-min"> 89765xxxxx </td> <td class="w-last"> Delhi </td> </tr> </tbody> </table> </body> </html> CSS table { width: 60%; border-collapse: collapse; } td { text-align: center; } td.w-min { white-space: nowrap; width: 1%; } td.w-last { width: auto; } Output:TableStyling using child selectorsIn this approach, the ":not(:last-child)" selector is used to target all table cells except the last one. It sets their width to 1% and prevents wrapping with "white-space: nowrap". The :last-child selector then applies "width: auto" to the last cell, allowing it to expand to fit the remaining width of the table. Example: The example shows table cell widths to a minimum except the last column using child selectors HTML <!DOCTYPE html> <html> <head> <title>Table</title> <link href="style.css" rel="stylesheet" /> </head> <body> <table border="1"> <thead> <tr> <th> Name </th> <th> Age </th> <th> Gender </th> <th> Contact </th> <th> City </th> </tr> </thead> <tbody> <tr> <td> Pradhyumn </td> <td> 25 </td> <td> M </td> <td> 90576xxxxx </td> <td> Bangalore </td> </tr> <tr> <td> Akash </td> <td> 23 </td> <td> M </td> <td> 87654xxxxx </td> <td> Hyderabad </td> </tr> <tr> <td> Manasvi </td> <td> 22 </td> <td> F </td> <td> 89765xxxxx </td> <td> Delhi </td> </tr> </tbody> </table> </body> </html> CSS table { width: 60%; border-collapse: collapse; } td { text-align: center; } td:not(:last-child) { white-space: nowrap; width: 1%; } td:last-child { width: auto; } Output:Table Comment More infoAdvertise with us I impradhyumn Follow Improve Article Tags : HTML CSS-Questions Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read HTML Tutorial HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript 11 min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac 13 min read CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi 6 min read Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i 6 min read Like