在上面的代码中,我试图仅使用h1
在没有任何CSS样式的情况下将bootstrap 4
元素在图像上垂直和水平居中。 该怎么做?
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div id="navbar" class="p-2">
<nav class="navbar navbar-expand-lg navbar-light">
<a class="navbar-brand" href="BR Architects"><span id="BR">BR</span> Architects</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="#">Projects</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</nav>
</div>
<div id="header-img">
<div class="card text-center">
<img class="card-img" src="https://www.w3schools.com/w3images/architect.jpg" alt="Card image">
<div class="card-img-overlay">
<h1 class="card-title"><span class="text-white">BR</span> Architects</h1>
</div>
</div>
</div>
谢谢!
答案 0 :(得分:1)
目前我能想到的最简单的解决方案
将此行添加到您的card-img-overlay
“ d-flex justify-content-center align-items-center
”
示例:
<div class="card-img-overlay d-flex justify-content-center align-items-center">
<h1 class="card-title "><span class="text-white">BR</span> Architects</h1>
</div>
希望这会有所帮助。
答案 1 :(得分:1)
一种解决方案是将d-flex
类添加到card-img-overlay
中,然后将justify-content-center类用于水平对齐,将align-items-center类用于垂直对齐。
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
<div id="header-img">
<div class="card">
<img class="card-img" src="https://www.w3schools.com/w3images/architect.jpg" alt="Card image">
<div class="card-img-overlay d-flex align-items-center justify-content-center">
<h1 class="card-title">
<span class="text-white">BR</span> Architects
</h1>
</div>
</div>
</div>
答案 2 :(得分:0)
<div id="header-img">
<div class="row justify-content-center align-self-center">
<img class="card-img" src="https://www.w3schools.com/w3images/architect.jpg" alt="Card image">
<div class="card-img-overlay">
<h1 class="card-title"><span class="text-white">BR</span> Architects</h1>
</div>
</div>
</div>
答案 3 :(得分:0)
顾名思义,text-center
类仅使文本居中,您可能想使用align-items-center
(用于垂直对齐)和/或justify-content-center
(用于水平对齐)来居中其他类型的元素)。
官方文档中的更多信息: Vertical alignment Horizontal alignment