How do I fill up the page grid and lay out the content widths in a single column?

拥有回忆 提交于 2020-06-23 14:23:29

问题


I'm trying to create flex box columns for web development. However, all I managed to do is produce one column of flex boxes.

What are some basic CSS codes that would verify that the div cards always fill up the page grid and that for a width small enough, the contents are laid out in a single column.

@charset "utf-8";

/*
      CSS Code
    */

section {
  display: flexbox;
  flex-direction: row;
  flex-wrap: wrap-reverse;
}

div.card {
  display: flex;
  flex-basis: 200px;
  flex-grow: 1;
  flex-shrink: 1;
  flex-direction: column;
  flex-wrap: nowrap;
  justify-content: space-between;
  align-items: center;
}
<!doctype html>
<html lang="en">

<head>
  <!--
      HTML Code  
       -->
  <meta charset="utf-8">
  <title>Coding Challenge 5-2</title>
  <link href="code5-2_layout.css" rel="stylesheet" />
  <link href="code5-2_flex.css" rel="stylesheet" />
</head>

<body>

  <h1>Social Media Sites</h1>

  <section>
    <div class="card">
      <header>
        <h1>Facebook</h1>
      </header>
      <img src="facebook.png" alt="Facebook" class="icon" />
      <footer>238,150 followers</footer>
    </div>
    <div class="card">
      <header>
        <h1>Twitter</h1>
      </header>
      <img src="twitter.png" alt="Twitter" class="icon" />
      <footer>48,871 followers</footer>
    </div>
    <div class="card">
      <header>
        <h1>Instagram</h1>
      </header>
      <img src="instagram.png" alt="Instagram" class="icon" />
      <footer>171,244 followers</footer>
    </div>
    <div class="card">
      <header>
        <h1>GooglePlus</h1>
      </header>
      <img src="google-plus.png" alt="GooglePlus" class="icon" />
      <footer>64,288 followers</footer>
    </div>
    <div class="card">
      <header>
        <h1>YouTube</h1>
      </header>
      <img src="youtube.png" alt="YouTube" class="icon" />
      <footer>Subscribe to our Channel</footer>
    </div>
    <div class="card">
      <header>
        <h1>Vimeo</h1>
      </header>
      <img src="vimeo.png" alt="Vimeo" class="icon" />
      <footer>Get the Vimeo Feed</footer>
    </div>
    <div class="card">
      <header>
        <h1>Skype</h1>
      </header>
      <img src="share.png" alt="Skype" class="icon" />
      <footer>Join a Skype Chat</footer>
    </div>
    <div class="card">
      <header>
        <h1>Pinterest</h1>
      </header>
      <img src="pinterest.png" alt="Pinterest" class="icon" />
      <footer>Create your Page</footer>
    </div>
    <div class="card">
      <header>
        <h1>Bloggr</h1>
      </header>
      <img src="bloggr.png" alt="Bloggr" class="icon" />
      <footer>Subscribe to our Feed</footer>
    </div>
    <div class="card">
      <header>
        <h1>Tumblr</h1>
      </header>
      <img src="tumblr.png" alt="Tumblr" class="icon" />
      <footer>Get Daily Updates</footer>
    </div>
    <div class="card">
      <header>
        <h1>Share</h1>
      </header>
      <img src="share.png" alt="Share" class="icon" />
      <footer>Share our Content</footer>
    </div>
    <div class="card">
      <header>
        <h1>E-mail</h1>
      </header>
      <img src="email.png" alt="email" class="icon" />
      <footer>E-mail Us!</footer>
    </div>
  </section>
</body>

</html>

回答1:


On your section CSS, change flexbox to flex and it shall work.

@charset "utf-8";

/*
      CSS Code
    */

section {
  display: flex; /* Change from flexbox to flex */
  flex-direction: row;
  flex-wrap: wrap-reverse;
}

div.card {
  display: flex;
  flex-basis: 200px;
  flex-grow: 1;
  flex-shrink: 1;
  flex-direction: column;
  flex-wrap: nowrap;
  justify-content: space-between;
  align-items: center;
}
<!doctype html>
<html lang="en">

<head>
  <!--
      HTML Code  
       -->
  <meta charset="utf-8">
  <title>Coding Challenge 5-2</title>
  <link href="code5-2_layout.css" rel="stylesheet" />
  <link href="code5-2_flex.css" rel="stylesheet" />
</head>

<body>

  <h1>Social Media Sites</h1>

  <section>
    <div class="card">
      <header>
        <h1>Facebook</h1>
      </header>
      <img src="facebook.png" alt="Facebook" class="icon" />
      <footer>238,150 followers</footer>
    </div>
    <div class="card">
      <header>
        <h1>Twitter</h1>
      </header>
      <img src="twitter.png" alt="Twitter" class="icon" />
      <footer>48,871 followers</footer>
    </div>
    <div class="card">
      <header>
        <h1>Instagram</h1>
      </header>
      <img src="instagram.png" alt="Instagram" class="icon" />
      <footer>171,244 followers</footer>
    </div>
    <div class="card">
      <header>
        <h1>GooglePlus</h1>
      </header>
      <img src="google-plus.png" alt="GooglePlus" class="icon" />
      <footer>64,288 followers</footer>
    </div>
    <div class="card">
      <header>
        <h1>YouTube</h1>
      </header>
      <img src="youtube.png" alt="YouTube" class="icon" />
      <footer>Subscribe to our Channel</footer>
    </div>
    <div class="card">
      <header>
        <h1>Vimeo</h1>
      </header>
      <img src="vimeo.png" alt="Vimeo" class="icon" />
      <footer>Get the Vimeo Feed</footer>
    </div>
    <div class="card">
      <header>
        <h1>Skype</h1>
      </header>
      <img src="share.png" alt="Skype" class="icon" />
      <footer>Join a Skype Chat</footer>
    </div>
    <div class="card">
      <header>
        <h1>Pinterest</h1>
      </header>
      <img src="pinterest.png" alt="Pinterest" class="icon" />
      <footer>Create your Page</footer>
    </div>
    <div class="card">
      <header>
        <h1>Bloggr</h1>
      </header>
      <img src="bloggr.png" alt="Bloggr" class="icon" />
      <footer>Subscribe to our Feed</footer>
    </div>
    <div class="card">
      <header>
        <h1>Tumblr</h1>
      </header>
      <img src="tumblr.png" alt="Tumblr" class="icon" />
      <footer>Get Daily Updates</footer>
    </div>
    <div class="card">
      <header>
        <h1>Share</h1>
      </header>
      <img src="share.png" alt="Share" class="icon" />
      <footer>Share our Content</footer>
    </div>
    <div class="card">
      <header>
        <h1>E-mail</h1>
      </header>
      <img src="email.png" alt="email" class="icon" />
      <footer>E-mail Us!</footer>
    </div>
  </section>
</body>

</html>


来源:https://stackoverflow.com/questions/62207730/how-do-i-fill-up-the-page-grid-and-lay-out-the-content-widths-in-a-single-column

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