distributing differnet elements horizontally inside a div with CSS - html

Here's the jsFiddle
Suppose I have the following HTML:
<div class="Trigger">click here</div>
<div id="TheContainer">
<div class="One">this is some text</div>
<input type="button" class="Two" value="button 1" />
<input type="button" class="Three" value="button 2" />
</div>
and the following CSS:
#TheContainer{
width:500px;
height:40px;
padding:10px 30px;
display:none;
background:red;}
.One{float:left;}
.Two{float:left;}
.Three{float:left;}
I use the following javascript to show/hide the container:
$(document).ready(function () {
$('.Trigger').click(function () {
var TheContainer = $('#TheContainer');
if (TheContainer.is(':visible') === false) {
TheContainer.show();
} else {
TheContainer.hide();
}
});
});
As you can see, we have a container of fixed width that's hidden, and that contains 3 elements: 1 div and 2 buttons, all 3 of variable sizes because the text of these elements changes at runtime.
I'm looking to horizontally distribute the 3 children elements evenly inside the container. I've looked around and for various reasons, none of the existing solutions seem to work for this particular case.
How can I distribute these elements horizontally and evenly using just CSS?
Note: I know I can do it in jQuery by calculating widths and then setting positions absolutely but I'm looking to see if there's a CSS only solution.
Thanks

I believe this is what you're looking for: http://jsfiddle.net/gq8s4/5/
It involves creating a div around each button so the buttons themselves are not really wide.
<div id="button1"><input type="button" class="Two" value="button 1" /></div>

EDIT: Adjusted answer to new details.
http://jsfiddle.net/gq8s4/3/
.triple-container {
float: left;
width: 33.333333%;
text-align: center;
border: 1px solid yellow;
box-sizing: border-box;
}
.One{
text-align: center;
width: 60px;
margin: 0 auto;
}
.Two{
width: 20px;
}
.Three{
width: 20px;
}

Flexbox is about as close as you're going to get.
http://tinker.io/eb980
#TheContainer {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-pack: justify;
-moz-box-pack: justify;
-ms-flex-pack: justify;
-webkit-justify-content: space-between;
justify-content: space-between;
width: 100%; /* for old Firefox */
}
Support: Firefox, Safari, Chrome, Opera, IE10. http://caniuse.com/flexbox
If you're comfortable adding extra markup, you could fake this with display: table-cell but it won't be perfect:
http://tinker.io/eb980/2
#TheContainer {
width: 100%;
display: table;
}
div {
display: table-cell;
width: 33%;
}
div:nth-child(2) {
text-align: center;
}
div:last-child {
text-align: right;
}
<div id="TheContainer">
<div class="One">this is some text</div>
<div><input type="button" class="Two" value="button 1" /></div>
<div><input type="button" class="Three" value="button 2" /></div>
</div>

Related

How can I display my buttons on their own line within a flexbox

I am trying to display the <h1> on it's own line as well as the <h3> and all the buttons on 1 line together. I am using flex and I know how to do this normally, what I tried to do was display: block; on the buttons and h3 to have them on their own lines, this didn't work and I tried googling my way and finding some kind of flexbox guide to figure it out myself.
body {
margin: 0%;
background-color: #6987D5;
font-family: Arial, Helvetica, sans-serif;
}
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container h1 {}
.container button {
border: 0px;
background-color: #315dcc;
padding: 1%;
color: white;
}
<div class="container">
<h1>Stopwatch</h1>
<h3>00:00</h3>
<button type="button">Start</button>
<button type="button">Stop</button>
<button type="button">Reset</button>
</div>
You have to add a <div> around the buttons to combine them.
body {
margin: 0%;
background-color: #6987D5;
font-family: Arial, Helvetica, sans-serif;
}
.container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
}
.container h1 {}
.buttons {
display: flex;
}
.container button {
border: 0px;
background-color: #315dcc;
padding: 1%;
color: white;
}
<div class="container">
<h1>Stopwatch</h1>
<h3>00:00</h3>
<div class="buttons">
<button type="button">Start</button>
<button type="button">Stop</button>
<button type="button">Reset</button>
</div>
</div>
If you want the tree buttons in a row, they need their own flex container. To display items in a column, you don't need flex since that is the default behaviour.
HTML
.container {
display:flex;
}
<div>
<h1>Stopwatch</h1>
<h3>00:00</h3>
<div class="container">
<button id="start">Start</button>
<button id="stop">Stop</button>
<button id="reset">Reset</button>
</div>
</div>
CSS
.container {
display:flex;
}
You can wrap buttons and text within separate container divs like this.
<div class="container">
<h1>Stopwatch</h1>
<h3>00:00</h3>
</div>
<div class="container">
<button type="button">Start</button>
<button type="button">Stop</button>
<button type="button">Reset</button>
</div>
Your .container is flexed which is why you have everything flowing from left to right. You can set flex-direction: column; but that will just be complicating what is there by default.
All h element is blocked element which means they will fill up any available space. So remove display: flex; from the .container class selector and wrap your buttons in grid or flex container.
.button-group {
display:grid;
grid-template-colums: repeat(3, 1frm);
gap:1rem;
}
or
.button-group button{
gap: 1rem; //May not work on all browsers
display:flex;
flex-wrap:wrap;
}
and
<div class='button-group'>
<button>Start</button>
<button>Stop</button>
<button>End</button>
</div>
In theory you can just add:
flex-direction: column;
to the .container css
but it will make also start and stop buttons in column, is this what you wanted?

