tags:

views:

513

answers:

3

I've got a form that I'm trying to layout semantically and format with CSS. As it's produced dynamically (from ASP.NET MVC) some elements may be rendered as text rather than input tags as they may be read-only for that screen. I am trying to add a width to the label tags to neaten up the form and align the values/value boxes, but it seems I can only do this if I float them (is that correct?). That works OK for when I have rendered values after the label tag, but if the value is blank the left float seems to stack against the previous label, even though both are contained within p tags (these should be block level, yes?). What, if anything, am I doing wrong?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head><meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" /><title>

    Edit

</title>
        <!-- Next Style Section Is Just To Set Up Classes, it will be moved to .CSS file -->
        <style type="text/css">
            div.FieldGroup {background-color: Blue; }
            p.Field { display: block; }
            span.NoteHeader { font-style: italic;}
            label { float: left; width: 150px;} 
        </style>
</head>

<body>
  <div id="main">


    <h2>Edit</h2>



    <form action="" method="post">
        <div id="MainSection" class="FieldGroup">
            <fieldset>
                <legend>Person Identification</legend>
                <p class="Field">
                    <label>Name:</label>
                    Name
                </p>
                <p class="Field">
                    <label for="Colour">ChildStatus:</label>
                    <select id="Colour" name="Colour">
                        <option value="10">Red</option>
                        <option value="20">Yellow</option>
                        <option selected="selected" value="30">Orange</option>
                    </select>
                </p>
                <p class="Field">
                    <label for="Age">Age:</label>
                    <input id="Age" name="Age" type="text" value="" />

                </p>
                <p class="Field">
                    <label>Birthplace:</label>
                </p>
                <p class="Field">
                    <label for="Reference">Reference:</label>
                    <input id="Reference" name="Reference" type="text" value="" />
                </p>
            </fieldset>
        </div>
    </form>
  </div>

</body>
</html>

Cheers

MH

(P.S. - I can't add in readonly textboxes to contain these blanks as they want plain text if they can't change the value)

A: 

Add a clear:left property to the label's css.

CptSkippy
this doesn't work, it means the following input box moves up to the end of the preceding line
Mad Halfling
+1  A: 

Add clear:both to your p.Field declaration:

p.Field { 
   display: block; 
   clear: both; 
}
Pat
thanks, someone else on another form suggested clear: left and this seems to suffice.
Mad Halfling
I changed the ps to divs, this worked a lot better (the ps were VS's default)
Mad Halfling
A: 

You don't need to float your labels in order to set their width, you just need to change their display to block. If you are floating them though, their containing p blocks won't expand to surround them unless given an overflow of hidden or auto. Clearing the label can also work to keep them from stacking, but it all depends on the look you want.

Eric Meyer