tags:

views:

259

answers:

3

I have a simple HTML table with 2 columns containing text fields and headers for 'Name', 'Comments' and 'Email'.

I'm looking for the best strategy on styling this HTML fragment if it were to appear on multiple pages - requiring different dimensions on each page. I've been reading a lot about CSS recently but havent stumbled across enough information yet that really makes me comfortable to know the best way to design such .css.

For instance I might show the comments form at 50% width on the 'comments' page, but only at 20% in a sidebar in some additional places on the site.

I am mainly concerned about styling the widths of the boxes - but of course the same approach applies for the text. For instance the name field should not be as wide as the email field. I'm thinking fixed widths are better than percentages.

There are obviously many ways to style it. Assume I have 1 master css file already.

1) Put percentage widths on the input tags and then the outer div would be 100% width for whatever panel it is contained in. This requires no page specific css but I don't like the idea of percentages inside the td tags, plus I cant change the height easily of the textarea.

2) create styles for #Name, #Comments and #Email in each individual page as additional styles in <head><style> *

3) style based on #Name, #Comments and #Email in a page specific css file. Are page specific files good or bad? I'm not even sure I like styling based on the ids here because they're dynamically generated and if for some reason they needed to change I'd have to update the css everywhere.

4) style based on #Name, #Comments and #Email but qualify them with a descendent selector specific to each page. So i'd have .faqPage #Name for when this appears on the FAQ page. Obviously these go in my master css file.

5) create class names for 'emailField, nameField and commentsField` [options 2,3,4 are repeated for this option]

6) create class names for 'shortField, fullWidthField and textInputField` [options 2,3,4 are repeated for this option]

7) you get the idea :)

8) something else

I'm just a little overwhelmed with all the options. How do I go about deciding which is the best way? A specific goal is to be able to style the same HTML on multiple pages (obviously thats what css is all about though - but it does affect which options I can use).

<div id="pnlSubmitComments">
<table class="fieldTable"> 
    <tr> 
    <td align="right"> 
        <label for="Comments">Name:</label> 
    </td> 
    <td> 
        <input id="Name" name="Name" type="text" value="" /> 
    </td> 
    </tr> 
    <tr> 
    <td align="right"> 
        <label for="Comments">Email:</label> 
    </td> 
    <td> 
        <input id="Email" name="Email" type="text" value="" /> 
    </td> 
    </tr> 
     <tr> 
    <td align="right" valign="top"> 
        <label for="Comments">Questions:</label> 
    </td> 
    <td> 
        <textarea id="Comments" name="Comments"> 
</textarea> 
    </td> 
    </tr> 
    <tr> 
    <td> 
    </td> 
    <td> 
        <input id="btnSubmitComments" name="btnSubmitComments" type="submit" value="Submit Questions" /> 
    </td> 
    </tr> 
</table> 
</div>
  • PS. The actual field names more specific such as CommentsName - its just easier to put Name here for readability.
A: 

1) Use common css and set some of the values like width in code behind.

2) Create multiple css files for different needs and link right css to page using code behind.

+4  A: 

Side comment: Maybe you shouldn't use tables to layout this form but fieldsets, it would leave you with more flexibility. For example if you decide to have the labels and input fields on top of each other in a more narrow column...

your example without tables (looks also much prettier):

<style type="text/css">
<!--

form { /* set width in form, not fieldset (still takes up more room w/ fieldset width */
    font: 100% verdana, arial, sans-serif;
    margin: 0;
    padding: 0;
    min-width: 500px;
    max-width: 600px;
    width: 560px;
}

form fieldset {
    /* clear: both; note that this clear causes inputs to break to left in ie5.x mac, commented out */
    border-color: #000;
    border-width: 1px;
    border-style: solid;
    padding: 10px; /* padding in fieldset support spotty in IE */
    margin: 0;
}

form fieldset legend {
    font-size: 1.1em; /* bump up legend font size, not too large or it'll overwrite border on left */
    /* be careful with padding, it'll shift the nice offset on top of border  */
}

form label {
    display: block; /* block float the labels to left column, set a width */
    float: left;
    width: 150px;
    padding: 0;
    margin: 5px 0 0; /* set top margin same as form input - textarea etc. elements */
    text-align: right;
}

form input, form textarea {
    /* display: inline; inline display must not be set or will hide submit buttons in IE 5x mac */
    width: auto; /* set width of form elements to auto-size, otherwise watch for wrap on resize */
    margin: 5px 0 0 10px; /* set margin on left of form elements rather than right of
     label aligns textarea better in IE */
}

textarea {
    overflow: auto;
}

 /* uses class instead of div, more efficient */
form br {
    clear: left; /* setting clear on inputs didn't work consistently, so brs added for degrade */
}
-->
</style>
<div id="pnlSubmitComments">
    <form>
        <fieldset>
            <label for="Comments">
                Name:
            </label>
            <input id="Name" name="Name" type="text" value="" /><br />
            <label for="Comments">
                Email:
            </label>
            <input id="Email" name="Email" type="text" value="" /><br />
            <label for="Comments">
                Questions:
            </label>
            <textarea id="Comments" name="Comments">
            </textarea><br />
      <label for="spacing"></label>
            <input id="btnSubmitComments" name="btnSubmitComments" type="submit" value="Submit Questions" />
        </fieldset>
    </form>
</div>

Now to your main question. I would do it as follows:

I would use the id's of the different layout columns I want to use the form in. So if I use it in my main column () I would write CSS accordingly like so:

#main .pnlSubmitComments form fieldset {
    /*your CSS*/ 
}

and for the side column respectively

#side .pnlSubmitComments form fieldset {
    /*your CSS*/
}

You can have control over each element by assigning classes like so:

<input type="text" class="email" name="email" id="email" />

and then you do exactly as described above:

#main .email {
   /*your css for the .email textbox/*
}
tharkun
ahh clever :-) the main reason for different widths is dependent on sidebar so that would work well. thanks for the fieldset tip. i need to watch out for using 'form' as a selector becasue i have multiple forms on the page.
Simon_Weaver
ps. what is 'form small' for and how would you recommend styling the widths of specific textboxes differently. i'd prefer name to be narrower than email and have control over each
Simon_Weaver
form small was superfluous, I deleted it now and added the answer to your second question.
tharkun
+3  A: 

You can easily do it with one css file, if you can add a style class on a container element.

For example, page 1 would have the following html:

<body class="page1">
  <!-- repeated html here -->
  <input />
</body>

And on page 2 you'd have:

<body class="page2">
  <!-- repeated html here -->
  <input />
</body>

In your single css file you can target the input tags based on the class of the body element:

body.page1 input { width: 25%; }
body.page2 input { width: 50%; }

So, you keep the html the same, just change the class (or id) of a container element, and use that to write different css rules.

Update: After rereading your list, i see this is more or less on your list as number 4. I think this is a good option if you can use it. I also use it to target different browsers, by adding a class indicating the browser on a body tag.

Andrej