Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300" rel="stylesheet">

<nav id="navbar">
  <header>CSS Grid, a basic website</header>
  <ul>
    <li><a class="nav-link" href="#Introduction">Introduction</a></li>
    <li><a class="nav-link" href="#Fixed_and_flexible_size">
      Fixed and flexible size</a></li>
    <li><a class="nav-link" href="#Creating_rows_and_columns">Creating rows and columns</a></li>
    <li><a class="nav-link" href="#Placement_of_an_item">Placement of an item</a></li>
    <li><a class="nav-link" href="#Alignment_of_the_grid_and_items">Alignment of the grid and items</a></li>
    <li><a class="nav-link" href="#Overlapping_items">Overlapping items</a></li>
  </ul>
</nav>

<main id="main-doc">
  <section class="main-section" id="Introduction">
    <header>Introduction</header>
    <article>
      <p>A simple layout of your page can be defined using CSS grid, which is basically a table. You place your items inside columns and rows. If you would make your whole page in one CSS grid, one can put the header in the first row stretching it over all
        the columns, the footer can be placed in the last row spreading all the columns. Everything in between can be defined as main, with an automatic number of rows depending on the amount of content.</p>
      <img src="http://hannaflemming.net//FCC/FCC_Responsive-Website/CSS-grid01.png" alt="Grid with one column, defined head, main, footer" />
      <pre><code class="code-css">  .website {
     display: grid;
     grid-template-columns: 100%;
     grid-auto-rows: 100px;
  }
  .header {
     grid-column-start: 1; 
     grid-column-end: 2; 
     grid-row-start: 1; 
     grid-row-end: 2; 
  }
  .main {
     grid-column-start: 1; 
     grid-column-end: 2; 
     grid-row-start: 2; 
     grid-row-end: 5; 
  }
  .footer {
     grid-column-start: 1; 
     grid-column-end: 2; 
     grid-row-start: 5; 
     grid-row-end: 6; 
  }</code></pre>
      <pre><code class="code-html"> &lt;div class="website"&gt;
    &lt;div class="header"&gt;Header&lt;/div&gt;
    &lt;div class="main"&gt;Main&lt;/div&gt;
    &lt;div class="footer"&gt;Footer&lt;/div&gt;
 &lt;/div&gt;</code></pre>
      <p>In this setup only one column is needed, and we can hardly speak of a table. If one would add a sidebar for advertisement, the currect main could be divided in main and advertisement. First column holding main, second column holding advertisement.
        Note that the header and footer are now placed in both columns, but still in only one row.</p>
      <img src="http://hannaflemming.net//FCC/FCC_Responsive-Website/CSS-grid02.png" alt="Grid with two columns 50/50, defined head, main, advertisement, footer" />
      <pre><code class="code-css">  .website {
     display: grid;
     grid-template-columns: 50% 50%;
     grid-auto-rows: 100px;
  }
  .header {
     grid-column-start: 1; 
     grid-column-end: 3; 
     grid-row-start: 1; 
     grid-row-end: 2; 
  }
  .main {
     grid-column-start: 1; 
     grid-column-end: 2; 
     grid-row-start: 2; 
     grid-row-end: 5; 
  }
  .ads {
     grid-column-start: 2; 
     grid-column-end: 3; 
     grid-row-start: 2; 
     grid-row-end: 5; 
  }  
  .footer {
     grid-column-start: 1; 
     grid-column-end: 3; 
     grid-row-start: 5; 
     grid-row-end: 6; 
  }</code></pre>
      <pre><code class="code-html"> &lt;div class="website"&gt;
    &lt;div class="header"&gt;Header&lt;/div&gt;
    &lt;div class="main"&gt;Main&lt;/div&gt;
    &lt;div class="ads"&gt;Advertisement&lt;/div&gt;
    &lt;div class="footer"&gt;Footer&lt;/div&gt;
 &lt;/div&gt;</code></pre>
      <p>Having main and advertisement share the vertical space 50/50 would not make sense. Using CSS grid one can define the width of columns and the height of rows. For example main 80% and advertisement 20%</p><img src="http://hannaflemming.net//FCC/FCC_Responsive-Website/CSS-grid03.png"
        alt="Grid with two columns 80/20, defined head, main, advertisement, footer" />
      <pre><code class="code-css">  .website {
     display: grid;
     grid-template-columns: 80% 20%;
     grid-auto-rows: 100px;
  }</code></pre>
      <p>Here I will go into design options for the layout of this simple website set-up. However, the content of this page is made while learning about CSS grid, please go to <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout/Basic_Concepts_of_Grid_Layout">Developer Mozilla</a>        or <a href="https://css-tricks.com/snippets/css/complete-guide-grid/">CSS-tricks</a> to learn about CSS grid.</p>
    </article>
  </section>

  <section class="main-section" id="Fixed_and_flexible_size">
    <header>Fixed and flexible size</header>
    <article>
      <p>Both rows and columns can be defined in their size. There are thee ways to define the width or height.
        <ul>
          <li><strong>Pixels (px)</strong></br>Pixels gives a fixed size to the column or row, no matter which device is used.</li>
          <li><strong>Percentages (%)</strong></br>Percentages gives the row or column the given percentile of the size of the website. So a 20% column of a website which is 1000 pixels wide will be 200 pixels. But when looked at on a smaller screen, the
            width of the website might be 400 pixels, so the column will be 80 pixels.</li>
          <li><strong>Flexible unit (fr)</strong></br>Flexible unit is a new CSS unit which is similar to percentages. 1fr gives the column 1 part of the available space, whereas 2fr gives the column a width of 2 parts of the available space. This is best
            explained using some examples.</li>
        </ul>
      </p>
      <p>If we have a website with a width of 1000px and two columns of 1fr;
        <pre><code class="code-css">  .website {
     display: grid;
     grid-template-columns: 1fr 1fr;
     grid-auto-rows: 100px;
  }</code></pre> The total available space is then 1000px which will be divided by two columns, each 1fr, thus each column will be 500px. </br>If the two columns would have different sizes, like 1fr and 3fr;
        <pre><code class="code-css">  .website {
     display: grid;
     grid-template-columns: 1fr 3fr;
     grid-auto-rows: 100px;
  }</code></pre> Then the total 1000px will be divided in four parts. (1fr plus 3fr is 4fr). The first column will be 250px, the second column will be 750px.
      </p>
      <img src="http://hannaflemming.net//FCC/FCC_Responsive-Website/CSS-grid04.png" alt="Grid with two columns, both 1fr" /><img src="http://hannaflemming.net//FCC/FCC_Responsive-Website/CSS-grid05.png" alt="Grid with two columns, the first 1fr, the second 3fr"
      />
      <p>All three units can also be mixed. The fr will divide whatever space is left after the defined pixel en percentage columns.</p>
    </article>
  </section>

  <section class="main-section" id="Creating_rows_and_columns">
    <header>Creating rows and columns</header>
    <article>
      <p>For making a template of columns and rows, we use<code class="code-inline">grid-template-columns</code>and<code class="code-inline">grid-template-rows</code>. The number of columns created depends on the number of units you write. <code class="code-inline">grid-template-columns: 1fr 3fr;</code>will produce two columns, whereas<code class="code-inline">grid-template-columns: 1fr 1fr 1fr 1fr;</code>will produce four. The latter code gives four columns with equal width, which can alternatively be written as <code class="code-inline">grid-template-columns: repeat(4, 1fr);</code>.</p>
      <p>It works the same for creating rows. If we would want to make the 5 rows from previously used examples, we could define 5 rows with<code class="code-inline">grid-template-rows: repeat(5, 100px);</code>. However, the content of a website might change regularly. So instead of redefining the number of rows each time we add or remove an item, we can make implicit rows with<code class="code-inline">grid-auto-rows: 100px;</code>that will make as many rows of 100px that are needed for the amount of items added.</p>
      <p>In addition to the number of columns or rows, one can also specify a minimum and maximum height with<code class="code-inline">grid-auto-rows: minmax(100px, auto);</code>. Here the rows will be minimally 100px and will re-size bigger with the content if need be. The 'auto' can also be set to a given value like 200px or 20%. So we could end up defining the layout of the website with four equal sized columns, and an automated number of rows having a height between 100 and 200 pixels:</p>
  <pre><code class="code-css">  .website {
     display: grid;
     grid-template-columns: repeat(4, 1fr);
     grid-auto-rows: minmax(100px, 200px);
  }</code></pre>
      <pre><code class="code-html"> &lt;div class="website"&gt;
 &lt;/div&gt;</code></pre>
    </article>
  </section>

  <section class="main-section" id="Placement_of_an_item">
    <header>Placement of an item</header>
    <article>
      <p>The lines of a grid are numbered from left to right, and from top to bottom, as seen in the figure below. </p>
      <img src="http://hannaflemming.net/FCC/FCC_Responsive-Website/CSS-grid06.png" alt="Grid with line numers" />
      <p>An item can be placed in the grid on several ways
        <ul>
          <li><strong>Using line numbers</strong></br>As shown in the introduction</li>
          <li><strong>Targeting an area of the grid</strong></br>Instead of using lines, placing items in the ‘boxes’ between the lines. <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout/Box_Alignment_in_CSS_Grid_Layout"> Learn how to target grid area’s.</a>            </li>
          <li><strong>Automatic placement</strong></br>If you do not define your items, they will be placed automatically. In principle, one item per box. <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout/Auto-placement_in_CSS_Grid_Layout">Learn more about automatic placement.</a></li>
        </ul>
      </p>
      <p>The items can be placed using <code class="code-inline">grid-column-start</code>,<code class="code-inline">grid-column-end</code>,<code class="code-inline">grid-row-start</code>, and<code class="code-inline">grid-row-end</code>. The item 'My item' is placed in the green box with;
      </p>
