尝试使用flexbox进行简单的网站娱乐。以下是最终目标(此步骤):
非常简单。但我正在努力调整文本。我能够将标题放在中心位置。主要是下面的段落,我无法居中。有没有办法将该段落垂直和水平放在其外部容器中? Flexbox新手,想要任何提示!以下是我的codepen和相关代码的链接:https://codepen.io/gkunthara/pen/qjympg
HTML
<div class = "content-container">
<h1 class = "main-header"> Connect with Subscribers Effortlessly </h1>
<p class = "sub-header"> Email Marketing Platform for Bloggers &
Authors. </p>
</div>
CSS
.content-container .main-header{
box-sizing: border-box;
font-weight: 600;
font-size: 45px;
height:75px;
padding-left: 5px;
padding-right: 5px;
color: rgb(51, 51, 51);
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
border-style: solid;
}
.content-container .sub-header {
border-style: solid;
display: flex;
height: 25px;
margin-top: 150px;
}
答案 0 :(得分:1)
如果您移除margin-top
规则中的.content-container .sub-header
并将flex-direction: column; align-items: center;
添加到.content-container
,则文字将正确居中
Stack snippet
* {
box-sizing: border-box;
}
.header-container {
display: flex;
width: 100%;
height: 100px;
align-items: center;
justify-content: space-between;
padding-left: 50px;
padding-right: 75px;
}
.header-container .logo {
width: 250px;
}
.header-container .logo .sb {
max-width: 100%;
}
.header-container .nav-bar-container {
display: flex;
}
.nav-link {
text-decoration: none;
color: #4B4B4B;
font-weight: bold;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
padding: 15px;
}
.content-container {
width: 100%;
height: 300px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
border-style: solid;
margin-top: 15px;
}
.content-container .main-header {
box-sizing: border-box;
font-weight: 600;
font-size: 45px;
height: 75px;
padding-left: 5px;
padding-right: 5px;
color: rgb(51, 51, 51);
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
border-style: solid;
}
.content-container .sub-header {
border-style: solid;
display: flex;
height: 25px;
}
/* Resize window to see its effect */
@media( max-width: 700px) {
.header-container {
flex-direction: column;
}
}
<div class="header-container">
<a class="logo"> <img class="sb" src="logo.png" alt="logo"> </a>
<nav class="nav-bar-container">
<a class="nav-link" href="#"> Tour </a>
<a class="nav-link" href="#"> Pricing </a>
<a class="nav-link" href="#"> Medium </a>
<a class="nav-link" href="#"> Sign Up </a>
<a class="nav-link" href="#"> Login </a>
</nav>
</div>
<div class="content-container">
<h1 class="main-header"> Connect with Subscribers Effortlessly </h1>
<p class="sub-header"> Email Marketing Platform for Bloggers & Authors. </p>
</div>
如果您打算将.content-container .sub-header
w / o考虑在内,请考虑.content-container .main-header
,您需要定位.content-container .main-header
绝对
有了这个,它将与我在第一个解决方案中添加的flex-direction: column;
一起使用或没有。{/ p>
请注意,在较宽的屏幕上,您需要调整两者,否则它们会重叠
在.content-container .main-header
position: absolute;
top: 0;
left: 0;
right: 0;
text-align: center;
答案 1 :(得分:1)
你只需做两件事。
1。添加
flex-direction: column;
align-items: center;
到你的.content-container
班。
2。从margin-top: 150px
移除.content-container .sub-header
,应该是它。
* {
box-sizing: border-box;
}
.content-container {
width: 100%;
height: 300px;
display: flex;
justify-content: center;
border-style: solid;
margin-top: 15px;
flex-direction: column;
align-items: center;
}
.content-container .main-header {
box-sizing: border-box;
font-weight: 600;
font-size: 30px;
height: 75px;
padding-left: 5px;
padding-right: 5px;
color: rgb(51, 51, 51);
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
border-style: solid;
}
.content-container .sub-header {
border-style: solid;
height: 25px;
}
&#13;
<div class="content-container">
<h1 class="main-header"> Connect with Subscribers Effortlessly </h1>
<p class="sub-header"> Email Marketing Platform for Bloggers & Authors. </p>
</div>
&#13;
Here is the fiddle to play with.
希望这有帮助。