CSS - How to line up buttons? - css

I am trying to create a form with multiple rows. Each row has an optional input field followed by a mandatory button. The buttons should line up vertically - something like this:
_____________ _______________
| input 1 | | button 1 |
|___________| |_____________|
_______________
| button 2 |
|_____________|
I tried to float the button left with a fixed left margin, but doing so moves the input field to the right of the button - even though the input field appears first in the markup:
<div>
<input type="text">
<button>Action 1</button>
</div>
Please see my jsfiddle here. Why is this happening and what's the correct solution?

You need thee div container to do this as shown in this jsFiddle.
HTML Code
<div class="container">
<div class="left">
<button>
</div>
<div class="right">
<button>
</div>
</div>
CSS Code
.container {
width: 190px;
height: 22px;
margin: 0;
}
.left {
float: left;
width: 95px;
height: 22px;
}
.right {
float: right;
width: 95px;
height: 22px;
}
​

Use rows.
<div class="row-rap">
<div class="right">
<input type="text">
</div>
<div class="left">
<input type="button" value="Action 1">
</div>
</div>
<div class="row-rap">
<div class="right">
<input type="text">
</div>
<div class="left">
<input type="button" value="Action 2">
</div>
</div>​
With the following styling.
div.row-rap {
width: 100%;
}
div.row-rap .right, div.row-rap .left {
width: 50%;
float: left;
}

Here's an alternative, the margins and colors may need modification. See jsfiddle link for sample result.
It has a left-aligned label and right-aligned input (button style) in a div, for each line. The non-breaking space is needed as a placeholder in the span element that represents an "empty label".
http://jsfiddle.net/qallar/kfgCb/5/
The html is:
<div class='line'>
<span class='formlabel'>label 1</span>
<input class='formbutton' type='button' value='button 1 text ' />
</div>
<div class='line'>
<span class='formlabel'> </span>
<input class='formbutton' type='button' value='button 2 text' />
</div>
and the css:
.line
{
display: block;
background-color: #ddd; /* also try #fff */
margin: 0px;
padding: 2px;
height: 30px;
width: 200px;
}
.formlabel
{
float: left;
background-color: #eee; /* also try #fff */
margin: 0px;
padding: 2px;
width: 75px;
height: 100%;
clear: both;
}
.formbutton
{
float: right;
background-color: #0f0;
margin: 0px;
padding: 0px;
width: 100px;
height: 100%;
}

The input field is flying to the right of the button because it is an inline element. Float works on block elements only, inline elements will always flow around the floated elements. This explains the behavior in the original jsFiddle.
Having said that, even if I put display:block on the input element it still behaves like inline. I was able to make the basic concept work for a div though, which is a true block element. See the jsFiddle here.
<div class="row">
<button>Action 1</button>
<div class="in"></div>
</div>
.row {
clear: both;
}
.in {
background-color: green;
height: 24px;
width: 100px;
}
button {
float: left;
margin-left: 110px;
width: 150px;
}
The only workaround seems to be the one offered by Musa (see this jsFiddle) where he aligns the buttons to the right using text-align and limiting the width of the div.

I am not a CSS expert and usually this task works for me using table
<table>
<tr><td>Optional Input</td><td>Button</td></tr>
<tr><td>Optional Input</td><td>Button</td></tr>
</table>
if table by some reason is not an option you can use div/span
<div style="display: table-row">
<span style="display: table-cell">Optional Input</span>
<span style="display: table-cell">Button</span>
</div>
It will about like this

using Block formatting context https://developer.mozilla.org/en-US/docs/CSS/Block_formatting_context
jsfiddle code: http://jsfiddle.net/EeNFH/9/
the html code:
<div class="inp">
<input type="text">
</div>
<div class="btns">
<p><button>Action 1</button></p>
<p><button>Action 2</button></p>
</div>
and the styles:
input {
width: 100px;
}
button {
width: 150px;
}
.inp{
float:left;
}
.btns{
overflow:hidden;
}
​

Related

css not being applied to a nested class

