我有一个简单的网页,其中有一个.topnav
酒吧和一个.container
,其中有几个元素。我正在尝试将.container
(而不是.topnav
)居中放置在页面的body
之内,以便它将垂直居中。然而,当我尝试过的造型body
有:中心只有一个div中的两个元素之一?
display:flex;
align-items:center;
justify-content:center;
无论是.topbar
和.container
居中。我如何去垂直集中.container
?
body,
html {
height: 100%;
}
.contact_box {
text-align: center;
background-color: red;
border: 1px solid #c0c0c0;
border-radius: 5px;
height: 20em;
width: 25em;
box-shadow: 1px 1px 6px #757677;
float: left;
}
.contact_box img {
margin-top: 3.3em;
margin-bottom: 1.2em;
height: 3em;
width: 3em;
}
.contact_box h3 {
color: #6d6d6d;
}
#contact .container {
display: flex;
align-items: center;
justify-content: center;
}
<body id="contact">
<div class="topnav" id="myTopnav">
<a href="index.html">Home</a>
<a href="about.html">About</a>
<a href="#" class="selected">Contact</a>
<a class="icon" onclick="myFunction()">☰</a>
</div>
<div class="container">
<div class="row" id="contact_section">
<div class="contact_box" class="col-md-4">
<a href="https://github.com/" target="_blank"><img src="resources/github_logo.png" alt="Github"></a>
<br>
<h3>GitHub</h3>
</div>
<div class="contact_box" class="col-md-4">
<a href="https://www.linkedin.com" target="_blank"><img src="resources/linkedin_logo.png" alt="LinkedIn"></a>
<h3>LinkedIn</h3>
</div>
<div class="contact_box" class="col-md-4">
<img src="resources/email_icon.png" alt="EMAIL">
<h3>Email</h3>
</div>
</div>
</div>
</body>
下面是它现在的样子:
由于您不想在导航栏上使用Flexbox行为,因此最简单的解决方案可能不是将导航栏放入Flexbox中。 –
它看起来不像导航栏在弹性盒中。只有'.container'被设置为'display:flex'。我很困惑。 – showdev
啊,你是对的@showdev。我也很困惑 - Nezdiro能否请你澄清你在问什么?你在这里发布的代码片段与你的描述不符...... –