我有一个页面,其中包含一个小图像和右侧的文本. 如何使用图像作为水平中心来将一切置于中心?

<div style="padding: 20px; border-radius: 10px; text-align: center;">
<h2 style="margin-bottom: 20px;"><b>Div Title</b></h2>

    <div style="display: flex; flex-direction: column; align-items: center; margin-bottom: 10px; width: 200px; margin-left: auto; margin-right: auto;">
        <div style="display: flex; align-items: center; justify-content: center;">
            <img src="https://upload.wikimedia.org/wikipedia/commons/f/f9/Phoenicopterus_ruber_in_S%C3%A3o_Paulo_Zoo.jpg" alt="medal" style="height: 50px; width: 50px; margin-right: 10px;">
            <div style="text-align: left;">
                <p style="margin: 0;">Title 1</p>
                <p style="margin: 0;"><b>Subtitle 1</b></p>
            </div>
        </div>
    </div>
    
    <div style="display: flex; flex-direction: column; align-items: center; width: 200px; margin-left: auto; margin-right: auto;">
        <div style="display: flex; align-items: center; justify-content: center;">
            <img src"https://upload.wikimedia.org/wikipedia/commons/f/f9/Phoenicopterus_ruber_in_S%C3%A3o_Paulo_Zoo.jpg" alt="medal" style="height: 50px; width: 50px; margin-right: 10px;">
            <div style="text-align: left;">
                    <p style="margin: 0;">Title 2</p>
                    <p style="margin: 0;"><b>Subtitle 2</b></p>
            </div>
        </div>
    </div>
</div>

谢谢

推荐答案

对于您的情况,您不需要添加更多css.

如果您想将图像和字幕置于标题下方的中心,则实际上可以修改您的HTML struct ,使标题位于图像和字幕上方:

<div style="padding: 20px; border-radius: 10px; text-align: center;">
    <h2 style="margin-bottom: 20px;"><b>Div Title</b></h2>

    <div style="display: flex; flex-direction: column; align-items: center; margin-bottom: 10px; width: 200px; margin-left: auto; margin-right: auto;">
      <p style="margin: 0;">Title 1</p>
        <div style="display: flex; align-items: center; justify-content: center;">
            <img src="https://upload.wikimedia.org/wikipedia/commons/f/f9/Phoenicopterus_ruber_in_S%C3%A3o_Paulo_Zoo.jpg" alt="medal" style="height: 50px; width: 50px; margin-right: 10px;">
            <div style="text-align: left;">
                <p style="margin: 0;"><b>Subtitle 1</b></p>
            </div>
        </div>
    </div>

    <div style="display: flex; flex-direction: column; align-items: center; margin-bottom: 10px; width: 200px; margin-left: auto; margin-right: auto;">
      <p style="margin: 0;">Title 2</p>
        <div style="display: flex; align-items: center; justify-content: center;">
            <img src="https://upload.wikimedia.org/wikipedia/commons/f/f9/Phoenicopterus_ruber_in_S%C3%A3o_Paulo_Zoo.jpg" alt="medal" style="height: 50px; width: 50px; margin-right: 10px;">
            <div style="text-align: left;">
                <p style="margin: 0;"><b>Subtitle 2</b></p>
            </div>
        </div>
    </div>
</div>

您的p(或title元素)标签是一个显示块(默认情况下),这会导致放置行:

enter image description here

欲了解更多信息,您可以查看The Paragraph element

Html相关问答推荐

将ANGLE模板高效地迁移到ANGLE 17中引入的新语法

将表格背景图像置于行背景 colored颜色 之上

我的Django应用程序中出现模板语法错误

carousel 的垂直滚动按钮居中

如何在VB.NET中用打印预览和直接打印的方式在Html中打印数据表的内容,包括页眉/设置纸张/方向/适合页面/总计

如何删除FONT创建的文本下方的空格

SVG的动态CSS

在屏幕上每行 css 中的特定列居中

如何将 元素与常规文本垂直对齐

:after 伪元素没有出现,即使它有 content 属性

如何使Bootstrap 5卡可点击

Angular中如何向单选按钮添加验证

如何使用jQuery将我制作的通用头部和底部加载到多个HTML文件中?

带有伪元素的不规则形状的渐变边框

使用 TailwindCSS 将徽标放在左上角,将菜单放在右上角

在 html 邮箱的左侧和右侧制作多个元素很热门吗?

CSS 变量不适用于自定义属性回退值

当我希望它只横向滚动时,可滚动菜单也会上下移动

动态使用时波浪号不转换为绝对路径

下拉菜单项在页面加载时显示,需要隐藏直到悬停