简体   繁体   English

中心文字和锚标签中的图标

[英]center text and icon in a anchor tag

I'm a beginner at web dev. 我是Web开发人员的初学者。 I want to make a button that has one text and one icon on two separate lines. 我想制作一个在两行中分别包含一个文本和一个图标的按钮。 Since the button is rectangular, I want the text and the icon to be both vertically and horizontally centered to make them look pretty. 因为按钮是矩形的,所以我希望文本和图标在垂直和水平方向上居中,以使它们看起来很漂亮。 How should I do about this? 我该怎么办? Thank you. 谢谢。

 .typeA { width: 80%; height: 90%; margin: 0 auto; display: flex; justify-content: center; } .typeA span { display: inline-block; } 
 <div class="wrapper"> <a href="#" class="btn typeA"> <span>ABC <br> <img src="https://placehold.it/60x60" alt="icon" style="width:60px;"> </span> </a> </div> 

Check the following code 检查以下代码

 .typeA{ width: 80%; height: 150px; margin:0 auto; display: flex; justify-content: center; align-items:center; background:pink; } .typeA span{ display: inline-block; } 
 <div class="wrapper"> <a href="test1.html" class="btn typeA"> <span>ABC <br> <img src=".." alt="icon" style="width:60px;"> </span> </a> </div> 

Suggesting you to give a certain height to your button in pixels since your height in percentage won't have any effect to your button 建议您为按钮提供一定的高度(以像素为单位),因为以百分比表示的高度不会对按钮产生任何影响

Just this CSS should do it: 只需此CSS即可:

.typeA {
    display: block;
    width: 80%;
    height: 90%;
    margin:0 auto;
    text-align: center;
}

You should center the content using text-align . 您应该使用text-align将内容居中。 There is no need for flex here, as flex actually tries to put several elements on one line. 这里没有必要使用flex ,因为flex实际上试图将多个元素放在一行上。

I used a button , and a <i> as fake icon. 我使用了一个button和一个<i>作为伪造的图标。 The text is in a span , so you can style the text via span if you want to. 文本位于span ,因此您可以根据需要通过span设置文本样式。

HTML HTML

<button>
  <i id="fake"></i>
  <span>Click</span>
</button>

CSS CSS

button {
  width: 100px;
}

#fake {
  width: 15px;
  height: 15px;
  background: #ff0000;
  position relative;
  margin: 0 auto;
}

button span {
  display: block;
  padding: 3px 0;
}

Hope you can work with it. 希望您可以使用它。 If you don't want to use a button , I would go the same way like the other answers. 如果您不想使用button ,我会像其他答案一样去做。 Set a width and height to <a> and center the text by align and the icon, like explained. widthheight设置为<a>并通过align和图标居中<a>文本, align

The following should work! 以下应该工作!

In HTML 在HTML中

<a href="">
 <div class="button">
  <img src="file" height="20px" width="20px"/>
  <div>Click me</div>
 </div>
</a>

In CSS 在CSS中

.button{
  text-align:center;
  padding:5px;
  background-color:#ddd;
  width:100px;
}
a{
  text-decoration:none;
}

Try this simple css to align div vertically and horizontally. 尝试使用此简单的CSS将div垂直和水平对齐。

 .typeA { width: 80%; margin:auto; height: 90vh; align-items: center; display: flex; } .typeA SPAN{ margin:auto; text-align:center;} 
 <div class="wrapper"> <a href="#" class="btn typeA"> <span>ABC <br> <img src="https://placehold.it/60x60" alt="icon" style="width:60px;"> </span> </a> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM