tags:

views:

64

answers:

2

I am trying to learn more CSS. I inherited a nice layout that I have been using for a little while now and I want to keep the CSS going instead of mixing tables in there. I am currently designing a separate form to handle side by side textboxes. I was using span tags to keep these textboxes side by side but now I'm wondering what the best practice for this type of design would be. Should I use a div container and spans as I was doing or should I just use straight divs and float them as in my example?

<div style="overflow:hidden; width:100%; border:1px solid #000;">
  <div>
    <div style="float:left"><input type="text" /></div>
    <div style="float:right"><input type="text" /></div>
  </div>

  <div style="clear:left">
    <div><input type="text" /></div>
  </div>
</div>
A: 

As far as markup choices are concerned, it is always a good hint to test your page in a text browser (Lynx, Links, Elinks), and check how it is displayed there. I am usually using some kind of list (ul, ol or dl) for my forms.

Don’t forget to checkout A List Apart’s Prettier Accessible Forms article, which gives a good start for styling forms.

igor
Thanks for the link igor. I will read that.
Jim
A: 

I'm not entirely sure what you're trying to achieve in terms of layout, but you can get the same result using a lot less markup:

  <div style="overflow:hidden; width:100%; border:1px solid #000;">
    <div>
      <input type="text" style="float:left" /><input type="text" style="float:right" />
    </div>
    <div style="clear:left">
      <input type="text" />
    </div>
  </div>

Make sure you move those in-line styles into class or id definitions too. Avoid having css definitions in your markup.

Bayard Randel
Thanks Baynard, I didn't know I could float the input boxes. :) All I am trying to do is have 3 rows. The first row should have 2 textboxes side by side. The second row should have a single textbox aligned left and the last row will hold a submit button.
Jim
For this specific case, you could do something like this: <ul> <li><input .../></li> <li class="special"><input .../></li> <li><input .../></li> <li><button type="submit">...</button></li> </ul>… and style it with CSS: ul { position: relative; } li.special { top: 0; right: 0; text-align: right; }You should really consider adding labels, though. (But again, I don‘t know everything of your specific case.)
igor
ooops… forgot the CSS rule "position: absolute" for li.special ;-)
igor
Igor, thanks for the markup. I also just downloaded lynx and the site looks good there. As far as the form goes, and correct me if I'm wrong, but I want to have the label **above** the textboxes. Would the ul tag still work for this?
Jim
Of course – just use <li><label for="…">…</label><input id="…" … /></li>Style label and input with "display: block".
igor
Thanks Igor. I'm going to try this. I think it will be the best solution. Of course, the ul needs to be contained within a div, correct?
Jim
Generally speaking: no—why should it? This basically just depends on your design, how many CSS hooks you need.
igor
CSS hooks? What is this? BTW: I just plugged in your ul code but each textbox is on its own line. I need the first row to have two textboxes side by side. Should I just put 2 within a single li tag?
Jim
No, it should work perfect as I described it. I just set up a quick test page to demonstrate it to you: http://www.obda.net/stackoverflow/form-test.html
igor
Wow Igor, that is exactly it. :) All I need to do now is simply style the li tags to not have the round disks. Thank you very much for that effort.
Jim
Concerning CSS hooks: In general, you should use as little (semantic) markup as necessary for your page. Then, it might be necessary to add a wrapping <div> here or there, to serve as hooks for CSS definitions to achieve the desired visual layout. You will need this e.g. if you need multiple background images for the »sliding doors« technique. (Again, see A List Apart: http://www.alistapart.com/articles/slidingdoors/)
igor
You can remove the discs by styling the <ul> with "list-style: none". You might also want to remove margins and/or paddings from the <ul> and/or <li> elements.
igor
Igor, thanks again for the explanation. I am designing this form that will ultimately go into the already completed site. Let me ask you another question if you don't mind. These hooks that you are talking about... Once I drop this into the site, I am certain that I will need to use blank divs, or hooks as you call them. Once I do this, how will I refer to them in the stylesheet? I suppose I am talking about the inheritance. Is there a document you can refer me to about this?
Jim
Yes, this can be done via inheritance. Basically, you assign an ID to your wrapping <div> and then use CSS rules that use descendant selectors. See the CSS specification for examples: http://www.w3.org/TR/CSS2/selector.html#descendant-selectors
igor