admin管理员组文章数量:1314471
I would like to change the background color of a specif column in a HTML table. Is it possible to use a property to do it. I found the way to do it with rows.
tr.even {
background-color: white;
}
I would like to change the background color of a specif column in a HTML table. Is it possible to use a property to do it. I found the way to do it with rows.
tr.even {
background-color: white;
}
Share
Improve this question
asked Feb 18, 2014 at 16:15
user3324283user3324283
111 gold badge1 silver badge2 bronze badges
2
- 1 Why not put an id on the columns so you can change it from css? – James Black Commented Feb 18, 2014 at 16:25
- Does this answer your question? Can I color table columns using CSS without coloring individual cells? – Spankied Commented Aug 8, 2021 at 3:34
6 Answers
Reset to default 2Why don't you try using the colgroup tag and bining that with the span attribute. that way if you have 3 columns for instance, you can do
<colgroup>
<col span="2" id="something">
<col id="something2">
</colgroup>
then you can style that whole column with css.
ex:
#something2 {background-color:red;}
If you are looking to style the background of a column, and not a row, you can try something like this (below). You'll need to change the selectors to match your HTML.
td:first-child { background-color: #ccc; }
Below is a link to a fiddle, along with a way to add classes via JS. There are many ways, but since you are dealing with columns, remember that the CSS is being applied to the same td
in each row you want to style.
JSFiddle
For modern browsers, you could use the nth-child()
selector:
element:nth-child(1) {
background-color:...
}
Or if you wanted to use jQuery, use eq()
:
$(element).eq(0).css("background-color","...");
Note: the CSS nth-child
selector is 1-based while the jQuery eq
selector is 0-based.
$('table tr:even').css('background-color', '#e5e5e5');
You can use :even and :odd selector:
$( "table tr:even" ).css( "background-color", "white" );
$( "table tr:odd" ).css( "background-color", "red" );
If you want to target the column then use:
$('table tr > :nth-child(even)').css('background-color','white');
$('table tr > :nth-child(odd)').css('background-color','red');
If you want to use just HTML and css you have to give every div even and odd class respectively. then give different css in you css file for both even and odd class.
<tr class="even"></tr>
<tr class="odd"></tr>
<tr class="even"></tr>
tr.even {
background-color: white;
}
tr.odd {
background-color: black;
}
if you want more dynamic way ,mean don't give class manually jquery will serach and add even odd div and give them specific class
Use jquery
$('tr:odd').addClass("odd")
$('tr:even').addClass("even")
and your css will be like this
tr.even {
background-color: white;
}
tr.odd {
background-color: black;
}
本文标签: javascriptChange Background color for a specific column in a HTML tableStack Overflow
版权声明:本文标题:javascript - Change Background color for a specific column in a HTML table - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741959847a2407208.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论