Vertically align the text of an input element

I've had a bit of an issue with some flex containers. They're a label and an input in a flex container. Unfortunately, if I change the height of the container from auto, the contents cease aligning - the label's text remains at the start of the flexbox, but the input's text follows the middle. I've written a MWE to demonstrate.
label, input {
border-style: solid;
border-width: 2px;
border-color: red;
background-color: black;
}
.stretch {
display:flex;
height: 4em;
}
.baseline {
display: flex;
height: 4em;
align-content: baseline;
}
.flex-innards {
display: flex;
height: 4em;
align-content: stretch;
}
.flex-innards * {
display: flex;
align-items: center;
}
.flex-innards-start {
display: flex;
height: 4em;
align-content: stretch;
}
.flex-innards-start * {
display: flex;
align-items: flex-start;
}
.short {
height: auto;
}
<div class="stretch">
<label>Stretch alignment</label>
<input value=":("></input>
</div>
<div class="baseline">
<label>How about baseline?</label>
<input value=">:("></input>
</div>
<div class="flex-innards">
<label>Flexing the children kinda works</label>
<input value=":o"></input>
</div>
<div class="flex-innards-start">
<label>But only if they're centered</label>
<input value=":("></input>
</div>
<div class="field short">
<label>Deceptive stretch</label>
<input value=":S"></input>
</div>
https://jsbin.com/nulobolulo/edit?html,css,output.
I've managed to make them vertically centered together in the third box there (using How to vertically align and stretch content using CSS flexbox), but I'd like to have them both have their text aligned to flex-start, as well as filling the complete height of their container.
If possible, I'd like to avoid adding extra elements to the HTML. Thank you.
I think the only way to address this problem with CSS uses the padding property.
input {
padding-bottom: 40px;
}
label,
input {
border-style: solid;
border-width: 2px;
border-color: red;
}
.stretch {
display: flex;
height: 4em;
}
<div class="stretch">
<label>Stretch alignment</label>
<input value=":(">
</div>

Centering my button with using a div element [duplicate]

