【问题标题】:Align text and images on the same vertical line在同一垂直线上对齐文本和图像
【发布时间】:2020-10-07 07:08:51
【问题描述】:

我有以下使用 bootstrap 4 的标记:-

<div class="container">
    <div class="row ">
        <div class="col-xl-12">
            <img src="~/img/img1.png" style="height:60.3px;width:750px" class="mx-auto d-none d-sm-block" />
            <p  style="color: #2EBDBE;font-weight:bold;font-size:13px;margin-bottom: 0px;">...</p>
            <p  style="color: #2EBDBE;font-weight:bold;font-size:13px;margin-bottom: 0px;">...</p>

            <img src="~/img/img2.png" class="mx-auto d-block" />

            <p  style="color: #2EBDBE;font-weight:bold;font-size:13px;margin-bottom: 0px;">...</p>
            <p  style="color: #2EBDBE;font-weight:bold;font-size:13px;margin-bottom: 0px;">...</p>


        </div>

    </div>

</div>

目前我得到了这个布局:-

文本左对齐时图像居中的位置,那么我如何强制文本与图像具有相同的垂直对齐方式?

【问题讨论】:

    标签: css twitter-bootstrap bootstrap-4 responsive-design


    【解决方案1】:

    试试这个,但编辑类或其他任何东西以使其在您的网站上运行:

    <div class="container" style="position:relative; display:inline-block;">
      <img class="mx-auto d-none d-sm-block" style="display:block; height:60.3px;width:750px" src="https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/close-up-of-tulips-blooming-in-field-royalty-free-image-1584131603.jpg">
      <div class="text-box" style="position:relative; height:100%; text-align: center; display:flex; justify-content: center; align-items: flex-start; width: 100%;">
        <p class="centeredTxt" style="display: block; color: #333;">Text is centered under image</p>
      </div>
      <div class="text-box" style="position:relative; height:100%; text-align: center; display:flex; justify-content: center; align-items: flex-start; width: 100%;">
        <p class="centeredTxt" style="display: block; color: #333;">Text is centered under image 2</p>
      </div>
    </div>
    

    由于我没有完整的代码,因此很难理解这在您的代码中是如何工作的,但我编辑了答案以更好地满足您的要求。

    【讨论】:

    • 如果&lt;p&gt; 包含不跨越多行的单词,则此方法有效,但对于长时间&lt;p&gt; 这没有任何效果..
    • 我认为您必须将图像和 &lt;p&gt; 标记作为容器包装在同一个 div 中,以便能够根据该新 div 将它们对齐在中心。您需要帮助进行设置吗?我可以举个例子。
    • 如果你能设置一个例子会很棒
    • 好的。我将编辑我的代码并粘贴我扔在一起的代码,我没有使用你的类名,因为你比我更清楚你可以/不能丢弃什么。但是看看这些 div 的样式是如何设置的,将其复制到您的需要,您应该有一个完美工作的 div,它也是响应式的。此代码适用于 1 张图片,只需粘贴其中 2 张,您就可以设置不同的样式。
    • 我会尝试在其中实现你的代码,但我没有你的图片,它可能不准确。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    • 1970-01-01
    • 2011-09-25
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多