将星级评分与Div的中心对齐

时间:2017-09-15 14:19:15

标签: 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>

如何将div starsdiv的中心对齐,而不会破坏hover效果,我尝试更改float,但要么正确,要么离开,或交换hover效果,我想动态地将其保持在div的中心,而不会向右或向左移动。

3 个答案:

答案 0 :(得分:4)

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

解决方案#1(使用flexbox):

&#13;
&#13;
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;
}
&#13;
<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>
&#13;
&#13;
&#13;

解决方案#2(使用text-align):

&#13;
&#13;
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;
}
&#13;
<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>
&#13;
&#13;
&#13;

解决方案#3(没有<form>使用flexbox):

&#13;
&#13;
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;
}
&#13;
<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>
&#13;
&#13;
&#13;

  

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

答案 1 :(得分:4)

您甚至可以使用if let fileURL = dir?.appendingPathComponent(fileName).appendingPathExtension("dgk") { var encryptDataFromFile: Data? do { encryptDataFromFile = try Data(contentsOf: fileURL) if let decryptData: Data = self.crypt.decryptWithRSAKey(encryptDataFromFile!){ let decryptString: String = String(data: decryptData, encoding: .utf8)! print(decryptString) } exit(0) } catch { print("Failed reading from URL: \(fileURL), Error: " + error.localizedDescription) } } 执行此操作,将主div transform - translate更改为position,这样您就可以利用relative属性在页面中间对齐然后使用转换将其与页面中心对齐。

&#13;
&#13;
left
&#13;
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;
}
&#13;
&#13;
&#13;

答案 2 :(得分:3)

添加以下CSS:

body {
  text-align: center;
}

可能值得将星星包裹在另一个div中以避免将中心文本放在整个身体上。

JSFiddle:https://jsfiddle.net/wa2vd409/