【问题标题】:Background color not filling full entire row height when using my-auto使用 my-auto 时背景颜色未填满整个行高
【发布时间】:2021-12-29 13:17:31
【问题描述】:

我有一个引导行,我希望第一列有蓝色背景,文本垂直和水平居中。

如果我不水平居中文本,它可以工作 HTML

 <div class = "shadow p-3 mb-5 bg-white rounded" style="padding: 0 !important;">
    <div class="container">
            <div class="row main-row">
                <div class="col-2 booking-flight-info-dir text-center   " style="">
                        <img  src="<?= plugin_dir_url( __DIR__ ).'/img/out_flight_icon.png'?>" alt="out_flight_icon.png" style="padding-left:10px " >
                        <span >תיסת הלוך</span>
                </div>
                <div class="col-2 booking-flight-info-flightnum text-center  my-auto" style="">
                <span>טיסה מספר:</span><BR>
                <span style="font-size: x-large;font-weight: bold;text-align: center"><?= $rowData->outTrip->flightNmr?></span>
                </div>
            </div>
        </div>
</div>

without my-auto

但是,一旦我将 my-auto 添加到水平居中的 tex 中,背景颜色就不会填充整个行高,只会填充文本区域。

<div class = "shadow p-3 mb-5 bg-white rounded" style="padding: 0 !important;">
<div class="container">

        <div class="row main-row">

            <div class="col-2 booking-flight-info-dir text-center my-auto " style="">
                    <img  src="<?= plugin_dir_url( __DIR__ ).'/img/out_flight_icon.png'?>" alt="out_flight_icon.png" style="padding-left:10px " >
                    <span >תיסת הלוך</span>
            </div>

            <div class="col-2 booking-flight-info-flightnum text-center  my-auto" style="">
            <span>טיסה מספר:</span><BR>
            <span style="font-size: x-large;font-weight: bold;text-align: center"><?= $rowData->outTrip->flightNmr?></span>
            </div>
        </div>

    </div>


</div>

with my-auto

【问题讨论】:

    标签: twitter-bootstrap background row height fill


    【解决方案1】:

    以下内容基于 Bootstrap 5(但在 Bootstrap 4 上也可以正常工作)。

    我不使用my-auto,而是添加justify-content-center align-items-center d-flextext-center 也不需要。

    <div class = "shadow p-3 mb-5 bg-white rounded" style="padding: 0 !important;" dir="rtl">
        <div class="container">
                <div class="row main-row">
                    <div class="col-2 booking-flight-info-dir justify-content-center align-items-center d-flex" style="">
                            <i class="fas fa-plane"></i>
                            <span >תיסת הלוך</span>
                    </div>
                    <div class="col-2 booking-flight-info-flightnum text-center  my-auto" style="">
                    <span>טיסה מספר:</span><br>
                    <span style="font-size: x-large;font-weight: bold;text-align: center">22</span>
                    </div>
                </div>
            </div>
    </div>
    

    看到它在行动12

    不要只是复制和粘贴,因为我修改了你的 HTML 以便能够通过 jsfiddle 运行。

    如果您使用my-auto,浏览器会将该元素包装到它的实际高度,然后将顶部和底部边距设置为自动。这样一来,您就不能拥有完整的元素高度并将文本垂直居中对齐。

    【讨论】:

    • 谢谢,我最后做的是在课堂上使用 d-table 然后 div d-table-cell align-middle
    【解决方案2】:

    这样解决

    <div class="shadow p-3 mb-2 bg-white rounded" style="padding: 0 !important;">
                <div class="card">
                    <div class="row main-row  ">
                        <div class="col-md-4">
                        <div class="row flight-num-row h-100 ">
                            <div class="col booking-flight-info-dir text-center d-table" style="">
                            <div class="d-table-cell align-middle">
                                <img src="<?= plugin_dir_url(__DIR__) . '/img/out_flight_icon.png' ?>" alt="out_flight_icon.png"
                                     style="padding-left:10px ">
                                <span>טיסת הלוך</span>
                            </div>
                        </div>
    
                        <div class="col booking-flight-info-flightnum text-center   d-table" style="line-height: 1">
                             <div class="d-table-cell align-middle">
                            <span>טיסה מספר:</span><BR>
                            <span style="font-size: x-large;font-weight: bold;text-align: center;"><?= $rowData->outTrip->airline . ' ' . $rowData->outTrip->flightNmr ?></span>
                             </div>
                        </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 2020-07-31
      • 2014-08-28
      • 1970-01-01
      • 2021-05-24
      • 2015-01-15
      • 2021-12-02
      • 1970-01-01
      相关资源
      最近更新 更多