Running into a clearfix type issue - html

So Two Divs Walk Into A Form
Looks like this:
<form>
<div id="top">
<label>Gimme a Number:<input type="number" /></label><br />
<label>Gimme a Word:<input type="textarea" /></label><br />
<label>Now tell me how you feel about HTML forms: <textarea rows="4" cols="50"></textarea></label><br />
</div>
<div id="bottom">
<label>Gimme a Number:<input type="number" /></label><br />
<label>Gimme a Word:<input type="textarea" /></label><br />
<label>Now tell me how you feel about HTML forms: <textarea rows="4" cols="50"></textarea></label><br />
</div>
</form>
There's some css positioning all the form fields absolute and left, a fiddle of the principal is here (Step 1)
So I fixed the first problem by adding additional line breaks (fiddle of step 2).
This is not an ideal solution for two reasons. Extra elements in the DOM for one, and um... well just look at it. The textarea still overflows into the bottom div.
I found a temporary hack when the miscellaneous clearfix answers I tried didn't work (Mostly from other SO questions) by inserting:
<hr class="clearfix" /><br />
in between the two, and giving the hr visibility:hidden in the CSS.
That made it work on my page, but as you can see here (Step 3) it doesn't actually work. In the form on my page the div's have transparent backgrounds, and it puts them far enough apart that the users are happy, and since this is an intranet order form (used from a phone no less) and not a SV startup webapp, I'm kind of content to leave it production for now, but in the end, what was the right way to do this?
How should I have gotten the div.top to encompass the full content, including the textarea? I've tried a lot of the "go to" solutions for elements with float, but as expected, they don't work since the textarea's aren't floated.
Ideally I want to ditch the extra <br>s, and have the <div>s actually contain their content without either manually positioning every single element or adding a ton of complexity to the page. Any ideas are appreciated.

A nice trick for what you're looking for is using a Html Description List.
For example, you can write this:
<dl id="top">
<dt>Gimme a Number:</dt>
<dd><input type="number" /></dd>
<dt>Gimme a Word:</dt>
<dd><input type="textarea" /></dd>
<dt>Now tell me how you feel about HTML forms:</dt>
<dd><textarea rows="4" cols="50"></textarea></dd>
</dl>
<dl id="bottom">
<dt>Gimme a Number:</dt>
<dd><input type="number" /></dd>
<dt>Gimme a Word:</dt>
<dd><input type="textarea" /></dd>
<dt>Now tell me how you feel about HTML forms:</dt>
<dd><textarea rows="4" cols="50"></textarea></dd>
</dl>
jsfiddle
And here is a bit more css to make it look like your fiddle:
#top{
color:white;
font-weight:700;
background-color:blue;
}
#bottom{
font-weight:700;
background-color:red;
}
dl {
padding: 0.5em;
height: 100%;
display: table;
}
dt {
float: left;
clear: left;
}
dd {
float: right;
clear: right;
}
fiddle
Edit
About label issue being non-superfluous - there is an easy solution using the "for" attribute. Here is an example:
<dt><label for="number1">Gimme a Number:</label></dt>
<dd><input id="number1" type="number" /></dd>
updated fiddle
Your original use of label:
<label>Gimme a Number:<input type="number" /></label><br />
will couple the two tags together and shouldn't be used. Plus talking about semantics, should the input really be part of the label?
Taking in acount your comment about non-semantic use of dd / dt, you can easily swap between the dt and dd, like this fiddle. Here is the updated example:
<dd><label for="number1">Gimme a Number:</label></dd>
<dt><input id="number1" type="number" /></dt>
Now we have a semantic HTML where the CSS takes care of the design choices.
Edit 2
new fiddle - no need in height: 100%; on dl

Related

Add label on top of two textarea [CSS]

I'm beginner on CSS.
I want to add a label on the top of the two textarea. When I tried Label tag (as shown below) , the second textarea ends up beeing under the first textarea( basically I want them side by side ).
Here is my JSFIDDLE
<label for="Coords">Past Coordinates here: </label>
<textarea id="Coords" cols="35" rows="20"></textarea>
<label for="Time">Time: </label>
<textarea id="Time" cols="25" rows="20"></textarea>
My two textarea are wrapped to put them on the right.
Can anyone help me ?
Thanks!
use this. http://fiddle.jshell.net/sherali/agr3a07m/209/show/
UPDATED:
Tip: remove your cols and rows from textarea. you should define from css(thorugh width, height)
in HTML:
<div class="box">
<label for="Coords">Title of Coords: </label>
<textarea id="Coords"></textarea>
</div>
<div class="box">
<label for="Time">Title of Time: </label>
<textarea id="Time"></textarea>
</div>
in CSS:
textarea#Coords{
width:270px;
height:300px;
}
textarea#Time{
width:215px;
height:300px;
}
label {display:block;}
.box{
display:inline-block;
}
Probably the simplest way is to wrap each label/textarea pair in a div and set display: inline-block; on each div. Though I'd recommend checking out some frameworks with grid systems, like Bootstrap, that abstract and simplify this.

Float left causes my input label to be on top instead v-align middle

I'm sure there are a lot of people with this problem, but I can't find a proper solution. That is basically is the problem. I've got a form with two pairs of label and field.
HTML:
<label for="Account">Inlognaam:</label>
<input class="field" id="Account" name="Account" type="
<br />
<label for="Wachtwoord">Wachtwoord:</label>
<input class="field" id="Wachtwoord" name="Wachtwoord" type="password" />
CSS:
label {
width: 150px;
float: left;
text-align: left;
}
So the problem is: when I don't use the 'float:left;' the input field will be not nice structured. BUT the label is going top-aligned. How can this be fixed?
An example is visible here: http://jsfiddle.net/ptKEh/9/ (comment float:left; to see what I mean)
EDIT::
Another thing... The input fields are in Chrome correct but in IE9 (9.0.8) the second field is a little shorter.
instead of floating the labels just use display: inline-block;
it will preserve the vertical alignment and it works even on IE6 and 7
I would recommend using padding to move the text down to be inline. This will only work with 1 line of text for the label and is cross browser capable.
I have put together an example jsfiddle http://jsfiddle.net/ptKEh/11/

Differents way to structure html inputs and labels in a form

I'm wondering what are the best solutions to structure a html form with labels and inputs.
I used to do this with float: left to the label and float: right for the inputs. And each line is surround with a block clear: both.
But i don't think these CSS property were made for something like that..
So what are the others solutions ? Tables ?
Well it really depends on what you want the form to look like. For example, if you want a clear grid with borders I recommend using a table.
To duplicate what you have, you can do this:
<label for='textbox'>Label</label><input type='text' id='textbox' />
And then this css:
label { display: inline-block; width: 100px; }
This will make the label stay on the same line as in input element but will push it the appropriate distance.
Personally, I try to avoid using floats to align elements. I would rather use position absolute and set left or right and top or bottom. To me floating is like asking the browser to do it for you, and maybe some browsers (cough ie cough) will decide to draw it a little differently.
Form markup and CSS will always be a personal choice. Sure, there are some rights and wrongs semantically and technically from a CSS point of view, but there certainly isn't one (or even a few) "right" techniques.
My personal preference is to float the label left and contain my inputs inside lists, which I personally consider more semantic than a div or p tag.
HTML:
<form>
<fieldset>
<ol>
<li>
<label for="input1">Label 1</label>
<input type="text" name="input1" id="input1">
</li>
<li>
<label for="input2">Label 2</label>
<input type="text" name="input2" id="input2">
</li>
<li>
<label for="input3">Label 3</label>
<input type="text" name="input3" id="input3">
</li>
</ol>
<button type="submit">Submit</button>
</fieldset>
</form>
CSS:
li {
clear: left;
margin-bottom: 10px;
}
label {
float: left; /* You could use "display: inline-block;" instead. */
margin-right: 10px;
width: 80px;
}
tables is also a solution.
also , Div with 2 inner divs( left and right)
or 1 div with both elements with float:left with margin-left.

having trouble styling the login username form tag

See this jsfiddle.
I want to style the script login form with username and password to the right of the unordered list.
Wrap the a container around both the UL and the form, and assign it a fixed width or a min-width to prevent a float drop (form dropping down below the ul and to the left).
http://jsfiddle.net/6e86p/
Adjust the form width to ensure the 2 fields line up vertically (as in my jsfiddle), or horizontally. If you need to play with the form width, you may want to wrap each label/input pair in a div of fixed with to ensure each label lines up horizontally with its input and doesn't drop below.
Use control + Mouse wheel to make the result pane smaller. You'll see that the login box is in the correct place. No changes needed!
First, wrap your form elements (I used a dl for this)
<form action="/bin/process.php" method="post" enctype="multipart/form-data">
<dl>
<dt>
<label>Username:</label>
</dt>
<dd>
<input type="text" value="" />
</dd>
<dt>
<label>Password:</label>
</dt>
<dd>
<input type="password" value="" />
</dd>
<dt></dt>
<dd>
<input type="submit" value="Login" />
</dd>
</dl>
</form>
Then I added the following CSS
dt { width: 50px; float: left; }
dd { margin-left: 100px; }
The result gives you a stacked Username/Password box to the right. See the results here, you will probably need to expand the result section for proper viewing.

Forms: Does your css accommodate your markup or vice versa?

