CSS selector, multiple class's to style diferente elements [duplicate] - css

This question already has answers here:
What do commas and spaces in multiple classes mean in CSS?
(9 answers)
Closed 1 year ago.
im trying to reduce lines of css and i cant figure out on how to do it. I have 2 tables and i want to style both with same styles and their's TD and TH too.
I have read, Multiple classes in CSS Selector , Multiple two-classes css-selector? , css multiple class / id selectors? , CSS selector for multiple sub-elements
What i have:
So 2 tables same styles. This is ok, it works.
.documents_table, #documento_detalhe {
font-family: 'Roboto', sans-serif;
border-collapse: collapse;
width: 100%;
}
But now how to select both tables and theirs TDs and THs?
This doenst work:
.documents_table, #documento_detalhe td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
border:0;
}
This is what i have but i want to reduce lines:
.documents_table td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
border:0;
}
#documento_detalhe td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
border:0;
}

You should know that by writing
.documents_table td, th, { styles } The styles are applied to all td inside .documents_table and to ALL th from your html. Not just the ones in the .documents_table . Your selector should be .documents_table td, .documents_table th
Having said that, to style the td and ths from both tables you should write
.documents_table td,
.documents_table th,
#documento_detalhe td,
#documento_detalhe th {
...styles
}
BUT, it would be better to add a common class to both tables like '.my-table' and so you would write just .my-table td, my-table th { styles } and it would apply to both.
See below
.my-table th, .my-table td {
color:red;
}
<table id="documento_detalhe" class="my-table">
<tr><th>TH #documento_detalhe<th></tr>
<tr><td> TD #documento_detalhe<th></tr>
</table>
<table class="documents_table my-table">
<tr><th>TH documents_table<th></tr>
<tr><td> TD documents_table<th></tr>
</table>

Related

How to override parent table style for child table in html?

I have defined nested table where parent table don't let to defined new styles for child table.
Some places in nested table I want to hide left and right cell border.
I have tried border-left-style:none !important; border-right-style:none !important; for td of child table but it not works.
Edited:
For parent table styles as
table tr td {
border: 0.1px solid black;
border-collapse: collapse;
font-size: 14px;
height:21px;
padding-left:4px;
}
for child table styles as
table tr td table tr td {
border: 1px solid black;
font-size: small;
border-collapse: collapse;
}
table tr td table .btlPropertyAddress td{
border-left-style:none !important;
border-right-style:none !important;
}
Where btlPropertyAddress added class to child table.

What exactly does this CSS selector with a comma match?

I have a question about CSS selectors.
In my CSS file I have the following code:
.table_legenda th, td {
text-align: left;
vertical-align: top;
font-weight: bold;
color: #76818a;
border-bottom: 1px solid #76818a;
border-left: 1px solid #76818a;
white-space: nowrap;
overflow: hidden;
}
Exactly what elements does that select?
I thought that it should select all the th and td elements inside a table having the class table_legenda.
However, when I test it, the style also gets applied to td elements inside other tables that do not have the table_legenda class (but do have another class).
Why does that happen? What am I missing?
You are misunderstanding the precedence of the comma.
.table_legenda th, td {}
is equivalent to:
.table_legenda th {}
td {}
and not to:
.table_legenda th {}
.table_legenda td {}
You need to specify the complete selector each time you have a comma:
.table_legenda th,
.table_legenda td {}
A preprocessing tool such as SASS can give you alternative syntax:
.table_legenda {
th, td {}
}
it selects tr inside table_legenda class , and in addition to that, all td.
The selector you want is
.table_legenda th, .table_legenda td
In this one, it selects all the th inside .table_legenda and all td inside .table_legenda
The , means selecting another attribute so what you should do is:
.table_legenda th,.table_legenda td {
text-align: left;
vertical-align: top;
font-weight: bold;
color: #76818a;
border-bottom: 1px solid #76818a;
border-left: 1px solid #76818a;
white-space: nowrap;
overflow: hidden;
}

How can I target <td> and <th> except for the first and last <tr>?

