tags:

views:

6126

answers:

13

I'm writing a web service, and I want to return the data as XHTML. Because it's data, not markup, I want to keep it very clean - no extra s or s. However, as a convenience to developers, I'd also like to make the returned data reasonably readable in a browser. To do so, I'm thinking a good way to go about it would be to use CSS.

The thing I specifically want to do is to insert linebreaks at certain places. I'm aware of display: block, but it doesn't really work in the situation I'm trying to handle now - a form with <input> fields. Something like this:

<form>
  Thingy 1: <input class="a" type="text" name="one" />
  Thingy 2: <input class="a" type="text" name="two" />
  Thingy 3: <input class="b" type="checkbox" name="three" />
  Thingy 4: <input class="b" type="checkbox" name="four" />
</form>

I'd like it to render so that each label displays on the same line as the corresponding input field. I've tried this:

input.a:after { content: "\a" }

But that didn't seem to do anything.

Assistance greatly appreciated.

+1  A: 

One option is to specify a XSLT template within your XML that (some) browsers will process allowing you to include presentation with mark-up, CSS, colors etc. that shouldn't affect consumers of the web service.

Once in XHTML you could simply add some padding around the elements with CSS, e.g.

form input.a { margin-bottom: 1em }

DamienG
A: 

Use javascript. If you're using the jQuery library, try something like this:

$("input.a").after("<br/>")

Or whatever you need.

Dergachev
Never rely on javascript to do what html/css can easily do because there is no graceful degradation.
Loren Segal
A: 

I agree with John Millikin. You can add in <span> tags or something around each line with a CSS class defined, then make them display:block if necessary. The only other way I can think to do this is to make the <input> an inline-block and make them emit "very large" padding-right, which would make the inline content wrap down.

Even so, your best bet is to logically group the data up in <span> tags (or similar) to indicate that that data belongs together (and then let the CSS do the positioning).

Thunder3
+1  A: 

the following would give you the newlines. It would also put extra spaces out in front though... you'd have to mess up your source indentation by removing the tabbing.

form { white-space: pre }
Jimmy
+13  A: 

It'd be best to wrap all of your elements in label elements, then apply css to the labels. The :before and :after pseudo classes are not completely supported in a consistent way.

Label tags have a lot of advantages including increased accessibility(on multiple levels) and more.


       <label for="one">

       Thingy one: <input type="text" id="one" name="one">

       </label>


then use css on your label elements...


label {display:block;clear:both;}

BrewinBombers
I had hoped to avoid the extra wrapping element, as the primary consumer of this page is software, not humans. But it looks like it's necessary.
+4  A: 

It looks like you've got a bunch of form items you'd like to show in a list, right? Hmm... if only those HTML spec guys had thought to include markup to handle a list of items...

I'd recommend you set it up like this:

<form>
  <ul>
    <li><label>Thingy 1:</label><input class="a" type="text" name="one" /></li>
    <li><label>Thingy 1:</label><input class="a" type="text" name="one" /></li>
 </ul>
</form>

Then the CSS gets a lot easier.

Jon Galloway
Don't forget your label "for" attributes, if you omit them there is no point to using the label in the first place since it's not related to the input element in any way.
Eric DeLabar
To follow up, you may also nest the input (or select etc.) element inside the label and safely omit the "for" attribute.
BrewinBombers
+1  A: 

Form controls are treated specially by browsers, so a lot of things don't necessarily work as they should. One of these things is generated content - it doesn't work for form controls. Instead, wrap the labels in <label> and use label:before { content: '\a' ; white-space: pre; }. You can also do it by floating everything and adding clear: left to the <label> elements.

Jim
A: 

The CSS clear element is probably what you are looking for the get linebreaks. Something along:

#login form input { clear: both; }

will make sure the no other floating elements are left to either side of you input fields.

Reference

+1  A: 
<form>
   <label>Thingy 1: <input class="a" type="text" name="one" /></label>
   <label>Thingy 2: <input class="a" type="text" name="two" /></label>
   <label>Thingy 3: <input class="b" type="checkbox" name="three" /></label>
   <label>Thingy 4: <input class="b" type="checkbox" name="four" /></label>
</form>

and the following css

form label { display: block; }
daniels
A: 
Right concept, but the "label" element is more semantically correct.
Eric DeLabar
A: 
<style type="text/css">
label, input { float: left; }
label { clear:left; }
</style>

<form>
    <label>thing 1:</label><input />
    <label>thing 2:</label><input />
</form>
Galen
A: 

The javascript options are all over complicating things. Do as Jon Galloway or daniels0xff suggested.

lordscarlet
It's not a real answer, but a comment.
Török Gábor
Comments did not exist when I wrote this over a year ago.
lordscarlet
A: 

form div.commentform label, input {

display:block; clear:both;

}

worked like a dream for me , thanks Jose

Beware: `form div.commentform label, input` != `form div.commentform label, form div.commentform input`!
Török Gábor