简体   繁体   English

如何将图标对准方形框的中心?

[英]How can I align my icon to be in center of my square box?

I'm trying to align my icon inside a square div to have both properties: CENTER + MIDDLE 我正在尝试在方形div中对齐我的图标以同时具有两个属性: CENTER + MIDDLE

This is what I hope to get : 这就是我希望得到的:

在此处输入图片说明

I have tried this on my CSS : 我已经在我的CSS上尝试过这个:

/* Slick Settings */

  .slick-next {
    right: 0px;
    border: 1px solid black;
    width:35px;
    height:35px;
    padding: 5px;
    display:table;
    text-align: center;
  }

  .slick-prev {
    left: 360px;
    border: 1px solid black;
    width:35px;
    height:35px;
    padding: 5px;
    display:table;
    text-align: center;
  }

  .slick-next:before {
    font-family: FontAwesome;
    content:"\f105";
    color:black;
    display: table-cell;
    vertical-align: middle;
    left: 50%;
  }

  .slick-prev:before {
    font-family: FontAwesome;
    content:"\f104";
    color:black;
    display: table-cell;
    vertical-align: middle;
    left: 50%;
  }

This is what I produce : 这是我生产的:

在此处输入图片说明

Can someone help me with this ? 有人可以帮我弄这个吗 ?

Feel free to suggest me if you have any other better way of archieving this. 如果您还有其他更好的归档方法,请随时建议我。

Thanks in advance. 提前致谢。

You could also do it this way. 您也可以这样做。

 .slick-next { border: 1px solid black; width: 40px; height: 40px; text-align: center; } .slick-next:after { display: inline-block; vertical-align: middle; content: ""; height: 100%; } 
 <link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet"/> <div class="slick-next"><i class="fa fa-angle-right"></i></div> 

left: 50%; would only work on an absolutely positioned element. 仅适用于绝对定位的元素。 So set those styles to position: absolute; 因此,将这些样式设置为position: absolute; or center them with the margin property like this: 或使用margin属性将它们居中,如下所示:

margin-left: auto;
margin-right: auto;

Maybe you can set display to BLOCK for before pseudoelement and set line-hight of it to match DIV. 也许您可以将伪元素之前的显示设置为“块”,并将其行高设置为与DIV匹配。 That will center text vertically. 这将使文本垂直居中。 Placing text-align to center and width to 100% will center text horizontally. 将文字对齐居中,宽度设置为100%将水平居中。

  .slick-prev { border: 1px solid black; width:35px; height:35px; } .slick-prev:before { font-family: FontAwesome; content:"\\f104"; color:black; display: block; text-align: center; width: 100%; line-height: 35px; } 
 <div class="slick-prev"></div> 

Opp... 奥普...

After playing around with my CSS. 在玩完我的CSS之后。 This seem to do the trick. 这似乎可以解决问题。

/* Slick Settings */

.slick-next {
  right: 10px;

  border: 1px solid black;
  width:35px;
  height:35px;
  padding: 5px;
  display:table;
  text-align: center;
  margin-left: auto;
  margin-right: auto;
}

.slick-prev {
  left: 360px;

  border: 1px solid black;
  width:35px;
  height:35px;
  padding: 5px;
  display:table;
  text-align: center;
  margin-left: auto;
  margin-right: auto;

}

.slick-next:before {
  font-family: FontAwesome;
  content:"\f105";
  color:black;


}

.slick-prev:before {
  font-family: FontAwesome;
  content:"\f104";
  color:black;


}

I think I am all-set. 我想我已经准备好了。 I got it now. 我知道了

在此处输入图片说明

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

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