CSS边界混乱 [英] CSS border confusion

查看:38
本文介绍了CSS边界混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于为什么将鼠标悬停在边框底部时边框会与内容很好地匹配,但是当更改为边框顶部时会压低内容却感到困惑.

Just confused as to why when hover with border-bottom the border plays well with the content but when changed to border-top it pushes down the content.

这是代码...

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;    
  width: 15%;
  position: fixed;
  background-color: yellow; 
} 


li a {
  display: block;
  height: 50px;
  color: red;
  text-decoration: none;
  text-align: center;
  text-transform: uppercase;
  line-height: 50px;
  transition: .2s all linear;
}


li a:hover {
  color: white;
  border-bottom: 50px solid lightskyblue;
  box-sizing: border-box;
} 

  <ul class="nav">
      <li><a href="#peace">peace</a></li>
      <li><a href="#love">love</a></li>  
  </ul>  

推荐答案

我将对结构进行一些更改,以使用覆盖而不是边框​​.而且不需要使用固定的高度.

I would change a bit the structure to use an overlay instead of borders. And without the need to use fixed heights.

从我看来,最重要的问题实际上是预期的行为. border-top border-bottom 都向下推送内容,但是由于您使用的是 box-sizing:border-box 并且底部边框是已经在文本下方,您不会看到内容下降.

From what I see the border-top issue is actually the expected behaviour. Both border-top and border-bottom push content downwards, but since you are using box-sizing: border-box and the bottom border is already underneath the text you won't see the content go down.

ul {
    list-style-type: none;
    margin: 0;
    padding: 0;    
    width: auto;
    position: fixed;
    background-color: yellow; 
} 

li a{
    display: block;
    position: relative;
    color: red;
    text-decoration: none;
    text-align: center;
    text-transform: uppercase;
    padding: 20px;
    line-height: 1;
}

li a .overlay{
    position:absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 100%;
    transition: .2s all linear;
    background-color: rgba(0,0,0,0.3);
}

li a:hover .overlay {
    bottom: 0;
}

<ul class="nav">
    <li>
        <a href="#peace">
            <div class="overlay"></div>
            peace
        </a>
    </li>
    <li>
        <a href="#love">
            <div class="overlay"></div>
            love
        </a>
    </li>
</ul>

这篇关于CSS边界混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