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="root"></div>
@import url('https://fonts.googleapis.com/css?family=Open+Sans:700');
/* define the fonts and colors used in the project */
:root {
--font: 'Open Sans', sans-serif;
--color-bg: #252525;
--color-txt: #fff;
--color-home: #F1C40F;
--color-about: #16A085;
--color-projects: #E74C3C;
--color-contacts: #2980B9;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
width: 100%;
font-family: var(--font);
color: var(--color-txt);
background: var(--color-bg);
}
.App .Navbar {
/* display the items of the navigation bar in a grid, where the anchor link elements take the space left available by the header */
display: grid;
grid-template-columns: auto 1fr;
/* center the items vertically */
align-items: center;
}
.App .Navbar h2 {
/* include padding instead of margin to have the header attached to the window's borders (and therefore to avoid the string included with a pseudo element from being displayed inappropriately to the left of the header) */
padding: 0.25rem 0.75rem;
font-size: 2.5rem;
color: var(--color-bg);
background: var(--color-home);
/* position relative to absolute position the pseudo element */
position: relative;
/* transition for the change in background color (occurring as the nav items are pressed) */
transition: all 0.3s ease-in-out;
}
.App .Navbar h2:before {
/* with a pseudo element include a string, which is hidden by default below the header, to the left of the page */
content: 'borntofrappé';
position: absolute;
left: 100%;
transform: translateX(-50vw);
/* top to match the vertical padding */
top: 0.25rem;
color: var(--color-txt);
/* transition for the transform property, to translate the string into view and to the right of the header */
transition: all 0.3s ease-in-out;
/* z-index to have the pseudo element below the header itself */
z-index: -5;
}
.App .Navbar h2:hover:before {
/* translate the pseudo element into view on hover */
transform: translateX(10px);
}
.App .Navbar ul {
/* display the unordered list in a single row, placing the elements at the end of the grid column */
display: flex;
justify-content: flex-end;
padding: 0.25rem 0.75rem;
list-style: none;
}
.App .Navbar ul li {
/* separate the list items more prominently */
margin: 0 0.75rem;
font-size: 1.4rem;
text-transform: uppercase;
}
.App .Navbar ul li a {
/* reset the anchor link styles */
color: inherit;
text-decoration: none;
padding: 0 0.25rem;
letter-spacing: 0.2rem;
/* position relative to absolute position the connected pseudo element */
position: relative;
}
.App .Navbar ul li a:before {
/* include a faux-border with a pseudo element */
content: '';
position: absolute;
/* by default have the faux-border hidden from view */
top: 100%;
bottom: 0;
left: 0;
right: 0;
/* transition for the hover state */
transition: all 0.3s ease-out;
}
/* include the different colors for the different anchor links */
.App .Navbar ul li:nth-of-type(1) a:before {
background: var(--color-about);
}
.App .Navbar ul li:nth-of-type(2) a:before {
background: var(--color-projects);
}
.App .Navbar ul li:nth-of-type(3) a:before {
background: var(--color-contacts);
}
.App .Navbar ul li a:hover:before {
/* on hover, change the bottom property to have the faux-border stretch from the top down */
bottom: -5px;
}
.App h1 {
/* style the header to have the color matching the background of the header in the navbar */
color: var(--color-home);
text-transform: uppercase;
text-align: center;
margin-top: 20vh;
font-size: calc(2rem + 1.5vw);
}
/*
for relatively smaller and smaller viewports:
- start by hiding the pseudo element responsible for the string
- continue by removing the header bearing the "logo" as well
*/
@media (max-width: 750px) {
.App .Navbar h2:before {
display: none;
}
}
@media (max-width: 400px) {
/* adjust the visible anchor link elements to show them in a single column, centered in the page */
.App .Navbar h2 {
display: none;
}
.App .Navbar ul {
grid-column: 1/3;
flex-direction: column;
align-items: center;
}
.App .Navbar ul li {
margin: 0.5rem 0;
}
}
const Component = React.Component,
Router = ReactRouterDOM.BrowserRouter,
Link = ReactRouterDOM.Link,
Switch = ReactRouterDOM.Switch,
Route = ReactRouterDOM.Route;
// in a stateful component render a simple navigation bar and an element which depends on which link is active
class App extends Component {
// when the component is updated (which also occurs as the new routes are included)
// consider the text of the main header and alter the CSS custom property to style the background of the header in the navigation bar and the color of the header according to which route is included
componentDidUpdate() {
let header = document.querySelector("h1");
let text = header.textContent.toLowerCase();
let color;
switch(text) {
case 'about':
color = "#16A085";
break;
case 'projects':
color = "#E74C3C";
break;
case 'contacts':
color = "#2980B9";
break;
default:
color = "#F1C40F";
break;
}
// update the custom property for the root element and have it cascade on the header as well
let root = document.querySelector(":root");
root.style.setProperty("--color-home", color);
}
/* render
- a nav with a header and three anchor links elements
in the nav, include Links from the routing library, directing toward differents paths
- a header displaying the path of the application
through a switch, include routes which exclude one another
render a simple element in the route to visualize the change in the URL path
*/
render() {
return (
<div className="App">
<nav className="Navbar">
<h2>btf</h2>
<ul>
<li>
<Link to="/about">about</Link>
</li>
<li>
<Link to="/projects">projects</Link>
</li>
<li>
<Link to="/contacts">contacts</Link>
</li>
</ul>
</nav>
<Switch>
<Route exact path="/">
<h1>Home</h1>
</Route>
<Route path="/about">
<h1>About</h1>
</Route>
<Route path="/projects">
<h1>Projects</h1>
</Route>
<Route path="/contacts">
<h1>Contacts</h1>
</Route>
</Switch>
</div>
);
}
}
// render the single component responsible for the navigation bar, wrapped in the router element
ReactDOM.render(
<Router>
<App />
</Router>
, document.getElementById('root'));
Also see: Tab Triggers