简体   繁体   English

你能做出“一个无形的边界”吗?

[英]Can you make “an invisible border”?

I'm trying to make a navbar as an exercise. 我正在尝试将导航栏作为练习。

I'm using a:hover to include a solid border around the button being hovered. 我正在使用a:hovera:hover的按钮周围包含一个坚实的边框。 However, this makes all the other buttons move by the size of the border. 但是,这会使所有其他按钮按边框大小移动。

What is the proper fix to this problem? 这个问题的正确解决方法是什么? I know there are others (discussed here ), I specifically tried to make the border "be invisible yet take up space even when not hovered". 我知道还有其他的( 这里讨论 ),我特意试图让边界“看不见,但即使没有徘徊也占用空间”。 I set border:transparent hoping it might do what I want, but it did not show take space at all. 我设置border:transparent希望它可以做我想要的,但它根本没有显示占用空间。

I know I could hand pick the border's color to be equal to the background and make it solid, but this is not what I want. 我知道我可以手工挑选边框的颜色等于背景并使其坚固,但这不是我想要的。 Is there a sane way to solve this issue? 有没有理智的方法来解决这个问题?

border: 10px solid transparent怎么样border: 10px solid transparent

Your best option would be to add padding or margins to your element that's the same size as the border and make the border have zero width, and then show the border and remove the padding with the a:hover selector. 您最好的选择是为元素添加填充或边距,其大小与边框相同,并使边框的宽度为零,然后显示边框并使用a:hover选择器删除填充。

Here's a sample. 这是一个样本。 You can often do this with margins too. 您也可以使用边距来执行此操作。

 a { display: inline-block; height: 2em; width: 100px; padding: 2px; background: #0ff; } a:hover { padding: 0; border :2px solid #000; } 
 <a href="#">Hello World</a> 

One reason this isn't working as you'd expect is because you are only applying display:block on :hover , it needs to be applied to the element without the :hover selector or you will get the "shifting" dimensions. 其中一个原因是你没有按预期工作,因为你只应用display:block on :hover ,它需要应用于没有:hover选择器的元素,否则你将获得“移动”维度。 It doesn't matter which display type you use, you just have to make sure they are the same, and by default <a> is inline. 使用哪种显示类型无关紧要,您只需确保它们是相同的,默认情况下<a>是内联的。

Another reason has something to do with your shorthand borders, you need to add a border type for the transparent version like solid instead of none . 另一个原因与您的速记边框有关,您需要为透明版本添加边框类型,例如solid而不是none

The technique you are using is totally legit, no need for padding hacks or outline (which doesn't add dimension). 您使用的技术是完全合法的,不需要填充黑客或大纲(不添加维度)。

http://jsfiddle.net/Madmartigan/kwdDB/ http://jsfiddle.net/Madmartigan/kwdDB/

Try this: 试试这个:

#wd-navbar li a {
     border: medium solid transparent;
     display: block;
     margin: 1px;
}

#wd-navbar li a:hover {
     background-color: #F5DEB3;
     border: medium solid;
}

You could use the outline CSS property instead of your border, which acts like a border but isn't taken into account in the sizing calculations. 您可以使用outline CSS属性而不是边框​​,边框的作用类似于边框,但在大小调整计算中不予考虑。

However, this does have some issues, not being supported by IEs 7 or earlier . 但是,这确实存在一些问题, IE 7或更早版本不支持

border:transparent means border: transparent 0 none border:transparent表示border: transparent 0 none

If you don't specify a property when using shorthand syntax then you reset all the properties to their defaults. 如果在使用速记语法时未指定属性,则将所有属性重置为其默认值。

You need to give it a border-style and a border-width. 你需要给它一个边框样式和边框宽度。

Setting border-color : transparent ; 设置border-color:透明; does the job. 做的工作。

a {
  border-color : transparent ;
}

a:hover {
   border-color : black;
}

使用伪元素::after::before来隐藏不可见的边界。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM