Opinions Needed for a Responsive Four-Column Layout Flexbox

余生长醉 提交于 2021-02-08 04:38:45

问题


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>

回答1:


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>

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).

What do you think about that ?



来源:https://stackoverflow.com/questions/54670754/opinions-needed-for-a-responsive-four-column-layout-flexbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!