mirror of
https://github.com/phuoc-ng/csslayout.git
synced 2025-09-08 21:30:50 +02:00
feat: Update contents
This commit is contained in:
122
contents/rating.mdx
Normal file
122
contents/rating.mdx
Normal file
@@ -0,0 +1,122 @@
|
||||
---
|
||||
category: Input
|
||||
created: '2019-11-26'
|
||||
description: Create a star rating with CSS flexbox
|
||||
keywords: css flexbox, css star rating
|
||||
thumbnail: /assets/css-layout/thumbnails/rating.png
|
||||
title: Rating
|
||||
---
|
||||
|
||||
## HTML
|
||||
|
||||
```html
|
||||
<div class="rating">
|
||||
<button class="rating__star">☆</button>
|
||||
<button class="rating__star">☆</button>
|
||||
<button class="rating__star">☆</button>
|
||||
<button class="rating__star">☆</button>
|
||||
<button class="rating__star">★</button>
|
||||
</div>
|
||||
```
|
||||
|
||||
## CSS
|
||||
|
||||
```css
|
||||
.rating {
|
||||
/* Center the content */
|
||||
align-items: center;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
|
||||
flex-direction: row-reverse;
|
||||
}
|
||||
|
||||
.rating__star:hover,
|
||||
.rating__star:hover ~ .rating__star {
|
||||
color: transparent;
|
||||
}
|
||||
|
||||
/*
|
||||
Make all previous stars selected when hover on a star
|
||||
Note that we use \`flex-direction: row-reverse\` on the container
|
||||
*/
|
||||
.rating__star:hover:before,
|
||||
.rating__star:hover ~ .rating__star:before {
|
||||
color: #eab308;
|
||||
content: '★';
|
||||
left: 0;
|
||||
position: absolute;
|
||||
}
|
||||
|
||||
.rating__star {
|
||||
font-size: 1.5rem;
|
||||
|
||||
/* Reset styles for button */
|
||||
background-color: transparent;
|
||||
border: transparent;
|
||||
margin: 0 2px;
|
||||
padding: 0;
|
||||
|
||||
/* Used to position the hover state */
|
||||
position: relative;
|
||||
}
|
||||
```
|
||||
|
||||
<Playground>
|
||||
```css styles.css hidden
|
||||
body {
|
||||
align-items: center;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.rating {
|
||||
/* Center the content */
|
||||
align-items: center;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
|
||||
flex-direction: row-reverse;
|
||||
}
|
||||
|
||||
.rating__star:hover,
|
||||
.rating__star:hover ~ .rating__star {
|
||||
color: transparent;
|
||||
}
|
||||
|
||||
/*
|
||||
Make all previous stars selected when hover on a star
|
||||
Note that we use \`flex-direction: row-reverse\` on the container
|
||||
*/
|
||||
.rating__star:hover:before,
|
||||
.rating__star:hover ~ .rating__star:before {
|
||||
color: #eab308;
|
||||
content: '★';
|
||||
left: 0;
|
||||
position: absolute;
|
||||
}
|
||||
|
||||
.rating__star {
|
||||
font-size: 1.5rem;
|
||||
|
||||
/* Reset styles for button */
|
||||
background-color: transparent;
|
||||
border: transparent;
|
||||
margin: 0 2px;
|
||||
padding: 0;
|
||||
|
||||
/* Used to position the hover state */
|
||||
position: relative;
|
||||
}
|
||||
```
|
||||
|
||||
```html index.html hidden
|
||||
<div class="rating">
|
||||
<button class="rating__star">☆</button>
|
||||
<button class="rating__star">☆</button>
|
||||
<button class="rating__star">☆</button>
|
||||
<button class="rating__star">☆</button>
|
||||
<button class="rating__star">★</button>
|
||||
</div>
|
||||
```
|
||||
</Playground>
|
Reference in New Issue
Block a user