自定义悬停效果位置问题

时间:2016-11-03 16:02:42

标签: html css

我试图将一个按钮10px放在屏幕的水平中心。这是按钮代码。该按钮使用一些花哨的悬停效果,不会随按钮移动。如何将按钮10px放在屏幕的水平中心?

body {
  background-color: green;
}
.button-2 {
  width: 140px;
  height: 50px;
  border: 2px solid white;
  float: left;
  text-align: center;
  cursor: pointer;
  position: relative;
  box-sizing: border-box;
  overflow: hidden;
  margin: 0 0 40px 50px;
}
.button-2 a {
  font-family: arial;
  font-size: 16px;
  color: white;
  text-decoration: none;
  line-height: 50px;
  transition: all .5s ease;
  z-index: 2;
  position: relative;
}
.eff-2 {
  width: 140px;
  height: 50px;
  top: -50px;
  background: white;
  position: absolute;
  transition: all .5s ease;
  z-index: 1;
}
.button-2:hover .eff-2 {
  top: 0;
}
.button-2:hover a {
  color: #666;
}
.watch-video-position {}
<div class="watch-video-position">
  <div class="button-2 watch-video-position">
    <div class="eff-2 watch-video-position"></div>
    <a href="#"> CLICK ME </a>
  </div>
</div>

4 个答案:

答案 0 :(得分:2)

I have made some changes in the css to class button-2.
.button-2 {
      float: none;
      margin: 0 auto;
      top: 10px;
    }

&#13;
&#13;
body {
      background-color: green;
    }
    .button-2 {
      border: 2px solid white;
      box-sizing: border-box;
      cursor: pointer;
      float: none;
      height: 50px;
      margin: 0 auto;
      overflow: hidden;
      position: relative;
      text-align: center;
      top: 10px;
      width: 140px;
    }
    .button-2 a {
      font-family: arial;
      font-size: 16px;
      color: white;
      text-decoration: none;
      line-height: 50px;
      transition: all .5s ease;
      z-index: 2;
      position: relative;
    }
    .eff-2 {
      width: 140px;
      height: 50px;
      top: -50px;
      background: white;
      position: absolute;
      transition: all .5s ease;
      z-index: 1;
    }
    .button-2:hover .eff-2 {
      top: 0;
    }
    .button-2:hover a {
      color: #666;
    }
    .watch-video-position {}
&#13;
<div class="watch-video-position">
      <div class="button-2 watch-video-position">
        <div class="eff-2 watch-video-position"></div>
        <a href="#"> CLICK ME </a>
      </div>
    </div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

你需要使用它:

.top {
  position: relative;
}
.top .button-2 {
  position: absolute;
  left: 50%;
  margin-left: -70px;
}

确保您将唯一的班级top添加到第一个<div>并使用它来定位。您还可以在.top上使用保证金和填充。

body {
  background-color: green;
}
.button-2 {
  width: 140px;
  height: 50px;
  border: 2px solid white;
  float: left;
  text-align: center;
  cursor: pointer;
  position: relative;
  box-sizing: border-box;
  overflow: hidden;
  margin: 0 0 40px 50px;
}
.button-2 a {
  font-family: arial;
  font-size: 16px;
  color: white;
  text-decoration: none;
  line-height: 50px;
  transition: all .5s ease;
  z-index: 2;
  position: relative;
}
.eff-2 {
  width: 140px;
  height: 50px;
  top: -50px;
  background: white;
  position: absolute;
  transition: all .5s ease;
  z-index: 1;
}
.button-2:hover .eff-2 {
  top: 0;
}
.button-2:hover a {
  color: #666;
}
.top {
  position: relative;
  padding-top: 25px;
}
.top .button-2 {
  position: absolute;
  left: 50%;
  margin-left: -70px;
}
<div class="top watch-video-position">
  <div class="button-2 watch-video-position">
    <div class="eff-2 watch-video-position"></div>
    <a href="#"> CLICK ME </a>
  </div>
</div>

答案 2 :(得分:1)

尝试将其父级居中并将其转移到其父级中:

body {
  background-color: green;
  text-align: center;
}
.outer {
  width: 200px;
  margin: auto;
  background-color: blue;
}
.button-2 {
  width: 140px;
  height: 50px;
  border: 2px solid white;
  cursor: pointer;
  position: relative;
  box-sizing: border-box;
  overflow: hidden;
  margin: 0 0 40px 50px;
}
.button-2 a {
  font-family: arial;
  font-size: 16px;
  color: white;
  text-decoration: none;
  line-height: 50px;
  transition: all .5s ease;
  z-index: 2;
  position: relative;
}
.eff-2 {
  width: 140px;
  height: 50px;
  top: -50px;
  background: white;
  position: absolute;
  transition: all .5s ease;
  z-index: 1;
}
.button-2:hover .eff-2 {
  top: 0;
}
.button-2:hover a {
  color: #666;
}
.watch-video-position {}
<div class="watch-video-position outer">
  <div class="button-2 watch-video-position">
    <div class="eff-2 watch-video-position"></div>
    <a href="#"> CLICK ME </a>
  </div>
</div>

答案 3 :(得分:1)

尝试div.button-2的90%宽度并将边框和填充设置为锚点。

A* ap = new C;
B* bp = dynamic_cast<B*>(ap);
body {
  background-color: green;
}
.button-2 {
  width: 90%;
  height: 50px;      
  float: left;
  text-align: center;
  cursor: pointer;
  position: relative;
  box-sizing: border-box;
  overflow: hidden;
  margin: 0 0 40px 50px;
}
.button-2 a {
  font-family: arial;
  font-size: 16px;
  color: white;
  text-decoration: none;
  line-height: 50px;
  transition: all .5s ease;
  z-index: 2;
  position: relative;
  border: 2px solid white;
  padding:15px;
}
.eff-2 {
  width: 140px;
  height: 50px;
  top: -50px;
  background: white;
  position: absolute;
  transition: all .5s ease;
  z-index: 1;
}
.button-2:hover .eff-2 {
  top: 0;
}
.button-2:hover a {
  color: #666;
}
.top {
  position: relative;
  padding-top: 25px;
}
.top .button-2 {
  position: absolute;
  left: 50%;
  margin-left: -70px;
}