简体   繁体   English

用边框图像勾勒边框

[英]Outlining border with border-image

C an't post images yet, so here's a link to the image for better understanding the problem. çan't发表图片呢,所以这里是一个以图像链接为了更好地理解这个问题。

1) The div, without border. 1) div,无边框。

2) The div and the border(yellow is above the blue). 2) div和边框(黄色在蓝色上方)。

3) This is what I need(yellow is outlined to bottom). 3)这就是我所需要的(黄色在底部概述)。

T he code should be something like this: 的代码应该是这样的:

CSS: CSS:

.thediv {
width: 500px;
height: 200px;
background: #0000FF;
-moz-border-image: url(img.png) 12 1 round;
-webkit-border-image: url(img.png) 12 1 round;
-ms-border-image: url(img.png) 12 1 round;
border-image: url(img.png) 12 1 round;
border-style: solid;
border-width: 0 0 10px;
}

HTML: HTML:

<div class="thediv"></div>

Two words about the problem: 关于这个问题的两个词:

I need to move border outside of the div, without new HTML tags and/or absolute properties. 需要将边框移到div之外,而没有新的HTML标签和/或绝对属性。

More than two words about the problem: 关于这个问题的两个以上的词:

E xample 2 on the image have the blue background under the yellow one. 图像E上xample 2有下一个黄色的蓝色背景。 The yellow background is the border(img.png). 黄色背景是边框(img.png)。 The problem is, if I'll use img.png half(or most) transparent, the blue color will appear, and I don't want this. 问题是,如果我将img.png使用一半(或大多数)透明,则会出现蓝色,而我不希望这样。 Also, I don't want to use absolute values( :before:after ). 另外,我也不想使用绝对值( :before:after )。 I'm looking for some elegant approach. 我正在寻找一些优雅的方法。

As I understand it you are using a .png image which has some transparent areas. 据我了解,您使用的是具有某些透明区域的.png图像。

As a result of using this as a border-image the background of the div is appearing through this border image. 将其用作边框图像的结果是,div的背景通过此边框图像出现。

There is a property, background-clip that allows us to limit the background to specific areas of an elements layout box. 有一个属性background-clip ,可让我们将背景限制为元素布局框的特定区域。

Background-Clip @ MDN 背景剪辑@ MDN

The background-clip CSS property specifies whether an element's background, either the color or image, extends underneath its border. background-clip CSS属性指定元素的背景(彩色还是图像)在其边框下方延伸。

Support is IE9 and up 支持 IE9及更高版本

In this case, you only want the background to extend to the edges of the padding (and not the border). 在这种情况下,您只希望背景延伸到填充的边缘(而不是边框​​)。

So: 所以:

 .thediv { width: 500px; height: 50px; /* for demo */ background: #0000FF; border-style: solid; border-width: 0 0 30px; /* for demo */ border-color: rgba(255, 0, 0, 0.5); margin-bottom: 1em; ; } .clip { background-clip: padding-box; ; } 
 <div class="thediv"></div> <div class="thediv clip"></div> 

As you can see, without the "clipping" the border and background are combined, with the clipping, the border stands alone. 如您所见,在没有“剪切”的情况下,边框和背景没有结合在一起,而在剪切时,边框是独立的。

Set the box-sizing property as content-box . box-sizing属性设置为content-box This will exclude the border from the box's width. 这将从框的宽度中排除边框。 I think it has been set as box-sizing: border-box; 我认为它已设置为box-sizing: border-box; somewhere. 某处。 Please use the below CSS: 请使用以下CSS:

.thediv {
    box-sizing: content-box;
}

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

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