Half star with CSS
Asked Answered
G

2

8

How can I have the same result (a half star) in IE and Chrome.

In chrome : http://jsfiddle.net/Auqj8/

.star {
    font-size: x-large;
    width: 50px;
    display: inline-block;
    color: gray;
}
.star:last-child {
    margin-right: 0;
}
.star:before {
    content: '\2605';
}
.star.on {
    color: gold;
}
.star.half:after {
        content: '\2605';
        color: gold;
        margin-left: -20px;
        width: 10px;
        position: absolute;
        overflow: hidden;
}

In IE: http://jsfiddle.net/86pqx/

Thank you.

Grooms answered 11/2, 2014 at 0:52 Comment(1)
Definitely doesn't work on Safari, though.Mf
A
11

This works in both chrome and IE

.star {
    font-size: x-large;
    width: 50px;
    display: inline-block;
    color: gray;
}
.star:last-child {
    margin-right: 0;
}
.star:before {
    content:'\2605';
}
.star.on {
    color: gold;
}
.star.half:after {
    content:'\2605';
    color: gold;
    position: absolute;
    margin-left: -20px;
    width: 10px;
    overflow: hidden;
}

Chrome: http://jsfiddle.net/Auqj8/1/

IE: http://jsfiddle.net/86pqx/3/

Array answered 11/2, 2014 at 1:11 Comment(2)
Thank you for your answer, but it doesn't work Here is a preview: img239.imagevenue.com/…Grooms
I fixed this (using it at work). Just set .star width to 24 and .star.half:after margin-left to -24px, width to 12px, and the half-stars will render correctly.Distemper
I
5

here is my solution:

SCSS:

%full-star {
   &:after {
   content: "\f005";
   }
};

%half-star {
  &:after {
    content: "\f123";
  }
};

.rating {
  margin: 0;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  color: #900;
  $this: &;

  &__item {
    font-size: 24px;
    display: block;
    font-family: FontAwesome;

    &::after {
      content: '\f006 ';
    }
  }

  @for $i from 0 to 6 {
    &[data-rating='#{$i}'] {
      #{$this}__item:nth-child(-n + #{$i}) {
        @extend %full-star;
      }
    }

    &[data-rating='#{$i + 0.5}'] {
      #{$this}__item:nth-child(-n + #{$i}) {
        @extend %full-star;
      }

      #{$this}__item:nth-child(#{$i + 1}) {
        @extend %half-star;
      }
    }
  }
}

HTML:

<ul class="rating" data-rating="3.5">
  <li class="rating__item"></li>
  <li class="rating__item"></li>
  <li class="rating__item"></li>
  <li class="rating__item"></li>
  <li class="rating__item"></li>
</ul>

https://codepen.io/mustra/pen/WmvzZG?editors=1100

Idocrase answered 27/2, 2019 at 16:40 Comment(2)
This is great -- do you know if there is a way to make this work at an even more granular level -- for instance 3.25 or 4.7?Northumberland
its not working properly for data-rating="3.4" or data-rating="4.7"Aleman

© 2022 - 2024 — McMap. All rights reserved.