【问题标题】:Separate paragraphs not displaying on their own lines单独的段落不单独显示
【发布时间】: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。愿意发布答案吗?

标签: html css flexbox


【解决方案1】:

这是使用 Flexbox 重写的代码,它应该可以实现您想要的行为。

.wrap {
  width: 100%;
  display: flex;
  background: pink;
  margin-top: 10px;
  margin-bottom: 10px;
  align-items: center;
  justify-content: center;
  flex-flow: row wrap;
}

.col1,
.col2 {
  padding: 50px;
}
<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>

【讨论】:

  • 真的很接近。内容只是不是水平居中的。我一直在试图弄清楚这一点。我只需要桌面上 50% 的宽度和移动设备上 100% 的宽度。问题是缺少水平居中和填充,但我可以删除它。
  • 如果你看一下我更新的问题,那会一样吗?
  • 我不确定这里有什么区别? col2 的样式不对?
  • 填充意味着在移动设备上的列太大了。为了清楚起见,我只是为不同类中的列设置了宽度。
【解决方案2】:

我没有应用正常大小的文本中心。我只将它应用于移动尺寸。我想这就是你想要的。

 .site-stats-group {
      background: pink;
      display: flex;
      justify-content: space-evenly;
      align-items: center;
      font-size: 2rem;
      flex-flow: row wrap;
    }

    @media screen and (max-width: 500px) {
        .site-stats-group {
          flex-direction: column;
        }
        .site-stat-info {
          text-align: center;
        }
    }
<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>
        </div>
        <div class="site-stat-info-display">
          <p class="site-stat-title">Title</p>
          <p class="site-stat-subtitle">A Short Description</p>
        </div>
      </div>
    </div>
  </div>

【讨论】:

  • 关闭,但列的内容不应决定它们的大小。它们应该始终在桌面上占 50%,然后在 50% 列中的内容应该居中
【解决方案3】:

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>

【讨论】:

    【解决方案4】:

    使用 flex 并将媒体查询中的 flex-direction 更改为 column 以获得更小的屏幕:

    html,
    body {
      height: 100%;
      margin: 0;
    }
    
    .wrap {
      width: 100%;
      height: 100%;
      display: flex;
      background: pink;
      align-items: center;
      align-content: center;
      justify-content: center;
      text-align: center;
    }
    
    .col1,
    .col2 {
      box-sizing: border-box;
      width: 50%;
      display: flex;
      flex-direction: column;
      justify-content: center;
    }
    
    @media only screen and (max-width: 500px) {
      .wrap {
        flex-direction: column;
      }
      .col1,
      .col2 {
        width: 100%;
        height: 50%;
      }
    }
    <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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-15
      • 2012-05-05
      • 1970-01-01
      • 2016-09-16
      • 1970-01-01
      • 1970-01-01
      • 2016-03-23
      相关资源
      最近更新 更多