html - 如何重新创建 "image on the left, text on the right"和 "text on the right image on the left"模式?

标签 html css

当我经常访问公司网站时,我看到一种常见的模式,即左侧有图像,右侧有描述,当我向下滚动时,第二列是相反的,图像在右侧,左侧的文本等等。

我尝试创建与 flexbox 类似的东西:

:root {
  box-sizing: border-box;
}

*,
*::before,
*::after {
  box-sizing: inherit;
}

body {
  margin: 0;
  padding: 0;
}

h2 {
  color: #5c3b65;
  font-size: 2.1rem;
}

.about-container {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
}

.about-container > * {
  width: 100%;
}

.row {
  display: flex;
  flex-wrap: wrap;
  border: 1px solid green;
  margin: 20px 0 50px;
}

@media (min-width: 52em) {
  [class*="col-"] {
    border: 1px solid orange;
  }

  .col-1 {
    flex: 25%;
  }

  .col-2 {
    flex: 50%;
  }
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="images-oddeven.css">
  <title>Odd and Even</title>
</head>
<body>
  <section class="about">
    <div class="about-container">
      <div class="row">
        <div class="col-1">
          <img src="https://picsum.photos/400" alt="">
        </div>
        <div class="col-2">
          <h2>Title</h2>
          <p>Text</p>
        </div>
      </div>
      <div class="row">
        <div class="col-1">
          <h2>Title</h2>
          <p>Text</p>
        </div>
        <div class="col-2">
          <img src="https://picsum.photos/400" alt="">
        </div>
      </div>
      <div class="row">
        <div class="col-1">
          <img src="https://picsum.photos/400" alt="">
        </div>
        <div class="col-2">
          <h2>Title</h2>
          <p>Text</p>
        </div>
      </div>
    </div>
  </section>
</body>
</html>

但是我确信我的代码无法正常工作,因为我想要图像的 25% 宽度,但图像的位置发生了变化。因此,有时我需要左侧 25% 的列宽,有时需要右侧。

您认为使用 CSS Grid 来解决这个问题会更容易吗?浏览器兼容性对我来说不是问题。我不需要 IE11 支持。

最佳答案

为所有行保留相同的 html,并更改偶数行中 col-1 的顺序。

.row:nth-child(2n) .col-1 {
  order: 2;
}

:root {
  box-sizing: border-box;
}

*,
*::before,
*::after {
  box-sizing: inherit;
}

body {
  margin: 0;
  padding: 0;
}

h2 {
  color: #5c3b65;
  font-size: 2.1rem;
}

.about-container {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
}

.about-container>* {
  width: 100%;
}

.row {
  display: flex;
  flex-wrap: wrap;
  border: 1px solid green;
  margin: 20px 0 50px;
}

@media (min-width: 52em) {
  [class*="col-"] {
    border: 1px solid orange;
  }
  .col-1 {
    flex: 25%;
  }
  .col-2 {
    flex: 50%;
  }
  
  .row:nth-child(2n) .col-1 {
    order: 2;
  }
}
<section class="about">
  <div class="about-container">
    <div class="row">
      <div class="col-1">
        <img src="https://picsum.photos/400" alt="">
      </div>
      <div class="col-2">
        <h2>Title</h2>
        <p>Text</p>
      </div>
    </div>
    <div class="row">
      <div class="col-1">
        <img src="https://picsum.photos/400" alt="">
      </div>
      <div class="col-2">
        <h2>Title</h2>
        <p>Text</p>
      </div>
    </div>
    <div class="row">
      <div class="col-1">
        <img src="https://picsum.photos/400" alt="">
      </div>
      <div class="col-2">
        <h2>Title</h2>
        <p>Text</p>
      </div>
    </div>
  </div>
</section>

如果你想使用网格,你可以尝试如下

:root {
  box-sizing: border-box;
}

*,
*::before,
*::after {
  box-sizing: inherit;
}

body {
  margin: 0;
  padding: 0;
}

.about-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 10px;
}

.row {
  display: grid;
  grid-template-columns: 25% 1fr; /* or use 50% instad of 1fr */
  margin: 0 0 50px;
}

img {
  max-width: 100%;
}

.row:nth-child(2n) {
  grid-template-columns: 1fr 25%;
}

.row:nth-child(2n) .col-2 {
  grid-area: 1;
}
<section class="about">
  <div class="about-container">
    <div class="row">
      <div class="col-1">
        <img src="https://picsum.photos/400" alt="">
      </div>
      <div class="col-2">
        <h2>Title</h2>
        <p>Text</p>
      </div>
    </div>

    <div class="row">
      <div class="col-1">
        <img src="https://picsum.photos/400" alt="">
      </div>
      <div class="col-2">
        <h2>Title</h2>
        <p>Text</p>
      </div>
    </div>

    <div class="row">
      <div class="col-1">
        <img src="https://picsum.photos/400" alt="">
      </div>
      <div class="col-2">
        <h2>Title</h2>
        <p>Text</p>
      </div>
    </div>

  </div>
  <!-- / .about-container -->
</section>
<!-- / .about -->

关于html - 如何重新创建 "image on the left, text on the right"和 "text on the right image on the left"模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66987495/

相关文章:

css - IE 中的并排 div

javascript - 更改 "md-menu"的 Angular 2/4 Material 默认样式

javascript - Material-UI [Select] - 如何在外部点击时移除焦点?

jquery - 计算无序列表的深度

html - 你如何防止 float 元素的 parent 崩溃?

html - PSQL输出html格式

javascript - $dialogs.create 在我的网站上显示一个弹出表单,但定义弹出窗口的代码具有格式限制

CSS:内联适合第一行的所有元素,每个下一个元素后换行

html - 在不影响导航栏或文本的情况下将背景图像变灰

css - 使用 <display :column> and when using table-layout:fixed 时如何改变列的宽度