Submit Your Article Forum Rules

Results 1 to 5 of 5

Thread: CSS: <label> ignores width styling

  1. #1
    Junior Member
    Join Date
    Jul 2008
    Posts
    20

    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...

  2. #2
    WebProWorld MVP wige's Avatar
    Join Date
    Jun 2006
    Posts
    3,138

    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.
    WigeDev - Freelance web and software development

  3. #3
    Junior Member
    Join Date
    Jul 2008
    Posts
    20

    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?

  4. #4
    Junior Member
    Join Date
    Oct 2009
    Posts
    1

    Re: CSS: <label> ignores width styling

    This is because the label is an inline element.


    Hope it helps.

  5. #5
    Senior Member Uncle Dog's Avatar
    Join Date
    Apr 2008
    Posts
    342

    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.

Similar Threads

  1. CSS styling for HR
    By ArthurKay in forum Graphics & Design Discussion Forum
    Replies: 3
    Last Post: 09-09-2007, 07:19 AM
  2. Styling Content with and without CSS
    By Dcrux in forum Content Discussion Forum
    Replies: 19
    Last Post: 02-07-2006, 05:30 PM
  3. Firefox ignores <comment>
    By RikR in forum Graphics & Design Discussion Forum
    Replies: 1
    Last Post: 11-03-2004, 12:10 AM
  4. Google Ignores <h1> tag
    By janeth in forum Google Discussion Forum
    Replies: 84
    Last Post: 10-07-2004, 07:40 PM
  5. Google Ignores Robots.txt
    By jestep in forum Google Discussion Forum
    Replies: 1
    Last Post: 09-03-2004, 12:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •