please have a look at this simple jsfiddle:
--> JSFiddle
html:
<form>
<p>
<input id="I1" value="above left" size="40" />
<input id="I2" value="above right" size="10" />
</p>
<p>
<input id="I3" value="below right" size="10" />
<input id="I4" value="below left" size="20" />
</p>
</form>
css:
form {
overflow: hidden;
border: 3px solid red;
padding: 5px;
}
#I1,
#I2 {
background-color: orange;
float: left;
}
#I3,
#I4 {
background-color: yellow;
float: right;
}
what I want is the red border (= form width) auto-adjust to the width of the two top inputs (+ padding, of course).
I would then expect, that the second line inputs appear right-aligned below the first, so "above right" would be exactly above "below right", and "below left" beeing right left of it, like so:
11111111111111111111 2222222
4444444 3333333
Can this be done without tables?
Thx, Armin.
It most certainly can be done without tables :). You could set, for form:
form {
display: inline-block;
padding: ...;
}
This will prevent the form from expanding to the entire width of the screen. It will be as wide as the elements inside it, or, if you manually define its width, as wide as specified.
If you would like no elements to appear on either side of the form within the form's containing element, you can wrap it in a div or other block level element:
<div>
<form>
<!-- Rest of content -->
</form>
</div>
In order to remove the spacing above and below each p, as per your comment, remove their margin-top and margin-bottom, as they have a margin by default:
p {
margin-bottom: 0;
margin-top: 0;
}
I've organized and cleaned-up your code. The way to make the div adjust to its content is by using display: inline-block. I've also fixed the padding issue so now the entire content is padded equally to 5px.
JsFiddle
HTML:
<form>
<p>
<input id="I1" value="above left" size="40" />
<input id="I2" value="above right" size="10" />
</p>
<p>
<input id="I3" value="below right" size="10" />
<input id="I4" value="below left" size="20" />
</p>
</form>
CSS:
.border {
border: 3px solid red;
padding: 5px;
display: inline-block;
}
#I1 {
background-color: orange;
}
#I2 {
background-color: yellow;
}
.top-block,
.bottom-block {
width: 100%;
}
I have update this answer. Probably this is what you need, but there are other options but I don't think is the scope of this question.
CSS Code:
form {
overflow: hidden;
border: 3px solid red;
padding: 5px;
width: 400px; // limites de form size
}
#I1,
#I2 {
position: absolute;
background-color: orange;
}
#I3,
#I4 {
position: absolute;
background-color: yellow;
}
p {
display: block;
border: 1px solid blue;
position: relative;
height: 2em;
}
#I1,
#I4 {
right: 25%;
}
#I2,
#I3 {
right: 0;
}
See here the full code: https://jsfiddle.net/uv8yrrav/18/
hope this helps!
Related
I have a 'row' div that contains, essentially, a label, then a text input, then a hyperlink. My problem here is I wish the width of the input to always fill the available width between the label and the hyperlink. I have tried numerous approaches, but with only varied forms of failure. I suspect maybe I can use some sort of margin voodoo to make the a tag not absolute, and always have the input neatly bounded by the lable and the a, but I am now at a complete loss as to what to try next.
Here is my HTML:
<fieldset id="ctl00_cph_previousContacts" class="previousContactsArea">
<legend>Previous Contacts</legend>
<div class="email-row">
<label for="ctl00_cph_addressTo">To</label>
<input name="ctl00$cph$addressTo" type="text" id="ctl00_cph_addressTo" class="email-address text" />
Add...
<span id="ctl00_cph_addressToValidator" dynamic="True" style="display:none;"></span>
</div>
</fieldset>
And here is the CSS:
.manualNotification #recipientsTab .email-row {
margin-top: 5px;
margin-bottom: 5px;
}
.manualNotification #recipientsTab .email-row label {
display: inline-block;
width: 50px;
text-align: center;
}
.manualNotification #recipientsTab .email-row a {
position: absolute;
width: 50px;
right: 10px;
margin-left: 10px;
}
Flexbox can do that
.email-row {
margin-top: 5px;
margin-bottom: 5px;
display: flex;
}
.email-row label {
flex: 0 0 50px;
text-align: center;
}
.email-row input {
flex: 1;
}
.email-row a {
flex: 0 0 50px;
margin-left: 10px;
}
<fieldset id="ctl00_cph_previousContacts" class="previousContactsArea">
<legend>Previous Contacts</legend>
<div class="email-row">
<label for="ctl00_cph_addressTo">To</label>
<input name="ctl00$cph$addressTo" type="text" id="ctl00_cph_addressTo" class="email-address text" />
Add...
<span id="ctl00_cph_addressToValidator" dynamic="True" style="display:none;"></span>
</div>
</fieldset>
JSfiddle Demo
please add flowing css end of your css:
div.email-row{max-width: 30%; float:left;margin-right:20px}
see the following links for more details:
http://www.cssdesk.com/gDzpd
I have the following fiddle consisting of this html markup and CSS:
.embers-info{
margin-top: 30px;
border: 1px solid black;
width: 600px;
padding: 20px 0px 30px 0px;
}
.embers-total{
width: 80px;
}
.embers-email{
margin-left: 20px;
}
.embers-phone{
margin: 10px 0px 0px 20px;
}
.embers-notes{
margin: 20px 0px 0px 20px;
}
.embers-pickup{
margin-left: 20px;
}
.embers-actions{
margin-left: 20px;
margin-top: 20px;
}
.af-commerce{
float: right;
}
.embers-checkout-header{
padding: 3px 10px 12px 5px;
}
.embers-user-info{
width: 300px;
border: 4px solid blue;
padding-bottom: 50px;
}
.embers-delivery-info{
width: 300px;
border: 4px solid red;
padding-bottom: 50px;
}
<div class='embers-info'>
<div class='embers-user-info'>
<div class='embers-email'>
email:<br />
<input type="text" size="30" name="email" id="email" />
</div>
<div class='embers-phone'>
phone number:<br />
<input type="text" size="30" name="phone_number" id="phone_number" />
</div>
<div class='embers-notes'>
notes:<br />
<textarea id='notes' cols='30' rows='5'></textarea>
</div>
<div class='embers-pickup'>
<br />pickup times:<br />
November 21 10am:<input type="radio" size="30" class="pickup" name="pickup" value="time1" /> <br />
November 21 noon:<input type="radio" size="30" class="pickup" name="pickup" value="time2" /><br />
November 21 2pm:<input type="radio" size="30" class="pickup" name="pickup" value="time3" /><br />
November 21 4pm:<input type="radio" size="30" class="pickup" name="pickup" value="time4" /><br />
November 22 8am:<input type="radio" size="30" class="pickup" name="pickup" value="time4" /><br />
</div>
<div class='embers-actions'>
<button id='save_order'>save order</button>
<button id="customButton">Purchase</button>
</div>
</div>
<div class='embers-delivery-info'>
here i am
</div>
</div>
and would like to get the blue and red div side-by-side. How would I do this?
I tried to use:
display:inline-block;
but that didn't matter.
Someone has mentioned flexbox, which while a perfectly valid solution may not be what you need depending on your minimum browser requirements as flexbox is still not as well supported as one would like. When aligning elements side by side using traditional methods you have a couple of options, display: inline-block and float.
display: inline-block : http://jsfiddle.net/bzkt2hcx/2/
.embers-info {
display: inline-block;
}
.embers-info > div {
display: inline-block;
vertical-align: top;
}
Notes:
inline-block elements may be rendered with spacing on some browsers, most notably chrome. To fix this set we can set font-size: 0 on the parent container and then reset the font-size on the children.
float: left/right: http://jsfiddle.net/bzkt2hcx/3/
.embers-info {
overflow: auto;
}
.embers-info > div {
float: left;
}
Notes: floated elements are taken out of the flow and do not affect their parents heights, causing sizing and layout issues. To alleviate this we need to apply a clearfix on the parent, the simplest of which is setting overflow: auto on the parent container.
You could use flexbox by adding display: flex to the containing div. This will default in a flex-direction of row, which will put them next to each other:
http://jsfiddle.net/bzkt2hcx/1/
More reading for laying out the children in a flexbox container:
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Using_CSS_flexible_boxes
I have created 4 column grid in my html form. i want to have last label and textbox field to be align right side of the page.
I have tried using float:right property but it doesn't seem to work
In fiddle example it is not in single line.
.row {
margin: 10px;
}
.elements {
padding: 10px;
margin-left: 50px;
margin-top: 10px;
border-bottom: ridge;
border-bottom-color: #1f6a9a;
padding-left: 0;
}
.field {
font-size: 15px;
color: #b6d6ed;
}
label {
display: inline-block;
}
input {
background-color: transparent;
border: 0px solid;
height: 25px;
width: 60px;
color: #b6d6ed;
text-align: center;
}
/* I Tried Below Line to Right Align */
.row > .elements:nth-child(2) {
float:right;
}
<div class="row">
<span class="elements">
<label class="field" for="Title">Title</label>
<input id="Title" name="Title" type="text">
</span>
<span class="elements">
<label class="field" for="DateOfBirth">Date of Birth</label>
<input id="DateOfBirth" name="DateOfBirth" type="text" value="" class="hasDatepicker">
</span>
</div>
jsfiddle
Float the first span to the left:
.row > .elements:first-child {
float: left;
}
See it here: http://jsfiddle.net/3DtqB/2/
You have to put the elements class into a <div class=".."></div> and add an CSS command
.elements {
float: right;
margin-top: -[x]px
}
Also you should use two id's instead of a class elements like left_box and right_box and add the commands to the right box.
Simple fix, just add white-space:nowrap; to the .elements class.
I'm trying to insert a search field in my header (black zone) but doesn't work. I want the search field inline with "SimpleCMS"...
See this screenshot to understand:
I want it on the same line as the header text...
There's my HTML code:
<div id="header"><h1><?php echo($header_text); ?></h1>
<div style="float: right;">
<form action="search.php" method="get">
<input type="text" name="q" id="q" value="Search..." />
<input type="submit" value="Search" />
</form>
</div>
</div>
And my CSS:
#header
{
padding: 5px 10px;
background: #000;
color: #FFF;
text-align: left;
}
The problem is that you use a <h1> element. This will span over the whole width (see here) of the top so that every other element will be placed below it. Use a <span> instead and style it according to your needs. Using position-absolute as alpaca lips nao suggests might work as well.
Update: Use position: absolute;
#header
{
padding: 5px 10px;
background: #000;
color: #FFF;
text-align: left;
position: relative;
}
#header div form {
position: absolute;
top: 75px;
right: 25px;
}
Im trying to get away from using the html TABLE tag, but cant figure out how to build, what I want it to look like. I have made a screenshot of me using the table tag,
How would I do this with divs or/and spans etc, and still retain the vertical alignment of the labels (firstname, lastname in this example)?
(font size and color etc is of course irrelevant here)
alt text http://img522.imageshack.us/img522/7857/forme.jpg
thankful for any input,
modano
It's good that you don't want to use the table tag for layout. The thing to keep in mind when switching is to try to make the HTML as semantical as possible. What this means might vary, since there are no real strict rules, but it could look something along these lines:
<form [..]>
<ul>
<li class="hasError">
<em class="feedback">error message here</em>
<div class="attribute">
<label for="firstName">First name:</label>
<em>(required)</em>
</div>
<div class="input">
<input type="text" name="firstName" id="firstName" />
<em class="description">optional description here</em>
</div>
<span class="clearBoth" />
</li>
<li>
<em class="feedback" />
<div class="attribute">
<label for="firstName">Last name:</label>
<em>(required)</em>
</div>
<div class="input">
<input type="text" name="lastName" id="firstName" />
<em class="description">optional description here</em>
</div>
<span class="clearBoth" />
</li>
</ul>
</form>
This achieves the following:
By placing the error feedback message above the divs, you can make an arbitrarily long error message without losing alignment
Each input element (and label) is kept in a single list item, thus grouping them logically. It also reads something like the following in a screen reader: "Form. List of two items. Label [...]". This gives the user a hint of that the form contains two inputs.
By adding the hasError class to a list item, you can easily target the descendant elements with CSS for error specific styling.
A sample CSS file could look something like (note that this is untested):
form li {
width: 300px;
}
form li.hasErrors {
width: 298px;
border: 1px red;
background-color: #C55;
}
form .attribute {
float: left;
clear: left;
width: 60px;
}
form .input {
float: right;
clear: none;
width: 240px;
}
form .feedback {
display: block;
padding-left: 50px;
color: red;
}
form .description {
display: block;
clear: both;
color: #888;
}
.clearBoth { display: block; clear: both; }
A very very good tutorial on creating accessible HTML/CSS forms can be found on A list Apart: Prettier Accessible Forms
Generally a fantastic site for information on how to create good, clean and accessible websites.
Simply give your labels a specific width; this will ensure your fields line up. You can also float your labels and inputs to easily break them into rows. Here's a minimal example:
<style type="text/css">
form { overflow: auto; position: relative; }
input { float: left; }
label { clear: left; float: left; width: 10em; }
</style>
<form>
<label>Field 1</label><input/>
<label>Field 2</label><input/>
<label>Field 3</label><input/>
</form>
I am no CSS expert, but this should get you started. Of course the styles should be in an external style sheet.
<html>
<head>
<style>
html {
font-size: 76%;
}
body {
font-size: 1.0em;
font-family: verdana;
}
div.input {
border: 1px solid white;
clear: left;
width: 25em;
height: 5em;
padding: 2px;
margin-bottom: 1.0em;
}
div.error {
border: 1px solid red;
}
div.label {
float: left;
width: 7em;
}
div.field {
float: left;
}
div.errormessage {
color: red;
}
div.description {
color: #bbb;
}
input.text {
width: 13em;
}
</style>
</head>
<body>
<form>
<div class="input error">
<div class="label">
<div> </div>
<label>First name:<br>(required)</label>
</div>
<div class="field">
<div class="errormessage">error message here</div>
<input type="text" name="FirstName" class="text">
<div class="description">optional description here</div>
</div>
</div>
<div class="input">
<div class="label">
<div> </div>
<label>Last name:<br>(required)</label>
</div>
<div class="field">
<div class="errormessage"> </div>
<input type="text" name="LastName" class="text">
<div class="description">optional description here</div>
</div>
</div>
</form>
</body>
</html>