2

I'm working through a tutorial and I have a form that uses float elements. The form currently has all the labels and input boxes next to each other.

To align the form so that everything is on the left the book teaches me to:

Put-> clear: left; as below

label {
  float: left;
  width: 5em;
  text-align: right;
  margin-right: .5em;
  **clear: left;**
}
input {
  background-color: #CCCCFF;
  float: left;
  clear: right;

The way I understand the clear attribute is that if I set it on a floating element, it means that I want nothing to the left/right/both of that element.

Therefore, in my head, instead of putting the clear attribute on the label, putting that clear:right; on the input should also work (because then nothing can be on the right side of the input box), but of course it doesn't.

There's a gap in my understanding, can someone please point out why putting the clear:right attribute on the inputs won't work the same as putting the clear:left attribute on the label?

Thank you

superquack
  • 43
  • 9
  • possible duplicate of [Why clear both CSS?](http://stackoverflow.com/questions/12871710/why-clear-both-css) – Sudheer Jul 03 '14 at 12:17

3 Answers3

0

basically the clear property kind of overwrites the float.

i prepared this example: http://jsfiddle.net/vlrprbttst/JF7wD/1/

all divs are floated to the left. when float is applied, they all go next to each other. what if you need to temporary block this behaviour but still need a floated element? that's when you use clear

not the best way to explain it but i hope you got it :)

valerio0999
  • 11,458
  • 7
  • 28
  • 56
  • But why is it that putting the clear:right attribute on the inputs won't work the same as putting the clear:left attribute on the label? – superquack Jul 03 '14 at 12:32
  • No man, go back to that guys demo: http://jsfiddle.net/mofeenster/UH7Hp/1/ Take away the clear attribute on the label and do clear:both on the input element. It messes up. – superquack Jul 03 '14 at 12:44
  • Hey, if you take clear: left off the labels they won't clear the fields. Don't you want the labels to clear the fields? – Morgan Feeney Jul 03 '14 at 21:14
0

The <input> and <label> elements are inline elements by default, so under normal circumstances they do not fill the width of their parent elements and do not require their own line. So having <input> elements and <label> elements one-after-the-other do not strictly require any floating logic:

<form>
    <input type="text"></input><label>test</label>
    <input type="text"></input><label>test</label>
    <input type="text"></input><label>test</label>
</form>

Without any CSS, the above markup produces this:

inputs in a line

The float:left CSS is used to force elements out of the flow of the document, and hug their adjacent counterparts. It's used mainly on block elements. The clear:left/right/both then overrides this functionality in order to bring the proceeding elements back into the flow of the document. Read about it here.

If you're simply wanting to force <input> elements to be on their own line, perhaps you could wrap them in a block element like a <div>:

<form>
    <div>
        <input type="text"></input><label>test</label>
    </div>
    <div>
        <input type="text"></input><label>test</label>
    </div>
    <div>
        <input type="text"></input><label>test</label>
    </div>
</form>

The above, without any CSS, will produce this:

block elements

Read about inline and block elements in order to further your understanding of how elements are positioned naturally within the document.

To summarise, the float CSS property is a way of taking an element out of the flow of the document and positioning it to the left or right of inline elements. To quote MDN:

The float CSS property specifies that an element should be taken from the normal flow and placed along the left or right side of its container, where text and inline elements will wrap around it.

shennan
  • 10,798
  • 5
  • 44
  • 79
  • "The float:left CSS is used to force elements out of the flow of the document, and hug their adjacent counterparts" - well as far as i know, `float` *was* used to *float* text around an element like news paper articles, which was *misused* for creating layouts since there was no other way back in time. times are changing :) – T J Jul 03 '14 at 12:23
  • @TilwinJoy *float* implies a change in the behaviour of a block-level element. See the quote from MDN at the end of my answer. Yes, it's used to *float* text around an element, but it does this by taking the element out of the document flow. – shennan Jul 03 '14 at 12:27
  • But why is it that putting the clear:right attribute on the inputs won't work the same as putting the clear:left attribute on the label? – superquack Jul 03 '14 at 12:36
  • @absurdity69 Perhaps that would be an easier question to answer by getting more of an understanding about what you'd *actually* like to achieve. Do you want the ``s and ` – shennan Jul 03 '14 at 12:44
0

it won't work as you are using float: left and want to clear: left, not clear: right. as demonstrated here

See my code below:

CSS

label {
  float: left;
  width: 5em;
  text-align: left;
  margin-right: .5em;
  clear: left;
}
input {
  background-color: #CCCCFF;
  float: left;
  clear: left;
}

HTML - ensure the order you want the labels in is correct here too

<form>
    <label>test</label><input type="text"></input>
    <label>test</label><input type="text"></input>
    <label>test</label><input type="text"></input>
</form>

You can choose to clear both if unsure, however if you are floating left use clear left, not clear right, as there is nothing to clear on the right.

Morgan Feeney
  • 737
  • 7
  • 11
  • Yes, if you take away clear:left attribute on the label elements, then the form becomes messed up. The clear:left or even clear:both attribute in the input element doesn't fix the form's appearance - I just don't get why the clear must be on the label element rather than on the input element. Do you understand my issue? – superquack Jul 03 '14 at 12:35
  • Hi there, why would you want to take it away? – Morgan Feeney Jul 03 '14 at 12:37
  • I wouldn't but if the clear attribute works then why won't it clear everything to the right of the input boxes thus having the same effect as clear:left on labels? – superquack Jul 03 '14 at 12:41
  • Hi, if you apply clear: right to an element, THAT element will clear the element it is next to it, providing that the element next to it is not set to display: block or width: 100% etc. Basically the elements have to be able to stack.. If you want the labels to clear the inputs they must be set to clear left. You can't apply a clear on an element unless you state it in CSS on the element you want to do the clearing. – Morgan Feeney Jul 03 '14 at 12:46
  • Thank you. That vaguely makes sense now. When you refer to "stack", what exactly do you mean? – superquack Jul 04 '14 at 20:36
  • Arrange items on top of each other, as opposed to next to each other. – Morgan Feeney Jul 04 '14 at 20:54