html - Float H2 - 需要将文本推到下方

标签 html css css-float css-position

我需要将文本推送到其下方的 H2 旁边。不知道为什么我不能解决这个问题,但任何帮助都会很棒。

注意:您正在看左侧。

Fiddle Here

HTML:

 <div class="item">
            <h2>Item 1</h2><span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. </span>
            <div class="button">Button</div>
        </div>

CSS:

.item {
    height: 60px;
    width: 98%;
    border: #000000 solid 1px;
    margin-bottom: 10px;
    padding: 5px;
}

.item span {
    font-size: 10px;
}

h2 {
    text-align: left;
    font-size: 14px;
    margin: 0;
    float: left;
}

.button {
    width: 50px;
    height: 98%;
    border: #000000 solid 1px;
    background: gray;
    float: right;
}

我曾尝试 float 文本,甚至只是将其缩小以更改显示,但我想要一个好的方法来做到这一点。任何帮助都会很棒,干杯!

最佳答案

实际上我想指出您的 DOM 结构不正确,请考虑使用子包装器,float一个到left和其他right .

您遇到该问题的原因是,h2 float ,spaninline所以它会坐在你的旁边 h2 .正在分配 display: block;将导致您的 right如果 width 则要向下推的 block 未分配。最好考虑更改 DOM,我在下面分享了我的解决方案。

Demo

HTML

<div class="box clear">
    <div>
        <h2>Item 1</h2>
        <span>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit.</span>
    </div>
    <div></div>
</div>

CSS

.box {
    border: 1px solid #f00;
    width: 550px;
}

.box > div:nth-of-type(1) {
    float: left;
    width: 400px;
}

.box > div:nth-of-type(2) {
    float: right;
    width: 100px;
    height: 100px;
    border: 1px solid #0f0;
}

.clear:after {
    clear: both;
    content: "";
    display: table;
}

关于html - Float H2 - 需要将文本推到下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19539356/

相关文章:

javascript - 点击外网链接后通过MySQL保存数据

css - 通过 css 对重复 html 结构的 div 进行交替着色

javascript - 在单词中间创建 'cursor'

html - 为什么上面的div向左浮动时div元素不向上移动?

java - 如何在 Spring Web 应用程序中实现文件夹上传?

html - 最高级别第一个标题的CSS选择器?

html - 使用 css 和 HTML 的 streamlabs OBS 的简单文本删除效果

html - 分组占位符颜色样式

javascript - 使用 DOM getElementsByName 更改 CSS

css - 两个 float div 和非文本元素的问题