css 将星星与Div的中心对齐

nr7wwzry  于 2023-08-09  发布在  其他
关注(0)|答案(4)|浏览(109)
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 stars对齐到div的中心而不破坏hover效果,我试图改变float,但它要么向右或向左,或者交换hover效果,我想动态地保持它在div的中心,而不向右或向左移动。

txu3uszq

txu3uszq1#

您可以尝试以下解决方案之一:

解决方案#1(使用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;
}

个字符

解决方案#2(使用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>

的字符串

解决方案#3(不使用<form>,使用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>

的字符串

**提示:**使用这些解决方案,您不需要float属性!

z9zf31ra

z9zf31ra2#

添加以下CSS:

body {
  text-align: center;
}

字符串
可能值得将星号 Package 在另一个div中,以避免将中心文本放在整个正文中。
JSFiddle:https://jsfiddle.net/wa2vd409/

bttbmeg0

bttbmeg03#

你甚至可以使用transform - translate,将主div position更改为relative,这样你就可以使用left属性在页面中间对齐,然后使用transform将其对齐到页面中心。

div.stars {
  display:inline-block;
  position:relative; /*Add this*/
  left:50%; /*Add this*/
  transform:translate(-50%,0); /*Add this*/
}

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;
}

个字符

nzrxty8p

nzrxty8p4#

如果你说的是水平对齐,那么是的
第一个月
应该做的工作,但如果你谈论的是垂直对齐,然后
vertical-align: middle
应该做好这份工作。我告诉你们两个是因为你们没说要哪一个。

相关问题