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>
$slide-count: 5;
$slide-width: 30rem;
html {
box-sizing: border-box;
font-size: 10px;
@media (max-width: 425px) {
font-size: 8px;
}
}
body {
font-size: 1.5rem;
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
*,
*:before,
*:after {
-webkit-box-sizing: border-box;
box-sizing: inherit;
}
ul {
list-style-type: none;
padding: 0;
}
a {
text-decoration: none;
color: inherit;
}
.carousel__wrap {
margin-top: 10%;
align-items: center;
display: flex;
flex: 1;
justify-content: center;
position: relative;
width: 80%;
left: 50%;
transform: translateX(-50%);
}
.carousel__inner {
height: 40rem;
position: relative;
width: calc(#{$slide-width * 3});
}
.carousel__container {
height: 100%;
overflow: hidden;
position: relative;
width: 100%;
}
.carousel__slide-list {
height: 100%;
left: 50%;
list-style-type: none;
margin: 0;
padding: 0;
position: absolute;
transform: translateX(-50%);
width: calc(#{($slide-count + 0.5) * $slide-width * 2});
}
.carousel__slide-item {
display: inline-block;
height: $slide-width;
margin: 0;
padding: 1rem;
position: absolute;
transition: all 0.3s;
width: $slide-width;
}
.carousel__slide-item-img-link {
cursor: zoom-in;
display: flex;
height: 100%;
overflow: hidden;
position: relative;
width: 100%;
img {
height: 100%;
object-fit: cover;
transition: all 0.5s ease;
width: 100%;
}
&::after {
align-items: center;
background: rgba(black, 0.5);
color: white;
content: 'read more';
display: flex;
height: 100%;
justify-content: center;
opacity: 0;
position: absolute;
transition: all 0.5s ease;
width: 100%;
}
&:hover {
&::after {
opacity: 1;
}
img {
transform: scale(1.3);
}
}
}
.carousel-slide-item__body {
bottom: -2.5rem;
height: 10%;
position: absolute;
h4 {
margin: 0.7rem 0 0;
text-transform: uppercase;
}
p {
font-size: 1.2rem;
line-height: 1.3;
margin: 0.7rem 0 0;
}
}
.carousel__btn {
align-items: center;
background: 0;
border: 0;
cursor: pointer;
display: flex;
justify-content: center;
position: absolute;
top: 50%;
transform: translateY(-50%);
&--prev {
left: -10rem;
}
&--next {
right: -10rem;
}
}
.carousel__btn-arrow {
border: solid black;
border-width: 0 0.4rem 0.4rem 0;
height: 6rem;
padding: 3px;
width: 6rem;
z-index: 10;
&--left {
transform: rotate(135deg);
}
&--right {
transform: rotate(-45deg);
}
}
.carousel__dots {
display: inline-block;
left: 50%;
margin-top: 2rem;
position: absolute;
transform: translateX(-50%);
.dot {
background: #ccc;
border: 0;
border-radius: 50%;
cursor: pointer;
height: 2rem;
margin: 0 0.3rem;
outline: none;
transform: scale(0.5);
width: 2rem;
&.active {
background: black;
}
}
}
/************************************
1. If you want to add or remove items you will need to change a variable called $slide-count in the CSS *minimum 3 slides
2. if you want to change the dimensions of the slides you will need to edit the slideWidth variable here 👇 and the $slide-width variable in the CSS.
************************************/
const slideWidth = 30;
const _items = [
{
player: {
title: 'Efren Reyes',
desc: 'Known as "The Magician", Efren Reyes is well regarded by many professionals as the greatest all around player of all time.',
image: 'https://i.postimg.cc/RhYnBf5m/er-slider.jpg',
},
},
{
player: {
title: "Ronnie O'Sullivan",
desc: "Ronald Antonio O'Sullivan is a six-time world champion and is the most successful player in the history of snooker.",
image: 'https://i.postimg.cc/qBGQNc37/ro-slider.jpg',
},
},
{
player: {
title: 'Shane Van Boening',
desc: 'The "South Dakota Kid" is hearing-impaired and uses a hearing aid, but it has not limited his ability.',
image: 'https://i.postimg.cc/cHdMJQKG/svb-slider.jpg',
},
},
{
player: {
title: 'Mike Sigel',
desc: 'Mike Sigel or "Captain Hook" as many like to call him is an American professional pool player with over 108 tournament wins.',
image: 'https://i.postimg.cc/C12h7nZn/ms-1.jpg',
},
},
{
player: {
title: 'Willie Mosconi',
desc: 'Nicknamed "Mr. Pocket Billiards," Willie Mosconi was among the first Billiard Congress of America Hall of Fame inductees.',
image: 'https://i.postimg.cc/NfzMDVHP/willie-mosconi-slider.jpg',
},
},
];
const length = _items.length;
_items.push(..._items);
const sleep = (ms = 0) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
const createItem = (position, idx) => {
const item = {
styles: {
transform: `translateX(${position * slideWidth}rem)`,
},
player: _items[idx].player,
};
switch (position) {
case length - 1:
case length + 1:
item.styles = {...item.styles, filter: 'grayscale(1)'};
break;
case length:
break;
default:
item.styles = {...item.styles, opacity: 0};
break;
}
return item;
};
const CarouselSlideItem = ({pos, idx, activeIdx}) => {
const item = createItem(pos, idx, activeIdx);
return (
<li className="carousel__slide-item" style={item.styles}>
<div className="carousel__slide-item-img-link">
<img src={item.player.image} alt={item.player.title} />
</div>
<div className="carousel-slide-item__body">
<h4>{item.player.title}</h4>
<p>{item.player.desc}</p>
</div>
</li>
);
};
const keys = Array.from(Array(_items.length).keys());
const Carousel = () => {
const [items, setItems] = React.useState(keys);
const [isTicking, setIsTicking] = React.useState(false);
const [activeIdx, setActiveIdx] = React.useState(0);
const bigLength = items.length;
const prevClick = (jump = 1) => {
if (!isTicking) {
setIsTicking(true);
setItems((prev) => {
return prev.map((_, i) => prev[(i + jump) % bigLength]);
});
}
};
const nextClick = (jump = 1) => {
if (!isTicking) {
setIsTicking(true);
setItems((prev) => {
return prev.map(
(_, i) => prev[(i - jump + bigLength) % bigLength],
);
});
}
};
const handleDotClick = (idx) => {
if (idx < activeIdx) prevClick(activeIdx - idx);
if (idx > activeIdx) nextClick(idx - activeIdx);
};
React.useEffect(() => {
if (isTicking) sleep(300).then(() => setIsTicking(false));
}, [isTicking]);
React.useEffect(() => {
setActiveIdx((length - (items[0] % length)) % length) // prettier-ignore
}, [items]);
return (
<div className="carousel__wrap">
<div className="carousel__inner">
<button className="carousel__btn carousel__btn--prev" onClick={() => prevClick()}>
<i className="carousel__btn-arrow carousel__btn-arrow--left" />
</button>
<div className="carousel__container">
<ul className="carousel__slide-list">
{items.map((pos, i) => (
<CarouselSlideItem
key={i}
idx={i}
pos={pos}
activeIdx={activeIdx}
/>
))}
</ul>
</div>
<button className="carousel__btn carousel__btn--next" onClick={() => nextClick()}>
<i className="carousel__btn-arrow carousel__btn-arrow--right" />
</button>
<div className="carousel__dots">
{items.slice(0, length).map((pos, i) => (
<button
key={i}
onClick={() => handleDotClick(i)}
className={i === activeIdx ? 'dot active' : 'dot'}
/>
))}
</div>
</div>
</div>
);
};
ReactDOM.render(<Carousel />, document.getElementById('root'));
Also see: Tab Triggers