HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<h1>Flex Grow, Flex Shrink and Flex Basis</h1>
<h1>Flex Basis</h1>
<p>Before we start growing or shrinking the elements, we need to should set the basis of the elements</p>
<p>The flex-basis CSS property specifies the flex basis which is the initial main size of a flex item. </p>
<p>This defines the default size of an element before the remaining space is distributed. It can be a length (e.g. 20%, 5rem, etc.) or a keyword. </p>
<p>Flex basis is similar to min-width. Try applying flex-wrap: wrap; on the flex containers.</p>
<ul class="flex-container flex-basis">
<li class="flex-item flex-1">flex-basis: 100px;</li>
<li class="flex-item flex-2">flex-basis: 200px;</li>
<li class="flex-item flex-3">flex-basis: 300px;</li>
</ul>
<h1 style="margin-top: 100px;">Flex Grows</h1>
<p>When we have extra space, how should the elements be divided among everybody on the same line?</p>
<h2 class="first-h2">Even Growth</h2>
<ul class="flex-container flex-grow-1">
<li class="flex-item flex-1">
flex-grow: 1;<br>
</li>
<li class="flex-item flex-2">
flex-grow: 1;<br>
</li>
<li class="flex-item flex-3">
flex-grow: 1;<br>
</li>
</ul>
<pre>
(700px / 3) * 1 = 233.33px
(700px / 3) * 1 = 233.33px
(700px / 3) * 1 = 233.33px
</pre>
<h2>Uneven Growth</h2>
<ul class="flex-container flex-grow-2">
<li class="flex-item flex-1">
flex-grow: 1;<br>
</li>
<li class="flex-item flex-2">
flex-grow: 3;<br>
</li>
<li class="flex-item flex-3">
flex-grow: 1;<br>
</li>
</ul>
<pre>
(700px / 5) * 1 = 140px
(700px / 5) * 3 = 420px
(700px / 5) * 1 = 140px
</pre>
<h1 style="margin-top: 100px;">Flex Grows with Flex Basis</h1>
<p>When we have extra space, how should the elements be divided among everybody on the same line?</p>
<p>Try setting the first box's flex-grow to 0</p>
<h2 class="first-h2">Flex Grow on one flex-item</h2>
<ul class="flex-container flex-grow-3">
<li class="flex-item flex-1">
flex-grow: 1;<br>
flex-basis: 100px;
</li>
<li class="flex-item flex-2">
flex-grow: 0;<br>
flex-basis: 200px;
</li>
<li class="flex-item flex-3">
flex-grow: 0;<br>
flex-basis: 300px;
</li>
</ul>
<pre>
width: 100px;
width: 200px;
width: 300px;
empty-space-left: 700px - 600px = 100px;
(flex-grow: 1) width: 200px (100px + 100px);
(flex-grow: 0) width: 200px;
(flex-grow: 0) width: 300px;
</pre>
<h2>Even Flex Growth on all flex items</h2>
<ul class="flex-container flex-grow-4">
<li class="flex-item flex-1">
flex-grow: 1;<br>
flex-basis: 100px;
</li>
<li class="flex-item flex-2">
flex-grow: 1;<br>
flex-basis: 200px;
</li>
<li class="flex-item flex-3">
flex-grow: 1;<br>
flex-basis: 300px;
</li>
</ul>
<pre>
width: 100px;
width: 200px;
width: 300px;
empty-space-left: 700px - 600px = 100px;
100px / 3 = 33.33px
(flex-grow: 1) width: 133.33px; (100 + 33.3)
(flex-grow: 1) width: 233.33px; (200 + 33.3)
(flex-grow: 1) width: 333.33px; (300 + 33.3)
</pre>
<h2>Uneven Flex Growth on all flex items</h2>
<ul class="flex-container flex-grow-5">
<li class="flex-item flex-1">
flex-grow: 1;<br>
flex-basis: 100px;
</li>
<li class="flex-item flex-2">
flex-grow: 3;<br>
flex-basis: 200px;
</li>
<li class="flex-item flex-3">
flex-grow: 1;<br>
flex-basis: 300px;
</li>
</ul>
<pre>
width: 100px;
width: 200px;
width: 300px;
empty-space-left: 700px - 600px = 100px;
100px / 5 = 20px
(flex-grow: 1) width: 120px; (100 + 20)
(flex-grow: 3) width: 260px; (200 + 20*3)
(flex-grow: 1) width: 320px; (300 + 20)
</pre>
<h2>Calculate the width of each of the flexboxes!</h2>
<p>Check the dimensions on Chrome DevTools to see if you got it right</p>
<ul class="flex-container flex-grow-6">
<li class="flex-item flex-1">
flex-grow: 1;<br>
flex-basis: 300px;
</li>
<li class="flex-item flex-2">
flex-grow: 3;<br>
flex-basis: 100px;
</li>
<li class="flex-item flex-3">
flex-grow: 2;<br>
flex-basis: 350px;
</li>
</ul>
<button class="js-flex-grow-show">show calculation</button>
<pre class="js-flex-grow-answer hide">
width: 300px
width: 100px
width: 350px
empty-space-left: 900px - 750px = 150px
150px / 6 = 25px
(flex-grow: 1) width: 325px; (300 + 25)
(flex-grow: 3) width: 175px; (100 + 25*3)
(flex-grow: 2) width: 400px; (350 + 25*2)
</pre>
<h1 style="margin-top: 100px;">Flex Shrinks</h1>
<p>What do we do if we don't have space?</p>
<p>The flex-shrink CSS property specifies the flex shrink factor of a flex item. Flex items will shrink to fill the container according to the flex-shrink number, when the default width of flex items is wider than the flex container.</p>
<p>If the parent has flex-wrap: wrap; flex-shrink won't work;</p>
<h2 class="first-h2">No Shrink</h2>
<ul class="flex-container flex-shrink-0">
<li class="flex-item flex-1">
flex-shrink: 0;<br>
flex-basis: 200px;
</li>
<li class="flex-item flex-2">
flex-shrink: 0;<br>
flex-basis: 200px;
</li>
<li class="flex-item flex-3">
flex-shrink: 0;<br>
flex-basis: 200px;
</li>
</ul>
<h2>Even Shrink</h2>
<ul class="flex-container flex-shrink-1">
<li class="flex-item flex-1">
flex-shrink: 1;<br>
flex-basis: 200px;
</li>
<li class="flex-item flex-2">
flex-shrink: 1;<br>
flex-basis: 200px;
</li>
<li class="flex-item flex-3">
flex-shrink: 1;<br>
flex-basis: 200px;
</li>
</ul>
<p>What if we give the flex-container a max-width of 300px?</p>
<pre>
/*Method 1*/
(300px / 3) * 1 = 100px
(300px / 3) * 1 = 100px
(300px / 3) * 1 = 100px
</pre>
<pre>
/*Method 2*/
total-flex-basis: 600px
container-size: 300px
space-remaining: -300px (300 - 600)
total-size: (1 * 200) + (1 * 200) + (1 * 200) = 600
How much each container shrinks?
(1 * 200) / 600 * -300 = -100px
(1 * 200) / 600 * -300 = -100px
(1 * 200) / 600 * -300 = -100px
Final Width
200 - 100 = 100px;
200 - 100 = 100px;
200 - 100 = 100px;
</pre>
<h2>Uneven Shrink</h2>
<ul class="flex-container flex-shrink-2">
<li class="flex-item flex-1">
flex-shrink: 1;<br>
flex-basis: 200px;
</li>
<li class="flex-item flex-2">
flex-shrink: 3;<br>
flex-basis: 200px;
</li>
<li class="flex-item flex-3">
flex-shrink: 1;<br>
flex-basis: 200px;
</li>
</ul>
<p>How about this one? What if we give it a width of 500px?</p>
<pre>
total-flex-basis: 600px
container-size: 500px
space-remaining: -100px
total-size: (1 * 200) + (3 * 200) + (1 * 200) = 1000
How much each container shrinks?
(1 * 200) / 1000 * -100 = -20px
(3 * 200) / 1000 * -100 = -60px
(1 * 200) / 1000 * -100 = -20px
Final Width
200 - 20 = 180px;
200 - 60 = 140px;
200 - 20 = 180px;
</pre>
<h2>flex-shrink and flex-basis</h2>
<ul class="flex-container flex-shrink-3">
<li class="flex-item flex-1">
flex-shrink: 2;<br>
flex-basis: 400px;
</li>
<li class="flex-item flex-2">
flex-shrink: 1;<br>
flex-basis: 100px;
</li>
<li class="flex-item flex-3">
flex-shrink: 3;<br>
flex-basis: 150px;
</li>
</ul>
<p>Final one! What are the width values for each flex-item if we were to shrink the flex-container to 450px?</p>
<pre>
total-flex-basis: 650px
container-size: 450px
space-remaining: -200px
total-size: (2 * 400) + (1 * 100) + (3 * 150) = 1350
How much each container shrinks?
(2 * 400) / 1350 * -200 = -118.50px
(1 * 100) / 1350 * -200 = -14.80px
(3 * 150) / 1350 * -200 = -66.67px
Final Width
400 - 118.50 = 281.50px;
100 - 14.80 = 85.20px;
150 - 66.67 = 83.33px;
</pre>
<p>Sidenote: Add flex-wrap: wrap to see how the layout changes</p>
<a href="https://madebymike.com.au/writing/understanding-flexbox/" target="_blank">Extra Read</a>
h2 {
margin: 80px 0px 20px;
&.first-h2 {
margin: 20px 0px;
}
}
pre {
margin: 30px 0px;
}
.flex-container {
margin: 0;
margin-bottom: 30px;
padding: 0;
list-style: none;
display: flex;
position: relative;
justify-content: flex-start;
background-color: beige;
padding: 10px 0px;
max-width: 700px;
box-sizing: border-box;
outline: 1px solid black;
&:after {
content:"700px";
font-style: italic;
font-size: 12px;
padding: 3px 10px;
position: absolute;
background-color: #8e44ad;
color: white;
bottom: -20px;
right: 50%;
transform: translateX(50%);
}
&:before {
content: "";
height: 2px;
width: 100%;
background-color: #8e44ad;
bottom: -10px;
position: absolute;
}
}
.flex-1 { background-color: #e74c3c;}
.flex-2 { background-color: #e67e22;}
.flex-3 { background-color: #3498db;}
.flex-item {
width: 100px;
height: 100px;
color: black;
font-size: 18px;
text-align: center;
}
.flex-1 {flex-basis: 100px;}
.flex-2 {flex-basis: 200px;}
.flex-3 {flex-basis: 300px;}
/*Flex Grows*/
.flex-grow-1 {
.flex-item {
flex-grow: 1;
flex-basis: 0;
}
}
.flex-grow-2 {
.flex-item {
flex-basis: 0;
}
.flex-1 {flex-grow: 1;}
.flex-2 {flex-grow: 3;}
.flex-3 {flex-grow: 1;}
}
.flex-grow-3 {
.flex-1 {flex-grow: 1;}
}
.flex-grow-4 {
.flex-item {flex-grow: 1;}
}
.flex-grow-5 {
.flex-1 {flex-grow: 1;}
.flex-2 {flex-grow: 3;}
.flex-3 {flex-grow: 1;}
}
.flex-grow-6 {
max-width: 900px;
&:after {
content:"900px"
}
.flex-1 {flex-basis: 300px; flex-grow: 1;}
.flex-2 {flex-basis: 100px; flex-grow: 3;}
.flex-3 {flex-basis: 350px; flex-grow: 2;}
}
.js-flex-grow-show {
margin-top: 0px;
}
.js-flex-grow-answer {
&.hide {
display: none;
}
}
.flex-shrink-0 {
.flex-item {
flex-basis: 200px;
flex-shrink: 0;
}
}
.flex-shrink-1 {
.flex-item {
flex-basis: 200px;
flex-shrink: 1;
}
}
.flex-shrink-2 {
.flex-item {
flex-basis: 200px;
flex-shrink: 1;
}
.flex-2 {
flex-shrink: 3;
}
}
.flex-shrink-3 {
.flex-1 {
flex-basis: 400px;
flex-shrink: 2;
}
.flex-2 {
flex-basis: 100px;
flex-shrink: 1;
}
.flex-3 {
flex-basis: 150px;
flex-shrink: 3;
}
}
Also see: Tab Triggers