How To Make First Column Of A Row Black And Second Column Red

    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs How to make first column of a row black and second column red Ask Question Asked 12 years, 10 months ago Modified 5 years, 11 months ago Viewed 24k times 8

I would like to give a css class to a row and i would like to make the first column black and the second column red. I dont want to use colgroup because this is a row specific action not whole table should be effected.

Share Improve this question Follow edited Oct 25, 2018 at 6:32 Alex Kulinkovich's user avatar Alex Kulinkovich 4,74815 gold badges49 silver badges54 bronze badges asked Mar 4, 2012 at 13:23 rematnarab's user avatar rematnarabrematnarab 1,3255 gold badges25 silver badges46 bronze badges 1
  • 1 Can you not just give each cell a class and style with CSS? – AlexKempton Commented Mar 4, 2012 at 13:27
Add a comment |

5 Answers 5

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 15

You could use:

td { color: black; } td:nth-child(2) { color: red; } Share Improve this answer Follow answered Mar 4, 2012 at 13:26 Qtax's user avatar QtaxQtax 33.9k9 gold badges89 silver badges125 bronze badges 2
  • Thisi s possible without CSS3 for better browser support. – Smamatti Commented Mar 4, 2012 at 13:30
  • @Qtax how about every odd rows second column? Would be nested? tr:nth-child(even) { td:nth-child(2) { color: red; }} – 3kstc Commented Mar 16, 2019 at 11:52
Add a comment | 9

This is possible without CSS3 !

Sample http://jsfiddle.net/Q3yu5/1/

CSS

tr.special_row td { background-color: #000; } tr.special_row td + td { background-color: #f00; } tr.special_row td+td+td { background-color: #fff; }

HTML

<table> <tr class="special_row"> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> </table> Share Improve this answer Follow edited Mar 4, 2012 at 18:35 answered Mar 4, 2012 at 13:29 Smamatti's user avatar SmamattiSmamatti 3,9313 gold badges33 silver badges44 bronze badges 2
  • 1 I'm afraid this is not exactly what @rematnarab was looking for. I didn't keep in mind, that not the whole table should be affected. edited tr to a more specific rule. – Smamatti Commented Mar 4, 2012 at 18:33
  • Just a note: I was in the need of your PREVIOUS Version of the code. +1 for review log! – Michel Ayres Commented Mar 13, 2014 at 20:46
Add a comment | 3

... this is a row specific action not whole table should be effected.

Then applying different styles to your first and second column of the given row could be useful:

<style type="text/css"> td.first { background-color: black; color: white; } td.second { background-color: red; color: white; } </style> <table> <tr> <td class="first">1st row, 1st column</td> <td class="second">1st row, 2nd column</td> </tr> <tr> <td>2nd row, 1st column</td> <td>2nd row, 2nd column</td> </tr> </table> Share Improve this answer Follow edited Mar 4, 2012 at 13:42 answered Mar 4, 2012 at 13:34 Juvanis's user avatar JuvanisJuvanis 25.9k5 gold badges73 silver badges88 bronze badges 1
  • +1 Whoops. You are right! --> not whole table should be effected – Smamatti Commented Mar 4, 2012 at 18:31
Add a comment | 2

In CSS3 you can use the :nth-child() pseudo-class.

See the Docs for how to use it. Also, as of early 2019, there's barely a reason not to use CSS3 selectors.

Share Improve this answer Follow edited Feb 3, 2019 at 14:12 answered Mar 4, 2012 at 13:27 m02ph3u5's user avatar m02ph3u5m02ph3u5 3,1617 gold badges41 silver badges57 bronze badges Add a comment | 0

You need to create 2 types of CSS. For each row you add the CSS you want.

Share Improve this answer Follow edited Mar 4, 2012 at 22:15 FelipeAls's user avatar FelipeAls 22.2k9 gold badges56 silver badges74 bronze badges answered Mar 4, 2012 at 13:26 Ofer's user avatar OferOfer 5,1796 gold badges22 silver badges27 bronze badges 2
  • 1/ Which type of CSS? Do you mean CSS rules? 2/ Are you saying that you add style attributes on the first two cells of each row? Better separate CSS from HTML whenever it's possible (and it's possible here) – FelipeAls Commented Mar 4, 2012 at 22:13
  • exactly, this css can reuse in the future – Ofer Commented Mar 5, 2012 at 6:56
Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • How AI apps are like Google Search
  • Featured on Meta
  • The December 2024 Community Asks Sprint has been moved to March 2025 (and...
  • Stack Overflow Jobs is expanding to more countries
1 How to set background color to the column using CSS? 4 change color for second column 0 Alternate row color 0 How to change the color of first row of first table using css 2 how to change the background color of first row in html table and all other rows alternate color 2 Alternating table row color with the first row a different color 5 change color of rows in a table in HTML and CSS 0 How can I change the background color for a single column in html? 3 Alternate row colours from 2nd row of the table using CSS 0 how to style rows with a specific position for each column (CSS)

Hot Network Questions

  • What happened to 1T-SRAM?
  • Growing plants on mars to increase the oxygen level
  • tabularray error
  • Publication in a journal that has now disappeared entirely. Can I publish the paper elsewhere?
  • How can I put logo on left of the page next to institute name?
  • What is the best way to prevent this ground rod from being a trip hazard
  • What does "the ridge was offset at right angles to its length" mean in "several places where the ridge was offset at right angles to its length"?
  • Online Service Course in the era of ChatGPT
  • What builds a nest or storage structure in the shoe?
  • Why don't bicycles have the rear sprocket OUTSIDE of the frame spacing? (Single speed)
  • Is this an effective way to quickly switch between two gain settings in an inverting amplifier configuration?
  • What's left of wine or vodka after the water and alcohol is boiled off?
  • Understanding the benefit of non principal repayment loan
  • Free Kei Friday
  • Tikz vphantom/hphantom for whole node/scope
  • Escape braces in C# interpolated raw string literal
  • How to swim while carrying fins (i.e., when the fins aren't positioned on my feet)?
  • How does FM preemphasis interact with maximum deviation?
  • What Battery Powered Part Is This?
  • Why do we define the standard error to ignore bias (unlike MSE which includes bias)?
  • Trying to find a middle grade book about a boy finding his way back to his reality/universe after he's taken away from it
  • Why does the engine prefer a5 (pass pawn) over axb5 (pass pawn+win a pawn)?
  • Is this 240V compressor plug wired correctly?
  • Is a rational decision (or claim) incompatible with determinism?
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

default

Từ khóa » Html Table First Column Background Color