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.
<div id='cssmenu'>
<ul>
<li><a href='#'>Home</a></li>
<li><a href='#'>Products</a>
<ul>
<li><a href='#'>Product 1</a>
<ul>
<li><a href='#'>Sub Product</a></li>
<li><a href='#'>Sub Product</a></li>
</ul>
</li>
<li><a href='#'>Product 2</a>
<ul>
<li><a href='#'>Sub Product</a></li>
<li><a href='#'>Sub Product</a></li>
</ul>
</li>
</ul>
</li>
<li><a href='#'>About</a></li>
<li><a href='#'>Contact</a></li>
</ul>
</div>
@import url(http://fonts.googleapis.com/css?family=Montserrat:400,700);
#cssmenu,
#cssmenu ul,
#cssmenu ul li,
#cssmenu ul li a,
#cssmenu #menu-button {
margin: 0;
padding: 0;
border: 0;
list-style: none;
line-height: 1;
display: block;
position: relative;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
#cssmenu:after,
#cssmenu>ul:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
#cssmenu #menu-button {
display: none;
}
#cssmenu {
font-family: Montserrat, sans-serif;
background: #333333;
}
#cssmenu>ul>li {
float: left;
}
#cssmenu>ul>li>a {
padding: 17px;
font-size: 12px;
letter-spacing: 1px;
text-decoration: none;
color: #dddddd;
font-weight: 700;
text-transform: uppercase;
-webkit-transition: color .25s ease;
-moz-transition: color .25s ease;
transition: color .25s ease;
}
#cssmenu>ul>li:hover>a {
color: #ffffff;
}
#cssmenu>ul>li:hover>a {
color: #ffffff;
}
#cssmenu ul>li.has-sub>a:after {
position: absolute;
top: 22px;
right: 11px;
width: 8px;
height: 2px;
display: block;
background: #dddddd;
content: '';
-webkit-transition: background .25s ease;
-moz-transition: background .25s ease;
transition: background .25s ease;
}
#cssmenu>ul>li.has-sub:hover>a:after,
#cssmenu>ul>li.has-sub>a:hover:after {
background: #ffffff;
}
#cssmenu ul>li.has-sub>a:before {
position: absolute;
top: 19px;
right: 14px;
display: block;
width: 2px;
height: 8px;
background: #dddddd;
content: '';
-webkit-transition: all .25s ease;
-moz-transition: all .25s ease;
-ms-transition: all .25s ease;
-o-transition: all .25s ease;
transition: all .25s ease;
}
#cssmenu ul>li.has-sub:hover>a:before,
#cssmenu ul>li.has-sub>a:hover:before {
top: 23px;
height: 0;
}
#cssmenu ul ul {
position: absolute;
left: -9999px;
}
#cssmenu li:hover>ul {
left: auto;
}
#cssmenu ul ul ul {
margin-left: 100%;
top: 0;
}
Project tracking, teamwork & client reporting like you've never seen before.
ads via Carbon
HTML & CSS Menus
Drop Down Menus
Responsive Menus
Accordion Menus
Horizontal Menus
Vertical Menus
Flyout Menus
Tabbed Menus
More Free Menus
100+ More Menu Templates
Creating a Responsive Menu
30 MAY 2017 Tutorials
In this tutorial, I'm going to show you how to create one of the responsive menus from our website. You can find the menu here. So, the final result will look like this:
Creating a Responsive Menu
The Markup
We're going to start with the HTML code. Our menus use nested unordered lists contained by a div.
<div id='cssmenu'>
<ul>
<li><a href='#'>Home</a></li>
<li><a href='#'>Products</a>
<ul>
<li><a href='#'>Product 1</a>
<ul>
<li><a href='#'>Sub Product</a></li>
<li><a href='#'>Sub Product</a></li>
</ul>
</li>
<li><a href='#'>Product 2</a>
<ul>
<li><a href='#'>Sub Product</a></li>
<li><a href='#'>Sub Product</a></li>
</ul>
</li>
</ul>
</li>
<li><a href='#'>About</a></li>
<li><a href='#'>Contact</a></li>
</ul>
</div>
The menu uses our jQuery plugin which can be found here. Let's download jQuery and the plugin, then include them:
Now we initialize the plugin. Our plugin has three options: title (which will be the text of the button that toggles the menu on mobile devices), breakpoint (the screen width at which the menu will switch to its mobile mode, the default value being 768) and format (it can take three values and determines what type of menu we'll have on mobile devices). For this menu, the format option will take the value multitoggle, which means each submenu can be opened/closed on its own. The other two values of this option are dropdown (the entire menu, including the submenus, will be opened/closed with a single touch) and select (the menu will be displayed as a select list of mobile devices).
The CSS
Our menus use LESS for the menu builder, but we want to make this tutorial accessible for everybody, so I'm going to use vanilla CSS this time. I'm also not going to include all the options of the menu in the tutorial, it would just make it unnecessarily long. First, we're going to include a font from Google Fonts and use a stylesheet reset targeting all the elements used by our menu:
@import url(http://fonts.googleapis.com/css?family=Montserrat:400,700);
#cssmenu,
#cssmenu ul,
#cssmenu ul li,
#cssmenu ul li a,
#cssmenu #menu-button {
margin: 0;
padding: 0;
border: 0;
list-style: none;
line-height: 1;
display: block;
position: relative;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
We use the stylesheet reset to prevent our menus from conflicting with other CSS from the user's page. Next, the clearfix:
#cssmenu:after,
#cssmenu>ul:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
We don't need the button which toggles the menu on mobile devices, so we're going to hide it for now:
#cssmenu #menu-button {
display: none;
}
The menu font and background color:
#cssmenu {
font-family: Montserrat, sans-serif;
background: #333333;
}
We need the menu items on the same line, so we'll float them to the left:
#cssmenu>ul>li {
float: left;
}
Next, we'll style the main links (padding, typography, and transitions):
#cssmenu>ul>li>a {
padding: 17px;
font-size: 12px;
letter-spacing: 1px;
text-decoration: none;
color: #dddddd;
font-weight: 700;
text-transform: uppercase;
-webkit-transition: color .25s ease;
-moz-transition: color .25s ease;
transition: color .25s ease;
}
The links change their color on hover, going from a light gray to white:
#cssmenu>ul>li:hover>a {
color: #ffffff;
}
The links which have submenus need extra space to the right of the submenu indicator. The list items which contain those links have a class called has-sub which is added by the jQuery plugin but can also by added by the menu builder.
#cssmenu>ul>li.has-sub>a {
padding-right: 30px;
}
At this point, the menu should look like this:
Creating a Responsive Menu
Now let's create the submenu indicator, which will be a "+" symbol. On hover, the submenu will be displayed and the indicator will become a horizontal line/a single dash. We'll create the horizontal line first with an after pseudoelement. The line will be 8px wide and 2px high and must have the same color as the links.
#cssmenu ul>li.has-sub>a:after {
position: absolute;
top: 22px;
right: 11px;
width: 8px;
height: 2px;
display: block;
background: #dddddd;
content: '';
-webkit-transition: background .25s ease;
-moz-transition: background .25s ease;
transition: background .25s ease;
}
Creating a Responsive Menu
On hover, the line changes its color just like the menu links:
#cssmenu>ul>li.has-sub:hover>a:after,
#cssmenu>ul>li.has-sub>a:hover:after {
background: #ffffff;
}
The vertical line is next. We'll use a before pseudoelement this time:
#cssmenu ul>li.has-sub>a:before {
position: absolute;
top: 19px;
right: 14px;
display: block;
width: 2px;
height: 8px;
background: #dddddd;
content: '';
-webkit-transition: all .25s ease;
-moz-transition: all .25s ease;
-ms-transition: all .25s ease;
-o-transition: all .25s ease;
transition: all .25s ease;
}
Creating a Responsive Menu
On hover, the vertical line will disappear. We'll do that by making its height 0. We'll also modify its vertical position to create a nice effect.
#cssmenu ul>li.has-sub:hover>a:before,
#cssmenu ul>li.has-sub>a:hover:before {
top: 23px;
height: 0;
}
The main menu is finished, it's time to move on to the submenus. They should be hidden by default:
#cssmenu ul ul {
position: absolute;
left: -9999px;
}
They will be displayed on hover:
#cssmenu li:hover>ul {
left: auto;
}
The second level submenus will be displayed to the right of their parent. We'll use margin-left to push them to the right:
#cssmenu ul ul ul {
margin-left: 100%;
top: 0;
}
Now we're going to give the submenus a nice expanding effect when they are displayed. First, we give the height 0 to the list elements of the submenus. Then we use CSS transitions and bring them to their normal height, which is 35px.
#cssmenu ul ul li {
height: 0;
-webkit-transition: height .25s ease;
-moz-transition: height .25s ease;
transition: height .25s ease;
}
#cssmenu li:hover>ul>li {
height: 35px;
}
#cssmenu ul ul li a {
width: 170px;
padding: 11px 15px;
border-bottom: 1px solid rgba(150, 150, 150, 0.15);
font-size: 12px;
text-decoration: none;
color: #dddddd;
font-weight: 400;
background: #333333;
}
#cssmenu ul ul li:last-child>a,
#cssmenu ul ul li.last-item>a {
border-bottom: 0;
}
#cssmenu ul ul li:hover>a,
#cssmenu ul ul li a:hover {
color: #ffffff;
}
#cssmenu ul ul li.has-sub>a:after {
top: 16px;
right: 11px;
background: #dddddd;
}
#cssmenu ul ul>li.has-sub:hover>a:after,
#cssmenu ul ul>li.has-sub>a:hover:after {
background: #ffffff;
}
#cssmenu ul ul li.has-sub>a:before {
top: 13px;
right: 14px;
background: #dddddd;
}
#cssmenu ul ul>li.has-sub:hover>a:before {
top: 17px;
height: 0;
}
#cssmenu.small-screen {
width: 100%;
}
#cssmenu.small-screen ul {
width: 100%;
display: none;
}
#cssmenu.small-screen ul li {
width: 100%;
border-top: 1px solid rgba(120, 120, 120, 0.2);
}
#cssmenu.small-screen ul ul li,
#cssmenu.small-screen li:hover>ul>li {
height: auto;
}
#cssmenu.small-screen ul li a,
#cssmenu.small-screen ul ul li a {
width: 100%;
border-bottom: 0;
}
#cssmenu.small-screen>ul>li {
float: none;
}
#cssmenu.small-screen ul ul,
#cssmenu.small-screen ul ul ul {
position: relative;
left: 0;
width: 100%;
margin: 0;
text-align: left;
}
#cssmenu.small-screen ul ul li a {
padding-left: 25px;
}
#cssmenu.small-screen ul ul ul li a {
padding-left: 35px;
}
#cssmenu.small-screen>ul>li.has-sub>a:after,
#cssmenu.small-screen>ul>li.has-sub>a:before,
#cssmenu.small-screen ul ul>li.has-sub>a:after,
#cssmenu.small-screen ul ul>li.has-sub>a:before {
display: none;
}
#cssmenu.small-screen #menu-button {
display: block;
padding: 17px;
color: #dddddd;
cursor: pointer;
font-size: 12px;
text-transform: uppercase;
font-weight: 700;
}
#cssmenu.small-screen #menu-button:after {
position: absolute;
top: 22px;
right: 17px;
display: block;
height: 4px;
width: 20px;
border-top: 2px solid #dddddd;
border-bottom: 2px solid #dddddd;
content: '';
}
#cssmenu.small-screen #menu-button:before {
position: absolute;
top: 16px;
right: 17px;
display: block;
height: 2px;
width: 20px;
background: #dddddd;
content: '';
}
#cssmenu.small-screen #menu-button.menu-opened:after {
top: 23px;
border: 0;
height: 2px;
width: 15px;
background: #ffffff;
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-ms-transform: rotate(45deg);
-o-transform: rotate(45deg);
transform: rotate(45deg);
}
#cssmenu.small-screen #menu-button.menu-opened:before {
top: 23px;
background: #ffffff;
width: 15px;
-webkit-transform: rotate(-45deg);
-moz-transform: rotate(-45deg);
-ms-transform: rotate(-45deg);
-o-transform: rotate(-45deg);
transform: rotate(-45deg);
}
#cssmenu.small-screen .submenu-button {
position: absolute;
z-index: 99;
right: 0;
top: 0;
display: block;
border-left: 1px solid rgba(120, 120, 120, 0.2);
height: 46px;
width: 46px;
cursor: pointer;
}
#cssmenu.small-screen ul ul .submenu-button {
height: 34px;
width: 34px;
}
#cssmenu.small-screen .submenu-button.submenu-opened {
background: #262626;
}
#cssmenu.small-screen .submenu-button:after {
position: absolute;
top: 22px;
right: 19px;
width: 8px;
height: 2px;
display: block;
background: #dddddd;
content: '';
}
#cssmenu.small-screen ul ul .submenu-button:after {
top: 15px;
right: 13px;
}
#cssmenu.small-screen .submenu-button.submenu-opened:after {
background: #ffffff;
}
#cssmenu.small-screen .submenu-button:before {
position: absolute;
top: 19px;
right: 22px;
display: block;
width: 2px;
height: 8px;
background: #dddddd;
content: '';
}
#cssmenu.small-screen ul ul .submenu-button:before {
top: 12px;
right: 16px;
}
#cssmenu.small-screen .submenu-button.submenu-opened:before {
display: none;
}
Also see: Tab Triggers