css 在div中居中显示图像和文本

5f0d552i  于 12个月前  发布在  其他
关注(0)|答案(4)|浏览(87)

HTML

<div class="leave-comment">
  <a href="example-video-8/" rel="bookmark" title="Leave a comment for Example Video 8"><span class="comment-bubble"></span>Leave a comment for Example Video 8!</a>
</div>

CSS

.leave-comment {
background: #010101;
clear: both;
font-size: 1.4em;
padding: 20px 0;
}

.comment-bubble {
background: url(http://i.imgur.com/iqOtL.png) no-repeat left center;
display: inline-block;
height: 24px;
width: 26px;
float: left;
margin-right: 10px;
  }

下面是JS Fiddle:http://jsfiddle.net/CeZLy/
我正试图将评论气泡和文本集中在黑框内。文本总是在变化,所以我不能在a元素上设置一个固定的宽度。有人能帮我一下吗?
注:抱歉,我不清楚。我希望注解气泡位于文本的左侧,然后我希望注解气泡和文本都位于黑色框的中心。

w7t8yxp5

w7t8yxp51#

拆下跨距。将图像设置为a元素的背景。使用text-align:center;并为图像添加左填充:

.leave-comment {
    background: #010101;
    clear: both;
    font-size: 1.4em;
    padding: 20px 0;
    text-align:center;
}

.leave-comment a {
    background: url(http://i.imgur.com/iqOtL.png) no-repeat left center;
}

<div class="leave-comment">
    <a href="example-video-8/" rel="bookmark" title="Leave a comment for Example Video 8">Leave a comment for Example Video 8!</a>
</div>

检查一下:
http://jsfiddle.net/C9HKr/

s71maibg

s71maibg2#

从您的评论中,我将省略float: left并添加text-align: center
JSFiddle
垂直居中似乎有点困难。如果你想垂直居中并且有固定的高度,你可以将链接的line-height设置为相同的高度。
参见JSFiddle
有一个关于Vertical Centering With CSS垂直居中的教程,解释了几种方法,并强调了每种方法的优缺点。

  • 更新 *:

我刚刚重读了你的评论。也许我误会你了如果你只是想让链接向上或向下移动一点,你也可以在顶部和底部使用不同的填充。
看到这个JSFiddle

xwbd5t1u

xwbd5t1u3#

.leave-comment {
background-color: #010101;
 clear: both;
font-size: 1.4em;
padding: 20px 0;
text-align: center;
}
.comment-bubble {
background: url(http://i.imgur.com/iqOtL.png) no-repeat left center;
display: inline-block;
height: 24px;
width: 26px;
margin-right: 10px;
position: relative;
top: 7px;
}

你现在需要跨度。
http://jsfiddle.net/CeZLy/7/在行动

moiiocjp

moiiocjp4#

在你的文字和图像定义的利润率像波纹管,在我的情况下,我使用的img流体的bootstrap recomendation的响应

.img-fluid {
        margin: 0% 0% 0% auto;
    }
    
    .banner-title {
        margin: 0% auto 0% 0%;
    }
<div class="container-fluid bg-banner flex">
        <img src="img/ba.png" class="img-fluid" alt="">
        <div class="banner-title">
            <h1 class="banner-title main-text text-center">
                    <a href="{% url 'vacancies:send_vacancy' %}" class="title-link border rounded
                    text-white p-1 shadow rounded">Connect</a> 
                    with Opportunities

相关问题