I have a div that's width is 100%.
I'd like to center a button within it, how might I do this?
<div style="width:100%; height:100%; border: 1px solid">
<button type="button">hello</button>
</div>
Updated Answer
Updating because I noticed it's an active answer, however Flexbox would be the correct approach now.
Live Demo
Vertical and horizontal alignment.
#wrapper {
display: flex;
align-items: center;
justify-content: center;
}
Just horizontal (as long as the main flex axis is horizontal which is default)
#wrapper {
display: flex;
justify-content: center;
}
Original Answer using a fixed width and no flexbox
If the original poster wants vertical and center alignment its quite easy for fixed width and height of the button, try the following
Live Demo
CSS
button{
height:20px;
width:100px;
margin: -20px -50px;
position:relative;
top:50%;
left:50%;
}
for just horizontal alignment use either
button{
margin: 0 auto;
}
or
div{
text-align:center;
}
You could just make:
<div style="text-align: center; border: 1px solid">
<input type="button" value="button">
</div>
Or you could do it like this instead:
<div style="border: 1px solid">
<input type="button" value="button" style="display: block; margin: 0 auto;">
</div>
The first one will center align everything inside the div. The other one will center align just the button.
et voila:
button {
width: 100px; // whatever your button's width
margin: 0 auto; // auto left/right margins
display: block;
}
Update: If OP is looking for horizontal and vertical centre, this answer will do it for a fixed width/height element.
With the limited detail provided, I will assume the most simple situation and say you can use text-align: center:
http://jsfiddle.net/pMxty/
Margin: 0 auto; is the correct answer for horizontal centering only.
For centering both ways something like this will work, using jquery:
var cenBtn = function() {
var W = $(window).width();
var H = $(window).height();
var BtnW = insert button width;
var BtnH = insert button height;
var LeftOff = (W / 2) - (BtnW / 2);
var TopOff = (H / 2) - (BtnH /2);
$("#buttonID").css({left: LeftOff, top: TopOff});
};
$(window).bind("load, resize", cenBtn);
Update ... five years later, one could use flexbox on the parent DIV element to easily center the button both horizontally and vertically.
Including all browser prefixes, for best support
div {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-align : center;
-moz-box-align : center;
-ms-flex-align : center;
-webkit-align-items : center;
align-items : center ;
justify-content : center;
-webkit-justify-content : center;
-webkit-box-pack : center;
-moz-box-pack : center;
-ms-flex-pack : center;
}
#container {
position: relative;
margin: 20px;
background: red;
height: 300px;
width: 400px;
}
#container div {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-align: center;
-moz-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
justify-content: center;
-webkit-box-pack: center;
-moz-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
}
<!-- using a container to make the 100% width and height mean something -->
<div id="container">
<div style="width:100%; height:100%">
<button type="button">hello</button>
</div>
</div>
Using flexbox
.Center {
display: flex;
align-items: center;
justify-content: center;
}
And then adding the class to your button.
<button class="Center">Demo</button>
You should take it simple here you go :
first you have the initial position of your text or button :
<div style="background-color:green; height:200px; width:400px; margin:0 0 0 35%;">
<h2> Simple Text </h2>
<div>
<button> Simple Button </button>
</div>
</div>
By adding this css code line to the h2 tag or to the div tag that holds the button tag
style:" text-align:center; "
Finaly The result code will be :
<div style="background-color:green; height:200px; width:400px; margin:0 0 0 35%;">
<h2 style="text-align:center;"> Simple Text </h2> <!-- <<--- here the changes -->
<div style="text-align:center"> <!-- <<--- here the changes -->
<button> Simple Button </button>
</div>
</div>
To center a <button type = "button"> both vertically and horizontally within a <div> which width is computed dynamically like in your case, this is what to do:
Set text-align: center; to the wrapping <div>: this will center the button whenever you resize the <div> (or rather the window)
For the vertical alignment, you will need to set margin: valuepx; for the button. This is the rule on how to calculate valuepx:
valuepx = (wrappingDIVheight - buttonHeight)/2
Here is a JS Bin demo.
Came across this and thought I'd leave the solution I used as well, which utilizes line-height and text-align: center to do both vertical and horizontal centering:
Click here for working JSFiddle
Super simple answer that will apply to most cases is to just make set the margin to 0 auto and set the display to block. You can see how I centered my button in my demo on CodePen
Supposing div is #div and button is #button:
#div {
display: table-cell;
width: 100%;
height: 100%;
text-align: center;
vertical-align: center;
}
#button {}
Then nest the button into div as usual.
Easiest thing is input it as a "div" give it a "margin:0 auto " but if you want it to be centered u need to give it a width
Div{
Margin: 0 auto ;
Width: 100px ;
}
Responsive CSS option to center a button vertically and horizontally without being concerned with parent element size (using data attribute hooks for clarity and separation concerns):
HTML
<div data-element="card">
<div data-container="button"><button>CTA...</button></div>
</div>
CSS
[data-container="button"] {
position: absolute;
top: 50%;
text-align: center;
width: 100%;
}
Fiddle: https://jsfiddle.net/crrollyson/zebo1z8f/
Responsive way to center your button in a div:
<div
style="display: flex;
align-items: center;
justify-content: center;
margin-bottom: 2rem;">
<button type="button" style="height: 10%; width: 20%;">hello</button>
</div>
div {
text-align : center;
}
button {
width: 50%;
margin: 1rem auto;
}
<div style="width:100%; height:100%; border: 1px solid">
<button type="button">hello</button>
</div>
This is what I mostly do.
I think bootstrap also uses this in "mx-auto".
var element = document.getElementById('btn1');
var elementwidth = element.clientWidth;
var elementheight = element.clientHeight;
element.style.left = 'calc(50% - ('+elementwidth+'px / 2) )';
element.style.top = 'calc(50% - ('+elementheight+'px / 2) )';
#btn1{
position: relative;
}
div{
height:200px;
width:200px;
border: 1px solid black;
}
<html>
<body>
<div>
<input type = 'button' id = 'btn1'>
</div>
</body>
</html>

