CSS中边框和轮廓属性的区别是什么?

如果没有区别,为什么同一事物有两种性质?


当前回答

除了一些其他的答案之外……以下是我能想到的更多不同之处:

1)圆角

Border使用Border -radius属性支持圆角。大纲没有。

div { width: 150px; height: 150px; margin: 20px; display: inline-block; position: relative; } .border { border-radius: 75px; border: 2px solid green; } .outline { outline: 2px solid red; border-radius: 75px; -moz-outline-radius: 75px; outline-radius: 75px; } .border:after { content: "border supports rounded corners"; position: absolute; bottom: 0; transform: translateY(100%); } .outline:after { content: "outline doesn't support rounded corners"; position: absolute; bottom: 0; transform: translateY(100%); } <div class="border"></div> <div class="outline"></div>

小提琴

(注意:虽然firefox有-moz-outline-radius属性,允许在轮廓上使用圆角…此属性在任何CSS标准中都没有定义,其他浏览器也不支持(源代码))

2)只做一面造型

Border具有属性,可以用Border -top:、Border -left:等来设置每条边的样式。

大纲不能这样做。没有大纲等等。要么全有,要么全无。(见这篇SO帖子)

3)抵消

Outline支持使用Outline -offset属性的偏移量。边界不。

.outline { 保证金:100 px; 宽度:150 px; 身高:150 px; outline-offset: 20 px; 轮廓:2px实心红色; 边框:2px纯绿色; 背景:粉色; } < div class = "大纲" > < / div >

小提琴

注意:除Internet Explorer外,所有主流浏览器都支持outline-offset

其他回答

Border是在元素内部创建的,而outline是在元素外部创建的。因此,border与元素的宽度和高度一起计算,而outline绘制在元素外部。

这是一个有点老的问题,但值得一提的是Firefox渲染错误(直到1月13日仍然存在),即大纲将在所有子元素的外部渲染,即使它们溢出了父元素(通过负边距,框阴影等)。

你可以用以下方法解决这个问题:

.container {
    position: relative;
}
.container:before {
   content: '';
   margin: 0;
   padding: 0;
   position: absolute;
   top: 0;
   left: 0;
   width: 100%;
   height: 100%;
   outline: 1px solid #ff0000;
}

非常不幸的是,它仍然没有修复。在许多情况下,我更喜欢轮廓,因为它们不会增加元素的尺寸,从而使您在设置元素尺寸时不必总是考虑边界宽度。

毕竟,哪个更简单?

.container {
    width: 960px;
    height: 300px;
    outline: 3px solid black;
}

Or:

.container {
    width: 954px;
    height: 294px;
    border: 3px solid black;
}

作为一个使用“outline”的实际示例,系统后面模糊的虚线边界集中在一个网页上(例如。可以使用outline属性来控制(至少,我知道它可以在Firefox中,没有尝试过其他浏览器)。

一种常见的“图像替换”技术是使用,例如:

<div id="logo"><a href="/">Foo Widgets Ltd.</a></div>

在CSS中:

#logo
{
    background: url(/images/logo.png) center center no-repeat;
}
#logo a
{
    display: block;
    text-indent: -1000em;
}

问题是当焦点到达标签时,轮廓会向左偏移1000em。Outline可以让您关闭这些元素的焦点轮廓。

我相信IE开发者工具栏在“选择”模式下突出显示要检查的元素时,也使用了一些类似“隐藏”的东西。这很好地说明了“提纲”不占空间的事实。

CSS中的outline属性在元素的外部画一条线。它和border很相似,只是:

它总是围绕所有的边,你不能指定具体的 它不是盒子模型的一部分,所以它不会影响 元素或相邻元素的位置

来源:https://css-tricks.com/almanac/properties/o/outline/

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 with width:100% in a parent with width:100px fills the parent completely, but when i add border: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.

简而言之: 优点:轮廓 它不会打乱间距和位置 缺点: 重叠几率高