I have this structure...
<body>
<div class="page-wrapper">
<div class="page-content">
<div class="login-welcome">
<h3 class="welcome">
<form class="form-login">
I'm able to apply css to login-welcome and welcome but not to form-login.
In Chrome Debugger, I don't see the styles I've set. These are the relevant styles...
.login-welcome {
position: absolute;
top: 15%;
left: 40%;
display: block;
}
.welcome {
font-weight: 600px;
font-size: 30px;
color: #653487;
}​
.form-login {
padding-top: 500px;
}
In debugger, I can adjust the same padding setting by adjusting element.style so I figured using form .form-login or .form-login would work but the classes I've tried has not applied any formatting to the class. Any reason why that would be the case?
When I copy your css to chrome, there is some weird character right after the closing } of .welcome
​
it seems like it's stopping chrome from interpreting the next css lines
When you remove this character the following css selectors (e.g. .form-login {) are evaluated again and will be applied to your form element - everything should work then
You can use the following:
div.login-welcome .welcome{
color:red;
}
<div class="page-wrapper">
<div class="page-content">
<div class="login-welcome">
Im not affected
<h3 class="welcome">Some heading text</h3>
<form class="form-login">
some form text
</form>
</div>
</div>
</div>
It works as follows: div.login-welcome .welcome is a CSS selector which applies styles for element which have a welcome class and a <div> parent elements which have the class login-welcome.
Hopefully this was helpful.
Your code works:
.login-welcome {
position: absolute;
top: 15%;
left: 40%;
display: block;
}
.welcome {
font-weight: 600px;
font-size: 30px;
color: #653487;
}
.form-login {
padding-top: 500px;
}
<div class="page-wrapper">
<div class="page-content">
<div class="login-welcome">
<h3 class="welcome">Welcome header</h3>
<form class="form-login">
<input type="text">
</form>
</div>
</div>
</div>
look at this fiddle: https://jsbin.com/segiqoyoci/edit?html,css,output

Placing other elements below the two div elements which were placed side-by-side

the code i have tried is
CSS Code
#tools
{
float:left;
}
#sketch
{
border: 10px solid grey;
float:left;
width: 700px;
height: 500px;
position: relative;
}
HTML code:
<div id="tools">
<p>These are my tools</p>
</div>
<div id="sketch">
<p>This is my sketch</p>
</div>
<button type="button">Click</button>
I am placing the two divs side-by-side
the button is geting displyed side to the divs
but I want the button below the div
Fiddle here
Clear the float:
button { clear: both; }
Add wrapper with a clearfix:
<div class="wrapper cf">
<div id="tools">
<p>These are my tools</p>
</div>
<div id="sketch">
<p>This is my sketch</p>
</div>
</div>
<button type="button" >Click</button>
http://jsfiddle.net/brutusmaximus/5KmK6/3/
Try to use "clear: left;" for the button to cancel the "float: left" of the previous div element.
See http://www.w3schools.com/cssref/pr_class_clear.asp for more details.
You can use any of the two :
1) button {
clear : both;
}
2) Add a class clearfix to the Div - "sketch" that adds a pseudo element to the DOM after it to clear the float added.
<div id="sketch" class="clearfix">
<p>This is my sketch</p>
</div>
.clearfix:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}

CSS fill remaining width

