Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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="content-container">
  <header>
    <h1>Defensive CSS</h1>
    <h2>Flex Wrapping</h2>
  </header>
  <main>
    <p>Since CSS flexbox gained wide support, its most frequent issue concerns items overflowing their container. This is because, by default, flex items do not wrap to a new row when they reach the edge of their container. To prevent this, and ensure flex items wrap to new lines as needed, always include a <code class="inline">flex-wrap</code> property on every flex container.</p>

    <div class="content-section">
      <section class="demo">
        <h3>Without Defensive CSS</h3>
        <div class="flex-container">
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
        </div>
      </section>

      <section class="demo">
        <h3>With Defensive CSS</h3>
        <div class="flex-container with-defensive">
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
          <div class="flex-item"></div>
        </div>
      </section>
    </div>
  </main>
  <footer>
    <p>Linting Defensive & Logical CSS With Stylelint Plugins</p>
    <a href="https://twitter.com/DanielYuschick" aria-label="@Daniel Yuschick on Twitter">@DanielYuschick</a>
  </footer>
</div>
              
            
!

CSS

              
                .flex-container {
  border: 1px solid var(--color-aux-100);
  display: flex;
  gap: var(--space-base-4x);
  overflow: hidden;
  max-inline-size: 100%;
  padding: 1em;

  .flex-item {
    background-color: var(--color-core-secondary);
    padding: var(--space-base-8x);
  }

  &.with-defensive {
    flex-wrap: wrap;
  }
}

/**********
*** SETUP
**********/
/***** Properties *****/
:root {
  --color-aux-50: rgb(10, 10, 11);
  --color-aux-100: rgb(29, 29, 32);
  --color-aux-200: rgb(48, 48, 54);
  --color-aux-300: rgb(77, 77, 86);
  --color-aux-400: rgb(96, 96, 108);
  --color-aux-500: rgb(115, 115, 130);
  --color-aux-600: rgb(147, 147, 159);
  --color-aux-700: rgb(169, 169, 178);
  --color-aux-800: rgb(190, 190, 197);
  --color-aux-900: rgb(223, 223, 226);
  --color-aux-950: rgb(242, 239, 233);

  --color-core-primary: #754abc;
  --color-core-secondary: #8b3257;

  --font-family-body: "Poppins", sans-serif;
  --font-family-heading: "Roboto Slab", serif;

  --space-base: 4px;
  --space-base-half: calc(var(--space-base) / 2);
  --space-base-1x: var(--space-base);
  --space-base-2x: calc(var(--space-base) * 2);
  --space-base-3x: calc(var(--space-base) * 3);
  --space-base-4x: calc(var(--space-base) * 4);
  --space-base-6x: calc(var(--space-base) * 6);
  --space-base-8x: calc(var(--space-base) * 8);
  --space-base-12x: calc(var(--space-base) * 12);
  --space-base-16x: calc(var(--space-base) * 16);
  --space-base-24x: calc(var(--space-base) * 24);
}

/***** Landmarks *****/
body {
  background-image: linear-gradient(
    335deg,
    hsl(263deg 46% 51%) 0%,
    hsl(263deg 44% 46%) 50%,
    hsl(263deg 46% 40%) 100%
  );
  padding: 1rem;
}

main {
  max-inline-size: 100vw;
  overflow: hidden;
}

footer {
  --padding-offset: var(--space-base-4x);

  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  justify-content: space-between;
  border-block-start: 1px solid var(--color-aux-50);
  padding-block: var(--padding-offset);
  margin-block-start: var(--padding-offset);
}

/***** Textual *****/
a,
a:visited,
a:active,
a:link {
  color: var(--color-core-secondary);
}

span,
p {
  font-family: var(--font-family-body);
}

p + p {
  margin-block-start: 1rem;
}

h1,
h2,
h3,
h4,
h5,
h6 {
  font-family: var(--font-family-heading);
}

code {
  background: var(--color-aux-100);
  color: var(--color-aux-950);
  display: block;
  padding: 0.25rem 0.5rem;

  &.inline {
    display: inline-block;
  }
}

