简体   繁体   English

在 div 中居中定位标签

[英]Centering an anchor tag inside a div

I have an anchor containing an image that I want to center within a div tag of the bootstrap navbar我有一个包含图像的锚点,我想将它放在引导程序导航栏的 div 标签中

The height and width of the image is unknown so I'd like to center it horizontally and vertically without having to calculate padding图像的高度和宽度未知,因此我想将其水平和垂直居中而无需计算填充

<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;
}

I highly recommend the following article: https://css-tricks.com/centering-css-complete-guide/我强烈推荐以下文章: 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>

Remove this CSS删除此 CSS

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

Add margin 0 auto on logo在徽标上添加边距0 auto

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

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

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