How do I make a transparent border with CSS?

后端 未结 9 1310
时光说笑
时光说笑 2020-12-13 23:04

I have an li styled as follows:

li{
    display:inline-block;
    padding:5px;
    border:1px solid none;
}
li:hover{
    border:1px solid #FC0;         


        
相关标签:
9条回答
  • 2020-12-13 23:36

    The easiest solution to this is to use rgba as the color: border-color: rgba(0,0,0,0); That is fully transparent border color.

    0 讨论(0)
  • 2020-12-13 23:40

    You could remove the border and increase the padding:

    li {
      display: inline-block;
      padding: 6px;
      border-width: 0px;
    }
    
    li:hover {
      border: 1px solid #FC0;
      padding: 5px;
    }
    <ul>
      <li>Hovering is great</li>
    </ul>

    0 讨论(0)
  • 2020-12-13 23:41

    Since you said in a comment that the more options you have, the better, here's another one.

    In CSS3, there are two different so-called "box models". One adds the border and padding to the width of a block element, while the other does not. You can use the latter by specifying

    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    -ms-box-sizing: border-box;
    box-sizing: border-box;
    

    Then, in modern browsers, the element will always have the same width. I.e., if you apply a border to it on hover, the width of the border will not add to the overall width of the element; the border will be added "inside" the element, so to speak. However, if I remember correctly, you must specify the width explicitly for this to work. Which is probably not an option for you in this particular case, but you can keep it in mind for future situations.

    0 讨论(0)
提交回复
热议问题