0

Our users are using a tool to create HTML content with, I don't have control over the HTML it generates but I do have control over the CSS behind it and I've managed to plugin jQuery as well.

Our design guidelines are that whenever a table is created with two column the width of the first column should be 300px and the second 380px.

When they create a table they have an option from the tool to set the width of each table cell however in order to save this for them I'd like to automatically apply this on the HTML.

Is there a way using CSS rules to set table widths of td1 and td2? I know in CSS I can do something like:

td
{
width: 300px;
}

but that will apply to all TDs.

If that's not possible using CSS I also have jquery plugged in and happy to use it if it's not possible using CSS.

EDIT: I need this solution to work with IE8 and above, old browser but most of our users still use it.

3 Answers 3

2

Use :nth-child()

table tr td:nth-child(n) { 
    width: 300px;
}

Also Read

:last-child

:first-child

Update

You can use http://selectivizr.com/ JS which support css3 selector for IE.

or

As IE8 supports first-child you can trick this to support nth-child in IE8

/*table tr td:nth-child(2)*/
    table tr td:first-child+ td { width: 300px; }/*Works for IE8*/
Sign up to request clarification or add additional context in comments.

Comments

1

ie8 doesn't support nth-child to workaround it you can do this

table tr td:first-child + td +td { //gets 3rd td
    width:300px;
}

DEMO

5 Comments

Thanks Anton however it seems like it only applies it to the first table on the page and I've got few. Any idea why?
Only first table, weird seems to work here jsfiddle.net/Alfie/mpHEK/2 with multiple tables @Joly
Probably something in the way the html is being generated by the tool, I'll keep digging the code it creates, thanks
I noticed the tables are being created under <p> element, should that make any difference?
@Joly No, it shouldn't just tested it out
0

you can do it in CSS and in JQuery

in CSS, you simply use the first-child pseudo-class selector http://www.w3.org/TR/CSS2/selector.html#first-child

   td { width: 380px; }
   td:first-child { width: 300px; }

2 Comments

Does this work with IE8? Also how do I do the second child? nth-child(n) doesn't seem to work with IE8 either
first-child is CSS2 and works with IE8. nth-child is CSS3 and does not. See caniuse.com/css-sel3

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.