我一直很难在父元素中垂直对齐div。我尝试了vertical-align: center;
,display: inline-block;
以及我在垂直对齐方面可以找到的任何其他内容,但实际上并没有对我有用。如果这会影响任何事情,我正在使用bootstrap。
我想在div #banner中垂直对齐div .bannerContent。 我希望这是一个简单的修复,我只是遗漏了一些简单的东西。
当我尝试使用display: table-cell;
时,它已经破坏了水平居中。
我已经包含了整个文件,以防万一从其他地方破坏它。万分感谢!
这是我的HTML:
<!doctype html>
<html>
<head>
<title>My title here</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/main.css">
<link href="https://fonts.googleapis.com/css?family=Oswald" rel="stylesheet">
</head>
<body>
<div id="banner">
<div class="bannerAlign">
<nav class="navbar navbar-default navbar-static-top">
<ul class="nav navbar-nav right">
<li><a href="#">Contact</a></li>
<li><a href="#">Help</a></li>
<li><a href="#">Another Link</a></li>
</ul>
</nav>
</div>
<div class="container bannerContent"> <!-- I want this div vertically aligned -->
<img id="profile" src="images/profile.jpg" />
<header>
<h1>My name here</h1>
<h4>More words here</h4>
</header>
</div>
<span class="glyphicon glyphicon-menu-down down-arrow"></span>
</div>
<div class="container">
<div class="content">
<div class="well">
<h1>Some content here</h1>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
我的CSS:
body {
background: url(../images/yosemite.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
font-family: Oswald;
}
.navbar {
background: none;
border: none;
color: white;
}
.right {
float: right;
}
.navbar a {
color: #fff !important;
text-transform: uppercase;
}
#banner {
background: rgba(0, 0, 0, .6);
color: white;
height: 100vh;
margin-bottom: 50px;
letter-spacing: 1.3px;
}
.bannerAlign {
margin-right: 5%;
}
.bannerContent {
text-align: center;
vertical-align: middle; /* Doesn't do anything */
}
.bannerContent header {
display: inline-block;
margin-left: 3em;
}
.bannerContent header > h1 {
margin-bottom: 13px;
}
#profile {
width: 16vw;
border-radius: 100%;
}
.down-arrow {
position: absolute;
bottom: 0;
top: 95%;
display: block;
left: 50%;
font-size: 2.5em;
}
/* latin-ext */
@font-face {
font-family: 'Oswald';
font-style: normal;
font-weight: 400;
src: local('Oswald Regular'), local('Oswald-Regular'), url(https://fonts.gstatic.com/s/oswald/v11/Qw6_9HvXRQGg5mMbFR3Phn-_kf6ByYO6CLYdB4HQE-Y.woff2) format('woff2');
unicode-range: U+0100-024F, U+1E00-1EFF, U+20A0-20AB, U+20AD-20CF, U+2C60-2C7F, U+A720-A7FF;
}
/* latin */
@font-face {
font-family: 'Oswald';
font-style: normal;
font-weight: 400;
src: local('Oswald Regular'), local('Oswald-Regular'), url(https://fonts.gstatic.com/s/oswald/v11/_P8jt3Y65hJ9c4AzRE0V1OvvDin1pK8aKteLpeZ5c0A.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215;
}
答案 0 :(得分:0)
只需使用以下css:
#banner {
display: flex;
align-items: center;
}
答案 1 :(得分:0)
您可以使用此:css-tricks。最简单的方法是使用flexbox作为超级用户写的。
答案 2 :(得分:0)
body {
background: url(../images/yosemite.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
font-family: Oswald;
width:100%;
max-width:100%;
overflow:hidden;
}
.navbar {
background: none;
border: none;
color: white;
}
.right {
float: right;
}
.navbar a {
color: #fff !important;
text-transform: uppercase;
}
#banner {
background: rgba(0, 0, 0, .6);
color: white;
height: 100vh;
margin-bottom: 50px;
letter-spacing: 1.3px;
display:flex;
max-width:100%;
flex-direction:column;
}
.bannerAlign {
margin-right: 5%;
}
.bannerContent {
text-align: center;
flex-grow:1;
/*vertical-align: middle;*/
display:flex;
align-items:center;
justify-content:center;
}
.bannerContent header {
display: inline-block;
margin-left: 3em;
}
.bannerContent header > h1 {
margin-bottom: 13px;
}
#profile {
width: 16vw;
border-radius: 100%;
}
.down-arrow {
position: absolute;
bottom: 0;
top: 95%;
display: block;
left: 50%;
font-size: 2.5em;
}
/* latin-ext */
@font-face {
font-family: 'Oswald';
font-style: normal;
font-weight: 400;
src: local('Oswald Regular'), local('Oswald-Regular'), url(https://fonts.gstatic.com/s/oswald/v11/Qw6_9HvXRQGg5mMbFR3Phn-_kf6ByYO6CLYdB4HQE-Y.woff2) format('woff2');
unicode-range: U+0100-024F, U+1E00-1EFF, U+20A0-20AB, U+20AD-20CF, U+2C60-2C7F, U+A720-A7FF;
}
/* latin */
@font-face {
font-family: 'Oswald';
font-style: normal;
font-weight: 400;
src: local('Oswald Regular'), local('Oswald-Regular'), url(https://fonts.gstatic.com/s/oswald/v11/_P8jt3Y65hJ9c4AzRE0V1OvvDin1pK8aKteLpeZ5c0A.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div id="banner">
<div class="bannerAlign">
<nav class="navbar navbar-default navbar-static-top">
<ul class="nav navbar-nav right">
<li><a href="#">Contact</a></li>
<li><a href="#">Help</a></li>
<li><a href="#">Another Link</a></li>
</ul>
</nav>
</div>
<div class="container bannerContent"> <!-- I want this div vertically aligned -->
<img id="profile" src="images/profile.jpg" />
<header>
<h1>My name here</h1>
<h4>More words here</h4>
</header>
</div>
<span class="glyphicon glyphicon-menu-down down-arrow"></span>
</div>
<div class="container">
<div class="content">
<div class="well">
<h1>Some content here</h1>
</div>
</div>
</div>