Vertical centering with flex box not working even with most basic code

I'm sure this is a duplicate question but I've eliminated almost everything from my code and still can't get it to work. I am new to Flexbox and tried to follow the code at https://philipwalton.github.io/solved-by-flexbox/demos/vertical-centering/ (as an aside, even that code doesn't work!). I am using Safari 11.1.1 and Chrome 67.0.3396.87 so it's not that they're old browsers.
I'm trying to center a <div> horizontally and vertically on the screen. The div contains a form which contains a couple of inputs and a button. I'm trying to center each of this horizontally and the group should be entered vertically within the <div>.
In Safari,nothing is centred. In Chrome, the div is centred horizontally but not vertically, but the inputs/button are not centred either horizontally and vertically.
body {
background: grey;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
#holder {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 300px;
height: 300px;
background: white;
}
<div id="holder">
<form action="">
<input type="text" placeholder="something">
<input type="text" placeholder="something else">
<button>An Action</button>
</form>
</div>
What am I missing? As said, even the sample code from the site mentioned above didn't work correctly, so I'm quite stumped.
You need to set height:100vh in body, or set height:100% on html,body
And center items in forms, use flexbox properties in form instead of #holder
body {
margin: 0;
height: 100vh;
background: grey;
display: flex;
justify-content: center;
align-items: center;
}
#holder {
width: 300px;
height: 300px;
background: white;
}
form {
display: flex;
height: 100%;
flex-direction: column;
justify-content: center;
align-items: center;
}
form>* {
margin-bottom: 20px
}
<div id="holder">
<form action="">
<input type="text" placeholder="something">
<input type="text" placeholder="something else">
<button>An Action</button>
</form>
</div>

inline-flex input element breaks to new line in IE11

I have a few html elements next to each other in a container positioned with display:inline-flex.
This works well for button elements, but as soon as I try to add an input type="text" element, the textbox is placed below the buttons (only in Internet Explorer 11; not sure about IE10 or below).
It works as expected (textbox in same line as buttons) in Firefox, Chrome and even Edge.
How can I get IE to display this correctly?
See jsFiddle for full html and css code to illustrate the problem: https://jsfiddle.net/vm2kcwd9/1/
.container {
height: 2em;
}
.container>* {
height: 100%;
display: inline-flex;
vertical-align: top;
justify-content: center;
align-items: center;
}
<div class="container">
<button>test</button>
<button>test 2</button>
<button>test 3</button>
<input type="text" value="hello" />
</div>
IE11 is known for having many flex-related bugs.
A simple and easy solution in this case would be to make the parent a flex container:
.container {
display: flex; /* new; forces all children into a single row */
height: 2em;
}
.container>* {
height: 100%;
display: inline-flex;
/* vertical-align: top; <--- not necessary anymore */
justify-content: center;
align-items: center;
margin: 5px; /* for demo only */
}
<div class="container">
<button>test</button>
<button>test 2</button>
<button>test 3</button>
<input type="text" value="hello" />
</div>
Also, since you're making button elements into flex containers, consider this:
Flexbox not working on <button> element in some browsers
Easy solution just add display:flex to parent instead
.container {
display: flex;
justify-content: center;
/* align-items: center; you might not need this */
height: 2em;
}
.container>* {
height: 100%;
margin: 10px;
}
<div class="container">
<button>test</button>
<button>test 2</button>
<button>test 3</button>
<input type="text" value="hello" />
</div>