html - 将 anchor 标记居中放置在 div 中

标签 html css twitter-bootstrap

我有一个包含图像的 anchor ,我想在 Bootstrap 导航栏的 div 标签中居中

图像的高度和宽度未知,所以我想将它水平和垂直居中,而不必计算填充

<div class="navbar-header">
    <a class="navbar-left" href="index.html"><span role="link" class="logo"></span></a>
</div>

.navbar-header{
    width: 250px;
    height: 60px;
    float: left;
}

.navbar-left{
    float: left!important;
}

.logo {
    background-image: url(/image.png);
    background-position: center;
    background-size: 100%;
    border: none;
    display: block;
    height: 51px;
    width: 185px;
    margin: 0;
    text-indent: -9999px;
}

最佳答案

我强烈推荐以下文章: https://css-tricks.com/centering-css-complete-guide/

.navbar-header{
    width: 250px;
    height: 60px;
    float: left;
    background-color: rgba(255,0,0,0.1);
    position: relative;
}

.navbar-left{
    /* float: left!important; */
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.logo {
    background-image: url(https://picsum.photos/id/10/555/153);
    background-position: center;
    background-size: 100%;
    border: none;
    display: block;
    height: 51px;
    width: 185px;
    margin: 0;
    padding: 0;
    text-indent: -9999px;
}
<div class="navbar-header">
    <a class="navbar-left" href="index.html">
      <span role="link" class="logo"></span>
    </a>
</div>

关于html - 将 anchor 标记居中放置在 div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43393079/

相关文章:

每次点击时 Jquery 触发动画(问题)

jquery - 需要滚动到位置 : fixed;

Javascript:输入值总和在鼠标滚轮事件上跳至最大值

html - css : floating elements only 中棘手的水平居中

css - 我的背景图片不适合引导超大屏幕

css - 多个切换按钮背景色 CSS

html - 向下滚动时文本移动到页面的一侧

css - material-ui:使用内联修改组件属性不起作用

css - Pages 中的 NoMethodError #welcome 安装 Bootstrap gem 后

css - Bootstrap.less 不加载 Bootstrap 的东西