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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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 class="wrapper">
<h1>Standalone SVG CSS-only Star Rating Component</h1>
<div class="rating-holder">
<div class="c-rating c-rating--small" data-rating-value="2">
<button>1</button>
<button>2</button>
<button>3</button>
<button>4</button>
<button>5</button>
</div>
</div>
<div class="rating-holder">
<div class="c-rating c-rating--regular" data-rating-value="3.25">
<button>1</button>
<button>2</button>
<button>3</button>
<button>4</button>
<button>5</button>
</div>
</div>
<div class="rating-holder">
<div class="c-rating c-rating--big" data-rating-value="4.5">
<button>1</button>
<button>2</button>
<button>3</button>
<button>4</button>
<button>5</button>
</div>
</div>
</div>
////
/// Star Rating
/// @group components
/// @author Damián Muti
////
@import url('https://fonts.googleapis.com/css?family=Roboto+Slab:400');
///
$rating-min-value: 0 !default;
///
$rating-max-value: 5 !default;
///
$rating-steps-value: .25 !default;
///
$rating-color-empty: '%23ddd' !default;
///
$rating-color-full: gold !default;
///
$rating-color-hover: grey !default;
/// This variable is a map containing the "d" attribute of each of the SVG icons
$rating-icons-paths: (
quarter: 'M196.208 415.2v-224.8l-139.504 20.272 100.944 98.384-23.84 138.928z',
half: 'M258.672 64l-62.384 126.4-139.504 20.272 100.944 98.384-23.84 138.928 124.768-65.6v-318.4z',
three-quarters: 'M321.616 190.496l-0.656-0.096-62.384-126.4-62.384 126.4-139.504 20.272 100.944 98.384-23.84 138.928 124.768-65.6 63.024 33.136z',
full: 'M457.888 210.672l-139.504-20.272-62.384-126.4-62.384 126.4-139.504 20.272 100.944 98.384-23.84 138.928 124.768-65.6 124.768 65.6-23.84-138.928c0 0 100.944-98.384 100.944-98.384z'
);
/// Creates a dynamic list of values that increment each .25 from 0 to 5
/// @return {list}
@function rating-values() {
$rating-values: ();
@for $i from $rating-min-value through ($rating-max-value / $rating-steps-value) {
$rating-value: abs($i * $rating-steps-value);
$rating-values: append($rating-values, $rating-value, 'comma');
}
@return $rating-values;
}
/// Generates an SVG with a given fill color depending on the type of icon passed as parameter. The SVG string is scaped for cross-browser support.
/// @param {string} $icon - Type of icon. Accepted values: `quarter`, `half`, `three-quarters` or `full`.
/// @param {color} $color - Passes the fill color of the SVG icon that is being generated. Note: Hexa color values must be escaped for cross-browser support.
/// @return {string} background value.
@function get-icon($icon, $color) {
@if not index(quarter half three-quarters full, $icon) {
@error "Interaction type must be either `quarter`, `half`, `three-quarters` or `full`.";
}
@return url('data:image/svg+xml;utf8,%3Csvg%20version%3D%221.1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20width%3D%22512%22%20height%3D%22512%22%20viewBox%3D%220%200%20512%20512%22%3E%3Cpath%20fill%3D%22' + $color + '%22%20d%3D%22' + map-get($rating-icons-paths, $icon) + '%22%3E%3C%2Fpath%3E%3C%2Fsvg%3E') center / cover no-repeat;
}
/// This is a CSS-only star rating component that shows the proper rating depending on a data-attribute value. JS logic behind this component should round up values per quarter.
/// @param {number} $star-size [20px] - Provides width and height for each of the stars.
/// @param {string} $rating-element [button] - Element to use for each of the stars.
/// @param {string} $interaction-type [representative] - Indicates whether this component should be clickable or representative. Accepted values: `clickable` or `representative`.
/// @example scss
/// .class {
/// c-rating(toem(20px), button, clickable);
/// }
/// @example markup
/// <div class="c-rating" data-rating-value="3.25">
/// <span>1</span>
/// <span>2</span>
/// <span>3</span>
/// <span>4</span>
/// <span>5</span>
/// </div>
@mixin c-rating($star-size: 20px, $star-element: button, $interaction-type: representative) {
@if not index(clickable representative, $interaction-type) {
@error "Interaction type must be either `clickable` or `representative`.";
}
#{$star-element} {
display: inline-block;
float: left;
width: $star-size;
height: $star-size;
border: 0;
text-indent: -9999px;
outline: none;
background: get-icon(full, $rating-color-empty);
@if $interaction-type == clickable {
cursor: pointer;
transition: background 0.25s ease;
// Override background images on hover state
&:hover,
&:hover ~ #{$star-element} {
background: get-icon(full, $rating-color-empty) !important;
}
}
}
// Override background images on hover state
@if $interaction-type == clickable {
&:hover {
#{$star-element} {
background: get-icon(full, $rating-color-hover) !important;
}
}
}
@each $rating-value in rating-values() {
// Get the next higher integer.
$rating-value-ceil: ceil($rating-value);
&[data-rating-value="#{$rating-value}"] {
#{$star-element}:nth-child(-n+#{$rating-value-ceil}) {
background: get-icon(full, $rating-color-full);
}
#{$star-element}:nth-child(#{$rating-value-ceil}) {
// Evaluate which fraction of a star this value is and add the proper background
@if str-slice("#{$rating-value}", 2, 4) == '.25' {
background: get-icon(quarter, $rating-color-full), get-icon(full, $rating-color-empty)
}
@else if str-slice("#{$rating-value}", 2, 4) == '.5' {
background: get-icon(half, $rating-color-full), get-icon(full, $rating-color-empty)
}
@else if str-slice("#{$rating-value}", 2, 4) == '.75' {
background: get-icon(three-quarters, $rating-color-full), get-icon(full, $rating-color-empty)
}
}
}
}
}
/*
* General styles.
* Not necessary for the component to work
*/
/// Convert to EMs function
/// @param {number} target - The value to be converted
/// @param {number} context [$msuxf-font-size] The base font size
/// @return {em} value
@function toem($target, $context: 16px) {
@if $target == 0 {
@return 0
}
@return $target / $context+0em;
}
body {
width: 100vw;
height: 100vh;
font-family: 'Roboto Slab', serif;
text-align: center;
background: linear-gradient(to bottom, #de6262, #ffb88c);
}
.wrapper {
position: absolute;
top: 50%;
left: 50%;
width: 700px;
transform: translate(-50%, -50%);
h1 {
margin-bottom: 75px;
font-size: 56px;
font-weight: 400;
color: #fff;
}
}
.rating-holder {
display: inline-block;
padding: toem(10px) toem(30px);
margin: 0 10px;
background-color: #fff;
border-radius: toem(25px);
box-sizing: border-box;
}
.c-rating {
@include c-rating(toem(20px), button, clickable);
&--small {
font-size: 50%
}
&--big {
font-size: 150%;
}
}
/// This is a CSS-only component that dynamically generates each of the SVG icons to be used as background images for each of the stars.
/// For the rating value to be displayed , "data-rating-value" HTML attribute must be updated and calculated rounding every 0.25 to reflect the proper rating.
Also see: Tab Triggers