html - 如何在 div 的中心并排显示图像和文本?

标签 html css

如果图像没有以 float 居中,我可以轻松地将图像居中放置在文本的左侧。

左侧内联的图像和文本: https://jsfiddle.net/gc19qqqp/3/

但是,对于 text-align: center 我不知道该怎么做。

我怎样才能使图像看起来与第一个 fiddle 中的图像相似,但文本/图像居中? (text-align: center)

需要将图像和文本内联在此处的中心: https://jsfiddle.net/cL55pzLa/

应如下所示:example

/* recall alert styling */
#recall-alert {
    background-color: #fefab1;
    color: #d10101;
    text-align: center;

    padding: 15px 0;
    margin: 5px 0;

    font-size: 15px;
    font-weight: bold;

}
#recall-alert img {
    height: 40px;
    margin-right: 5px;

}

#recall-alert a {
    color: #98bcb0;
    font-style: italic;
}
#recall-alert a:visited {
    color: #98bcb0;
    outline: none;
}
<div id="recall-alert" class="">
    <img src="http://www.hospitalsafetyscore.org/media/image/hss-alert-icon.png" />
    <span>
        Recall from chicken of the sea effective July 2015<br />
        <a href="#">Click to Read More</a>
    </span>
</div>

最佳答案

我想这可能对你有帮助:

DEMO

您的HTML

没有变化

CSS

/* recall alert styling */

#recall-alert {
    display:block; /*Added*/
    text-align:center;  /*Added*/ 

    background-color: #fefab1;
    color: #d10101;
    /*text-align: left; REMOVED */

    padding: 15px 0;
    margin: 5px 0;

    font-size: 15px;
    font-weight: bold;

}
#recall-alert img {
    display:inline-block;  /*Added*/ 
    height: 40px;
    margin-right: 5px;
    /*float: left; REMOVED */
}
 /*Added span selector */
#recall-alert span {  
    display:inline-block;    
}

#recall-alert a {
    color: #98bcb0;

关于html - 如何在 div 的中心并排显示图像和文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31575085/

相关文章:

javascript - 响应式下拉导航问题

javascript - 使用 AJAX 验证用户时获取正确返回值的问题

html - 子菜单垂直对齐

html - 我想将 div 向左移动

html - 在输入类型文本上显示 "small popup"的最佳方式

html - Bootstrap 4 - Carousel 自动放大图像的一部分以使其适合有没有办法改变它?

javascript - 如何在 JSON 中使用换行符

用于查找两个字符串之间的子字符串的 Javascript REGEX

css - 如何制作一个高度可变的 DIV 下推内容?

html - 如何使用css3隐藏背景颜色