CSS styling for all cells in a table - html

I want to apply a style to all cells (td elements) inside a given table, and not any other tables. Is there a way to do this in CSS without having to specify a class for each td in the table I want to apply the style to?
For example, let's say I want to make all cells in a particular table have a padding of 3px on all four edges. To do this for every td I could declare:
td { padding: 3px 3px 3px 3px; }
Problem: this applies to every single td on the page. I just want to apply to this a single, named table, or alternatively, to all tables of a given class.
Any ideas?

#someTable td { padding: 3px 3px 3px 3px; }
This will apply your style to only a specific table
.someTable td { padding: 3px 3px 3px 3px; }
This will apply the change to all tables with the given class

As others have answered, having the td element's styles declared as a descendant selector (learn more here) or a child selector (learn more here) belonging to a parent selector, instead of declaring them without the hierarchy would solve your problem.
The only thing I'd like to add is that the descendant and child selectors operate with a slight difference as mentioned below in the comments below:
.my_table_class td { padding: 3px; } // will apply to all td elements underneath
// .my-table-class (also to tables nested
// below)
.my_table_class > td { padding: 3px; } // will apply to td elements immediately
// under .my-table-class

add class to or id to table that you want to target for example
.yourTableClass td{Your Style here}

Use This.
td.tbl_class{your style;}

Related

HTML Removing specific borders

I want to remove the borders of the last row so it just seems like a gap. Any suggestions? Thank you!
Remove the border of the last row's cells
tr:last-of-type td {
border: none;
}
If you want it to look like a gap, use pseudo-selectors
table tr:last-child td{
border: none;
}
You can use the nth element of a particular type using the nth-of-type pseudo-selector.
<style>
tr:nth-of-type(n){
border: 0px;
}
</style>
Although it is not advisable to use a style tag in your HTML. You can always use a separate CSS file.
Alternatively, for the first and the last element, first-of-type and last-of-type can also be used.

Why doesn't the nth-child selector get overwritten by another?

I have set up the following example: http://jsfiddle.net/SXEty/
<style>
table td, th { padding: 8px; text-align: left; }
table td:nth-child(1) { color: red; }
table td { color: blue }
</style>
...
<table>
<tr><th>Name</th><th>Age</th><th>City</th></tr>
<tr><td>Bob</td><td>27</td><td>Los Angeles</td></tr>
<tr><td>Charlie</td><td>34</td><td>San Diego</td></tr>
<tr><td>Daniel</td><td>41</td><td>San Francisco</td></tr>
</table>
I'm curious why the first column is colored as red instead of blue.
In my CSS, I set every first child to color "red". But then my next line of CSS sets every element to color "blue". Wouldn't the second line of CSS (color: blue) override the previous line (color: red)? Or is it that the nth-child property has precedence? If it does have precedence, is this true for all browsers?
Because td:nth-child(1) is more specific than just td.
There's a great overview of CSS specificity with a Star Wars theme that I suggest
http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html
Because table td:nth-child(1) is a more specific selector, it will have precedence over table td even if the latter is declared later.
What's interesting is if you target a parent ID with table td, everything will turn blue - even if table td:nth-child(1) is declared later in the CSS sheet.
http://jsfiddle.net/mLrAf/2/

Override CSS table formatting

I use the following CSS to apply a top border to the last-child row of various tables:
.mytabs tr:last-child {
border-top:1px solid #000;
font-weight:bold;}
Now what I need to do is to override this style for one particular table. How can I override this style in one specific instance? Thanks.
You can wrap it a custom div id name example <div id="customFormat"></div> or give the table an id <table id="customFormat"></table>
In CSS, you can specify the custom styling just for that 1 table.
#customFormat {
border-bottom: 1px solid rgba(69,54,37,0.2);
}
Give a class specific table or to the last row of specific table and apply style
table.<specific_table> tr:last-child {
...your style
}
or
.mytabs tr.<specific_row> {
..your style}

In CSS, is "table" in "table td { ... }" really needed? Can "td" occur not within a table?

