如何使HTML按钮中的图标居左文本居中对齐?

5
如何在按钮中放置一个图标并使其靠左对齐(即在按钮的最左侧),同时文本仍保持居中对齐?

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

2个回答

10

5行CSS代码和额外的span标签(用于文本)。也适用于导航栏布局(左侧徽标和中心菜单)。

flex container
-- flex item 1 
-- flex item 2

"The trick": 将 flex-item-1 设置为 margin-right: auto (现在flex-item-2移动到右边缘“推动效果”)。 接下来,对于 flex-item-2 使用 one more margin-right: auto(“完美地”向右推到中心)。

enter image description here

代码段

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>

重要为了获得完美的垂直/水平对齐,不要为按钮内部内容-图标或文本添加额外的上下边距/填充("著名"的错误)。

重要-2移除按钮固定高度(避免overflow-y问题)。按钮的高度由内容(字体大小、图像)和填充/边框(盒模型)声明。

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>

完成 :)

相关(额外阅读):


0

由于您在GoogleSignIn按钮中使用了display:flex属性,因此可以使用justify-content: space-between来指定项目之间的间距。此外,您需要添加额外的元素并使用opacity: 0将其隐藏。从GoogleIcon类中删除额外的边距。您可以使用此技巧对齐项目。

请查看下面的工作示例。我已经修改了className为class以使其正常工作。

.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: space-between;
  
}

.GoogleIcon{
    /* margin: -40px 0px -12px -440%; */
}

.GoogleIconContainer{
    align-self: flex-end;
    display: flex;
}

.hiddenItem {
    opacity: 0;
}
<button class="GoogleSignIn">
        <div class="GoogleIconContainer">
            <img src="edit.png" class="GoogleIcon" />
        </div>
        <div class="GoogleIconText">
            Sign in with google
        </div>
        <div class="hiddenItem"> </div>
    
  </button>

编辑图标 在此输入图片描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接