Let's suppose that I have a HTML page that renders something like bellow:
labelA [___INPUT-TEXT____] labelB [___INPUT-TEXT____]
However, I want to "break the line" on the second label, something like:
labelA [___INPUT-TEXT____]
labelB [___INPUT-TEXT____]
Assuming that I can not change the HTML code. How can I do it with CSS?
Here is an example:
<html><head><style>
#labelA { display:block; }
#labelB { display:block; }
</style></head>
<body>
<label for="ia" id="labelA">labelA</label><input id="ia" type="text"/>
<label for="ib" id="labelB">labelB</label><input id="ib" type="text"/>
</body></html>
Any help is appreciated.
Cheers,
EDIT:
I mean { display: block; } instead of {inline:block}, thanks for pointing it out.
5 Answers 5
Use display: block; instead of inline: block; and I think it should do what you want.
EDIT Didn't read your question carefully enough. If you want to group the label and text input together on a line, you can use something like:
label, input {
float: left;
}
label {
clear: left;
}
...and here's a more polished jsFiddle demonstration courtesy of Tim Medora
2 Comments
Here's a non-obvious way if you can use :before:
#labelB:before {
white-space: pre;
content: '\A';
}
4 Comments
\A is a just a newline character that works in CSS (where \n would not).Assuming that I can not change the HTML code. How can I do it with CSS?
The easy way to do it would be to just wrap each label/input pair in a div (or other block element, or element styled as a block element). However, if you can't change the HTML...
Demo: http://jsfiddle.net/bdWRV/
#labelB:before{ content: ""; display: block; }
This works in Chrome 25, Firefox, and IE 9/10. In my opinion, it's sort of a hack. At best, it's not ideal.
1 Comment
You have placed wrong statement inline: block; this should be display: inline; or display: inline-block; or display: block; like this display should be defined. I think you need to learn css more then go to w3school or search for css tutorial in google or perhaps may be you have placed inline: block; mistakenly. For your question, you need to define display: block; is okay as other answers.
1 Comment
just simple use <br /> but in your case, just simple used float using float is not guarantee unless you use 100% width in your element
below example using <br />
<label for="ia" id="labelA">labelA</label><input id="ia" type="text"/><br />
<label for="ib" id="labelB">labelB</label><input id="ib" type="text"/>
using float;
float:left;
width:100%;
display: blockshould fix this, assuming you don't have it floated.clear: leftwould fixfloat.