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.
- var i = 1;
- var n = 1;
.container
while i < 4
.hs__wrapper
.hs__header
h2.hs__headline Headline
= i++
.hs__arrows
a.arrow.disabled.arrow-prev
a.arrow.arrow-next
ul.hs
while n < 21
li.hs__item
.hs__item__image__wrapper
//img.hs__item__image(src="https://source.unsplash.com/random/300×300/?album&sig"+[n]+[i], alt="")
img.hs__item__image(src="https://picsum.photos/id/"+1+[n]+[i]+"/300/300", alt="")
.hs__item__description
span.hs__item__title
| Amazing title
= n++
span.hs__item__subtitle
| some subtitle
-n = 0
.description
h3 Description
p
strong Work in progress –
| Horizontal scroll containers are hyped right now. Whether you browse the Appstore, search for articles on Medium or set some filters in Instagram. Many apps use this kind of pattern. But most of them are very touch and mobile oriented. So I wondered how to make it nice on touch AND desktop.
p Depending on your desktop layout, you probably don't want the cards to run over the edge of the screen. Let's take a look at the "Home" section of Spotify. On the desktop, they show 4 to 6 cards in a slider with arrows at the top. And with touch/mobile they use the horizontal overflow scrolling.
p So let's recreate exactly this behaviour for responsive web use.
p Things already achieved:
ol
li 4 column grid on desktop. Tease the 4th on touch.
li Hide arrows on touch
li Disable corresponding arrow when end or start has been reached
li (As suggested) - Toggle arrow state on drag and scroll as well
li Always keep left and right spacings
li No ugly scrollbars on any browser
li Horizontal scrolling, dragging or arrow-click
li Multiple instances
li CSS Grid and CSS Vars are nice, but let's make this work on IE11 as well
p Todo / open (feel free to left suggestions in the comments)
ol
li If you click an arrow after box has been dragged / scrolled, it is not snapping to the grid anymore.
li Get rid of jQuery
p
strong BONUS –
| I made another alternative version here:
a(href="https://codepen.io/kilianso/pen/WqdVLx?editors=1010" target="_blank") https://codepen.io/kilianso/pen/WqdVLx
$gutter: 10px;
$bg: white;
$dark: #212121;
$darkest: #121212;
$green: #1cff65;
$maxwidth: 990px;
$colsBigDesktop: 4;
$colsSmallDesktop: 3;
* {
box-sizing: border-box;
}
::-webkit-scrollbar { /* Webkit */
width: 0;
height: 0;
}
.hs {
display: flex;
overflow-x: scroll;
justify-content: space-between;
scrollbar-width: none; /* Firefox */
-ms-overflow-style: none; /* IE 10+ */
-webkit-overflow-scrolling: touch;
margin: 0 -20px;
&__header {
display: flex;
align-items: center;
width: 100%;
}
&__headline {
flex: 1;
}
&__arrows {
align-self: center;
.arrow {
&:before {
content: '';
display: inline-block;
vertical-align: middle;
content: "";
background: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNSIgaGVpZ2h0PSI5IiB2aWV3Qm94PSIwIDAgMTUgOSI+Cgk8cGF0aCBmaWxsPSIjMzMzMzMzIiBkPSJNNy44NjcgOC41NzRsLTcuMjItNy4yMi43MDctLjcwOEw3Ljg2NyA3LjE2IDE0LjA1Ljk4bC43MDYuNzA3Ii8+Cjwvc3ZnPgo=");
background-size: contain;
filter: brightness(5);
width: 18px;
height: 12px;
cursor: pointer;
}
&.disabled:before {
filter: brightness(2);
}
&.arrow-prev:before {
transform: rotate(90deg);
margin-right: $gutter;
}
&.arrow-next:before {
transform: rotate(-90deg);
}
}
}
&__item {
flex-grow: 1;
flex-shrink: 0;
flex-basis: calc(100% / #{$colsBigDesktop} - (#{$gutter} * 2) - (20px / #{$colsBigDesktop}));
// how it works: total width devided by columns minus left and right margin per box minus negativ margin of outer container.
// oh and IE11 does not support the shorthand "flex" with calc, so you have to use flex-basis etc. separately;
margin: $gutter;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
position: relative;
user-select: none;
@media only screen and (max-width: 990px) {
flex-basis: calc(100% / #{$colsSmallDesktop} - (#{$gutter} * 2) - (20px / #{$colsSmallDesktop}));
}
&:last-child:after {
content: "";
display: block;
position: absolute;
width: $gutter;
height: 1px;
right: calc(#{$gutter} * 2 * -1);
}
&:first-child {
margin-left: calc(#{$gutter} * 2);
}
&__description {
z-index: 1;
align-self: flex-start;
margin: 10px 0;
}
&__subtitle {
color: #aaa;
display: block;
}
&__image__wrapper{
position: relative;
width: 100%;
height: 0;
padding-bottom: 100%;
}
&__image {
pointer-events: none;
position: absolute;
width: 100%;
height: 100%;
object-fit: cover;
}
}
&__wrapper {
@media only screen and (min-width: 990px) {
overflow: hidden;
}
@media (hover: none) and (pointer: coarse) {
.hs__arrows {
display: none;
}
.hs__item {
flex: 1 0 calc(23% - #{$gutter} * 2);
}
@media only screen and (max-width: 990px) {
.hs__item {
flex: 1 0 calc(45% - #{$gutter} * 2);
}
}
}
}
}
/* Base Styling */
body {
margin: 0;
padding: 0;
overflow-x: hidden;
font-family: 'Muli', sans-serif;
color: white;
background: $bg;
line-height: 1.5;
letter-spacing: .15px;
}
.container {
max-width: $maxwidth;
padding: calc(#{$gutter} * 2);
margin: 0 auto;
background: $darkest;
mix-blend-mode: invert;
position: relative;
&:after {
content: '';
width: 100vw;
height: 100%;
background: $dark;
position: absolute;
left: 50%;
top: 0;
transform: translateX(-50%);
z-index: -1;
}
@media only screen and (min-width: 990px) {
padding: calc(#{$gutter} * 4) calc(#{$gutter} * 8);
}
}
.description {
max-width: $maxwidth;
color: $dark;
margin: 0 auto;
padding: calc(#{$gutter} * 4);
}
ul {
padding: 0;
margin: 0;
}
// work in progress - needs some refactoring and will drop JQuery i promise :)
var instance = $(".hs__wrapper");
$.each( instance, function(key, value) {
var arrows = $(instance[key]).find(".arrow"),
prevArrow = arrows.filter('.arrow-prev'),
nextArrow = arrows.filter('.arrow-next'),
box = $(instance[key]).find(".hs"),
x = 0,
mx = 0,
maxScrollWidth = box[0].scrollWidth - (box[0].clientWidth / 2) - (box.width() / 2);
$(arrows).on('click', function() {
if ($(this).hasClass("arrow-next")) {
x = ((box.width() / 2)) + box.scrollLeft() - 10;
box.animate({
scrollLeft: x,
})
} else {
x = ((box.width() / 2)) - box.scrollLeft() -10;
box.animate({
scrollLeft: -x,
})
}
});
$(box).on({
mousemove: function(e) {
var mx2 = e.pageX - this.offsetLeft;
if(mx) this.scrollLeft = this.sx + mx - mx2;
},
mousedown: function(e) {
this.sx = this.scrollLeft;
mx = e.pageX - this.offsetLeft;
},
scroll: function() {
toggleArrows();
}
});
$(document).on("mouseup", function(){
mx = 0;
});
function toggleArrows() {
if(box.scrollLeft() > maxScrollWidth - 10) {
// disable next button when right end has reached
nextArrow.addClass('disabled');
} else if(box.scrollLeft() < 10) {
// disable prev button when left end has reached
prevArrow.addClass('disabled')
} else{
// both are enabled
nextArrow.removeClass('disabled');
prevArrow.removeClass('disabled');
}
}
});
Also see: Tab Triggers