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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <div class="latest-posts">
  <h3 class="latest-posts-title">Latest posts - The complicated way with borders and an ad inserted</h3>
  <div class="latest-posts-items">
    <article class="post">
      <h3 class="post-title">The title of the post #1</h3>
      <figure class="post-featured-image">
        <img src="https://via.placeholder.com/350x150"/>
      </figure>
      <div class="post-excerpt">
        This is a short description (a.k.a excerpt) of around a few dozens of characters associated with and for the post #1.
      </div>
    </article>
    <article class="post">
      <h3 class="post-title">The title of the post #2</h3>
      <figure class="post-featured-image">
        <img src="https://via.placeholder.com/350x150"/>
      </figure>
      <div class="post-excerpt">
        This is a short description (a.k.a excerpt) of around a few dozens of characters associated with and for the post #2.
      </div>
    </article>
    <article class="post">
      <h3 class="post-title">The title of the post #3</h3>
      <figure class="post-featured-image">
        <img src="https://via.placeholder.com/350x150"/>
      </figure>
      <div class="post-excerpt">
        This is a short description (a.k.a excerpt) of around a few dozens of characters associated with and for the post #3.
      </div>
    </article>
    <article class="post">
      <h3 class="post-title">The title of the post #4</h3>
      <figure class="post-featured-image">
        <img src="https://via.placeholder.com/350x150"/>
      </figure>
      <div class="post-excerpt">
        This is a short description (a.k.a excerpt) of around a few dozens of characters associated with and for the post #4.
      </div>
    </article>
    <div class="ad-container">
      <div class="ad">The ad is inserted here</div>
    </div>
    <article class="post">
      <h3 class="post-title">The title of the post #5</h3>
      <figure class="post-featured-image">
        <img src="https://via.placeholder.com/350x150"/>
      </figure>
      <div class="post-excerpt">
        This is a short description (a.k.a excerpt) of around a few dozens of characters associated with and for the post #5.
      </div>
    </article>
    <article class="post">
      <h3 class="post-title">The title of the post #6</h3>
      <figure class="post-featured-image">
        <img src="https://via.placeholder.com/350x150"/>
      </figure>
      <div class="post-excerpt">
        This is a short description (a.k.a excerpt) of around a few dozens of characters associated with and for the post #6.
      </div>
    </article>
    <article class="post">
      <h3 class="post-title">The title of the post #7</h3>
      <figure class="post-featured-image">
        <img src="https://via.placeholder.com/350x150"/>
      </figure>
      <div class="post-excerpt">
        This is a short description (a.k.a excerpt) of around a few dozens of characters associated with and for the post #7.
      </div>
    </article>
    <article class="post">
      <h3 class="post-title">The title of the post #8</h3>
      <figure class="post-featured-image">
        <img src="https://via.placeholder.com/350x150"/>
      </figure>
      <div class="post-excerpt">
        This is a short description (a.k.a excerpt) of around a few dozens of characters associated with and for the post #8.
      </div>
    </article>
  </div>
</div>
    
              
            
!

CSS

              
                // Drawing borders for the flexbox grid
