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="app"></div>
:root {
--font-sans: 'Josefin Sans', sans-serif;
--font-serif: 'Tinos', serif;
--base-gutter: 28px;
--product-inner-padding: 5vw;
--color-primary: #000000;
--color-secondary: #23CE6B;
--color-tertiary: #FFFFFF;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
min-height: 100vh;
}
main {
margin: 0 auto;
max-width: 1080px;
padding: var(--base-gutter) 0;
width: 95%;
}
@media only screen and (max-width: 40em) {
main {
display: block !important;
}
}
header {
font-family: var(--font-sans);
position: sticky;
top: 0;
left: 0;
width: 100%;
background: var(--color-primary);
padding: 20px 4vw;
color: var(--color-tertiary);
z-index: 100;
}
header h1 {
margin: 0;
font-size: 20px;
line-height: 1;
margin: 0 auto;
max-width: 1080px;
}
hr {
margin: var(--base-gutter) auto;
width: 100%;
height: 10px;
border: 0;
background: var(--color-primary);
}
p {
line-height: 1.7;
font-size: 14px;
opacity: 0.7;
text-align: left;
}
/* Helpers */
.df { display: flex }
.ait { align-items: flex-start }
.ff-sans { font-family: var(--font-sans) }
.ff-serif { font-family: var(--font-serif) }
/* Image Gallery */
.image-gallery {
width: 60%;
padding: 2vh 0;
}
@media only screen and (max-width: 70em) {
.image-gallery { width: 100%; }
}
.image-gallery-main {
width: 100%;
height: auto;
display: block;
}
/* Product */
.product {
width: 50%;
min-height: 100vh;
text-align: center;
padding: 0 var(--product-inner-padding);
display: flex;
flex-direction: column;
}
@media only screen and (max-width: 40em) {
.product { width: 100%; }
}
.product-title {
max-width: 85%;
margin: 0 auto;
margin-bottom: var(--base-gutter);
font-size: 24px;
padding-top: 2.5vh;
}
.product-price {
margin-bottom: var(--base-gutter);
font-weight: 400;
font-size: 22px;
}
/* Thumbnails */
.thumbnails {
display: flex;
width: 240px;
}
.thumbnails-item {
list-style: none;
margin-top: var(--base-gutter);
margin-right: 15px;
opacity: 0.5;
transition: opacity 350ms linear;
}
.thumbnails-item-active {
opacity: 1;
}
.thumbnails-item:last-of-type { margin-right: 0; }
.thumbnails-item img {
width: 100%;
display: block;
height: auto;
}
/* Button */
.button {
box-sizing: border-box;
font-family: var(--font-sans);
font-size: 22px;
background: var(--color-primary);
color: var(--color-tertiary);
border: 0;
-webikit-appearance: none;
padding: 20px 30px;
width: 100%;
display: block;
transition: background 250ms ease-in-out;
}
.button:hover {
background: var(--color-secondary);
cursor: pointer;
}
.button:disabled {
background: var(--color-secondary);
cursor: not-allowed;
}
.count {
position: fixed;
top: 19px;
right: 20px;
z-index: 100;
background: white;
padding: 3px 10px 0 10px;
border-radius: 100px;
font-weight: 500;
min-height: 20px;
display: flex;
align-items: center;
justify-content: center;
}
.pulse {
animation: pulse 350ms ease-in-out;
}
@keyframes pulse {
0% {
transform: scale(1);
}
50% {
transform: scale(1.5);
}
100% {
transform: scale(1);
}
}
.offer {
margin-top: var(--base-gutter);
color: var(--color-secondary);
border: 2px solid var(--color-secondary);
padding: 8px 10px;
}
function Header() {
return (
<header>
<h1>Shop.</h1>
</header>
);
}
function ImageGallery(props) {
const [image, setImage] = React.useState(0);
return (
<div className="image-gallery">
<img className="image-gallery-main" src={props.images[image].src} />
<ul class="thumbnails">
{props.images.map((item, index) => {
return (
<li key={image.id} className={`thumbnails-item ${image == item.id && `thumbnails-item-active`}`}>
<img onClick={() => setImage(item.id) } src={item.src} />
</li>
);
})}
</ul>
</div>
);
}
function Product(props) {
const [count, setCount] = React.useState(0);
const addToBasket = (e) => {
const button = e.target;
const counter = document.querySelector('.count');
setCount(count + 1);
counter.classList.add('pulse');
button.innerText = 'Added to basket!'
button.setAttribute('disabled', true);
setTimeout((e) => {
button.removeAttribute('disabled');
button.innerText = 'Add to basket'
counter.classList.remove('pulse');
}, 1000)
}
return (
<div className="product">
<div className="product-info">
<h2 className="product-title ff-sans">{props.product.name}</h2>
<h4 className="product-price ff-sans">{props.product.price}</h4>
<button onClick={addToBasket} className="button">Add to basket</button>
</div>
<span className="offer ff-sans">20% off with voucher: FU3L3D</span>
<div className="product-info">
<hr />
<p className="product-description ff-serif">{props.product.description}</p>
<hr />
</div>
<span className="count ff-sans">{count}</span>
</div>
)
}
class App extends React.Component {
render() {
const galleryImages = [
{
id: 0,
src: `https://s3-us-west-2.amazonaws.com/s.cdpn.io/3219/polar-multi-colour-t-shirt-black-multi-1.jpg`
},
{
id: 1,
src: `https://s3-us-west-2.amazonaws.com/s.cdpn.io/3219/polar-multi-colour-t-shirt-black-multi-2.jpg`
},
{
id: 2,
src: `https://s3-us-west-2.amazonaws.com/s.cdpn.io/3219/polar-multi-colour-t-shirt-black-multi-3.jpg`
},
];
const productInfo = {
name: `Polar Multi Colour T-Shirt Size L`,
price: `$45.00`,
description: `Adorned with multi coloured stripes, this Multi Colour T-Shirt from Polar is made from 210gsm 100% cotton and includes a black colour block curved neckline and subtle logo embroidery on the chest. The body has been cut to a regular fit whilst the short sleeves allow for varying styling possibilities. Flowing vertically down the body and at an angle on the sleeves, the stripes have been roll printed and vary between tones of green, white and yellow, alongside a decorative embroidered logo which has been stitched to sit against the design in a shade of dark green. The body has a straight finish which allows the fabric to be easily tucked into trousers or shorts.`
}
return (
<div>
<Header />
<main className="df ait">
<ImageGallery images={galleryImages} />
<Product product={productInfo} />
</main>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById("app"));
Also see: Tab Triggers