在网上挖了大约5个小时之后,我得到的唯一结果是该列的每个孩子都会水平排列。
我已经将导航栏包含在内,因为有时会干扰它。
<header>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark sticky-top">
<div class="container">
<a class="navbar-brand" href="/">Name</a>
<!-- Collapse button -->
<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>
<!-- Collapsible content -->
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<!-- Links -->
<div class="navbar-nav">
<form class="form-inline" action="/search">
<input class="form-control bg-dark text-light mr-sm-2 py-1" placeholder="Search" aria-label="Search" name="q" required type="search">
</form>
<a class="nav-item nav-link" href="#">One</a>
<a class="nav-item nav-link" href="#">Two</a>
<a class="nav-item nav-link" href="#">Three</a>
<!-- Right alignment needed -->
<a class="nav-item nav-link" href="/sign-in/"><i class="fa fa-user-circle-o" aria-hidden="true"></i> Sign in</a>
</div>
</div>
</div>
</nav>
<!-- HERE - Hero image -->
<div id="intro">
<div class="container">
<div class="row">
<div class="col-md-10">
<h2 class="display-4 mb-2">My website</h2>
<hr>
<h4 class="my-4">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</h4>
<a href="#">
<button class="btn btn-outline-light px-4 py-2">Action <i class="fa fa-rocket" aria-hidden="true"></i></button>
</a>
<div>
</div>
</div>
</div>
</header>
CSS
html,
body,
header,
#intro {
height: 100%;
}
#intro {
background: url("via.placeholder.com/2000x1000") no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
color: white;
}
我尝试了一系列的东西并删除了大部分内容但仍然有这些尝试,但我清理了html。
.parent {
position: relative;
margin: auto;
text-align: center;
align-items: center;
}
.container {
position: relative;
text-align: center;
color: white;
}
.centered {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
还试过d-flex解决方案,证明解决方案和其他解决方案......
所以我现在相信它与bootstrap有关,但我不知道。
编辑:忘了说,水平对齐可以通过以下方式实现:
margin: auto;
text-align: center;
align-items: center;
列上的。
答案 0 :(得分:0)
这是你的意思吗?
https://jsfiddle.net/Aorus/z3p4s0t9/1/
您缺少的是将以下类添加到您的行中;
同时将标题或任何其他父标题设置为高度:100%。
html,
body,
header{
height: 100%;
}
#bg-container {
height: 100%;
background-image: url("http://i350.photobucket.com/albums/q409/spawn_065/galaxy.jpg");
background-position: center;
background-size: cover;
}
#image-container {
position: relative;
height: 300px;
width: 300px;
background-color: green;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark sticky-top">
<div class="container">
<a class="navbar-brand" href="/">Name</a>
<!-- Collapse button -->
<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>
<!-- Collapsible content -->
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<!-- Links -->
<div class="navbar-nav">
<form class="form-inline" action="/search">
<input class="form-control bg-dark text-light mr-sm-2 py-1" placeholder="Search" aria-label="Search" name="q" required type="search">
</form>
<a class="nav-item nav-link" href="#">One</a>
<a class="nav-item nav-link" href="#">Two</a>
<a class="nav-item nav-link" href="#">Three</a>
<!-- Right alignment needed -->
<a class="nav-item nav-link" href="/sign-in/"><i class="fa fa-user-circle-o" aria-hidden="true"></i> Sign in</a>
</div>
</div>
</div>
</nav>
<div id="bg-container">
<div class="row align-items-center justify-content-center h-100">
<div class="col-4">
<div id="image-container">
Image
</div>
</div>
</div>
</div>
</header>