@mixin flexbox-grid-borders($items-per-row, $margin-right: 0, $grid-title: false) {
	> * {
		border-right: 1px solid gray;

		@if ($margin-right != 0) {
			padding-right: calc(#{$margin-right} / 2);
			margin-right: calc(#{$margin-right} / 2);
		}
	}

	// Right margins
	@if ($grid-title == true) {
		> *:not(h3) {
			&:nth-child(#{$items-per-row}n + 1) {
				border-right: 0;
				padding-right: 0;
			}
		}
	} @else {
		> * {
			&:nth-child(#{$items-per-row}n) {
				border-right: 0;
				padding-right: 0;
			}
		}
	}
}

// A flexbox grid with margins / column gap set manually 
@mixin flexbox-grid(
	$items-per-row,
	$margin-right: 0,
	$margin-bottom: 0,
	$max-items-to-display: null,
	$grid-title: false,
	$iepx: 0px
) {
	$item-width: percentage(1 / $items-per-row);
	$item-flex-basis: calc(
		#{$item-width} - (((#{$items-per-row} - 1) * #{$margin-right}) / #{$items-per-row * 2}) - #{$iepx}
	);
	display: flex;
	flex-direction: row;
	flex-wrap: wrap;

	> * {
		// Now we can have padding and border for items
		box-sizing: border-box;
		margin-bottom: $margin-bottom;
		margin-right: $margin-right;

		// Only $max-items-to-display number of items will be shown
		@if ($max-items-to-display != null) {
			display: none;

			@for $i from 0 through $max-items-to-display - 1 {
				&:nth-child(#{$i + 1}) {
					display: flex;
				}
			}
		}
	}

	// Right margins
	@if ($grid-title == true) {
		> *:not(h3) {
			width: $item-flex-basis;

			&:nth-child(#{$items-per-row}n + 1) {
				margin-right: 0;
			}
		}
	} @else {
		> * {
			width: $item-flex-basis;

			&:nth-child(#{$items-per-row}n) {
				margin-right: 0;
			}
		}
	}
}

// A responsive grid with flexbox
@mixin responsive-flexbox-grid(
	$margin-right: 0,
	$margin-bottom: 0,
	$borders: true,
	$max-items-to-display-map: null
) {
	@media (max-width: 767px) {
		$max-items-to-display: map-get($max-items-to-display-map, mobile);
		@include flexbox-grid(1, $margin-right, $margin-bottom, $max-items-to-display);
    @include flexbox-grid-borders(1, $margin-right);
	}

	@media (min-width: 768px) and (max-width: 959px) {
		$max-items-to-display: map-get($max-items-to-display-map, tablet);
		@include flexbox-grid(2, $margin-right, $margin-bottom, $max-items-to-display);
    @include flexbox-grid-borders(2, $margin-right);
	}

	@media (min-width: 960px) and (max-width: 1279px) {
		$max-items-to-display: map-get($max-items-to-display-map, laptop);
		@include flexbox-grid(3, $margin-right, $margin-bottom, $max-items-to-display);
    @include flexbox-grid-borders(3, $margin-right);
	}

	@media (min-width: 1280px) {
		$max-items-to-display: map-get($max-items-to-display-map, desktop);
		@include flexbox-grid(4, $margin-right, $margin-bottom, $max-items-to-display);
    @include flexbox-grid-borders(4, $margin-right);
	}
}

// A special mixin to regrid posts for the ad
@mixin latest-posts--regrid-for-ad($max-items-to-display-map, $margin-right) {
	.ad-container {
		width: 100%;
	}

	@media (max-width: 767px) {
		$max-items-to-display: map-get($max-items-to-display-map, mobile);
		$half: round($max-items-to-display / 2);

		> * {
			@for $i from 1 to 9 {
				&:nth-child(#{$i}) {
					@if ($i < $half) {
						order: #{$i};
					}
					@if ($i > $half) {
						order: #{$i + 1};
					}
					@if ($i == 5) {
						order: #{$half};
					}
				}
			}
		}
	}

	@media (min-width: 768px) and (max-width: 959px) {
		$max-items-to-display: map-get($max-items-to-display-map, tablet);
		$half: round($max-items-to-display / 2);

		> * {
			@for $i from 1 to 9 {
				&:nth-child(#{$i}) {
					@if ($i < $half) {
						order: #{$i};
					}
					@if ($i >= $half) {
						order: #{$i + 1};
					}
					@if ($i == 5) {
						order: #{$half};
					}
				}
			}
		}
	}

	@media (min-width: 960px) and (max-width: 1279px) {
		$max-items-to-display: map-get($max-items-to-display-map, laptop);
		$half: round($max-items-to-display / 2);

		> * {
			@for $i from 1 to 9 {
				&:nth-child(#{$i}) {
					@if ($i < $half) {
						order: #{$i};
					}
					@if ($i >= $half) {
						order: #{$i + 1};
					}
					@if ($i == 5) {
						order: #{$half};
					}
					@if ($i == 6) {
						margin-right: calc(#{$margin-right} / 2);
						padding-right: calc(#{$margin-right} / 2);
						border-right: 1px solid gray;
					}
					@if ($i == 7) {
						margin-right: 0;
						padding-right: 0;
						border: none;
					}
				}
			}
		}
	}

	@media (min-width: 1280px) {
		$max-items-to-display: map-get($max-items-to-display-map, desktop);
		$half: round($max-items-to-display / 2);

		> * {
			@for $i from 1 to 10 {
				&:nth-child(#{$i}) {
					order: #{$i};
					display: flex;

					@if ($i == 8) {
						margin-right: calc(#{$margin-right} / 2);
						padding-right: calc(#{$margin-right} / 2);
						border-right: 1px solid gray;
					}

					@if ($i == 9) {
						margin-right: 0;
						padding-right: 0;
						border: none;
					}
				}
			}
		}
	}
}

// Styling posts
@mixin post {
  flex-direction: column;
  
  figure {
    margin: 0;
    
    img {
      max-width: 100%;
      height: auto;
    }
  }
}

// Styling the ad 
@mixin ad-container {
  display: flex;
  justify-content: center;
  
  .ad {
    background-color: black;
    color: white;
    width: 728px;
    height: 90px;
    
    display: flex;
    justify-content: center;
    align-items: center;
    
    @media (max-width: 767px) {
      width: 300px;
      height: 250px;
    }
  }
}

// The main styling
.latest-posts {
  display: grid;
  
  .latest-posts-items {
    
    $max-items-to-display-map: (
			mobile: 2,
			tablet: 5,
			laptop: 7,
			desktop: 9
);
    
    @include responsive-flexbox-grid(1.25em, 1.25em, true, $max-items-to-display-map);
    
    @include latest-posts--regrid-for-ad($max-items-to-display-map, 1.25em);
    
    .post {
      @include post;
    }
    
    .ad-container {
      @include ad-container;
    }
  }
}


              
            
!

JS

              
                
              
            
!
999px

Console