I think a td not within a table probably won't validate as HTML or XHTML and therefore the behavior is not well defined, so there probably is no practical use of td without table.
So in CSS, the table in
table td { padding: 0 2em }
is really not need, as td must be inside a table, isn't it true?
(Update: table td occurs such as in Sass, where programmers probably write style such as
table:
background: #fff
td:
border: 1px solid #000
and it will get compiled to table td for the td part)
The only difference is that table gives the selector higher specificity => rules in table td selector will always override rules in td selector.
No, it is not needed.
This, however, is why people do it:
table#tps_report td{ padding: 0 2em }
And then table is left as an orphan when it has no id purely out of habit. I am sure it also makes the task easier for the CSS+DOM parser.
It should not be necessary.
td { padding: 0 2em }
Just using TD will be more efficient as well not only in bytes but also in the application of the CSS rules to the markup.
Yeah td can only occur within a table element so styling via:
td { padding: 0em 2em; }
Is fine :)
This also applies to form and list tags (as well as others).
Typing:
form input { width: 50px; color:red; }
ul li { padding:5px 0; }
is the same as:
input { width: 50px; color:red; }
li { padding:5px 0; }

How to avoid CSS selector applying to more than one level of descendants

How do I prevent CSS from applying my styles do descendants of descendants without giving them classes or ids? e.g.
#mycontent table {border: 1px solid red;}
Will style the correct table, and then ALL tables inside that one too. All I want is the first table to be selected, not all the subtables.
What you describe is not the cascade, it is simple selector matching.
There are a few approaches that you could take.
The first is to use a selector that is more specific. If the table you want to style is a child (rather than a grandchilder or other deeper descendent) then you can use a child selector instead of a descendent selector. This is not supported in MSIE 6.
#mycontent > table { border: 1px solid red; }
The second is to add more information to the tables you actually want to style (with an id or class).
#mycontent table.tableThatIWantToStyle {}
The third is to make use of the cascade and style the deeper descendants differently.
#mycontent table { border: 1px solid red; }
#mycontent table table { border: none; }
That said, if you have tables inside tables then the odds are that you are abusing them for layout - which is wrong - and you should fix your markup instead.
You can use the child selector:
#mycontent > table {border: 1px solid red;}
The problem is the child selector isn't supported in IE6. If you need to support that browser you'll have to do something a lot more clunky:
#mycontent table {border: 1px solid red;}
#mycontent table table {border: none;}
Alternatively you can discriminate the tables using classes so you don't get this cascade effect.
Give your table a type (class):
table.mycontent {border: 1px solid red;}
All child tables should not be marked with .mycontent. Then the style will only be applied to the parent table.
Why don't you just give that one HTML table tag an id, 'mycontent', and write the CSS as follows:
table#mycontent {
border: 1px solid red;
}
I had a similar dilemma and here is how I worked around it. My solution is similar to what others have posted with regard to using more specific selectors to match appropriately.
Suppose I have a nested span as follows and I don't want styles to cascade to it.
<span class="boldText">
some bolded text
<span class="italicText">some italic text</span>
</span>
In other words, I want it to appear as "some bolded text some italic text" rather than "some bolded text some italic text" (and vice versa). By setting a specific selector to clear these styles on nested children this can be achieved as follows.
span span {
font-weight:normal; /*clear bold style from 'cascading' by specifying it*/
font-style:normal; /*clear italic style from 'cascading' by specifying it*/
}
.boldText {
font-weight:bold;
}
.italicText {
font-style:italic;
}
The "span span" selector can ensure that the nested span will have its styles set to anything necessary to override normally cascaded style from the parent span, setting them back to a "normal" un-styled state. Then the specific boldText or italicText classes on the spans themselves will apply next giving the content of that span either bold or italics (or both) since they are more specific selectors.
The convenience here is that I no longer have to worry about the font formatting style on a parent cascading to a child. I can simply add the appropriate formatting class to any span and have only that class apply, without that of the parent.
I could just as easily have added a ".clearBold" and ".clearItalic" class to all nested spans, but this is cleaner and resulted in less code in my application.