I have this header bar.
<div id="header">
<div class="container">
<img src="img/logo.png"/>
<div id="searchBar">
<input type="text" />
</div>
<div class="buttonsHolder">
<div class="button orange inline" id="myAccount">
My Account
</div>
<div class="button red inline" id="basket">
Basket (2)
</div>
</div>
</div>
</div>
I need the searchBar to fill whatever the remaining gap is in the div. How would I do this?
Here's my CSS
#header {
background-color: #323C3E;
width:100%;
}
.button {
padding:22px;
}
.orange {
background-color: #FF5A0B;
}
.red {
background-color: #FF0000;
}
.inline {
display:inline;
}
#searchBar {
background-color: #FFF2BC;
}
Use calc!
https://jsbin.com/wehixalome/edit?html,css,output
HTML:
<div class="left">
100 px wide!
</div><!-- Notice there isn't a space between the divs! *see edit for alternative* --><div class="right">
Fills width!
</div>
CSS:
.left {
display: inline-block;
width: 100px;
background: red;
color: white;
}
.right {
display: inline-block;
width: calc(100% - 100px);
background: blue;
color: white;
}
Update: As an alternative to not having a space between the divs you can set font-size: 0 on the outer element.
You can realize this layout using CSS table-cells.
Modify your HTML slightly as follows:
<div id="header">
<div class="container">
<div class="logoBar">
<img src="http://placehold.it/50x40" />
</div>
<div id="searchBar">
<input type="text" />
</div>
<div class="button orange" id="myAccount">My Account</div>
<div class="button red" id="basket">Basket (2)</div>
</div>
</div>
Just remove the wrapper element around the two .button elements.
Apply the following CSS:
#header {
background-color: #323C3E;
width:100%;
}
.container {
display: table;
width: 100%;
}
.logoBar, #searchBar, .button {
display: table-cell;
vertical-align: middle;
width: auto;
}
.logoBar img {
display: block;
}
#searchBar {
background-color: #FFF2BC;
width: 90%;
padding: 0 50px 0 10px;
}
#searchBar input {
width: 100%;
}
.button {
white-space: nowrap;
padding:22px;
}
Apply display: table to .container and give it 100% width.
For .logoBar, #searchBar, .button, apply display: table-cell.
For the #searchBar, set the width to 90%, which force all the other elements to compute a shrink-to-fit width and the search bar will expand to fill in the rest of the space.
Use text-align and vertical-align in the table cells as needed.
See demo at: http://jsfiddle.net/audetwebdesign/zWXQt/
I know its quite late to answer this, but I guess it will help anyone ahead.
Well using CSS3 FlexBox. It can be acheived.
Make you header as display:flex and divide its entire width into 3 parts. In the first part I have placed the logo, the searchbar in second part and buttons container in last part.
apply justify-content: space-between to the header container and flex-grow:1 to the searchbar.
That's it. The sample code is below.
#header {
background-color: #323C3E;
justify-content: space-between;
display: flex;
}
#searchBar, img{
align-self: center;
}
#searchBar{
flex-grow:1;
background-color: orange;
padding: 10px;
}
#searchBar input {
width: 100%;
}
.button {
padding: 22px;
}
.buttonsHolder{
display:flex;
}
<div id="header" class="d-flex justify-content-between">
<img src="img/logo.png" />
<div id="searchBar">
<input type="text" />
</div>
<div class="buttonsHolder">
<div class="button orange inline" id="myAccount">
My Account
</div>
<div class="button red inline" id="basket">
Basket (2)
</div>
</div>
</div>
This can be achieved by wrapping the image and search bar in their own container and floating the image to the left with a specific width.
This takes the image out of the "flow" which means that any items rendered in normal flow will not adjust their positioning to take account of this.
To make the "in flow" searchBar appear correctly positioned to the right of the image you give it a left padding equal to the width of the image plus a gutter.
The effect is to make the image a fixed width while the rest of the container block is fluidly filled up by the search bar.
<div class="container">
<img src="img/logo.png"/>
<div id="searchBar">
<input type="text" />
</div>
</div>
and the css
.container {
width: 100%;
}
.container img {
width: 50px;
float: left;
}
.searchBar {
padding-left: 60px;
}
in css:
width: -webkit-fill-available
I would probably do something along the lines of
<div id='search-logo-bar'><input type='text'/></div>
with css
div#search-logo-bar {
padding-left:10%;
background:#333 url(logo.png) no-repeat left center;
background-size:10%;
}
input[type='text'] {
display:block;
width:100%;
}
DEMO
http://jsfiddle.net/5MHnt/
Include your image in the searchBar div, it will do the task for you
<div id="searchBar">
<img src="img/logo.png" />
<input type="text" />
</div>
I did a quick experiment after looking at a number of potential solutions all over the place. This is what I ended up with:
http://jsbin.com/hapelawake

How to put 2 pictures next to each other without moving when adding more text in div?

I just started to design a small Webpage to present some designs.
It's a page with 2 columns, with a picture and some text for each.
The problem I have right now: when I add more text to one column, the picture of the other column moves.
Check out my fiddle:
http://jsfiddle.net/JannikS/tMY57
My HTML markup:
<div id="designrow">
<div class="design">
<img src="http://www.webdesign-is-art.com/wp-content/uploads/2008/05/goodbytes-webdesign.jpg" />
<h3>Title </h3>
<p>Short description of our design..</p>
</div>
<div class="design">
<img src="http://www.webdesign-is-art.com/wp-content/uploads/2008/05/goodbytes-webdesign.jpg" />
<h3>Title </h3>
<p>Short description of our design..<br /> but with some more text!</p>
</div>
</div>
and CSS:
.designrow {
float: left; }
.design {
width: 300px;
margin-left: 20px;
margin-right: 20px;
margin-bottom: 20px;
display: inline-block;
}
.design img{
width: 100%;
height: 100%;
}
you're giving your columns display:inline-block, remove that and float:left instead
heres the fiddle: http://jsfiddle.net/tMY57/3/

Vertically align center inside div element

My text have only 1 line and my picture will fit the container height. I want text and picture to align center vertically. I try margin top text class with negative value and position relative but nothing work what I really want.
jsfriddle here
HTML
<div id="friend_select">
<div class="Friend">
<img class="Picture" src="http://graph.facebook.com/harell/picture?type=square&width=80&height=80" />
<span class="Name">First Name Last Name 1</span>
</div>
<div class="Friend">
<img class="Picture" src="http://graph.facebook.com/harell/picture?type=square&width=80&height=80" />
<span class="Name">First Name Last Name 2</span>
</div>
</div>
CSS
#friend_select{ padding: 5px; width: 400px; height: 500px; }
.Friend{ height: 80px; }
.Friend .Name{ line-height: 80px; }
.Friend .Picture{ line-height: 80px; width: 80px; height: 80px; }
​
I propose this fix:
.Friend .Name{ line-height: 80px; vertical-align:top; }
Live demo here: http://jsfiddle.net/keaukraine/WNT7U/
Works in latest Firefox, Chrome and IE down to IE8. For IE7, apply this hack:
.Friend .Name { *zoom:1; }

Resources