I have two forms that I want to align. I was able to figure out how to align the height, but I can't get the width to work the way I want.
In my situation, I want the left box to fill in the remaining space. Using 'auto' just fills in the space to fit the contents. So instead of an expanding form, there is a gap between the two forms.
The reason I require this is I also have PHP around this form. There is a flag that dictates whether or not the second form shows up. So if there is only one form, I want it to expand the entire space. If there is two forms, I want them to share the space.
The way I thought of doing this would be to set the right form to a specific width and have the left form mold to whether or not anything else exists. I just can't figure out how to get the left form to expand itself without specifying an exact width.
I've included the HTML and CSS below as well as a JSFiddle reference.
HTML
<div class="section">
<div class="container">
<form>
<fieldset class="left">
<legend>PC Management</legend>
<div>
<input type='submit' value='Backup' />
<input type='submit' value='Backup' />
<input type='submit' value='Backup' />
</div>
</fieldset>
</form>
<form>
<fieldset class="right">
<legend>PC Management</legend>
<div>
</div>
</fieldset>
</form>
</div>
</div>
CSS
.section {
margin: 0 auto;
text-align: center
}
.container {
height: 100px;
width: 500px;
}
.container form, .container fieldset, .container input {
height: 100%;
display: inline;
}
.left {
width: auto;
float: left;
}
.right {
width: 40%;
float: right;
}
display: flex will get you the dynamic resizing that you want without needing any JavaScript:
.section {
margin: 0 auto;
text-align: center
}
.container {
height: 100px;
width: 500px;
display: flex;
}
.container form {
flex: 1 1 auto;
}
.container form, .container fieldset, .container input {
height: 100%;
}
<div class="section">
<div class="container">
<form class="left">
<fieldset>
<legend>PC Management</legend>
<div>
<input type='submit' value='Backup' />
<input type='submit' value='Backup' />
<input type='submit' value='Backup' />
</div>
</fieldset>
</form>
<form class="right">
<fieldset>
<legend>PC Management</legend>
<div>
</div>
</fieldset>
</form>
</div>
</div>
http://jsfiddle.net/eeLfx9d6/1/
Related
I have a div with two inputs inside. One text box and one button. I would like to set the div to be a certain width (say 30em) and have the text box fill 80% of it and the button the other 20%. The inputs should be side by side. The div is centered in the page.
<div class="container">
<input class="input" type="text" placeholder="Some text">
<input class="button" type="submit">
</div>
I have this scss so far
.container {
margin-left: auto;
margin-right: auto;
width: 30em;
.input {
width: 80%;
}
.button {
width: 20%;
}
}
So far the text box takes up 80% but floats in the middle and the button under it. How can I line them up side by side to fill the div?
Those elements are inline by default, so the white space between them is preserved, which makes their overall width 20% + 80% + [a space], which is > 100%.
You can...
remove the white space between them
use display: flex on the parent
float the inputs.
* {margin:0;padding:0;box-sizing:border-box;}
.container {
margin-left: auto;
margin-right: auto;
width: 30em;
}
.flex {
display: flex;
}
.input {
width: 80%;
}
.button {
width: 20%;
}
.float {
overflow: auto;
}
.float input {
float: left;
}
<div class="container">
<input class="input" type="text" placeholder="Some text"><input class="button" type="submit">
</div>
<div class="container flex">
<input class="input" type="text" placeholder="Some text">
<input class="button" type="submit">
</div>
<div class="container float">
<input class="input" type="text" placeholder="Some text">
<input class="button" type="submit">
</div>
Consider the following.
2 DIVS - the left one of known width, the right one of unknown width.
We can make the right-hand side fill the remaining space, however if I exchange the right-hand DIV to a textbox, it then does not fill the space, but wraps below the left-hand div.
Here's a fiddle: example
<div>
<div id="left">
left
</div>
<input type="textbox" id="right">
right
</input>
</div>
#left {
float:left;
width:180px;
background-color:#ff0000;
}
#right {
width: 100%;
background-color:#00FF00;
}
I'm confused - any advice?
Still not behaving as it should!
New fiddle here: updated fiddle
JSFiddle
Inputs are inline bydefault and only the
Block level elements can aquire the remaining space left after a floating element. So you should change the display property for input to block i.e. display:block
#left {
float:left;
width:180px;
background-color:#ff0000;
}
#right {
display:block;
background-color:#00FF00;
}
<div>
<div id="left">
left
</div>
<input type="textbox" value="right" id="right"/>
</div>
EDIT: http://jsfiddle.net/naeemshaikh27/MHeqG/1522/ using Calc.
Using Calc
If You wanted to set the width of only a single element, you may want to look at the calc() option.
Something like:
width: calc(100% - width px);
in which could be incorporated into most projects nowadays, as you can see from its browser support.
You could also make use of the auto width:
.secondElement{
width:auto;
}
to fill the space left
Have a look here...
* {
margin: 0;
padding: 0;
}
div {
display: inline-block;
width: 50%;
background: blue;
}
input {
width: 50%;
display: inline-block;
}
.fix {
border: none;
background: gray;
}
.now {
width: 49.5%;
}
.nowNew {
width: auto;
}
<div>Div on left</div>
<input type="text" placeholder="text here" />
<br/>Notice the lengths aren't the same? Yet both are defined as 50%?
<br/><br/>
<br/>That's due to the border around the input!
<br/><br/><br/>
<div>Div on left</div><input class="fix" type="text" placeholder="text here" />
<br/><br/>
<br/>To fix 'stuff' like this, I feel the general rule in web dev. is to aim to make it 99.9% instead:
<br/><br/><br/>
<div class="now">Div on left</div><input class="now" type="text" placeholder="text here" />
<br/><br/>
<br/>Or make the input width auto:
<br/><br/><br/>
<div>Div on left</div>
<input class="nowNew" type="text" placeholder="text here" />
You can accomplish this using display: table and display: table-cell.
JSFIDDLE
HTML:
<div class="container">
<div id="left">
left
</div>
<input type="textbox" value="right" id="right" />
</div>
CSS:
#left {
display: table-cell;
width: 180px;
background-color:#ff0000;
}
#right {
display: table-cell;
width: 100%;
background-color:#00FF00;
}
.container {
display: table;
width: 100%;
}
So i wanted to make my website fluid. So i started with my wrapper and header. The css:
#wrapper {
max-width:1600px;
width:100%;
margin:0 auto;
}
#header {
margin: 0 auto;
width: 100%;
height: 7.5%;
background-color: #0066FF;
display: table-cell;
vertical-align: middle;
}
The HTML:
<div id="wrapper">
<div id="header">
<div id="logo_wrapper">
<span class="logonaam1">O</span><span class="logonaam2">b</span><span class="logonaam1">l</span><span class="logonaam2">e</span><span class="logonaam1">c</span><span class="logonaam2">t</span><span class="logonaam1">a</span><span class="logonaam2">r</span><span class="logonaam1">e</span>
</div>
<form action="login.php" method="post">
<div id="aanmeldform_submit">
<input type="submit" name = "submit_login" value="Aanmelden" id="submit_knop" />
</div>
<div id="aanmeldform_wachtwoord">
<input type="password" name ="password" value="" id="aanmeld_knop" required placeholder="Voer je wachtwoord in" />
</div>
<div id="aanmeldform_email">
<input type="email" name="email" value="" id="aanmeld_knop" required placeholder="Voer je e-mail in" autofocus/>
</div>
</form>
</div>
The header should span the entire width of the screen, just like the wrapper. But for some reason, when i set the width of the header also to 100% the header only is like 1/3 of the screen. When i change the width of my header to around 1 - 10 % it DOES span the whole width for some reason. Anyone know what is causing this? Also included a fiddle for better understanding: http://jsfiddle.net/4J7JJ/
When you set the header in the fiddle to 1% you can see it does span the entire width for some reason..
Thanks in advance!
Try change display
#header {
margin: 0 auto;
width: 100%;
height: 7.5%;
background-color: #0066FF;
display: inline-table; /* that fix the problem */
vertical-align: middle;
}
Task: Make text box 100% width but allow enough room for button.
Problem: Button appears on next line and text box exceeds width of its container.
<div class="field">
<input type="text" name="my-field" />
<input type="button" id="my-button" value="Add +" />
</div>
.field {
margin-right: -70px;
width: 100%;
}
.field input[type=text] {
display: block;
float: left;
margin-right: 70px;
}
.field input[type=button] {
display: block;
float: right;
}
My primary layout uses the following trick to achieve flexible width with fixed sidebar, but for some reason this is not working on the above.
<div class="outer-wrap">
<div class="content">
...
</div>
<div class="sidebar">
...
</div>
</div>
.outer-wrap {
margin-right: -300px;
width: 100%;
}
.content {
float: left;
margin-right: 300px;
}
.sidebar {
float: right;
}
What mistake am I making here?
You have to screw with the HTML a bit, but otherwise this works perfectly in IE7+ and all modern browsers.
See: http://jsfiddle.net/thirtydot/25bZC/
CSS:
.field > span {
display: block;
overflow: hidden;
padding-right: 10px
}
.field input[type=text] {
width: 100%
}
.field input[type=button] {
float: right
}
HTML:
<div class="field">
<input type="button" id="my-button" value="Add +" />
<span><input type="text" name="my-field" /></span>
</div>
To pull this off you must ensure that the element which you are floating right comes before the one floating left. Like this
<div class="field">
<input type="button" id="my-button" value="Add +" />
<input type="text" name="my-field" />
</div>
try giving fixed width to
field input[type=text]
and
.field input[type=button]
I don't know where to begin learning about doing such a layout without tables, or at most one two column table as a simple container. Where should I start?
So after seeing your comment about you looking for a layout with two columns of fieldsets, I went and wrote up this one without the use of tables:
<html>
<head>
<title>Two Column Fieldsets</title>
<style>
html, body
{
background: #156;
text-align: center;
}
#container
{
background: #FFF;
border: 1px #222 solid;
height: 600px;
margin: 0 auto;
text-align: left;
width: 700px;
}
#container form
{
margin: 0 auto;
}
label
{
float: left;
width: 50px;
}
.singleRow
{
float: left;
width: 322px;
}
</style>
</head>
<body>
<div id="container">
<form action="">
<fieldset class="singleRow">
<label for="1">Text:</label>
<input id="1" type="text" />
<input type="submit" value="submit" />
</fieldset>
<fieldset class="singleRow">
<label for="2">Text:</label>
<input id="2" type="text" />
<input type="submit" value="submit" />
</fieldset>
</form>
</div>
</body>
</html>
Most of the styles are just to make it line up nice and centered and to provide some contrast. The way to get two columns is to float both fieldsets left. Each fieldset has a class called .singleRow, whose only important style is float: left; (the width is in there to make them line up nicely). By floating both of the elements (in this case fieldsets, but they could be divs lis, any element) left, and making sure their combined widths are less than the width of their container, you can achieve a nice two-column layout.
Hope this helps.