float left如何影响transform translate

时间:2015-07-09 08:45:04

标签: html css

如果我从float:left删除.action-button属性,则转换动画不起作用。你能解释一下这里发生了什么吗?

标记:

<a href="#" class="action-button animate green">Are</a>

样式:

.action-button
{
    position: relative;
    padding: 10px 40px;
  margin: 0px 10px 10px 0px;
  float: left;
    border-radius: 8px;
    font-family: 'Pacifico', cursive;
    font-size: 25px;
    color: #FFF;
    text-decoration: none;  
}

.green
{
    background-color: #82BF56;
    border-bottom: 5px solid #669644;
    text-shadow: 0px -2px #669644;
}

.action-button:active
{
    transform: translate(0px,5px);
  -webkit-transform: translate(0px,5px);
    border-bottom: 1px solid;
}

Codepen第17行:http://codepen.io/koriolis/pen/euAEg

2 个答案:

答案 0 :(得分:1)

这是因为a元素是inline元素,因为它们对齐父元素并且不会清除任何一方。

当您添加float: left时,它会将元素从inline更改为block,但也会移除clear: both并允许元素彼此相邻对齐。

如果您不希望标记在页面上浮动,则可以使用float获得与inline-block相同的效果。

.action-button {
  position: relative;
  padding: 10px 40px;
  margin: 0px 10px 10px 0px;
  display: inline-block;
  border-radius: 8px;
  font-family: 'Pacifico', cursive;
  font-size: 25px;
  color: #FFF;
  text-decoration: none;
}

CodePen Example

答案 1 :(得分:0)

  1. 标记a - 默认display: inline
  2. float: left - 显示属性设置为阻止
  3. 当您再次移除float: left - a display: inline

    时,
  4. 使用display: inline-block代替float: left

  5. &#13;
    &#13;
    body {
        padding: 50px;
    }
    .animate {
        transition: all 0.1s;
        -webkit-transition: all 0.1s;
    }
    .action-button {
        position: relative;
        padding: 10px 40px;
        margin: 0px 10px 10px 0px;
        display: inline-block;
        border-radius: 8px;
        font-family:'Pacifico', cursive;
        font-size: 25px;
        color: #FFF;
        text-decoration: none;
    }
    .blue {
        background-color: #3498DB;
        border-bottom: 5px solid #2980B9;
        text-shadow: 0px -2px #2980B9;
    }
    .red {
        background-color: #E74C3C;
        border-bottom: 5px solid #BD3E31;
        text-shadow: 0px -2px #BD3E31;
    }
    .green {
        background-color: #82BF56;
        border-bottom: 5px solid #669644;
        text-shadow: 0px -2px #669644;
    }
    .yellow {
        background-color: #F2CF66;
        border-bottom: 5px solid #D1B358;
        text-shadow: 0px -2px #D1B358;
    }
    .action-button:active {
        transform: translate(0px, 5px);
        -webkit-transform: translate(0px, 5px);
        border-bottom: 1px solid;
    }
    &#13;
    <a href="#" class="action-button animate blue">Hello</a>
     <a href="#" class="action-button animate red">How</a>
     <a href="#" class="action-button animate green">Are</a>
     <a href="#" class="action-button animate yellow">You?</a>
    &#13;
    &#13;
    &#13;

    fiddle