html - 文本不留在 div 中

标签 html css

我有一些文本 block 甚至没有保留在页面 div 的父级中。我试图在图像上放置一段文本,但它的作用就好像它根本没有附加到图像上一样。将图像设置为 position: absolute; 和文本position:relative;似乎打破了整个布局。

as seen in this image.

HTML

      <section id="productInfo">
        <section class="productDescLargeLeft">
            <img src="images/combat_image1.jpg" alt="combat image" width="410" height="300">
            <p><span>Experience the combat of an MMO, with an oldschool Final Fantasy twist.</span></p>
        </section><!--end productDesc1-->
        <section class="productDescLargeRight">
            <img src="images/craft_image1.jpg" alt="craft image" width="410" height="300">
            <p><span>Use life skills to gather raw materials and create a variety of items...</span></p>
        </section><!--end productDesc4-->
    </section><!--end productInfo-->

CSS

.productDescLargeLeft {
    float:left;
    width: 410px;
}

.productDescLargeLeft p {
    background: none;
    bottom: 120px;
    text-align: center;
    font-family: 'Times New Roman', Times, serif;
    font-size: 16px;
}

.productDescLargeRight {
    float:right;
    width: 410px;
}

.productDescLargeRight p {
    background: none;
    bottom: 120px;
    text-align: center;
    font-family: 'Times New Roman', Times, serif;
    font-size: 16px;
}

最佳答案

在您的示例中,您缺少 position: absolute 尽管正如您所说,这导致布局很奇怪,在这个 fiddle 中测试后 https://jsfiddle.net/0tcjpbkc/1/我发现只需在 P 元素上将最大宽度设置为以匹配图像即可:

.productDescLargeRight p {
    background: none;
    bottom: 120px;
    text-align: center;
    font-family: 'Times New Roman', Times, serif;
    font-size: 16px;
    max-width: 410px;
    position: absolute;
}

然后文本愉快地位于图像容器中,就像在 fiddle 中一样,您还需要对其他图像容器执行相同操作 .productDescLargeLeft p

关于html - 文本不留在 div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40987553/

相关文章:

javascript - 在使整个 div 可点击时,标签从开始的地方开始自动关闭

html - Div 长于 100%

html - css 的内联 block 属性未按预期运行

html - 如何在 bootstrap 中居中 div 列

html - SMACSS、BEM 和 OOCSS 不是可移植的吗?

javascript - 从 A 点到 B 点的图像过渡

javascript - 为什么 offsetWidth 显示不正确的值?

css - 在 CSS 中保留背景图像的同时移动文本

css - DIV 在浏览器调整大小时换行

按钮上的 CSS3 颜色过渡