<pre><code class="code-css">  .website {
     display: grid;
     grid-template-columns: 1fr 1fr;
     grid-auto-rows: 100px;
  }
  .my-item {
     grid-column-start: 2; 
     grid-column-end: 3; 
     grid-row-start: 2; 
     grid-row-end: 5; 
  }</code></pre>
<pre><code class="code-html"> &lt;div class="website"&gt;
    &lt;div class="my-item"&gt;My item&lt;/div&gt;
 &lt;/div&gt;</code></pre>
    </article>
  </section>

  <section class="main-section" id="Alignment_of_the_grid_and_items">
    <header>Alignment of the grid and items</header>
    <article>
      <p>Items can be placed within the created grid, either in one box (called cell) or in an area covering several cells. By default the item will be stretched to fill the box. Using<code class="code-inline">align-items</code>one can define how all items should be aligned within the grid. With<code class="code-inline">align-self</code>a single item can be aligned.</p>
      <img src="http://hannaflemming.net//FCC/FCC_Responsive-Website/CSS-grid07.png" alt="Grid with items aligned using align-self" />
<pre><code class="code-css">  .website {
     display: grid;
     grid-template-columns: 1fr 1fr;
     grid-auto-rows: 200px;
     align-items: stretch;
  }
  .yellow {
  }
  .red {
     align-self: start;
  }
  .blue {
     align-self: end; 
  }
  .green {
     align-self: center; 
  }</code></pre>
      <pre><code class="code-html"> &lt;div class="website"&gt;
    &lt;div class="yellow"&gt;Stretch&lt;/div&gt;
    &lt;div class="red"&gt;Start&lt;/div&gt;
    &lt;div class="blue"&gt;End&lt;/div&gt;
    &lt;div class="green"&gt;Center&lt;/div&gt;
 &lt;/div&gt;</code></pre>
      <p>Similar to<code class="code-inline">align-items</code>and<code class="code-inline">align-self</code>, items can be aligned on the other axis with<code class="code-inline">justify-items</code>and<code class="code-inline">justify-self</code>. </p>
      <img src="http://hannaflemming.net//FCC/FCC_Responsive-Website/CSS-grid08.png" alt="Grid with items aligned using justify-self" />
      <pre><code class="code-css">  .website {
     display: grid;
     grid-template-columns: 1fr 1fr;
     grid-auto-rows: 200px;
     justify-items: stretch;
  }
  .yellow {
  }
  .red {
     justify-self: start;
  }
  .blue {
     justify-self: end; 
  }
  .green {
     justify-self: center; 
  }</code></pre>
      <pre><code class="code-html"> &lt;div class="website"&gt;
    &lt;div class="yellow"&gt;Stretch&lt;/div&gt;
    &lt;div class="red"&gt;Start&lt;/div&gt;
    &lt;div class="blue"&gt;End&lt;/div&gt;
    &lt;div class="green"&gt;Center&lt;/div&gt;
 &lt;/div&gt;</code></pre>
      <p>There are more align and justify options than stretch, start, end, and center. <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout/Box_Alignment_in_CSS_Grid_Layout">Read about more align options.</a></p>
    </article>
  </section>

  <section class="main-section" id="Overlapping_items">
    <header>Overlapping items</header>
    <article>
      <p>Items can be placed in an overlapping fashion, like in the example below where blue is overlapping yellow in the red box:</p>
      <img src="http://hannaflemming.net//FCC/FCC_Responsive-Website/CSS-grid09.png" alt="Grid with blue overlapping yellow" />
      <pre><code class="code-css">  .website {
     display: grid;
     grid-template-columns: 1fr 1fr;
     grid-auto-rows: 100px;
  }
  .yellow {
     grid-column-start: 1; 
     grid-column-end: 3; 
     grid-row-start: 1; 
     grid-row-end: 3; 
  }
  .blue {
     grid-column-start: 1; 
     grid-column-end: 2; 
     grid-row-start: 2; 
     grid-row-end: 5; 
  }
  .green {
     grid-column-start: 2; 
     grid-column-end: 3; 
     grid-row-start: 3; 
     grid-row-end: 5; 
  }</code></pre>
      <pre><code class="code-html"> &lt;div class="website"&gt;
    &lt;div class="yellow"&gt;Yellow&lt;/div&gt;
    &lt;div class="blue"&gt;Blue&lt;/div&gt;
    &lt;div class="green"&gt;Green&lt;/div&gt;
 &lt;/div&gt;</code></pre>
      <p>The blue box is last placed and will therefor be placed over yellow. Using<code class="code-inline">z-index</code>one can adjust the order of layers. The lower numbers will be placed near the background with the higher numbers on top. If we give Yellow a higher z-index than Blue it will be placed on top.</p>
      <img src="http://hannaflemming.net/FCC/FCC_Responsive-Website/CSS-grid10.png" alt="Grid with yellow overlapping blue using z-index" />
      <pre><code class="code-css">  .website {
     display: grid;
     grid-template-columns: 1fr 1fr;
     grid-auto-rows: 100px;
  }
  .yellow {
     grid-column-start: 1; 
     grid-column-end: 3; 
     grid-row-start: 1; 
     grid-row-end: 3;
     z-index: 2;
  }
  .blue {
     grid-column-start: 1; 
     grid-column-end: 2; 
     grid-row-start: 2; 
     grid-row-end: 5; 
     z-index: 1;
  }
  .green {
     grid-column-start: 2; 
     grid-column-end: 3; 
     grid-row-start: 3; 
     grid-row-end: 5; 
  }</code></pre>
      <pre><code class="code-html"> &lt;div class="website"&gt;
    &lt;div class="yellow"&gt;Yellow&lt;/div&gt;
    &lt;div class="blue"&gt;Blue&lt;/div&gt;
    &lt;div class="green"&gt;Green&lt;/div&gt;
 &lt;/div&gt;</code></pre>
    </article>
  </section>

