Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning a Star Rating to the center of the Div

Tags:

html

css

div.stars {
  display: inline-block;
}

input.star { display: none; }

label.star {
  float: right;
  padding: 10px;
  font-size: 36px;
  color: #444;
  transition: all .2s;
}

input.star:checked ~ label.star:before {
  content: '\f005';
  color: #FD4;
  transition: all .25s;
}

input.star-5:checked ~ label.star:before {
  color: #FE7;
  text-shadow: 0 0 20px #952;
}

input.star-1:checked ~ label.star:before { color: #F62; }

label.star:hover { transform: rotate(-15deg) scale(1.3); }

label.star:before {
  content: '\f006';
  font-family: FontAwesome;
}
<div class="stars">
  <form action="">
    <input class="star star-5" id="star-5" type="radio" name="star"/>
    <label class="star star-5" for="star-5"></label>
    <input class="star star-4" id="star-4" type="radio" name="star"/>
    <label class="star star-4" for="star-4"></label>
    <input class="star star-3" id="star-3" type="radio" name="star"/>
    <label class="star star-3" for="star-3"></label>
    <input class="star star-2" id="star-2" type="radio" name="star"/>
    <label class="star star-2" for="star-2"></label>
    <input class="star star-1" id="star-1" type="radio" name="star"/>
    <label class="star star-1" for="star-1"></label>
  </form>
</div>

How do i align divstars to the center of the div without ruining the hover effect, I'm tried changing the float but it either go right or left, or swap the hover effect, I want to keep it at the center of the div dynamically without moving a bit right or left.

like image 970
AXAI Avatar asked Sep 15 '17 14:09

AXAI


People also ask

How to center a Div vertically and horizontally?

How to Center a Div Vertically and Horizontally with CSS Absolute Positioning and Negative Margins This is very similar to the method above to center an element vertically. Like last time, you must know the width and height of the element you want to center. Set the display property of the parent element to relative.

What is the best way to position the content of a Div?

Show activity on this post. Show activity on this post. Show activity on this post. Flex is the best solution, perfect position. If you want this for a loader, just do a full size div with position fixed and use flex for the div content.

How to center text in a Div using CSS?

How to Center Text with the CSS Text-Align Center Property. To center text or links horizontally, just use the text-align property with the value center: <div class="container"> <p>Hello, (centered) World!</p> </div> p { text-align: center; } How to Center a Div with CSS Margin Auto

How do I center the child element in a Div?

To truly center the child element, apply a negative top margin set to half the child element's height, and a negative left margin set to half the child element's width: <div class="container"> <div class="child"></div> </div>


1 Answers

You can try one of the following solutions:

solution #1 (using flexbox):

div.stars {
  display:flex;
  flex-direction:column;
}
div.stars form {
  display:flex;
  align-self:center;
  flex-direction:row-reverse;
}
input.star {
  display: none;
}
label.star {
  display:inline-block;
  padding: 10px;
  font-size: 36px;
  color: #444;
  transition: all .2s;
}
input.star:checked ~ label.star:before {
  content: '\f005';
  color: #FD4;
  transition: all .25s;
}
input.star-5:checked ~ label.star:before {
  color: #FE7;
  text-shadow: 0 0 20px #952;
}
input.star-1:checked ~ label.star:before {
  color: #F62;
}
label.star:hover {
  transform: rotate(-15deg) scale(1.3); 
}
label.star:before {
  content: '\f006';
  font-family: FontAwesome;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="stars">
  <form action="">
    <input class="star star-5" id="star-5" type="radio" name="star"/>
    <label class="star star-5" for="star-5"></label>
    <input class="star star-4" id="star-4" type="radio" name="star"/>
    <label class="star star-4" for="star-4"></label>
    <input class="star star-3" id="star-3" type="radio" name="star"/>
    <label class="star star-3" for="star-3"></label>
    <input class="star star-2" id="star-2" type="radio" name="star"/>
    <label class="star star-2" for="star-2"></label>
    <input class="star star-1" id="star-1" type="radio" name="star"/>
    <label class="star star-1" for="star-1"></label>
  </form>
</div>

solution #2 (using text-align):

div.stars {
  text-align:center;
  direction:rtl;
}
div.stars form {
  display:inline-block;
}
input.star {
  display: none;
}
label.star {
  display:inline-block;
  padding: 10px;
  font-size: 36px;
  color: #444;
  transition: all .2s;
}
input.star:checked ~ label.star:before {
  content: '\f005';
  color: #FD4;
  transition: all .25s;
}
input.star-5:checked ~ label.star:before {
  color: #FE7;
  text-shadow: 0 0 20px #952;
}
input.star-1:checked ~ label.star:before {
  color: #F62;
}
label.star:hover {
  transform: rotate(-15deg) scale(1.3); 
}
label.star:before {
  content: '\f006';
  font-family: FontAwesome;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="stars">
  <form action="">
    <input class="star star-5" id="star-5" type="radio" name="star"/>
    <label class="star star-5" for="star-5"></label>
    <input class="star star-4" id="star-4" type="radio" name="star"/>
    <label class="star star-4" for="star-4"></label>
    <input class="star star-3" id="star-3" type="radio" name="star"/>
    <label class="star star-3" for="star-3"></label>
    <input class="star star-2" id="star-2" type="radio" name="star"/>
    <label class="star star-2" for="star-2"></label>
    <input class="star star-1" id="star-1" type="radio" name="star"/>
    <label class="star star-1" for="star-1"></label>
  </form>
</div>

solution #3 (without <form> using flexbox):

div.stars {
  display:flex;
  flex-direction:row-reverse;
  justify-content:center;
}
input.star {
  display: none;
}
label.star {
  display:inline;
  padding: 10px;
  font-size: 36px;
  color: #444;
  transition: all .2s;
}
input.star:checked ~ label.star:before {
  content: '\f005';
  color: #FD4;
  transition: all .25s;
}
input.star-5:checked ~ label.star:before {
  color: #FE7;
  text-shadow: 0 0 20px #952;
}
input.star-1:checked ~ label.star:before {
  color: #F62;
}
label.star:hover {
  transform: rotate(-15deg) scale(1.3); 
}
label.star:before {
  content: '\f006';
  font-family: FontAwesome;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="stars">
  <input class="star star-5" id="star-5" type="radio" name="star"/>
  <label class="star star-5" for="star-5"></label>
  <input class="star star-4" id="star-4" type="radio" name="star"/>
  <label class="star star-4" for="star-4"></label>
  <input class="star star-3" id="star-3" type="radio" name="star"/>
  <label class="star star-3" for="star-3"></label>
  <input class="star star-2" id="star-2" type="radio" name="star"/>
  <label class="star star-2" for="star-2"></label>
  <input class="star star-1" id="star-1" type="radio" name="star"/>
  <label class="star star-1" for="star-1"></label>
</div>

Hint: With these solutions you don't need the float property!

like image 53
Sebastian Brosch Avatar answered Sep 22 '22 22:09

Sebastian Brosch