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.
<label for="responsive-button" class="responsive-button">Menu</label>
<input type="checkbox" id="responsive-button" role="button">
<nav class="menu">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Menu Item</a></li>
<li><a href="#">Categories <span class="menu-arrow">▼</span></a>
<ul>
<li><a href="#">First Category</a></li>
<li><a href="#">Longer Category</a></li>
<li><a href="#">One more Category</a></li>
<li><a href="#">Last Category</a></li>
</ul>
</li>
<li><a href="#">Tags <span class="menu-arrow">▼</span></a>
<ul>
<li><a href="#">First Tag</a></li>
<li><a href="#">Next Tag</a></li>
<li><a href="#">Longer Tag</a></li>
<li><a href="#">Last Tag</a></li>
</ul>
</li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
/******************************** Layout ********************************/
/* applying a natural box layout model to all elements */
html {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
*, *:before, *:after {
-webkit-box-sizing: inherit;
-moz-box-sizing: inherit;
box-sizing: inherit;
}
/******************************** Nav Menu *****************************/
/* This is the very basic steps of creating a horizontal menu */
/* Reset from the regular list definitions above */
.menu, .menu ul, .menu ul li, .menu ul li a,
.menu ul ul, .menu ul ul li, .menu ul ul li a {
margin: 0;
padding: 0;
border: 0;
line-height: 1;
}
/* Clearfix needed only in case of floating the LI, but not when using the inline method.
.menu:after, .menu > ul:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
*/
/* list-style-type: none to remove the bullets.
text-align: center in combination with LI set to inline is centering the whole menu, great.
If inline cannot be used due to the white-space problem, check this method for absolute centering:
http://www.smashingmagazine.com/2013/08/09/absolute-horizontal-vertical-centering-css/ */
.menu ul {
list-style-type: none;
text-align: center;
}
/* position: relative is needed for the position: absolute of submenu to refer to its parent element.
LI needs to be set to inline to go into horizontal.
The only disadvantage compared to float:left is a mysterious 4px gap between the elements -
on how to remove it see https://css-tricks.com/fighting-the-space-between-inline-block-elements/
text-align: center makes the A links centered inside their LI containers, only makes sense if A links have a fixed width */
.menu ul li {
position: relative;
display: inline;
text-align: center;
}
/* text-decoration: none is needed to remove the underlining of links.
display: inline-block is needed to apply a fixed width or padding to this otherwise inline element of A. */
.menu ul li a {
text-decoration: none;
display: inline-block;
width: 200px;
padding: 20px;
color: #333;
background: RoyalBlue;
}
/* Declaration of hover, focus and active state - can be done separately of course if desired */
.menu ul li a:hover, .menu ul li a:focus, .menu ul li a:active {
color: #777;
background: CornflowerBlue;
}
/* Styling of the arrow that indicates a submenu and which by default is way too big. */
.menu-arrow {
font-size: 10px;
}
/******* Here starts the submenu section. *******/
/* First, the submenu is positioned in relation to the LI of parent absolutely.
To not be visible, there are three options: left-9999px, display: none or opactity.
I have chosen opacity because it allows smooth blending in while not having any disadvantages.
Using this method instead of display:none screenreaders will still be able to see the whole menu.
Yet on veeery large sites this might lead to an information overflow and display:none here and display:initial on the next item might be preferable:
http://manwithnoblog.com/2009/12/06/the-case-for-the-use-of-display-none/ */
.menu ul ul {
position: absolute;
left:0;
opacity: 0;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)"; /* IE 8 */
filter: alpha(opacity=0); /* IE 5-7 */
}
/* When the parent LI is hovered, submenu is made visible. */
.menu ul li:hover ul {
opacity: 1;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)"; /* IE 8 */
filter: alpha(opacity=100); /* IE 5-7 */
}
/* The LI of the submenu are set back to block, so they are aligned vertically again.
Also text-align is set back to left, because centered entries don't look good in the submenu.*/
.menu ul ul li {
display: block;
text-align: left;
}
/* Styling of the submenu entries.
Width: 100% is needed here so the border goes to the end of the LI which is giving the width based on the longest entry.
Min-width is set to have a more consistent appearance if some submenu items would otherwise be very short.
White-space is set to nowrap so they stay in one line.
Border bottom is simply a nice thing in a submenu :) */
.menu ul ul li a {
width: 100%;
min-width:200px;
padding: 20px;
white-space: nowrap;
border-bottom: 1px solid #ccc;
color: #333;
background: DodgerBlue;
}
/* Declaration of hover, focus and active state - can be done separately of course if desired */
.menu ul ul li a:hover, .menu ul ul li a:focus, .menu ul ul li a:active {
color: #777;
background: DeepSkyBlue;
}
/******* Goodies *******/
/* Applying smooth transitions on color changes and opacity fade in of dropdown menu.
As of June 2015 and according to caniuse.com, this two should be enough, since anyhow no harm will be done in older browsers that simply loose the smoothness but not functionality. */
.menu ul li a, .menu ul ul li, .menu ul li:hover, .menu ul ul {
-webkit-transition: all .4s ease-in-out;
transition: all .4s ease-in-out;
}
/* Styles and hides the responsive button.
This is a pure CSS solution for the moment, using an invisible checkbox as toggle element. */
.responsive-button {
padding: 20px;
color: #333;
font-weight: bold;
background: RoyalBlue;
border-bottom: 1px solid #ccc;
text-align: center;
display: none;
}
/* Permanently hides the checkbox that helps us as a menu toogle element */
input[id=responsive-button] {
display: none;
}
/* Shows menu when invisible checkbox is checked */
input[id=responsive-button]:checked ~ .menu {
display: block;
}
/******* Responsiveness *******/
/* This is the very basic one-step solution without any intermediary stages.
The menu is directly collapsing at a certain breakpoint that needs to be set according to the width of the respective menu.
For later WordPress use, meaning dynamic content, we need to expand this solution.
*/
@media screen and (max-width : 780px){
/* Shows the responsive button */
.responsive-button {
display: block;
}
.menu {
display: none;
}
/* Reset menu items position to stack up vertically.
Adding a bottom line for better separation. */
.menu ul li {
display: block;
border-bottom: 1px solid #ccc;
}
/* Making all items full width */
.menu ul li, .menu ul li a {
width: 100%;
}
/* Reset to position: relative is needed so the submenu is opening inside the element flow, not covering the following parent menu items.
This is why hiding them can not be done with the left:-9999px method, and also opacity doesn't work here because it leaves an empty space between the parent items.
So for the moment it seems that responsive dropdown menus can not be accomplished accessible using CSS only. */
.menu ul ul {
position: relative;
display: none;
}
/* Showing the submenu */
.menu ul li:hover ul {
display: block;
}
/* text-align is now set to center, because left entries don't look good in a responsive submenu. */
.menu ul ul li {
text-align: center;
}
}
Also see: Tab Triggers