按钮悬停效果CSS两个按钮都在移动

时间:2015-03-11 14:56:27

标签: html css css3 flat

我正在尝试为我的网站制作按钮设计。当用户将鼠标悬停在按钮上时,我希望它向下移动,就像它是一个正在按下的真实按钮一样。它可以工作,但如果我有2个按钮,它也会移动另一个按钮。我怀疑其原因是悬停效应的上边缘。但为什么另一个按钮也会向下移动? 我使用的代码位于底部。 如果你愿意,我也可以在codepen上找到它。

提前问候和致谢,

Max Rumpf

HTML

<html>
    <head>
        <link rel="stylesheet" href="base.css">
        <script src=""
        <script src="base.js"></script>
   </head>
   <body>
       <div class="container">
           <button>Click Me!</button>
           <button>Hi</button>
      </div>
  </body>
</html>

CSS:

@import url(http://fonts.googleapis.com/css?  family=Open+Sans:300italic,400italic,600italic,700italic,400,300,600,700);
* {
    margin: 0;
    padding: 0;
    font-family: 'Open Sans';
    font-weight: 700;
}
body {
    background-color: #ecf0f1;
}
.container {
    margin: 0 auto;
    width: 400px;
    height: 100%;
    text-align: center;
    padding-top: 100px;
}
.container > button {
    font-size: 2em;
    background-color: #3498db;
    border: none;
    padding: 10px;
    padding-left: 20px;
    padding-right: 20px;
    border-radius: 0px;
    color: #ecf0f1;
    -webkit-transition: all 0.1s ease-in 0s;
    border-bottom: 8px solid #2980b9;
}
.container > button:hover {
    border-bottom: 4px solid #2980b9;
    margin-top: 4px;
}

2 个答案:

答案 0 :(得分:3)

您需要将按钮与

对齐
.container > button {
    vertical-align: top;
}

&#13;
&#13;
* {
  margin: 0;
  padding: 0;
  font-family: 'Open Sans';
  font-weight: 700;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
body {
  background-color: #ecf0f1;
}
.container {
  margin: 0 auto;
  width: 400px;
  height: 100%;
  text-align: center;
  padding-top: 100px;
}
.container > button {
  vertical-align: top;
  font-size: 2em;
  background-color: #3498db;
  border: none;
  padding: 10px;
  padding-left: 20px;
  padding-right: 20px;
  border-radius: 0px;
  color: #ecf0f1;
  -webkit-transition: all 0.1s ease-in 0s;
  border-bottom: 8px solid #2980b9;
}
.container > button:hover {
  border-bottom: 4px solid #2980b9;
  margin-top: 4px;
}
&#13;
<div class="container">
  <button>Click Me!</button>
  <button>Hi</button>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

请更新此css或http://codepen.io/anon/pen/ZYqBjL

&#13;
&#13;
@import url(http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,400,300,600,700);
* {
	margin: 0;
	padding: 0;
	font-family: 'Open Sans';
	font-weight: 700;
}
body {
	background-color: #ecf0f1;
}
.container {
 	margin: 0 auto;
	width: 400px;
	height: 100%;
	text-align: center;
	padding-top: 100px;
}
.container > button {
	font-size: 2em;
	background-color: #3498db;
	border: none;
	padding: 10px;
	padding-left: 20px;
	padding-right: 20px;
	border-radius: 0px;
	color: #ecf0f1;
	-webkit-transition: all 0.1s ease-in 0s;
	border-bottom: 8px solid #2980b9;
    display: inline-block;
    vertical-align: top;
}
.container > button:hover {
	border-bottom: 4px solid #2980b9;
}
&#13;
&#13;
&#13;