Centering my button with using a div element [duplicate] - html

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>

Related

Flexbox with h2 and button

I am pretty new to React.js and flexbox, and I currently have a flexbox with both an h2 and a button that I want to center. Unfortunately, even when I put justify-content: center, margin: auto, and text-align center, the formatting is still now what I'm looking for. I also know that there must be something that I am missing, but I just don't know what right now. Any suggestions would be greatly appreciated.
<header className={styles.header}>
<div className={styles.spacing}></div>
<div className={styles.logoaccount}>
<img></img>
</div>
<div className={styles.spacing}></div>
<div className={styles.searchsearch}>
<h2>Japanese</h2>
<button>Search</button>
</div>
<div className={styles.spacing}></div>
<div className={styles.spacing}></div>
<div className={styles.spacing}></div>
<div className={styles.spacing}></div>
<div className={styles.filters}>
<button className="location">Location</button>
<button className="food-type">Food-type</button>
<button className="rate">Rate</button>
<button className="apply-filters">Apply-Filters</button>
</div>
*{
margin: 0;
padding: 0;
}
.header{
text-align: center;
}
.logoaccount{
width: 50%;
display: flex;
align-items: center;
justify-content: center;
margin: auto;
}
.logoaccount > button{
border-radius: 5px;
}
.filters{
width: 75%;
display: flex;
margin: auto;
justify-content: space-between;
}
.filters > button{
width: 15%;
}
.searchsearch{
width: 50%;
display: flex;
align-items: center;
justify-content: space-between;
margin: auto;
}
.spacing{
height: 20px;
}
This is what I get:
however, I want the second line containing Japanese and "Search" to be centered. I tried all over to look for the solution but I unfortunately couldn't find anything. thanks so much!
Edit: background color for clarification.
Have you tried inspecting the page and using the Select Page Item tool (Ctrl+Shift+C) to see if the problem is visible?. If not, just like you did with .filters > button, try adding the width: 15% style (or try other percentages, like 50%) to the .searchsearch > button. Otherwise, maybe using the filters style instead of the searchsearch one in the div's className changes something that makes it work.

html center align vertically not working

I am having this simple issue here, which worked before at a lot of places.
I am trying to align items inside a div vertically and at the center. Here in this code the margin-left works, but the margin top doesn't, I tried changing it to bigger values, still no effect at all.
.footer {
background-color: #2E7FB6;
color:white;
height:50px;
}
<div class="footer">
<section style="margin-left:15px; margin-top:10px;">FETCHED: {{ recordsFetched }} Work Order(s)</section>
</div>
Remove the inline styling, use flexbox with flex-direction: column; justify-content: center; and text-align center; on the footer.
.footer {
background-color: #2E7FB6;
color:white;
height:50px;
text-align: center;
display: flex;
flex-direction: column;
justify-content: center;
}
<div class="footer">
<section>FETCHED: {{ recordsFetched }} Work Order(s)</section>
</div>
Use flexbox tips just #rprm192 say. But if you want to make it more simple and support older browser, you can use line-height. Here's code for you
.footer {
height: 50px;
}
.footer section {
height: 100%;
line-height: 50px; //make it same as height value
}

How to center two buttons next to each other but center of the screen?

This is definitely such a basic question but I'm trying to do this in Ionic. I've been trying
margin: 0 auto;
text-align: center;
position: relative;
so many things but it's not working, help?
UPDATE: So I've tried one of the solutions and this is my CSS, but it still doesn't work, its center, but all the way at the top
.square{
width: 25vw;
height: 8vw;
}
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
HTML:
<div class="container"
<div class="button-wrapper">
<button class="button button-outline square button-calm">
Male
</button>
<button class="button button-outline square button-royal">
Female
</button>
</div>
</div>
You can use flexbox to achieve this. Firstly wrap the buttons within a container:
<div class="container">
<div class="button-wrapper">
<button class="male">Male</button>
<button class="female">Female</button>
</div>
</div>
Then, in the CSS, apply vertical and horizontal centring to the children of the container:
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
Codepen example
I have no experience with iconic framework. But I think following suggestions might work, give it a try:
1) You can try <center> tag
2) or you can try left:50%; (which is setting the left margin)
I've decided to do
.square{
width: 25vw;
height: 10vw;
}
.container {
padding-top: 35vh;
display: flex;
justify-content: center;
align-items: center;
height: 100%
}
not sure how good of a convention this is, but it seemed to center it decently on different devices

HTML / CSS - Center multiple divs inline

I'm try to accomplish something rather basic but I failed to succeed, so far.
I have an image and a title which should be displayed in the center of the parent selector (div). Unfortunately I don't know how to properly align them when I center both the logo and the text.
Html:
<div class="content">
<div class="header">
<img src="path/to/image">
<span>Hey this is my title</span>
</div>
</div>
CSS:
.header{
text-align: center;
width: 100%;
height: 40px;
}
.header img{
height: 40px;
width: auto;
display: inline-block;
}
.header span{
display: inline-block;
}
As you can see the logo has a static width/height while the text can have a variable height.
The code above makes the styling like the example below:
Can anyone tell me how to do this? I basically want several divs next to eachother, but all aligned in the center.
Are you looking for vertical-align: middle to center vertically?
.header {
text-align: center;
width: 100%;
height: 40px;
}
.header img {
height: 40px;
width: auto;
display: inline-block;
vertical-align: middle;
}
.header span {
display: inline-block;
vertical-align: middle;
background: #eef;
}
<div class="content">
<div class="header">
<img src="//placehold.it/40?text=Logo" />
<span>Hey this is my title</span>
</div>
</div>
Note: I have added background for better visibility of the borders.
A better explanation:
Use flexbox. I don't know if I correctly got that you want your items to be centered both horizontally and vertically inside header. If not just delete the justify-content part.
.header {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
More info about flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

distributing differnet elements horizontally inside a div with CSS

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>