Regarding html forms, a very common markup pattern is:
<form ...>
<p>
<label>Name:</label>
<input .../>
</p>
<p>
<label>Birthdate:</label>
<input .../>
</p>
..
<input type=submit/>
</form>
How much markup (classes, etc.) do you typically provide to allow for the most flexible visual formatting of the form? That is, how much markup do you add to help with your css selectors and do you use generic selectors?
<form ...>
<p class='name'>
<label>Name:</label>
<input .../>
</p>
<p class='birthdate'>
<label>Birthdate:</label>
<input .../>
</p>
..
<input type=submit/>
</form>
vs.
<form class='person' ...>
<p class='name string'>
<label>Name:</label>
<input .../>
</p>
<p class='birthdate date'>
<label>Birthdate:</label>
<input .../>
</p>
..
<input type=submit/>
</form>
In the second case, adding generic types ("date") straight from the database can make it more easy to consistently format date fields. Wrapping a grouping ("person") to show the model from which the fields come, can help too. (Or I could have used an internal DIV.) Yet, to increase css reuse, I find myself adding extra markup. In some books I've read I hear that the less markup, the better (and that line can be very gray though it rings true to me). For example, I could very well have used the markup from one of the prior blocks and added a lot more selectors to the css.
What are your principles for deciding just how much markup makes sense? Or how much to put on the css side?
Also, I know that I can select against the name of the input, but since that's a nested element I lose my ability to control formatting from the outer wrapper ("p") which is usually where I want that extra control.
I tend to use definition list tags to style my forms.
<form>
<dl>
<dt><label for="name">Name:</label></dt>
<dd><input name="name" /></dd>
<dt><label for="birthdate">Birthdate:</label></dt>
<dd><input name="birthdate" /></dd>
...
</dl>
</form>
I also use the following CSS:
FORM DT {
clear:both;
width:33%;
float:left;
text-align:right;
}
FORM DD {
float:left;
width:66%;
margin:0 0 0.5em 0.25em;
}
More information here: http://www.clagnut.com/blog/241/
It's a lot of markup, but the effect is consistent and effective.
Another arguably acceptable method of styling forms is to use tables. Just think of the form as "interactive tabular data."
I wouldn't use a <p> tag to group a label and its field, since it's not a paragraph. If you have no other use for <fieldset> you could use one per "row". If you have three inputs for birthday then a fieldset is totally appropriate.
A definition list as Gavin suggested isn't a bad idea but it does seem like unnecessary markup - you can just style the labels and inputs to the right widths and float them.
Adding wrapper classes is also perfectly valid - remember that you don't have to use them in CSS, they add a semantic layer regardless. There may even be a microformat you can use in some cases.
You can also use attribute selectors to style inputs nicely:
input[type="text"], input[type="password"] {
border: 1px solid #ccc;
background: #fff;
}
input[type="submit"], input[type="reset"] {
border: 1px solid #666;
background: #ccc;
}
I do try and keep html markup to a minimum.
HTML forms are the hardest thing to keep html and css to a minimum, as it is very hard to target all the various inputs across all browsers without adding classes to them, such as Textbox to textboxes etc.
If all your forms for that site use simple textboxes and not much of anything else, the minimal mark-up approach works just fine. However controls with complex mark-up such as the telerik RAD controls don't play with simple mark-up and often extra markup and classes are needed.
These small tricks add mark-up, but also make the css much cleaner and will no doubt making styling such elements much easier.
For other general html/css, I tend to use as few classes as possible, such as
.Menu {}
.Menu li {}
.Menu li a {}
This sort of pattern can be re-used a lot for repeated data, and templates can be made and designed with very little html mark-up.
Sometimes its un-avoidable adding classes and whatnot, but I think if your generally thinking about both css and html you should end up with slick markup.
From site to site, I rarely re-use CSS. Its so quick and easy knocking up styles for whatever you wish, re-designing an existing skin to fit a new site is often not worth it IMO.
Mainly with CSS I tend to take the knowledge i've learnt from previous sites and apply it to the new sites, to make coding for all browsers easy :)
After many years, I've arrived at:
<fieldset>
<div>
<label for="Whatever">A text field</label>
<input type="text" id="Whatever" />
</div>
<div class="required">
<label for="RequiredField">A required field</label>
<input type="text" id="RequiredField" />
</div>
<div class="stretch">
<label for="LongField">A long field (stretched across 100% form width)</label>
<input type="text" id="LongField" />
</div>
<div class="cmd">
<button type="submit">Do whatever</button>
</div>
<fieldset>
Additionally, I have two CSS classes that I can apply:
fieldset div {
clear: both;
}
fieldset.block label {
display: block;
font-weight: bold; /* labels above fields */
}
fieldset.aligned label:first-child {
width: 20%;
float: left;
}
fieldset.block .stretch input,
fieldset.block .stretch textarea,
fieldset.block .stretch select {
width: 100%;
}
fieldset.aligned .stretch input,
fieldset.aligned .stretch textarea,
fieldset.aligned .stretch select {
width: 79%; /* leave space for the label */
}
Personally I just do:
<form>
<label for="foo">Foo</label>
<input type="text" id="foo" name="foo" />
<br />
<label for="foo2" class="block">Foo 2</label>
<textarea id="foo2" name="foo2"></textarea>
<br />
Then for css it depends whether or not I want the element to be inline with it or not
form label.block{
display: block;
}
Or you can block + float them like #DisgruntledGoat wrote. (I really hate extra markup)