html - margin : 0 auto; not working on a div the needs to inherit a width from 2 enclosed elements

标签 html css

enter image description here

我正在尝试将火焰和航向居中放置在白框的中间。

HTML

        <div class="contentheading">
            <div class="floatmiddle">
                <img src="images/flame45x45.png">
                <h3>Receive only the email you want.</h3>
            </div>
        </div>

CSS

.contentheading {
       position: relative;
       height: 45px;
       margin-top: 30px;
       width: 636px;           //this is the full width of the white box//
}

.floatmiddle {
       margin: 0 auto;
       height: 45px;
       display: block;
}

.contentheading img {
       position: absolute;
}

.floatmiddle > h3 {
       font-family: "signika";
       font-size: 22px;
       font-weight: 500;
       color: #37434f;
       height: 45px;
       line-height: 45px;
       margin: 0 0 0 60px;
       text-align: center;
       vertical-align: top;
       position: absolute;
}

我需要 .float 中间部分继承两个封闭元素的宽度 - 图像(45 x 45px)和文本(我拥有的每一章的长度都不同)所以我需要一个类/公式所以我可以直接穿过并弹出标题,无论标题长度如何,标题和火球都将在白色 div 中居中。

最佳答案

您可以使用 display: inline-block; 将此 div 居中。

http://jsfiddle.net/d8gyd9gu/

HTML

<div class="contentheading">
    <div class="floatmiddle">
        <img src="http://www.neatimage.com/im/lin_logo.gif" alt="">
        <h3>Receive only the email you want.</h3>
    </div>
</div>

CSS

.contentheading {
       height: 45px;
       margin-top: 30px;
       width: 636px;
       text-align: center;
}


.floatmiddle {
       height: 45px;
       display: inline-block;
}

.contentheading img {
       float: left;
        margin: 20px 10px 0px 0px;
}

.floatmiddle > h3 {
       font-family: "signika";
       font-size: 22px;
       font-weight: 500;
       color: #37434f;
       height: 45px;
       line-height: 45px;
       padding: 0px 0px 0px 60px;
}

关于html - margin : 0 auto; not working on a div the needs to inherit a width from 2 enclosed elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26517597/

相关文章:

javascript - 带有 css 值的 jQuery/javascript if 语句

javascript - 如何将图像标签更改为 svg 中的另一个标签?

css - 首先将文本与大字母对齐

html - 如何使 <aside> 作为包含内容的列

html - grid-column-gap 在行中最后一个 div 的末尾添加间隙

html - CSS 边框折叠不适用于值折叠

javascript - 有没有办法修复一个没有 float 的div?

javascript - 仅 HTML/JS 和谷歌地图上的实时 GPS 跟踪器可以在手机上运行?可能吗?

css - Div 未正确显示悬停功能

html - 如何使我的图像可缩放并同时保持它们的宽高比?