123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <h1>Using flexbox to make responsive widgets without media queries.
  <h2>With one fixed width column (unless its on its own row) [in <span class="fixed-color">dark blue</span>].</h2>
<p>Allows for widget layout where parts of the widget have minimum widths and <strong>when&hellip;:</strong></p>
<ol>
  <li><strong>&hellip;we don't know</strong> the placement of the widget on a page: whether the widget would be in a main column or a side column</li>
  <li><strong>&hellip;we don't know</strong> the width of the viewport, ever.</li>
  <li><strong>&hellip;we don't know</strong> which pre-existing media queries exist</li>
</ol>

<h2>Various width containers</h2>
<p>Example use case <a href="https://codepen.io/mkstix6/pen/jPxMje">Flexbox responsive sign up widget. No media queries</a></p>
  <p>If anything is unclear, leave me a comment :)</p>
  <p><em>Get changing that browser window width already</em></p>
  <h2>Pros</h2>
  <ul>
    <li>No media queries</li>
    <li>Items can be set to wrap when they reach their minimum width.</li>
  </ul>
  <h2>Cons</h2>
  <ul>
    <li>Predicting exactly when [viewport width] something will wrap is tricky (math!). E.g. when something wraps, you also want a different text size (this is a P.I.T.A. to achieve without media queries to orchestrate changes to properties other than width).</li>
  </ul>
<h4 class="column100">Container 100% width</h4>
<div class="container column100">
 <div class="fluidWidthDownToAPoint"></div>
 <div class="fixedUnlessOnOwnRow"></div>
</div>

<h4 class="column80">Container 80% width</h4>
<div class="container column80">
 <div class="fluidWidthDownToAPoint"></div>
 <div class="fixedUnlessOnOwnRow"></div>
</div>

<h4 class="column60">Container 60% width</h4>
<div class="container column60">
 <div class="fluidWidthDownToAPoint"></div>
 <div class="fixedUnlessOnOwnRow"></div>
</div>

<h4 class="column40">Container 40% width</h4>
<div class="container column40">
 <div class="fluidWidthDownToAPoint"></div>
 <div class="fixedUnlessOnOwnRow"></div>
</div>

<h4 class="column20">Container 20% width</h4>
<div class="container column20">
 <div class="fluidWidthDownToAPoint"></div>
 <div class="fixedUnlessOnOwnRow"></div>
</div>

<hr>

<h3>Nesting the HTML</h3>
<div class="container">
 <div class="fluidWidthDownToAPoint">
   <div class="container">
     <div class="fluidWidthDownToAPoint"></div>
     <div class="fixedUnlessOnOwnRow"></div>
   </div>
 </div>
 <div class="fixedUnlessOnOwnRow">
  <div class="container">
   <div class="fluidWidthDownToAPoint"></div>
   <div class="fixedUnlessOnOwnRow"></div>
 </div>
</div>
</div>



            
          
!
            
              ///////////////// interesting flexbox bits /////////////////////////
.container {
  display: flex;
  flex-wrap: wrap;
}
.fluidWidthDownToAPoint {
  flex-grow: 999999; // a relatively big number: needed for OTHER column to be fixed width // if it's too big IE fails to compute
  flex-shrink: 1;
  flex-basis: 400px; //effectively it's min-width
}
.fixedUnlessOnOwnRow {
  flex-grow: 1; // a relatively small number: needed for THIS column to be fixed width. Not "0" so it will also fill to expand a row if it is on it's own in the row.
  flex-shrink: 1;
  flex-basis: 300px; //effectively it's min-width
}

///////////////// stylings /////////////////////////

.fluidWidthDownToAPoint {
  $color: #00ffb4;
  background: rgba($color, 0.4);
  padding: 20px;
  box-sizing: border-box;
  &::before {
    display: block;
    margin-bottom: 1em;
    color: rgba(darken($color, 60%), 0.4);
    content: "Fluid width down to 400px, then 100% width when on own row.";
  }
  &:empty::before {
    margin-bottom: 0;
  }
}
$fixedColor: #06006d;
.fixedUnlessOnOwnRow {
  color: white;
  background: rgba($fixedColor, 0.5);
  padding: 20px;
  box-sizing: border-box;
  &::before {
    display: block;
    margin-bottom: 1em;
    color: white;
    content: "Fixed width of 300px unless on own row. Width 100% when on own row.";
  }
  &:empty::before {
    margin-bottom: 0;
  }
}

.fixed-color {
  color: white;
  padding: 0 0.2em;
  border-radius: 0.45em;
  background-color: rgba($fixedColor, 0.5);
}

%hmeasureguide {
  $color: rgba(black, 0.3);
  color: $color;
  margin-left: auto;
  margin-right: auto;
  position: relative;
  border-bottom: 1px solid $color;
  &:after {
    box-shadow: inset 1px 0 0 $color, inset -1px 0 0 $color;
    content: "";
    display: block;
    position: absolute;
    height: 100%;
    top: 50%;
    left: 0;
    right: 0;
  }
}

hr {
  opacity: 0.5;
  font-size: 8px;
  border: none;
  height: 0.5em;
  width: 100%;
  display: block;
  background-color: transparent;
  background-image: radial-gradient(
    circle at center center,
    white,
    white 0.25em,
    transparent 0.25em,
    transparent
  );
  background-repeat: repeat-x;
  background-size: 0.75em 0.5em;
  margin-top: 40px;
  margin-bottom: 40px;
}

html {
  font-family: sans-serif;
  background: #fff;
  /*background-image: linear-gradient(to bottom right, #00FFB4 0%, #0EB0DC 48%, #1D43B7 80%, #06006D 100%);*/
  background-image: linear-gradient(to bottom right, #eee 0%, #ccc 100%);
  background-size: 100vw 100vh;
  background-attachment: fixed;
  padding: 40px 50px 100px;
}
h4 {
  text-align: center;
  margin-top: 3em;
  @extend %hmeasureguide;
}

.container {
  box-sizing: border-box;
  background: rgba(white, 0.9);
  background: transparent;
  border: 1px solid rgba(black, 0.1);
  border: 2px solid white;
  border: none;
  /*box-shadow:inset 0 0 0 10px rgba(white,1);*/
  box-shadow: 0 0 0 0.2em white;
  border-radius: 1em;
  overflow: hidden;
  margin-left: auto;
  margin-right: auto;
  padding: 1px;
}
.container .container {
  border-radius: 0.5em;
}
.column100 {
  width: 100%;
}
.column80 {
  width: 80%;
}
.column60 {
  width: 60%;
}
.column40 {
  width: 40%;
}
.column20 {
  width: 20%;
}
html,
body,
h1,
h2,
h3,
h4,
h5,
h6,
p {
  font-family: helvetica, arial, sans-serif;
  font-weight: 300;
  line-height: 1.5;
}

            
          
!
            
              var h4s = $('h4');
h4s.append('<span class="sizemeasure" />');

measureh4s();
$(window).resize(function(){
	measureh4s();
});


function measureh4s(){
    h4s.each(function(){
    var elemwidth = $(this).width();
    $(this).children('.sizemeasure').text(' : '+elemwidth+'px');
  });
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console