【发布时间】:2020-05-27 08:49:48
【问题描述】:
我正在尝试为网站创建统计信息区域。在台式机上应该是两个,在移动设备上应该是堆叠的。如果内部 DIV 的内容被堆叠起来会很好,但我不知道该怎么做。
这是我的代码
.site-stats-wrap {
width: 100%;
margin-left: auto;
margin-right: auto;
background: pink;
margin-top: 10px;
margin-bottom: 10px;
}
@media only screen and (max-width: 500px) {
.site-stats-wrap {
width: 100%;
margin-left: auto;
margin-right: auto;
background: pink;
margin-top: 15px;
margin-bottom: 15px;
}
}
.site-stats-group {
width: 100%;
display: table;
}
.site-stat-title {
width: 50%;
float: left;
}
@media only screen and (max-width: 500px) {
.site-stat-title {
width: 100%;
float: left;
}
}
.site-stat-info {
width: 50%;
float: left;
}
@media only screen and (max-width: 500px) {
.site-stat-info {
width: 100%;
float: left;
}
}
.site-stat-title-display {
width: 80%;
margin-left: auto;
margin-right: auto;
text-align: center;
}
.site-stat-info-display {
width: 80%;
margin-left: auto;
margin-right: auto;
text-align: center;
margin-top: 20px;
margin-bottom: 20px;
}
.site-stat-title {
font-family: 'Roboto', sans-serif;
font-size: 35px;
line-height: normal;
margin: 0;
}
@media only screen and (max-width: 500px) {
.site-stat-title {
font-family: 'Roboto', sans-serif;
font-size: 25px;
line-height: normal;
margin: 0;
}
}
.site-stat-subtitle {
font-family: 'Roboto', sans-serif;
font-size: 30px;
line-height: normal;
margin: 0;
}
@media only screen and (max-width: 500px) {
.site-stat-subtitle {
font-family: 'Roboto', sans-serif;
font-size: 20px;
line-height: normal;
margin: 0;
}
}
<div class="site-stats-wrap">
<div class="site-stats-group">
<div class="site-stat-title">
<div class="site-stat-title-display">
<p class="site-stats-title">Statistics</p>
</div>
</div>
<div class="site-stat-info">
<div class="site-stat-info-display">
<p class="site-stat-title">Title</p>
<p class="site-stat-subtitle">A Short Description</p>
<br>
<p class="site-stat-title">Title</p>
<p class="site-stat-subtitle">A Short Description</p>
</div>
</div>
</div>
</div>
我现在面临的问题是 A)每列的内容不是居中的(他们只是根据内容在移动设备上获取高度,但在包装器中的桌面高度需要相同) B) 统计部分中的文本不会单独出现在一行中。
任何以内容为中心并帮助解决文本问题的答案将不胜感激。
UPDATE
基于Andrew Halpern 的回答。
.wrap {
width: 100%;
display: flex;
background: pink;
margin-top: 10px;
margin-bottom: 10px;
align-items: center;
justify-content: center;
flex-flow: row wrap;
text-align: center;
}
.col1 {
width: 50%;
}
@media only screen and (max-width: 500px) {
.col1 {
width: 100%;
}
}
.col2 {
width: 50%;
}
@media only screen and (max-width: 500px) {
.col2 {
width: 100%;
}
}
<div class="wrap">
<div class="col1">
<h1>I'm Centered</h1>
</div>
<div class="col2">
<h1>I'm Centered Too</h1>
<h1>I'm Centered Too</h1>
<h1>I'm Centered Too</h1>
</div>
</div>
【问题讨论】:
-
抱歉,这段代码不会带您到任何地方。解释每一个细节都太多了,但总的来说,使用 CSS flexbox 可以更轻松地完成此类工作:css-tricks.com/snippets/css/a-guide-to-flexbox
-
该代码是正常的。采用不同方法的答案很有帮助。
-
我会使用 CSS 网格和 flexbox 来解决这个问题。它将使响应更容易以及代码更清晰。
-
我已经尝试了一段时间@JasonB95。愿意发布答案吗?