如何使两列垂直对齐?

时间:2018-11-20 19:16:32

标签: css twitter-bootstrap

我有一个用于引导超长波的代码,但是我想使左侧的列与h1垂直对齐,并且段落与右侧的图像垂直对齐,以便标题和段落与中心对齐图片。你知道如何实现吗?

您还知道为什么超长机与导航栏不对齐? “标题”比左侧的导航栏图像更左侧,并且超大屏幕上的图像比导航栏的最后一项更右侧。你知道为什么吗?

我在这里演示了这些问题:

https://jsfiddle.net/upmanct4/

HTML:

<div class="container">
    <nav class="navbar navbar-expand-lg navbar-light py-4">
        <a class="navbar-brand">
            <img src="https://via.placeholder.com/110" width="110" height="25">
        </a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
                aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav ml-auto">
                <li class="nav-item active">
                    <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Link</a>
                </li>
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button"
                       data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Dropdown
                    </a>
                    <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                        <a class="dropdown-item" href="#">Action</a>
                        <a class="dropdown-item" href="#">Another action</a>
                        <div class="dropdown-divider"></div>
                        <a class="dropdown-item" href="#">Something else here</a>
                    </div>
                </li>
                <li class="nav-item">
                    <a class="nav-link disabled" href="#">Disabled</a>
                </li>
            </ul>
        </div>
    </nav>
</div>

<div class="jumbotron jumbotron-fluid">
    <div class="container">
        <div class="row">
            <div class="col-7">
                <h1 class="display-5 font-weight-bold text-dark">Title</h1>
                <p class="lead text-light">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestias earum tempore, eveniet quis quae quo placeat ullam, maxime harum at corrupti, vel, cumque error pariatur veritatis doloribus libero quibusdam hic.</p>
            </div>

            <div class="col-5">
                <img src="https://via.placeholder.com/490" width="490" height="300">
            </div>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

在.row上使用.align-items-center 这将在.row

中将.col-7和.col-5垂直对齐
<div class="row align-items-center">
    <div class="col-7">
        <h1 class="display-5 font-weight-bold text-dark">Title</h1>
        <p class="lead">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestias earum tempore, eveniet quis quae quo placeat ullam, maxime harum at corrupti, vel, cumque error pariatur veritatis doloribus libero quibusdam hic.</p>
    </div>

    <div class="col-5">
        <img src="https://via.placeholder.com/490" width="490" height="300">
    </div>
</div>

或者,您可以在.col-7上使用.align-self-center 这只会垂直对齐.col-7 如果.col-7的高度恰好大于.col-5的高度,则.col-5将保留在.row的顶部,因为.col-5不在.row

内垂直对齐
<div class="row">
    <div class="col-7 align-self-center">
        <h1 class="display-5 font-weight-bold text-dark">Title</h1>
        <p class="lead">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestias earum tempore, eveniet quis quae quo placeat ullam, maxime harum at corrupti, vel, cumque error pariatur veritatis doloribus libero quibusdam hic.</p>
    </div>

    <div class="col-5">
        <img src="https://via.placeholder.com/490" width="490" height="300">
    </div>
</div>