views:

370

answers:

5

In my forms, I want to make HTML labels bold in general, but not bold when they are labelling radio buttons and checkboxes.

How can I do that in a CSS that can be imported and works by default, without requiring local overrides of the styling?

Example Radio Label (should not be bold)

<label for="banana">
<input type="radio" name="banana" id="banana" value="banana" />Banana
</label>

Example non-Radio Label (should be bold)

<label for="id">ID</label>
<input type="text" size="12" id="id" name="id" />

The way I'm associating labels can be changed if it helps.

Thanks!

A: 

You could add a class to the labels for radio buttons, and then style them differently.

<label for="banana" class="radio">

Then the CSS could by default make labels bold, and then override it for the radio ones to make them normal.

label { font-weight: bold }
label.radio { font-weight: normal; }
Rich Adams
Thanks for that - I'd like to know if there's any way of setting the styling up so 'it just works' though, without having to remember to add a class... kinda feels like it should be possible...
Brabster
well, no, it's not.
gizmo
There's no way that I'm aware of to do that directly with CSS without giving the different labels a different class. The only way to do it would be with JavaScript.
Rich Adams
Technically speaking there *is* but it's not supported widely enough
annakata
It can be done in CSS3, but CSS3 is still under development (http://www.w3.org/Style/CSS/#specs) so not a very good idea to use it just yet.
Rich Adams
In dev doesn't mean what it used to with the CWG. See here: http://www.stuffandnonsense.co.uk/malarkey/more/css_unworking_group/
annakata
Ah interesting, thanks for the link!
Rich Adams
+2  A: 

You can't do that without giving the right labels a class or by using Javascript. With jQuery selectors you could do that:

$("label:not(:has(radio))").addClass("boldmenow");
...
.boldmenow {
  font-weight: bold;
}
cletus
A: 

Basically, you can't because IE doesn't support it (so use a class instead) but if IE worked properly and CSS3 was well supported you could use a general sibling selector like so:

input[type="radio"]~label {font-weight: bold;}

note that IE doesn't really support the attribute selector there either.

annakata
CSS3 isn't even finalized. How could it be supported?
cletus
It doesn't quite mean what you think it does / what it should. The CWG has basically said that the spec is now going to be defined by common implementation which means it's up to the big browser players to just get on with it...
annakata
See here for details: http://www.stuffandnonsense.co.uk/malarkey/more/css_unworking_group/
annakata
+1  A: 

If you extend the markup to the following:

<label for="banana">
    <input type="radio" name="banana" id="banana" value="banana" /><span>Banana</span>
</label>

Then you could use this CSS:

label {
    font-weight: bold;
}
label span {
    font-weight: normal;
}
Gumbo
+1  A: 

The short answer is you can't, reliably. If you're trying to make it work in a number of commonly used browsers such as IE6, 7, Firefox etc you'd have to rely on JavaScript rather than CSS to identify elements within your document that fit your rules for styling and apply a class to each one of those elements.

For cross-browser compatibility, you'd need to wrap arrayed field labels in a classifying element or apply classes to the labels to separate them from normal labels.

What I would do here is wrap the list of radio options in an (un)ordered list (for semantic meaning), and normal text fields in a single paragraph.

E.g. Normal Name:Value pair:

<div class="form_row">
  <label for="txtFirstName">First Name</label>
  <p>
    <input type="text" class="text" name="firstName" id="txtFirstName" />
  </p>
</div>

And then for a Name:Array pair:

<div class="form_row">
  <label>Gender</label>
  <ul>
    <li><label for="rdoGenderMale"><input type="radio" class="radio" name="gender" id="rdoGenderMale" value="male" />&nbsp;Male</label></li>
    <li><label for="rdoGenderFemale"><input type="radio" class="radio" name="gender" id="rdoGenderFemale" value="female" />&nbsp;Female</label></li>
  </ul>
</div>

Now to style it appropriately, you write your normal rules for Name labels:

div.form_row label {font-weight:bold;}
div.form_row ul {list-style:none;}
  div.form_row ul label {font-weight:normal;}

hth, ndorfin

ndorfin