when:hover li tag为什么边框会导致移动物体

时间:2015-01-08 09:57:53

标签: html css



* {
   margin: 0;
   padding: 0;
 }
 body {
   margin: 10px;
   padding: 10px;
 }
 ul {
   list-style-type: none;
   width: 500px;
   height: 150px;
   background-color: gray;
 }
 ul li {
   display: inline-block;
   -webkit-transition: all0.4s ease-in-out;
 }
 ul li:hover {
   background-color: green;
   border: solid 2px red;
 }

<ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
</ul>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:3)

将透明边框设置为正常状态:

* {
  margin: 0;
  padding: 0;
}
body {
  margin: 10px;
  padding: 10px;
}
ul {
  list-style-type: none;
  width: 500px;
  height: 150px;
  background-color: gray;
}
ul li {
  display: inline-block;
  -webkit-transition: all 0.4s ease-in-out;
  border: solid 2px transparent
}
ul li:hover {
  background-color: green;
  border: solid 2px red;
}
<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
</ul>

答案 1 :(得分:0)

您可以在之前使用填充,然后在悬停时删除 - 请参阅下面的示例:

* {
   margin: 0;
   padding: 0;
 }
 body {
   margin: 10px;
   padding: 10px;
 }
 ul {
   list-style-type: none;
   width: 500px;
   height: 150px;
   background-color: gray;
 }
 ul li {
   display: inline-block;
   -webkit-transition: all0.4s ease-in-out;
   padding:2px;
 }
 ul li:hover {
   background-color: green;
   border: solid 2px red;
   padding:0;
 }
<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
</ul>