How can I target some child elements except where the parent is the first or last child to its parent?
See the comments in my CSS for a better idea of what I mean.
CSS:
/* Always apply */
tr th, tr td
{
text-align: left;
vertical-align: top;
}
/* Apply to all except first <tr> */
tr th, tr td
{
padding-top: 5px;
}
/* Apply to all except last <tr> */
tr th, tr td
{
border-bottom: 1px solid #c4c4c4;
padding-bottom: 5px;
}
HTML:
<table>
<tr>
<th>Born</th>
<td><time datetime="1986-11-05">5<sup>th</sup> November 1986</time></td>
</tr>
<tr>
<th>Gender</th>
<td>Male</td>
</tr>
<tr>
<th>Sports</th>
<td>Football<br />Tennis</td>
</tr>
<tr>
<th>Teams</th>
<td>Liverpool FC<br />Spain FC</td>
</tr>
</table>
Well you have a solution in the questions you linked. Use the :not() pseudo-class:
/* Apply to all except first <tr> */
tr:not(:first-child) th, tr:not(:first-child) td{
padding-top: 5px;
}
/* Apply to all except last <tr> */
tr:not(:last-child) th, tr:not(:last-child) td{
border-bottom: 1px solid #c4c4c4;
padding-bottom: 5px;
}
fiddle
(this won't work in IE 8 though)
Alternative for IE 7+, if the border styles can change:
/* Apply to all */
tr th, tr td{
border-top: 1px solid #c4c4c4;
padding-top: 5px;
}
/* Remove styles from first */
tr:first-child th, tr:first-child td{
border-top: 0;
padding-top: 0;
}
or
/* Apply to all but first */
tr + tr th, tr + tr td{
border-top: 1px solid #c4c4c4;
padding-top: 5px;
}
(but not sure if + is supported by IE 7)
tr th,
tr td
{} /* always apply */
tr:first-child th,
tr:first-child td
{} /* apply only to td and th of the first tr */
tr:last-child th,
tr:last-child td
{} /* apply only to td and th of the last tr */
tr:first-child ~ tr:not(:last-child) td,
tr:first-child ~ tr:not(:last-child) th
{background:red;}
this is the best you can archive with siblings combinators, but it will not work with older browsers, because of :not and probably also ~
You could achieve this by doing
tr:not(:first-child) th, tr:not(:first-child) td{
padding-top:5px;
}
tr:not(:last-child) th, tr:not(:last-child) td{
border-bottom: 1px solid #c4c4c4;
padding-bottom: 5px;
}
You could also add one class for the first and last of each, perhaps something like firstth and lastth. This would most certainly be the easiest solution. It is supported by all browsers as well
Also, as wes said, you could define the normal, then apply different styles for the first and last, although this is not as great of a method of completion
tr th, tr td{
padding: 5px 0; /*I would combine the padding for top and bottom for a more condensed and semantically correct markup*/
border-bottom: 1px solid #c4c4c4;
}
tr:first-child th, tr:first-child td{
padding:top: 0;
}
tr:last-child th, tr:last-child td{
border-bottom:0;
padding-bottom:0

css - how to remove inherited style

Please see this : http://jsfiddle.net/ymSpY/. If you can see <td> has an inner table. The inner table inherits the style from the parent table. The inner table has borders. How can I make the border invisible or remove it? As you can see the markup of the inner table it has style="border-collapse: collapse;" and I even tried border=0 but it doesn't work. The border of the inner/child table is still there.
You could apply your rules only to immediate children:
.dataTable > thead,
.dataTable > thead > tr > th,
.dataTable > tbody,
.dataTable > tbody > tr > td {
padding: 2px;
border-top: 1px solid #F5F2EF;
border-left: 1px solid #F5F2EF;
border-bottom: 1px solid #F5F2EF;
border-right: 1px solid #F5F2EF;
}
This way, the border rules don't trickle down to the nested table. The only other option is to do a whole lot of resetting, which will quickly cause your CSS to turn into kudzu.
Demo: http://jsfiddle.net/ymSpY/10/
I'd probably create a separate class for the child table, but short of that, here is one solution:
.dataTable td table, .dataTable td table tbody, .dataTable td table td {
border:none;
}
http://jsfiddle.net/ymSpY/6/
EDIT: here's one with a class defined for a child table, this may give you more flexibility once you start adding other elements to your main dataTable.
http://jsfiddle.net/ymSpY/11/
I'm confused. You have changed the colour, for the borders, so why don't you just set them to 0 instead?
.dataTable td table td {
border-top: 0;
border-left: 0;
border-bottom: 0;
border-right: 0;
}
Example http://jsfiddle.net/ymSpY/4/
This works assuming you're using bootstrap to style your ui.
table.noborder td {
border: none !important;
}
table.noborder td table.table td {
border: 1px solid #dee2e6 !important;
}

GWT FlexTable Header Column

I have CSS definitions for various elements around Table as shown below (Details omitted, only style names for reference).
table.entities {
border: 1px solid #c5d7ef;
border-collapse: collapse;
width: 100%;
margin-bottom: 0;
}
table.entities th, table.entities td {
padding: .25em 1.5em .5em .5em;
}
table.entities th {
font-weight: bold;
text-align: left;
background: #e5ecf9;
white-space: nowrap;
}
table.entities th a, table.entities th a:visited {
color: black;
text-decoration: none;
}
table.entities td {
background-color: #fff;
text-align: left;
vertical-align: top;
cursor: pointer;
}
table.entities tr.even td {
background-color: #f9f9f9;
}
And I apply the styles for even rows like:
flexTable.getRowFormatter().getElement(row).setClassName("even");
But for some reason, the header styles are not applied to table. Here is what I have tried
As it is I would expect th to apply style to first row, but not sure if that is a valid assumption to make
Changed the header style from th to something like theader and then explicitely applied to first row using row formatter for first row. I tried first row value as 1 & 0 both but either didn't give me results
Can anyone help me spotting where I might be going wrong?
I am pretty sure that the FlexTable does not render <th> tags. Everything is contained in the <tbody>.
You must also remember that the <tr> tag is pretty much unstylable so to style the whole row you need to apply the same style to all child <td> tags.
tr.someStyle td {
/* all styles that apply to the row */
}
If you post your actual styles and what is not being applied, maybe we can help further.

Resources