iEntry 10th Anniversary Forum Rules Search
WebProWorld
Register FAQ Calendar Mark Forums Read
Graphics & Design Discussion Forum Post your graphics design questions/comments/ideas in here. Ask questions, post tutorials, discuss trends and best practices. Sub-forum for website accessibility and usability.

Share Thread: & Tags

Share Thread:

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-15-2008, 07:35 PM
WebProWorld New Member
 
Join Date: Jul 2008
Posts: 2
hughes RepRank 0
Default CSS: <label> ignores width styling

Code:
<html>
<head>
<style>
label {
	width: 5em;
	border: 1px dotted;
}
</style>
</head>

<body>
<form>
		<label for="xyz">
				XYZ:
				<input id="xyz" type="text" size="20">(additional hint)
		</label>
</form>
</body>
</html>
I added the border only so we can see the size of the label. In this example, all three parts (XYZ, the input field, the add'l hint) are laid out in one line making the label field significantly longer than 5em. However, if I add either display: block; or float: left;, it suddenly obeys they width rule and adds line breaks to put the parts in three different lines.

I'd expect that even in the default (display: inline; or no display rule as above) it breaks lines when it reaches the width limit. Why does it not do that?

And anyway, does that display or float rule not apply to how the label shows in it's containing element (form or fieldset)? Why would it affect the stuff INSIDE the label, since float is not inheritable?

Confused...
Reply With Quote
  #2 (permalink)  
Old 08-18-2008, 01:17 PM
wige's Avatar
Moderator
WebProWorld Moderator
 
Join Date: Jun 2006
Location: United States
Posts: 2,629
wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9
Default Re: CSS: <label> ignores width styling

I am not sure, but I think label is considered an inline element, and input is a block level element. Generally, label should not contain anything but the text, as it is a feature intended for accessibility, to link a string of text to a form field. (ie, a form element can not be part of it's own label)

I think for the best results the following code would do what you need:
HTML Code:
<div class="formelement">
   <label for="xyz">XYZ:</label>
   <input id="xyz" type="text" />
   (additional hint)
</div>
__________________
The best way to learn anything, is to question everything.
Reply With Quote
  #3 (permalink)  
Old 08-18-2008, 06:15 PM
WebProWorld New Member
 
Join Date: Jul 2008
Posts: 2
hughes RepRank 0
Default Re: CSS: <label> ignores width styling

Thanks for the reply. It may have to do sth with inline vs. block-level elements. But wrapping labels around input fields is definitely allowed. W3.org, the HTML standards body, even has an example showing this technique in the HTML specs:
Forms in HTML documents
(see third example at 17.9.1 "The LABEL element")

I wanted to use this to avoid cluttering up my code with so many divs, instead just styling fieldsets and labels directly.

But anyway, yes, it does seem like <label> behaves like an inline element by default. So I guess all inline elements can't have width styles?
Reply With Quote
  #4 (permalink)  
Old 10-27-2009, 07:53 PM
WebProWorld New Member
 
Join Date: Oct 2009
Posts: 1
lorelai RepRank 0
Default Re: CSS: <label> ignores width styling

This is because the label is an inline element.


Hope it helps.
Reply With Quote
  #5 (permalink)  
Old 10-27-2009, 08:12 PM
Uncle Dog's Avatar
WebProWorld Pro
 
Join Date: Apr 2008
Location: Scotland
Posts: 235
Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5
Default Re: CSS: <label> ignores width styling

I agree with wige and w3schools.com - label tags shouldn't normally contain anything other than text.

The W3.org example (no 3) you quote refers to implicit association. Notice the label tags don't contain a for attribute.
__________________
A computer once beat me at chess, but it was no match for me at kick boxing.
Reply With Quote
Reply

  WebProWorld > Site Design > Graphics & Design Discussion Forum

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
CSS styling for HR ArthurKay Graphics & Design Discussion Forum 3 09-09-2007 08:19 AM
Styling <ul> as <select>with CSS thindenim Graphics & Design Discussion Forum 2 06-05-2007 04:45 PM
Styling Content with and without CSS Dcrux Content Discussion Forum 19 02-07-2006 06:30 PM
Google Ignores <h1> tag janeth Google Discussion Forum 84 10-07-2004 08:40 PM
Google Ignores Robots.txt jestep Google Discussion Forum 1 09-03-2004 01:39 PM


All times are GMT -4. The time now is 01:03 PM.



Search Engine Optimization by vBSEO 3.3.0