Click here to Skip to main content
15,917,991 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following code generates star based rating system.However, the stars are right aligned and I want them Center aligned. Code (css)-
fieldset, label { margin: 0; padding: 0; }
    body{ margin: 20px; }
    h1 { font-size: 1.5em; margin: 10px; }

   

    .rating { 
    border: none;
    }

    .rating > input { display: none; } 
    .rating > label:before { 
    margin: 5px;
    font-size: 1.25em;
    font-family: FontAwesome;
 
    content: "\f005";
    }
    .rating > label .float_center{ 
    color: #ddd; 
    float: left;
    }
    .rating > input:checked ~ label, 
    .rating:not(:checked) > label:hover, 
    .rating:not(:checked) > label:hover ~ label { color: #FF0000;  }

    .rating > input:checked + label:hover,
    .rating > input:checked ~ label:hover,
    .rating > label:hover ~ input:checked ~ label, 
    .rating > input:checked ~ label:hover ~ label { color: #B22222;  } 


HTML

<h2>



    <fieldset class="rating">
    <input type="radio" id="star5" name="rating" value="5" /><label class = 
    "full" for="star5" title="Awesome - 5 stars"></label>
   
    <input type="radio" id="star4" name="rating" value="4" /><label class = 
    "full" for="star4" title="Pretty good - 4 stars"></label>
   
    <input type="radio" id="star3" name="rating" value="3" /><label class = 
    "full" for="star3" title="Meh - 3 stars"></label>
  
    <input type="radio" id="star2" name="rating" value="2" /><label class = 
    "full" for="star2" title="Kinda bad - 2 stars"></label>
   
    <input type="radio" id="star1" name="rating" value="1" /><label class = 
    "full" for="star1" title="Sucks big time - 1 star"></label>
    </fieldset>
    </h2>


What I have tried:

I've tried changing the float:right; to left, and also tried to replace it with position: center. I want the stars to be Center aligned. Newbie here!
Posted
Updated 6-May-18 10:18am

1 solution

You have to do two things to center some block element:
1. Set it's width, otherwise it fills all the container
2. Make it's margin auto (and not 0!)
CSS
fieldset {
  margin: auto;
  width: 200px;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900