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.
<h1>Pure CSS RWD *accessible* radio input carousel</h1>
<!-- start carousel section -->
<section class="carousel" aria-label="carousel" role="radiogroup" aria-labelledby="radiogroup_title" id="radiogroup">
<!-- carousel skip link -->
<a class="carousel__skip-link" href="#skip-link">Skip the Carousel</a>
<p id="radiogroup_title" class="carousel_title">Carousel</p><!-- start carousel title -->
<ol class="carousel__viewport"><!-- start carousel viewport -->
<!-- start radio inputs -->
<input class="carousel__activator" type="radio" name="carousel" id="1" checked="checked" role="radio" aria-checked="true"/>
<input class="carousel__activator" type="radio" name="carousel" id="2" role="radio" aria-checked="false"/>
<input class="carousel__activator" type="radio" name="carousel" id="3" role="radio" aria-checked="false"/>
<!-- end radio inputs -->
<!-- start carousel prev/next -->
<div class="carousel__controls">
<label class="carousel__control carousel__control--backward" for="3" aria-label="Go to slide 3" >Go to slide 3</label>
<label class="carousel__control carousel__control--forward" for="2" aria-label="Go to slide 2">Go to slide 2</label>
</div>
<div class="carousel__controls">
<label class="carousel__control carousel__control--backward" for="1" aria-label="Go to slide 1">Go to slide 1</label>
<label class="carousel__control carousel__control--forward" for="3" aria-label="Go to slide 3">Go to slide 3</label>
</div>
<div class="carousel__controls">
<label class="carousel__control carousel__control--backward" for="2" aria-label="Go to slide 2">Go to slide 2</label>
<label class="carousel__control carousel__control--forward" for="1" aria-label="Go to slide 1">Go to slide 1</label>
</div><!-- end carousel prev/next -->
<!-- start carousel slide track -->
<div class="carousel__track">
<!-- start slide 01 -->
<li id="carousel__slide1" class="carousel__slide" aria-label="slide 1 of 3">
<div class="carousel__internal">
</div>
</li><!-- end slide 01 -->
<!-- start slide 02 -->
<li id="carousel__slide2" class="carousel__slide" aria-label="slide 2 of 3">
<div class="carousel__internal">
</div>
</li><!-- end slide 02 -->
<!-- start slide 03 -->
<li id="carousel__slide3" class="carousel__slide" aria-label="slide 3 of 3">
<div class="carousel__internal">
</div>
</li><!-- end slide 03 -->
</div><!-- end carousel slide track -->
<!-- start carousel bullet nav -->
<aside class="carousel__navigation">
<ol class="carousel__navigation-list">
<li class="carousel__navigation-item"><a href="#carousel__slide1">
<label class="carousel__navigation-button" for="1" aria-label="Go to slide 1">1</label></a></li>
<li class="carousel__navigation-item"><a href="#carousel__slide2">
<label class="carousel__navigation-button" for="2" aria-label="Go to slide 2">2</label></a></li>
<li class="carousel__navigation-item"><a href="#carousel__slide3">
<label class="carousel__navigation-button" for="3" aria-label="Go to slide 3">3</label></a></li>
</ol> <!-- end carousel bullet nav -->
</aside><!-- end carousel bullet nav -->
</ol><!-- end carousel viewport -->
<!-- skip anchor -->
<div class="carousel__skip-message" id="skip-link"></div>
</section><!-- end carousel section -->
<h2 style="margin-top: 6rem;">CSS-only Carousel</h2>
<p>This carousel is created with HTML and CSS only. Features;</p>
<ul class="features">
<li>No auto-play/scroll</li>
<li>Loop back to start</li>
<li>Skip content anchor link for screen-readers</li>
<li>Tab index</li>
<li>Aria labelling</li>
<li>Numbered bullet nav for accessibility, bullet nav :hover</li>
<li>Automated-numbering bullet nav, using CSS counter ~ disabled as pseudo elements may not be accessible for screen-readers</li>
<li>Styled :focus outline</li>
<li>Peeking next and previous slides (needs tweaking but possible based on transform: translate)</li>
<li>:active state on controls & :hover for next/prev buttons (rebuilt as radio group)</li>
<li>Radio group slider doesn't jump to anchor</li>
<li>Opacity, position and scale transitions all working</li>
<li>Radio group left and right navigation, next/prev are mouse select only</li>
</ul>
<p>
Desires;</p>
<ul class="features">
<li>Draggable with click and drag</li>
<li>Radio buttons can't work with trackpad scrolling beause they rely on :checked</li>
<li>Can't link #id URL to specific slides as that ignores :checked and breaks nav?</li>
</ul>
/* Default styles */
$focus: 2px solid orange;
html {
scroll-behavior: smooth;
}
body {
width: 100%;
margin: 0 auto;
padding: 10vw;
font-family: sans-serif;
background-color: #111;
color: #fff;
}
* {
box-sizing: border-box;
scrollbar-color: transparent transparent; /* thumb and track color */
scrollbar-width: 0px;
-ms-overflow-style: none;
}
:focus {
outline: $focus;
}
.carousel:focus-within {
outline: $focus;
}
ol, li {
list-style: none;
margin: 0;
padding: 0;
}
ul.features li {
list-style: disc;
}
/* Hide scrollbars */
.carousel::-webkit-scrollbar {
width: 0;
display: none;
}
.carousel::-webkit-scrollbar-track {
background: transparent;
display: none;
}
.carousel::-webkit-scrollbar-thumb {
background: transparent;
border: none;
display: none;
}
/* Carousel, slide & track setup */
.carousel {
position: relative;
padding-top: 56.25%;
aspect-ratio: 16 / 9;
filter: drop-shadow(0 0 10px rgba(0,0,0,0.25));
perspective: 100px;
scrollbar-width: none; /* hides in FireFox */
overflow: hidden;
display: block;
border-radius: 12px;
}
.carousel input[type="radio"] {
opacity: 0;
z-index: 1000;
bottom: 50%;
left: 0;
position: absolute;
border: 0;
outline: 0;
clip: rect(1px, 1px, 1px, 1px);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
width: 1px;
}
.carousel input[type="radio"]:focus {
outline: none;
}
.carousel_title {
width: 0;
height: 0;
font-size: 1px;
line-height: 0;
text-indent: -9999px;
padding: 0;
margin: 0;
position: absolute;
}
.carousel__viewport {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
width: 100%;
margin: 0 auto;
display: flex;
overflow-x: hidden;
counter-reset: item;
scroll-behavior: smooth;
scroll-snap-type: x mandatory;
}
.carousel__slide {
flex: 0 0 95%;
width: 95%;
/*margin: 0 0.5%;*/
border-radius: 12px;
scroll-behavior: smooth;
counter-increment: item;
display: block;
top: 0;
left: 0;
right: 0;
height: 100%;
/*position: absolute;*/
opacity: 0; /* opacity slide transition */
transform-origin: center center;
transition: opacity ease 1200ms 0ms, transform ease 600ms 0ms;
overflow: hidden;
}
.carousel__slide .carousel__internal {
background-color: #9201ed;
width: 100%;
height: 100%;
}
.carousel__slide:nth-child(even) .carousel__internal {
background-color: #00f;
}
.carousel__track {
position: absolute;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
align-content: center;
justify-content: flex-start;
align-items: stretch;
top: 0;
right: 0;
bottom: 0;
left: 0;
padding: 0;
margin: 0;
transition: transform 400ms ease 0ms;
}
.carousel__track .carousel__slide:nth-of-type(1) {
transform: translateX(calc(0% + 0%)); /* position slide transition */
}
.carousel__track .carousel__slide:nth-of-type(2) {
transform: translateX(calc(100% + 7.5%)); /* position slide transition */
}
.carousel__track .carousel__slide:nth-of-type(3) {
transform: translateX(calc(200% + 12.5%)); /* position slide transition */
}
.carousel__slide:before {
content: counter(item);
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
color: #ffffff;
font-size: 6rem;
}
/* Skip carousel link */
.carousel__skip-link {
height: 1px;
overflow: hidden;
position: absolute;
top: 0;
opacity: 0;
width: 1px;
}
.carousel__skip-link:focus {
align-items: center;
background-color: #fff;
color: #111;
display: flex;
font-size: 1rem;
height: auto;
justify-content: center;
opacity: 1;
text-decoration: underline;
width: fit-content;
padding: 1rem;
z-index: 100;
}
/* Carousel track position change */
.carousel__activator:nth-of-type(1):checked ~ .carousel__track,
.carousel__activator:nth-of-type(1):target ~ .carousel__track {
transform: translateX(calc(0% + 2.5%)); /* position slide transition */
}
.carousel__activator:nth-of-type(2):checked ~ .carousel__track,
.carousel__activator:nth-of-type(2):target ~ .carousel__track {
transform: translateX(-92.5%); /* position slide transition */
}
.carousel__activator:nth-of-type(3):checked ~ .carousel__track,
.carousel__activator:nth-of-type(3):target ~ .carousel__track {
transform: translateX(-187.5%); /* position slide transition */
}
/* Carousel slide scale + opacity animation */
.carousel__activator:nth-of-type(1):checked ~ .carousel__track .carousel__slide:nth-of-type(1),
.carousel__activator:nth-of-type(2):checked ~ .carousel__track .carousel__slide:nth-of-type(2),
.carousel__activator:nth-of-type(3):checked ~ .carousel__track .carousel__slide:nth-of-type(3) {
top: 0;
left: 0;
right: 0;
opacity: 1; /* opacity slide transition */
animation: scale-in 400ms ease 0ms 1 forwards; /* scale slide transition */
}
.carousel__activator:nth-of-type(1):not(:checked) ~ .carousel__track .carousel__slide:nth-of-type(1),
.carousel__activator:nth-of-type(2):not(:checked) ~ .carousel__track .carousel__slide:nth-of-type(2),
.carousel__activator:nth-of-type(3):not(:checked) ~ .carousel__track .carousel__slide:nth-of-type(3) {
animation: scale-out 400ms ease 0ms 1 forwards; /* scale slide transition */
}
/* Carousel next/prev controls */
.carousel__controls {
opacity: 0;
}
.carousel__activator:nth-of-type(1):checked ~ .carousel__controls:nth-of-type(1),
.carousel__activator:nth-of-type(1):target ~ .carousel__controls:nth-of-type(1),
.carousel__activator:nth-of-type(2):checked ~ .carousel__controls:nth-of-type(2),
.carousel__activator:nth-of-type(2):target ~ .carousel__controls:nth-of-type(2),
.carousel__activator:nth-of-type(3):checked ~ .carousel__controls:nth-of-type(3),
.carousel__activator:nth-of-type(3):target ~ .carousel__controls:nth-of-type(3){
display: block;
opacity: 1;
}
.carousel__control {
display: block;
cursor: pointer;
position: absolute;
top: 50%;
margin: 0;
width: 3rem;
height: 3rem;
transform: translateY(-50%) scale(0.9);
border-radius: 50%;
font-size: 0;
line-height: 0;
z-index: 100;
background-color: #fff;
color: #111;
text-align: center;
border: 1px solid #111;
transform-origin: center center;
box-shadow: 0 6px 10px 4px rgba(0,0,0,0.25);
transition: all ease 400ms;
}
.carousel__control:hover, .carousel__control:focus {
background: #111;
color: #fff;
border: 1px solid #fff;
transform: translateY(-50%) scale(1);
}
.carousel__control:active {
outline: $focus;
}
.carousel__control--backward:hover:after, .carousel__control--backward:focus:after {
/*border-color: #fff;*/
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='24px' viewBox='0 0 24 24' width='24px' fill='%23fff'%3E%3Cpath d='M0 0h24v24H0V0z' fill='none'/%3E%3Cpath d='M20 11H7.83l5.59-5.59L12 4l-8 8 8 8 1.41-1.41L7.83 13H20v-2z'/%3E%3C/svg%3E");
}
.carousel__control--forward:hover:after, .carousel__control--forward:focus:after {
/*border-color: #fff;*/
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='24px' viewBox='0 0 24 24' width='24px' fill='%23fff'%3E%3Cpath d='M0 0h24v24H0V0z' fill='none'/%3E%3Cpath d='M12 4l-1.41 1.41L16.17 11H4v2h12.17l-5.58 5.59L12 20l8-8-8-8z'/%3E%3C/svg%3E");
}
.carousel__control--backward {
left: -0rem;
}
.carousel__control--forward {
right: -0rem;
}
.carousel__control--backward:after, .carousel__control--forward:after {
content: '';
display: block;
/*border-style: solid;
border-color: #111;
top: 50%;
left: 50%;
width: 20px;
height: 20px;
border-radius: 4px;*/
width: 100%;
height: 100%;
background-size: 2rem 2rem;
background-repeat: no-repeat;
background-position: center center;
transform-origin: center center;
position: absolute;
}
.carousel__control--backward:after {
/*border-width: 0 0 4px 4px;
transform: rotate(45deg) translate(-60%, -10%);*/
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='24px' viewBox='0 0 24 24' width='24px' fill='%23111'%3E%3Cpath d='M0 0h24v24H0V0z' fill='none'/%3E%3Cpath d='M20 11H7.83l5.59-5.59L12 4l-8 8 8 8 1.41-1.41L7.83 13H20v-2z'/%3E%3C/svg%3E");
}
.carousel__control--forward:after {
/*transform: rotate(
225deg) translate(75%, -10%);
border-width: 0 0 4px 4px;*/
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='24px' viewBox='0 0 24 24' width='24px' fill='%23111'%3E%3Cpath d='M0 0h24v24H0V0z' fill='none'/%3E%3Cpath d='M12 4l-1.41 1.41L16.17 11H4v2h12.17l-5.58 5.59L12 20l8-8-8-8z'/%3E%3C/svg%3E");
}
/* Carousel bullet nav */
.carousel__activator:nth-of-type(1):checked ~ .carousel__navigation .carousel__navigation-list .carousel__navigation-item:nth-of-type(1) .carousel__navigation-button,
.carousel__activator:nth-of-type(1):target ~ .carousel__navigation .carousel__navigation-list .carousel__navigation-item:nth-of-type(1) .carousel__navigation-button,
.carousel__activator:nth-of-type(2):checked ~ .carousel__navigation .carousel__navigation-list .carousel__navigation-item:nth-of-type(2) .carousel__navigation-button,
.carousel__activator:nth-of-type(2):target ~ .carousel__navigation .carousel__navigation-list .carousel__navigation-item:nth-of-type(2) .carousel__navigation-button,
.carousel__activator:nth-of-type(3):checked ~ .carousel__navigation .carousel__navigation-list .carousel__navigation-item:nth-of-type(3) .carousel__navigation-button,
.carousel__activator:nth-of-type(3):target ~ .carousel__navigation .carousel__navigation-list .carousel__navigation-item:nth-of-type(3) .carousel__navigation-button {
background: #fff;
color: #111;
border-color: #111;
}
.carousel__activator:nth-of-type(1):checked ~ .carousel__navigation .carousel__navigation-list .carousel__navigation-item:nth-of-type(1) .carousel__navigation-button,
.carousel__activator:nth-of-type(2):checked ~ .carousel__navigation .carousel__navigation-list .carousel__navigation-item:nth-of-type(2) .carousel__navigation-button,
.carousel__activator:nth-of-type(3):checked ~ .carousel__navigation .carousel__navigation-list .carousel__navigation-item:nth-of-type(3) .carousel__navigation-button {
outline: $focus;
text-decoration: underline;
}
.carousel__navigation {
width: 100%;
position: absolute;
right: 0;
bottom: 5%;
left: 0;
text-align: center;
z-index: 100;
}
ol.carousel__navigation-list {
display: flex;
flex-direction: row;
align-content: center;
justify-content: center;
align-items: center;
list-style: none;
/*counter-reset: custom-counter; */
}
.carousel__navigation-item a {
text-decoration: none;
}
.carousel__navigation-button {
display: flex;
cursor: pointer;
flex-direction: row;
align-content: center;
justify-content: center;
align-items: center;
width: 40px;
height: 40px;
margin: 0 10px;
background-color: #111;
background-clip: content-box;
border: 1px solid #fff;
border-radius: 50%;
font-size: 1rem;
color: #fff;
text-decoration: none;
transition: all ease 400ms;
box-shadow: 0 6px 10px 4px rgba(0,0,0,0.25);
/*counter-increment: custom-counter; */
}
.carousel__navigation-button:before {
/*content: counter(custom-counter); */
}
.carousel__navigation-button:hover, .carousel__navigation-button:focus {
background: #fff;
text-decoration: underline;
text-decoration-color: #111;
color: #111;
transform: scale(1.15);
border: 1px solid #111;
}
/* Scaling animations */
@keyframes scale-in {
0% {
transform: scale(0);
}
100% {
transform: scale(1);
}
}
@keyframes scale-out {
0% {
transform: scale(1);
}
100% {
transform: scale(0);
}
}
// Forked from @ https://codepen.io/Schepp/pen/WNbQByE and https://codepen.io/jh3y/pen/WwVKLN
// Benefits over other iteration; better animation, doesn't jump to #anchor, next/prev buttons with hover states, proper active state on bullet nav
Also see: Tab Triggers