如何在不影响行的情况下将margin-top设置为元素?

时间:2018-01-30 12:28:03

标签: javascript jquery html css css3

这是我的代码:

div {
  border: 1px solid;
  padding: 10px;
}

span {
  font-size: 12px;
  font-weight: normal;
  line-height: 1.7;
  text-align: center;
  padding: 4px 14px 3px 14px;
  display: inline-block;
  border-radius: 2px;
  font-family: arial;
  background-color: rgba(251, 251, 251, 0.97);
  border: 1px solid #F3F3F3;
  color: #666;
  margin-left: 10px;
  vertical-align: middle;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div>
  <i class="fa fa-star"></i>
  <span>whatever</span>
</div>

我想把那个明星1px打倒。当我将margin-top: 1px;设置为.fa-star时,整行都会关闭。我怎么才能打倒那个明星?

4 个答案:

答案 0 :(得分:6)

将这个额外的CSS用于font-awesome star:

.fa-star {
  position: relative;
  top: 1px;
}

它将星球1px向下移动相对到其原始位置而不影响其他元素(position: relative有利于什么)。

&#13;
&#13;
div{
  border: 1px solid;
  padding: 10px;
}
span{
    font-size: 12px;
    font-weight: normal;
    line-height: 1.7;
    text-align: center;
    padding: 4px 14px 3px 14px;
    display: inline-block;
    border-radius: 2px;
    font-family: arial;
    background-color: rgba(251,251,251,0.97);
    border: 1px solid #F3F3F3;
    color: #666;
    margin-left: 10px;
    vertical-align: middle;
    
}
.fa-star {
  position: relative;
  top: 1px;
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div>
<i class="fa fa-star"></i>
<span>whatever</span>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

将div中的所有元素设置为vertical-align top | middle

.div {
  border: 1px solid;
  padding: 10px;
}
.div > * {
  vertical-align: middle;
}
.span1 {
  font-size: 12px;
  font-weight: normal;
  line-height: 1.7;
  text-align: center;
  padding: 4px 14px 3px 14px;
  display: inline-block;
  border-radius: 2px;
  font-family: arial;
  background-color: rgba(251, 251, 251, 0.97);
  border: 1px solid #F3F3F3;
  color: #666;
  margin-left: 10px;
  vertical-align: middle;
}
.fa-star{
  margin-top:20px; /*change this for 1px*/
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div class="div">
  <i class="fa fa-star"></i>
  <span class="span1">whatever</span>
</div>

答案 2 :(得分:0)

因为您使用内联块,使用垂直对齐,您需要使用顶部和相对定位来移动星形。请参阅下面的代码示例

div{
  border: 1px solid;
  padding: 10px;
}
span{
    font-size: 12px;
    font-weight: normal;
    line-height: 1.7;
    text-align: center;
    padding: 4px 14px 3px 14px;
    display: inline-block;
    border-radius: 2px;
    font-family: arial;
    background-color: rgba(251,251,251,0.97);
    border: 1px solid #F3F3F3;
    color: #666;
    margin-left: 10px;
    vertical-align: middle;
    
}

.fa {
     position:relative;
     top:1px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div>
<i class="fa fa-star"></i>
<span>whatever</span>
</div>

答案 3 :(得分:0)

假设您希望将星形向下移动1px以使其垂直居中...您可以使用flexbox对齐它(以及div中的所有其他内容)。

您还可以从display: inline-block删除vertical-alignspan

&#13;
&#13;
div {
  border: 1px solid;
  padding: 10px;
  display: flex;
  align-items: center;
}

span {
  font-size: 12px;
  font-weight: normal;
  line-height: 1.7;
  text-align: center;
  padding: 4px 14px 3px 14px;
  border-radius: 2px;
  font-family: arial;
  background-color: rgba(251, 251, 251, 0.97);
  border: 1px solid #F3F3F3;
  color: #666;
  margin-left: 10px;
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div>
  <i class="fa fa-star"></i>
  <span>whatever</span>
</div>
&#13;
&#13;
&#13;