html - 问题保持元素在 float div 中居中

标签 html css

我一直在做一个元素,通过各种方法设法让文本在我使用的平台上小心地放在图像旁边。图片和文字均居中,但文字左对齐。

页面应该是响应式的,并且通常运行良好,但是随着浏览器尺寸的缩小,最终文本会被推到下一行,这是应该的。不幸的是,当发生这种情况时,它不再居中,并且图像变得偏离中心。

我已经被困了一段时间,试图找出在我当前的主题/用例中可以修复的确切内容 - 并且会喜欢任何建议。 https://jsfiddle.net/xLwy83rj/

<div style="width: 50%; margin: 0 auto; min-width: 250px;">
<!--I edit the margins in the next block to better prevent the text from flowing over the bottom of the image if its a smaller or larger block of text -->
<img style="float: left; margin-right: 1rem; margin-top: 15px; margin-bottom: 15px;" src="http://via.placeholder.com/128x128" />
</div>

<div style="display: inline-block; width: 200px;">
<p style="text-align: left;"><strong>Attack Gloves</strong><br />
Base DPS: ~<strong>572.5</strong><br />
Attack Range: <strong>1100</strong><br />
Heat Buildup: <strong>95</strong><br />
Heat Buildup: <strong>95</strong><br />
Heat Buildup: <strong>95</strong><br />
Heat Buildup: <strong>95</strong></p>

具体来说,目标是始终保持 2 个主要元素居中,它们之间只有一点空间。如果浏览器太小而无法并排显示它们,它会将文本向下推到下方,并使所有内容保持居中。

最佳答案

demo used flex

.main {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
}

.p{
    margin: 0;
}
<div class="main">
<div class="top">
<img class="alignnone second" src="http://via.placeholder.com/128x128" />
</div>

<div class="third">   
    <p class="p"><strong>Attack Gloves</strong><br />
    Base DPS: ~<strong>572.5</strong><br />
    Attack Range: <strong>1100</strong><br />
    Heat Buildup: <strong>95</strong><br />
    Heat Buildup: <strong>95</strong><br />
    Heat Buildup: <strong>95</strong><br />
    Heat Buildup: <strong>95</strong></p>
</div>
</div>

关于html - 问题保持元素在 float div 中居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45031939/

相关文章:

html - 当图片大小不同时,如何制作 HTML/CSS 幻灯片作为背景填充屏幕?

jQuery UI ipod 向下钻取菜单

javascript - 直接移动带有范围的元素 -javascript

javascript - 将 +1 附加到电话号码字段值

javascript - 来自 JavaScript 函数的 Ajax 异步和循环 PHP 请求

html - 使按钮响应并水平居中

html - 论文BG图像: Make It Appear To Fill Window

javascript - 鼠标移动时在 Canvas 上显示矩形

php - 当我使用 codeigniter url 时,Materialize 消失了

HTML5 - 符号和图标