How can you have a button with an icon that is aligned left (far left in the button) and the text still centred in the button?
.GoogleSignIn{
border-radius: 50px;
padding: 10px;
text-align: center;
align-items: center;
width: 100%;
height: 42px;
font-size: 14px;
font-weight: 500;
background-color: white;
border: none;
color: black;
margin-bottom: 10px;
cursor: pointer;
outline: none;
display: flex;
/* justify-content: center; */
}
.GoogleIcon{
margin: -40px 0px -12px -440%;
}
.GoogleIconContainer{
align-self: flex-end;
display: flex;
}
<button className={classes.GoogleSignIn}>
<div className = {classes.GoogleIconContainer}>
<img src={GoogleIcon} className = {classes.GoogleIcon}/>
</div>
Sign in with google
</button>
Output. Left Align Button. Right Align Button. Probably, you may also like to adjust the button to the center position. The CSS text-align property can also be used to set the button to the center position. You have to use center as the property value and place the button inside the <div> element to move the button to the center position.
Since the HTML button is an inline element, not a block-level element, the text-align property cannot be used directly on the button to center it. Instead, we can place the button inside a div, the generic block-level element, then apply text-align: center to this div container: HTML Align Text Left
You used to be able to simply use the HTML align attribute to change the alignment of text. If you wanted to center the title of this web page, for example, then you’d have written the following line of HTML: <h1 align="center"> How to Right, Left, & Center Align Text in HTML </h1>. But this attribute has since been deprecated ...
However, you may encounter situations when you want to left-align a piece of content that is inside an element set to a different alignment, like right or center. So, it’s still good to know how. To left justify in CSS, use the CSS rule text-align: left. In the example below, the div element is set to center all content inside it.
5 lines of CSS code and extra span (For the text). ** Also useful for navbar layout (left logo & center menu).
flex container
-- flex item 1
-- flex item 2
"The trick": set flex-item-1 margin-right: auto
(Now flex-item-2 move to the right edge "push effect").
Next, use one more margin-right: auto
for flex-item-2 ("push to the right perfectly center")
button{
display: flex;
align-items: center;
justify-content: center;
}
button img{
margin-right: auto;
border: 1px solid red;
}
button span{
border: 1px solid red;
margin-right: auto;
}
<button style="width: 100%;">
<img src="https://svgshare.com/i/5vv.svg">
<span>
Hello world
</span>
</button>
<br>
<button style="width: 200px;">
<img src="https://svgshare.com/i/5vv.svg">
<span>
Hello world
</span>
</button>
important
To get "perfect V/H align" Do not add any extra top/bottom margin/padding for the content inside the button - icon -or- the text ("Famous" mistake).
important-2
Remove button fixed height (Avoid overflow-y issues). The height of the button declares by content (font-size, image) & padding/border (box-model).
button{
display: flex;
align-items: center;
justify-content: center;
}
button img{
margin-right: auto;
border: 1px solid red;
}
button span{
border: 1px solid red;
margin-right: auto;
}
<button style="height: 20px;">
<img src="https://svgshare.com/i/5vv.svg">
<span style="font-size: 30px;">
40px height ==> ovefrlow issues
</span>
</button>
<br><br><br><br>
<button style="height: 30px; overflow-y: scroll">
<img src="https://svgshare.com/i/5vv.svg">
<span style="font-size: 40px;">
40px height ==> ovefrlow issues
</span>
</button>
Done :)
Related (Extra reading):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With