</main>
              
            
!

CSS

              
                html,
body {
  background-color: #fdf4f1;
  font-family: "Open Sans", sans-serif;
  color: #322d2b;
  line-height: 1.5;
}

#navbar {
  position: fixed;
  top: 0;
  left: 0;
  width: 300px;
  border-right: 2px solid #322d2b;
  background-color: #fae2db;
  width: 350px;
  height: 100%;
  padding: 15px 0px 0px 15px;
}

@media screen and (max-width: 960px){
  #navbar {
    position: absolute;
    max-height:200px;
    top: 0;
    padding:0;
    padding-bottom: 50px;
    width: 100%;
    border: none;
    border-bottom: 2px solid #322d2b;
    z-index:1;
  }
  nav ul {
    max-height:150px;
  }
  
  nav li {
  border-top: 1px solid #322d2b;
}
  
  #main-doc {
  margin-top: 200px;
  }
}

nav ul {
  height: 80%;
  list-style-type: none;
  overflow-y:auto;
  overflow-x:hidden;
}

nav li {
  position:relative;
}

nav a {
  display: block;
  color: black;
  text-decoration: none;
  font-weight: semi-bold;
  text-align: left;
  font-size: 18px;
  padding: 20px 0px 20px 0px;
}

#main-doc {
  position: absolute;
  padding: 30px 80px 0px 80px;
}

@media screen and (min-width: 960px){
  #main-doc{
  margin-left: 350px;
  }
}
@media screen and (max-width: 610px){
  #main-doc {
    margin: 0px;
    margin-top: 250px;
    padding: 30px 80px 0px 80px;
    width: 70%;
  }
}

.main-section {
  font-weight: semi-bold;
}
header {
  color: #004000;
  font-weight: bold;
  font-size: 28px;
  padding-top: 20px;
  text-align:center;
}

img {
  margin-left: auto;
  margin-right: auto;
  width: 250px;
}

code {
  display: block;
  background-color: #fae2db;
  width: 100%;
  line-height: 1.5;
  margin: 10px;
}

.code-inline {
  display: inline;
}

.code-css::before {
  content: "CSS";
  display: block;
  color: white;
  background-color: #004000;
  padding: 3px;
}

.code-html::before {
  content: "HTML";
  display: block;
  color: white;
  background-color: #004000;
  padding: 3px;
}

              
            
!

JS

              
                
              
            
!
999px

Console