轮廓和边框 CSS 属性有什么区别?
CSS 中的 border
和 outline
属性有什么区别?
如果没有区别,那么为什么同一事物有两个属性呢?
What is the difference between border
and outline
properties in CSS?
If there is no difference, then why are there two properties for the same thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
来自: http://webdesign.about.com/od/advancedcss/a/outline_style .htm
From: http://webdesign.about.com/od/advancedcss/a/outline_style.htm
除了其他一些答案之外......这里还有一些我能想到的差异:
1)圆角
border
支持带有border-radius
属性的圆角。outline
没有。FIDDLE
(注意:尽管 firefox具有
-moz-outline-radius
属性,该属性允许轮廓上有圆角...该属性未在任何 CSS 标准中定义,并且不受其他浏览器支持(来源))2) 样式化仅一侧
边框具有样式属性每边都有
border-top:
、border-left:
等。轮廓不能做到这一点。 没有大纲顶部:等等。要么全有,要么全无。 (参见这篇文章)
3)偏移
大纲支持使用属性轮廓偏移。 边界没有。
FIDDLE
注意:所有主流浏览器都支持
outline-offset Internet Explorer 除外
In addition to some other answers... here are a few more differences I can think of:
1) Rounded corners
border
supports rounded corners with theborder-radius
property.outline
doesn't.FIDDLE
(NB: Although firefox has the
-moz-outline-radius
property which allows rounded corners on outline... this property it is not defined in any CSS standard, and is not supported by other browsers (source))2) Styling one side only
border has properties to style each side with
border-top:
,border-left:
etc.outline can't do this. There's no outline-top: etc. It's all or nothing. (see this SO post)
3) offset
outline supports offset with the property outline-offset. border doesn't.
FIDDLE
Note: All major browsers support
outline-offset
except Internet Explorer除了其他答案之外,大纲通常用于调试。 Opera 有一些不错的用户 CSS 样式,它们使用大纲属性来向您显示所有元素在文档中的位置。
如果您试图找出某个元素没有出现在您预期的位置或未达到您预期的大小的原因,请添加一些轮廓并查看元素所在的位置。
正如已经提到的,轮廓不占用空间。 添加边框时,文档中元素的总宽度/高度会增加,但轮廓不会发生这种情况。 此外,您无法在特定侧面(例如边框)设置轮廓; 要么全有,要么全无。
Further to other answers, outlines are usually used for debugging. Opera has some nice user CSS styles that use the outline property to show you where all the elements are in a document.
If you're trying to find out why an element isn't appearing where you expected or at the size you expected, add a few outlines and see where the elements are.
As already mentioned, outlines do not take up space. When you add a border, the element's total width/height in the document increases, but that doesn't happen with outline. Also you can't set outlines on specific sides like borders; it's all or nothing.
太棒了;
W3C 将其解释为具有以下差异:
来源
应使用大纲以实现可访问性
还应注意,大纲的主要内容目的是可访问性。 将其设置为轮廓:不应避免。
如果您必须删除它,那么提供替代样式可能是更好的主意:
资料来源:“不要从链接和表单控件中删除大纲”,365 Berea Street
更多资源
tldr;
The W3C explains it as having the following differences:
Source
Outline should be used for accessibility
It should also be noted that outline's primary purpose is accessibility. Setting it to outline: none should be avoided.
If you must remove it it maybe a better idea to provide alternative styling:
Source: "Do Not Remove the Outline from Link and Form Controls", 365 Berea Street
More Resources
大纲的实际用途涉及透明度。 如果您有一个带有背景的父元素,但希望子元素的边框是透明的,以便显示父元素的背景,则必须使用“轮廓”而不是“边框”。 虽然边框可以是透明的,但它会显示孩子的背景,而不是父母的背景。
换句话说,此设置创建了以下效果:
A practical use of outline deals with transparency. If you have a parent element with a background, but want a child element's border to be transparent so that the parent's background will show through, you must use "outline" rather than "border." While a border can be transparent, it will show the child's background, not the parent's.
In other words, this setting created the following effect:
边框是在元素内部创建的,而轮廓是在元素外部创建的。 因此,边框是与元素的宽度和高度一起计算的,而轮廓则绘制在元素外部。
Border is created inside the element, where as outline is created outside the element. So border is computed along with the width and height of the element, while outline draws outside the element.
来自 W3 School 网站
CSS border 属性允许您指定元素边框的样式和颜色。
轮廓是围绕元素(边框外)绘制的一条线,以使元素“突出”。
大纲简写属性在一个声明中设置所有大纲属性。
可以设置的属性有(按顺序):轮廓颜色、轮廓样式、轮廓宽度。
如果缺少上述值之一,例如“outline:solid #ff0000;”,则将插入缺少属性的默认值(如果有)。
请在此处查看更多信息:
http://webdesign.about.com/od/advancedcss/a/outline_style.htm
From W3 School Site
The CSS border properties allow you to specify the style and color of an element's border.
An outline is a line that is drawn around elements (outside the borders) to make the element "stand out".
The outline shorthand property sets all the outline properties in one declaration.
The properties that can be set, are (in order): outline-color, outline-style, outline-width.
If one of the values above are missing, e.g. "outline:solid #ff0000;", the default value for the missing property will be inserted, if any.
Check here for more information :
http://webdesign.about.com/od/advancedcss/a/outline_style.htm
这是一个有点老的问题,但值得一提的是 Firefox 渲染错误(截至 13 年 1 月仍然存在),其中轮廓将在所有子元素的外部渲染,即使它们溢出其父元素(通过负边距、框阴影)等)
您可以通过以下方式修复此问题:
超级不幸的是它仍然没有修复。 在许多情况下,我更喜欢轮廓,因为它们不会增加元素的尺寸,从而使您在设置元素的尺寸时不必总是考虑边框宽度。
毕竟,哪个更简单?
或者:
A little bit of an old question, but worth mentioning a Firefox rendering bug (still present as of Jan '13) where the outline will render on the outside of all child elements even if they overflow their parent (through negative margins, box-shadows, etc.)
You can fix this with:
Super unfortunate that it's still not fixed. I much prefer outlines in many cases since they do not add to the dimensions of an element, saving you from always having to consider border widths when setting dimensions of an element.
After all, which is simpler?
Or:
还值得注意的是,W3C 的轮廓就是IE 的边框,因为IE 不实现W3C 盒子模型。
在w3c盒子模型中,边框不包括元素的宽度和高度。 在 IE 中它是包容性的。
It is also worth noting, that W3C's outline is IE's border, since IE does not implement W3C box model.
In w3c box model, the border is exclusive of element's width and height. In IE it is inclusive.
边框和轮廓之间的差异:
边框是盒模型的一部分,因此它会根据元素的大小进行计算。
轮廓不是盒模型的一部分,因此它不会影响附近的元素。
演示:
Other differences:
The outline is displayed outside the border.
Outlines cannot have rounded corners; borders can.
Differences between border and outline:
Border is part of the box model so it counts against the element's size.
Outline is not part of the box model so it doesn't affect nearby elements.
Demo:
Other differences:
The outline is displayed outside the border.
Outlines cannot have rounded corners; borders can.
我编写了一小段 css/html 代码只是为了看看两者之间的区别。
outline
最好将潜在溢出的子元素包含在内,尤其是放入 inline 容器中。border
更适合块行为的元素。为您小提琴,先生!
I've made a little piece of css/html code just to see the difference between both.
outline
is better to inclose potential overflowing child elements, especially into an inline container.border
is much more adapted for block-behaving elements.Fiddle for you sir!
CSS 中的outline 属性在元素外部绘制一条线。 它与 border 类似,不同之处在于:
元素或相邻元素的位置
来源:https://css-tricks.com/almanac /属性/o/大纲/
The outline property in CSS draws a line around the outside of an element. It's similar to border except that:
position of the element or adjacent elements
Source: https://css-tricks.com/almanac/properties/o/outline/
作为使用“大纲”的一个实际示例,可以使用大纲属性来控制网页上的系统焦点后面的微弱点状边框(例如,如果您通过选项卡浏览链接)(至少,我知道它可以在 Firefox 中) ,没有尝试过其他浏览器)。
一种常见的“图像替换”技术是使用,例如:
在 CSS 中使用以下内容:
问题是,当焦点到达标签时,轮廓向左偏移 1000em。 Outline 可以让您关闭此类元素上的焦点轮廓。
我相信,当在“选择”模式下突出显示要检查的元素时,IE 开发人员工具栏也在“幕后”使用类似轮廓的东西。 这很好地说明了“轮廓”不占空间的事实。
As a practical example of using "outline", the faint dotted border that follows the system focus on a webpage (eg. if you tab through the the links) can be controlled using the outline property (at least, I know it can in Firefox, not tried other browsers).
A common "image replacement" technique is to use, for example:
with the following in the CSS:
The problem being that when the focus reaches the tag, the outline heads off 1000em to the left. Outline can allow you to turn off the focus outline on such elements.
I believe that the IE Developer Toolbar is also using something like outline "under the hood" when highlighting elements for inspection in "select" mode. That shows well the fact that "outline" takes up no space.
将轮廓视为投影仪在某物外部绘制的边界,因为边界是该物周围的实际对象。
投影很容易重叠,但边框不会让您通过。
有时,当我使用
grid+%width
时,边框会更改视口上的缩放比例,例如在具有宽度的父级中具有
完全填充父级,但是当我向 div 添加width:100%
的div: 100pxborder:solid 5px
时,它会使 div 变小,为边框腾出空间(尽管这种情况很少见且可以解决!)但使用轮廓就不会有这个问题,因为轮廓更加虚拟:D 它只是元素外部的一条线
但问题是如果你没有正确地进行间距,它就会与其他内容重叠。
简而言之:
概述优点:
它不会扰乱间距和位置
缺点:
重叠的可能性很高
think about outline as a border that a projector draw outside something as a border is an actual object around that thing.
a projection can easily overlap but border don't let you pass.
some times when i use
grid+%width
, border will change the scaling on view port,for example a div withwidth:100%
in a parent withwidth:100px
fills the parent completely, but when i addborder:solid 5px
to div it make the div smaller to make space for border(although it's rare and work-aroundable!)but with outline it doesn't have this problem as outline is more virtual :D it's just a line outside the element
but the problem is if you don't do spacing properly it would overlap with other contents.
to make it short:
outline pros:
it doesn't mess with spacing and positions
cons:
high chance of overlapping
Google web.dev 对盒子模型有很好的解释。
Google web.dev has a good explaintion for Box Model.
复制自 W3Schools:
Copied from W3Schools: