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
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 :)
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.
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:
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:
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.
Related
I am new to CSS/HTML and struggling to understand why when putting text "Title" out of div.position container label goes down. Thanks for you help.
My code
CSS code:
div {
display: inline-block;
}
.position {
float: left;
}
HTML code:
<div>
<label>Label</label>
<div class="group">
<div class="position">
<input type="radio" />
</div>
Title
</div>
</div>
The default value for vertical-align is baseline.
That's why the label appears at the baseline of the parent div. (see the image)
However when the title text is within the position div the height of the div is less - that's why it looks like the label stays on top. In reality it is still at the baseline of the parent div.
If you set vertical-align:top on the label it will appear at the top like it should
FIDDLE
You need to set the label float to left:
label {
float: left;
}
http://jsfiddle.net/w6kosc2v/2/
Here is a good article about how floating works: All About Floats which might explain a little more about floats to you.
Shortly said a float will float to the side you set it to; float: right floats to the right, while an image is floated to the right, all other inline elements tend to wrap around your float, a your <label> tag did. You can fix this by either removing the float: left from your .position or also by adding it to the label.
I hope this awnser makes it somewhat more clear.
I'm new in CSS, and I've got one question. I'd like to make a good simple form, and there is the following code:
<form>
<div class="row"><label for="name">Some text field</label><input type="text" name="name" /></div>
<div class="row"><label for="surname">Some another text field</label><input type="text" name="surname" /></div>
</form>
Some CSS code:
label {
float: left;
width: 230px;
text-align: right;
margin: 5px;
}
.row {
clear: left;
}
I copied and pasted this code from some book. I understand floating, clearing, but I don't understand why does "width" attribute work with label (because it inline element) and, in this case, why doesn't "width" work without "float"? Please, make me clear. Thanks
The Label element is defaulted to inline display mode.
Inline elements don't accept a width property, they will be rendered in the width of their content.
Floated elements on the other hand, are like inline-blocks. they will accept a width property.
By applying a float property to an element you are essentially changing it's display property to something like (but not exactly) inline-block.
label is inline element so it doesn't accept the width. To accept the width of any element it should be either inline-block or block element. And by setting float to any element behaves like this is an inline-block element and also another info for you position absolute or fixed also thinks as it is a block level element.
If you want to make label accept the width you should define label as inline-block.
label {
/*float: left;*/
display: inline-block;
width: 230px;
text-align: right;
margin: 5px;
}
I have a three-column page. Each column is a div with the following styles:
.Row1 {
text-align:center;
display:inline-block;
vertical-align:top;
padding:0.5%;
width:31.5%;
}
The HTML is as follows:
<div class="Row1">
<h2>Header</h2>
<br />
<form>
Form stuff goes here
</form>
</div>
My form consists of nothing more than a text field, a few radio buttons, and a submit button. Problem is, putting the form into the above code will give me a centered form, with the text field, radio buttons, and submit button also aligned in the center.
What I am trying to do is to just center the form itself (as a container), but keep everything aligned to the left.
I have tried wrapping the form in a div that is text-aligned centrally, and the form style set to left align, but that didnt work. The auto margin thing didn't work, either, whether it was on the wrapping div, or on the form, without the wrapping div (that is, an extra div. the Row1 div always remains).
EDIT: http://jsfiddle.net/pJwqe/
text-align is not a property to 'align' divs or forms; instead, as it says, it aligns text.
If you want to center a block-level element, you'll have to use margin. The default way to center a single element with margin is to set it , for example for a top-level container, to margin: 0px auto 0px;.
If you want a lay-out with 3 columns, I'd rather think you'd want something like this.
You can style the margins of your divs by setting margin-left or margin-right, then style the first/last element with advanced selectors (so you don't have a margin-left for your first element, or a margin-right for your last).
As you can see, the forms are now centered, but their content is not. (You need to give your forms a width, else they will fill the containing block by default if they have content).
NB: It might be relevant in the rest of your code, but in this test-case vertical-align does nothing.
Hope this fiddle link will work for you. I have done the following changes in your css
.midAlign form {
border: 1px solid #C2C3C0;
margin: 0 auto;
padding: 10px;
width: 155px;
}
I often find that I want an element to adjust its width to the size of the elements it contains. inline-block acheives this. However, I do NOT want the inline part of inline-block -- i.e., I still want the next inline-block element to appear below it.
Is there a simple way to achieve this in CSS? I know I can't always but <br> tags after the element in my HTML, but that's annoying.
You can do that with two elements:
<div>
<div class="element">
content...
</div>
</div>
With the CSS rule:
.element { display: inline-block; }
Treat .element as the "real" element that you're adjusting the width of. The enclosing <div> is just there to force each element into its own inline flow.
there are many solutions, the most common one is to use float
<div class="float">
<div class="child">here is content</div>
</div>
.float{float: left;}
if you want enforce that an element is in the new line you add clear: both (or left or right, depending on your needs)
please take into account, that display: inline-block does not work in IE7. The only problem with float is when you want this div to adjust to the width of a child AND to position it at the middle of the page horizontaly
one more note, remember that overflow: hidden property is your best friend whenever you encounter any issues with floated divs :)
Here is a fiddle that achieves what you describe: http://jsfiddle.net/PhilippeVay/VwCgJ/
It uses floating elements (thus width is ajusted to content), a class on the last element you want on a line and the clear property on the next element, with the help of the adjacent selector .rightmost + span
HTML:
<p>
<span>lorem</span>
<span class="rightmost">ipsum</span>
<span>third item: below please</span>
<span>fourth and last</span>
</p>
CSS:
span {
display: block;
float: left;
padding: 20px;
background: lightblue;
border-right: 2px solid white;
}
.rightmost {
background: red;
color: white;
}
.rightmost + span {
clear: both;
}
Inline content (as for inline-block) will occupy the whole width of its container and you've to force a new line with the br element.
On the other side, floating elements can be cleared (and with adjacent selector, you can clear the element after a particular one).
I have been using a lot of floats recently like this:
<div id="buttons">
<input type="button" id="btn1" value="Button One">
<input type="button" id="btn2" value="Button Two">
<input type="button" id="btn3" value="Button Three">
</div>
Sometimes I'll float a button to the right. Sometimes I'll float all of them to the right. This is where the problem starts. It really throws the flow off if I do that so I have to keep putting in these:
<div style="clear:both;"></div>
at the end. That throws off the layout if I'm not floating them all.
Is there a good solution to this?
Yes, use overflow: hidden on the container ie:
<style type="text/css">
#buttons { overflow: hidden; }
</style>
This is a big part of the CSS learning curve. When you float items, their containing element no longer takes the vertical height of the floated elements into account. There are a couple of solutions you could use to get around your dilemma.
Simply specify a hight for your #button container to the hight of your buttons:
#button { height: 30px; }
A fancier solution would be the clearfix hack. It's pretty bullet proof and will also do the trick without the need to add the extra markup and inline CSS.
#buttons:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
#buttons {
display: inline-block;
}
html[xmlns] #buttons {
display: block;
}
* html #buttons {
height: 1%;
}
As long as you define the overflow of the parent, it will clear all floats.
Use overflow:auto on the parent, and your good to go!
http://www.sitepoint.com/blogs/2005/02/26/simple-clearing-of-floats/
(I have heard this works using display:inherit as well, but have not tried.)
When you float items with CSS, they're removed from the natural flow state of the page.
If they're in a container DIV, that will shift the item out and have its parent not see where the child elements have gone. The container would then shrink back to boxing as much of the area as if the elements were not even there in the first place.
In order to cover for that, you have to specify overflow:hidden for the property of the container.
By default, it is set to visible and will allow anything to just "fall out" of the box if it has been floated as such.
Correct it by setting this in your CSS:
#buttons
{
overflow:hidden;
}
This will now constrain the floating elements to show within the context and confines of the parent DIV.
Usually, the best options are the clearfix method or the method of setting 'overflow: hidden' on the containing parent.
In your specific example you do have another option: you could not float any of the inputs at all, and set 'text-align: right' on #buttons
#buttons {
text-align: right;
}
While I rely on 'overflow: hidden' quite a bit, it is worth noting that if you're trying to position any elements outside (or partially outside) of the containing element that has 'overflow: hidden' set on it, the positioned elements will be clipped off at the boundary of the containing element. (this doesn't come up too often, but is a "gotcha" to look out for.)
You might also find the blog post "Lessons Learned Concerning the Clearfix CSS Hack" by Jeff Starr interesting.