CSS中边框和轮廓属性的区别是什么?
如果没有区别,为什么同一事物有两种性质?
CSS中边框和轮廓属性的区别是什么?
如果没有区别,为什么同一事物有两种性质?
当前回答
我做了一小段css/html代码,只是为了看看两者之间的区别。
Outline最好包含可能溢出的子元素,特别是在内联容器中。
Border更适合于块行为元素。
为您演奏,先生!
其他回答
Border是在元素内部创建的,而outline是在元素外部创建的。因此,border与元素的宽度和高度一起计算,而outline绘制在元素外部。
同样值得注意的是,W3C的大纲是IE的边界,因为IE没有实现W3C的盒子模型。
在w3c box模型中,边框与元素的宽度和高度无关。在IE中,它是包容性的。
摘自W3Schools:
定义和用法 轮廓是画出来的一条线 在元素周围(边界之外) 让元素“脱颖而出”。
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.
简而言之: 优点:轮廓 它不会打乱间距和位置 缺点: 重叠几率高
这是一个有点老的问题,但值得一提的是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;
}