Published on: 07 July, 2016

Learn how easily you can give an altering color to your html table row using CSS3 only

Posted by andy | 871 views 0 likes 2 favourites 0 comments
Tumblr Digg Google Plus

Learn how easily you can give an altering color to your html table row using CSS3 only.

Back to couple years ago before CSS3 is introduced, we have to manually specify the altering color to row background. This can be done in two ways. The first option is to add the style directly to the tr tag. While the second option is to add a css class into the row tag and define what will be the style for that css class.

Since the CSS3 has been introduced, you can now easily using the CSS3 style property even and odd rules. Let see the following example of html code we have.

<style>
	table.tblAltering td{
		padding:5px 10px;
	}
	
	table.tblAltering tr:nth-child(1n+even) {
		background: #eff4fd;
	}
	
	table.tblAltering tr:nth-child(1n+odd) {
		background: #ffffff;
	}
</style>

<table class='tblAltering' cellpadding='0' cellspacing='0'>
	<tr>
		<td>James</td>
		<td>Plumber</td>
		<td>35</td>
	</tr>
	<tr>
		<td>Adrian</td>
		<td>Teacher</td>
		<td>22</td>
	</tr>
	<tr>
		<td>Ben</td>
		<td>Truck Driver</td>
		<td>Age</td>
	</tr>
	<tr>
		<td>Kelly</td>
		<td>Cashier</td>
		<td>28</td>
	</tr>
</table>
James Plumber 35
Adrian Teacher 22
Ben Truck Driver Age
Kelly Cashier 28

So basically the rule is the odd keyword represents the odd rows which is 1,3,5,7 and so on, while the even number represent 2,4,6, 8 and so on.

Is it possible to start from specific row?

This can be easily achieve by specifying the row number using the number with n keyword. For example if you want the row to start from row number 10. Then use the following css.

Comments

There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.
Share your article with us and add your own google adsense account to earn extra money. Plus, you can promote a link back to your site.
Related Articles
Published on: 13 July, 2016
Ever imagine that you can now easily create a bouncing ball using CSS3 only? No javascript or image is required to do this. Thanks to the powerful attribute of CSS3 animation.
Published on: 24 March, 2016
You probably never thought about creating a heart shape completely using CSS3. Furthermore, you can include an animation to show the heart is actually beating. This is can be easily done with CSS3 animation. No flash or images are required.
Published on: 16 December, 2015
Creating a mobile menu button is really simple using CSS3. You may find others still using an image to create those 3 horizontal lines. With the introduction of :after and :before css3 attributes, this menu can be easily done by specifying the position content of the object and adding a content attribute.
Related Tutorials
Published on: 28 June, 2014
Learn how to use media queries in CSS3. Media queries are simple conditions used to targetting different devices for tables, phones or desktop computers. As each device may have different view size, media queries become so handy to use to display the content dimension correctly.
Published on: 28 June, 2014
Learn how to use advanced selectors in CSS3. By using the built in pseudo-class, performing a selector in CSS3 is so much easy.
Published on: 14 May, 2014
Learn how to use multiple-column property in CSS3. To create multiple columns in CSS3 is now so handy, you just need to include a css built-in keyword column-count to do the job for you. No more table layout required.
Published on: 13 May, 2014
Learn how to use box-sizing property in CSS3. As you know many browsers treat margin and padding differently. When you combine with width or height of an element box, you find out that the final dimension is completely different when you view with different browser. Well, this now can be easily taken care by using the new CSS box-sizing property.
Published on: 09 May, 2014
Learn how to use opacity property in CSS3. So what is actually opacity? CSS3 Opacity defines a transparency level of an element. The property value of opacity is range from 0 to 1.