/***** Other *****/
.content-container {
  inline-size: 75%;
  margin-inline: auto;
}

/**********
*** RESET
**********/
* {
  margin: 0;
  padding: 0;
}

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

*:where(:not(fieldset, progress, meter)) {
  background-origin: border-box;
  border-style: solid;
  border-width: 0;
}

html {
  -webkit-text-size-adjust: none;
  block-size: 100%;
}

@media (prefers-reduced-motion: no-preference) {
  html:focus-within {
    scroll-behavior: smooth;
  }
}

body {
  -webkit-font-smoothing: antialiased;
  line-height: 1.5;
  min-block-size: 100%;
  text-rendering: optimizeSpeed;
}

:where(img, svg, video, canvas, audio, iframe, embed, object) {
  display: block;
}
:where(img, svg, video) {
  block-size: auto;
  max-inline-size: 100%;
}

:where(svg) {
  fill: currentColor;
  stroke: none;
}

:where(svg):where(:not([fill])) {
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke: currentColor;
}

:where(svg):where(:not([width])) {
  inline-size: 5rem;
}

:where(input, button, textarea, select),
:where(input[type="file"])::-webkit-file-upload-button {
  color: inherit;
  font-size: inherit;
  font: inherit;
  letter-spacing: inherit;
  word-spacing: inherit;
}

:where(textarea) {
  resize: block;
}

:where(span, p, h1, h2, h3, h4, h5, h6) {
  overflow-wrap: break-word;
  max-inline-size: 90ch;
}

:where(ul, ol) {
  list-style-position: inside;
}

:where(ul, ol, [role="list"]) {
  list-style: none;
}

:where(ul, ol, [role="list"]) li::before {
  border: 0px;
  clip: rect(0px, 0px, 0px, 0px);
  content: "\\200B";
  height: 1px;
  margin: -1px;
  overflow: hidden;
  padding: 0px;
  position: absolute;
  white-space: nowrap;
  width: 1px;
}

a:not([class]) {
  text-decoration-skip-ink: auto;
}

:where(a[href], area, button, input, label[for], select, summary, textarea, [tabindex]:not([tabindex*="-"])) {
  cursor: pointer;
  touch-action: manipulation;
}
:where(input[type="file"]) {
  cursor: auto;
}
:where(input[type="file"])::-webkit-file-upload-button,
:where(input[type="file"])::file-selector-button {
  cursor: pointer;
}

@media (prefers-reduced-motion: no-preference) {
  :focus-visible {
    transition: outline-offset 145ms cubic-bezier(0.25, 0, 0.4, 1);
  }
  :where(:not(:active)):focus-visible {
    transition-duration: 0.25s;
  }
}
:where(:not(:active)):focus-visible {
  outline-offset: 5px;
}

:where(button, button[type], input[type="button"], input[type="submit"], input[type="reset"]),
:where(input[type="file"])::-webkit-file-upload-button,
:where(input[type="file"])::file-selector-button {
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  text-align: center;
  user-select: none;
}

:where(button, button[type], input[type="button"], input[type="submit"], input[type="reset"])[disabled] {
  cursor: not-allowed;
}

/**********
*** UTILITIES
**********/
.content-container {
  min-block-size: calc(100vb - 2rem);
  inline-size: 100%;
  max-inline-size: 100vi;
  overflow: hidden;
  background-color: var(--color-aux-950);
  border: 1px solid bvar(--color-core-primary);
  border-radius: 4px;
  padding: 0 2rem;
  display: grid;
  grid-template-rows: min-content 1fr min-content;
}

.content-section {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-base-8x);
  align-items: start;
  padding-block: var(--space-base-8x);
  justify-content: space-between;
  overflow: hidden;
  max-inline-size: 100vi;
}

.demo {
  inline-size: max(calc(50% - var(--space-base-8x)), 500px);
}

              
            
!

JS

              
                
              
            
!
999px

Console