Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align icon left and text centred in a html button?

Tags:

html

css

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>
like image 593
zach_21 Avatar asked Apr 11 '20 16:04

zach_21


People also ask

How do I center align a button in HTML?

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.

How do you center center text on an inline button?

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

How to change the alignment of text in HTML?

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 ...

How to left-align text in CSS?

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.


1 Answers

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")

enter image description here

snippet

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):

  • https://css-tricks.com/how-auto-margins-work-in-flexbox/
  • https://www.smashingmagazine.com/2018/08/flexbox-alignment/
  • https://scrimba.com/learn/flexbox
like image 166
Ezra Siton Avatar answered Oct 12 '22 10:10

Ezra Siton