html - 如何将图标垂直和水平居中

标签 html css twitter-bootstrap

我想要一个图标在 div 上垂直和水平居中。

.exhibitor_image_div{
    width: 200px;
    height: 200px;
    border: thin black solid;
    -webkit-border-radius: 3px ;
    -moz-border-radius: 3px;
    border-radius: 3px;
    margin: 0 auto;
    text-align: center;
}

.exhibitor_image_default_icon{
    height: 70px;
    width: 70px;
    margin: 0 auto;
    position:relative;
    top: 50%;
}


 span i {
     font-size: 65px;
 }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<div class="exhibitor_image_div">
    <div class="exhibitor_image_default_icon">
        <span><i class="glyphicon glyphicon-user"></i></span>
    </div>
</div>

我已经从上面给出了50%。但它不是垂直居中的。

最佳答案

您可以使用 CSS 转换将图标向上移动 50% 的高度,从而使图标在其父元素中居中。

.exhibitor_image_div {
  width: 200px;
  height: 200px;
  border: thin black solid;
  -webkit-border-radius: 3px ;
  -moz-border-radius: 3px;
  border-radius: 3px;
  margin: 0 auto;
  text-align: center;
}
.exhibitor_image_default_icon {
  height: 70px;
  width: 70px;
  margin: 0 auto;
  position:relative;
  top: 50%;
  transform: translateY(-50%);
  // border: thin red solid;
}
span i {
  font-size: 65px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<div class="exhibitor_image_div">
  <div class="exhibitor_image_default_icon">
    <span><i class="glyphicon glyphicon-user"></i> </span>
  </div>
</div>

关于html - 如何将图标垂直和水平居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41017989/

相关文章:

jquery - 似乎无法让 Bootstrap 导航工作

Javascript/Html/CSS 转换切换和 slideToggle

html - 绝对位置的文本换行遵循父级的边缘

Jquery 自动完成显示 1 项的 2 行而不是将其切断

javascript - 我可以将搜索分成不同的 div 吗?

jquery - 将值从模态传递到 Bootstrap 表

css - 流体布局中像素大小元素的精确中心 Twitter Bootstrap

Javascript AppendTo 动画不起作用

jquery - Highslide JS 字幕(桌面左侧面板)和(移动底部)

html - 比窗口大的背景图片