自适应四列布局Flexbox所需的意见 [英] Opinions Needed for a Responsive Four-Column Layout Flexbox

查看:27
本文介绍了自适应四列布局Flexbox所需的意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用移动优先方法创建了一个响应式四列布局.最小的屏幕显示1列,较大的屏幕显示2列,最大的屏幕显示4列.

I created a responsive four-column layout by using a mobile-first approach. The smallest screen shows 1 column, the larger screen 2 columns, and the largest screen 4 columns.

到目前为止,它似乎仍然有效,但是我希望您看一下我的代码,并告诉我我的方法是否有问题.感谢您的意见.

It seems to work so far, but I'd like you to take a look at my code and tell me if there's anything wrong with my approach. I would be grateful for your opinions.

<!DOCTYPE html>
<html lang="en-US">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>FlexBox Test</title>
  <style>
  :root {
    box-sizing: border-box;
  }

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

  /* mobile phone */
  .flex-container {
    display: flex;
    flex-wrap: wrap;
    max-width: 1400px;
    margin: 0 auto;
    border: 1px solid red;
    padding: 1em;
  }

  .flex-item-1 {
    background: indianred;
  }

  .flex-item-2 {
    background: blue;
  }

  .flex-item-3 {
    background: tomato;
  }

  .flex-item-4 {
    background: coral;
  }

  .flex-item {
    flex: 100%;
    height: 100px;
  }

  /* tablet */
  @media screen and (min-width: 640px) {
    .flex-item {
      flex: calc(50% - 1em);
    }

    .flex-item:nth-child(2n) {
      margin-left: 1em;
    }
  }

  /* desktop */
  @media screen and (min-width: 960px) {
    .flex-item {
      flex: calc(25% - 1em);
    }

    .flex-container > * + * {
      margin-left: 1em;
    }
  }
  </style>
</head>
<body>
  <div class="flex-container">
    <div class="flex-item flex-item-1">
      1
    </div>

    <div class="flex-item flex-item-2">
      2
    </div>

    <div class="flex-item flex-item-3">
      3
    </div>

    <div class="flex-item flex-item-4">
      4
    </div>
  </div>
</body>
</html>

推荐答案

如果要从CSS中删除calc,可以执行类似的操作.

If you want remove calc from the css you can do something like that.

<!DOCTYPE html>
<html lang="en-US">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>FlexBox Test</title>
  <style>
  :root {
    box-sizing: border-box;
  }

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

  /* mobile phone */
  .flex-container {
    display: flex;
    flex-wrap: wrap;
    max-width: 1400px;
    margin: 0 auto;
    border: 1px solid red;
    padding: 1em;
  }

  .flex-item-1 {
    background: indianred;
  }

  .flex-item-2 {
    background: blue;
  }

  .flex-item-3 {
    background: tomato;
  }

  .flex-item-4 {
    background: coral;
  }

  .flex-item {
    flex: 100%;
    height: 100px;
  }

  /* tablet */
  @media screen and (min-width: 675px) and (max-width: 960px) {
    .flex-item {
      flex: 1 0 19em;
    }

    .flex-item:nth-child(2n) {
	  margin-left: 1em;
    }
  }

  /* desktop */
  @media screen and (min-width: 960px) {
    .flex-item {
	  flex: 1 0;
    }

    .flex-container > * + * {
      margin-left: 1em;
    }
  }
  </style>
</head>
<body>
  <div class="flex-container">
    <div class="flex-item flex-item-1">
      1
    </div>

    <div class="flex-item flex-item-2">
      2
    </div>

    <div class="flex-item flex-item-3">
      3
    </div>

    <div class="flex-item flex-item-4">
      4
    </div>
  </div>
</body>
</html>

但是,要保持您在第一个示例中的边距,我必须更改 @media屏幕以及(最小宽度:675px)和(最大宽度:960px)用于平板电脑屏幕.如果不是,则针对特定浏览器宽度的第一行将显示三个块(与您想要的行为不同).

However to keep the margin as you have on your initial example, I have to change @media screen and (min-width: 675px) and (max-width: 960px) for tablet screen. If not, three block appears on the first line for specific browser width (not the same behaviour as you want).

您对此有何看法?

这篇关于自适应四列布局Flexbox所需的意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