首页 > 解决方案 > 单独的段落不单独显示

问题描述

我正在尝试为网站创建一个统计区域。在台式机上应该是两个,在移动设备上应该是堆叠的。如果将内部 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)内的文本stats 部分不会出现在单独的行上。

任何以内容为中心并帮助解决文本问题的答案将不胜感激。

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>

标签: htmlcssflexbox

解决方案


CSS:

.container {
                width: 50%;
                margin: 0 auto;
                display: grid;
                grid-template-columns: repeat(2, 1fr);
            }

            .left-side,
            .right-side {
                padding: 2rem;
                display: flex;
                justify-content: center;
                align-items: center;
            }

            .left-side {
                background-color: crimson;
            }

            .right-side {
                background-color: cornflowerblue;
            }

            @media screen and (max-width: 768px) {
                .container {
                    width: 100%;
                    grid-template-columns: 1fr;
                }
            }

的HTML:

<div class="container">
        <div class="left-side">
            <h1>Centered.</h1>
        </div>
        <div class="right-side">
            <div class="items">
                <h1>Centered.</h1>
                <h1>Centered.</h1>
                <h1>Centered.</h1>
            </div>
        </div>
    </div>

桌面视图 屏幕尺寸缩小到 768 像素或以下时查看


推荐阅读