I have an HTML page in which I want my content to be centered, but in a specific table on this page, I need many of the cells to be left-aligned and many to be right-aligned and one cell to be aligned to the center. Here is a snippet of HTML and CSS that should give you an idea of ​​what I'm trying to do:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <style> .contentWrapper { width: 1000px; border: 1px solid black; margin: auto; } .centerAligned { text-align: center; } .myTable td { width: 200px; text-align: left; } .myTable td.label { text-align: right; } </style> </head> <body> <div class="contentWrapper centerAligned"> <p>A label for this table...</p> <table class="myTable" border="1" align="center"> <tr> <td class="label">Label 1 (Right Aligned)</td> <td>Value 1 (Left Aligned)</td> <td class="label">Label 2 (Right Aligned)</td> <td>Value 2 (Left Aligned)</td> </tr> <tr> <td class="label">Label 3 (Right Aligned)</td> <td>Value 3 (Left Aligned)</td> <td class="label">Label 4 (Right Aligned)</td> <td>Value 4 (Left Aligned)</td> </tr> <tr> <td colspan="4" class="centerAligned"> <input type="button" value="Push Me!"> </td> </tr> </table> <p>Some more content...</p> </div> </body> </html>
I really didn’t want to put the class in each separate td just to handle how they should be aligned, so I decided to set the default alignment for “.myTable td” on the left. This allowed me to leave all the cells “value” without a class, but I still need to define one for my “labels” in order to get the correct alignment for them.
When it comes to the button below, which I would like to center, I want to be able to use the "centerAligned" class. Unfortunately, using it here does nothing, because the ".myTable td" class is considered "more accurate" and that the cell is assigned left alignment instead of centered.
I use "centerAligned" in other places, so I don’t want to just delete this class and don’t want to change the name to something else. I can do it:
.centerAligned, .myTable td.centerAligned { text-align: center; }
It sounds like work, but it all smelly to me. Is there a better way to handle styling these table cells to get the effect I want without having to define a specific class for each individual td?
Thanks!
source share