简体   繁体   English

CSS边框图片比div宽

[英]CSS border image is wider than the div

I am adding an image border in bottom of my div like this : 我在div底部添加一个图像边框,如下所示:

HTML: HTML:

<div class="view">
    <div class="shadow_overlay"></div>
</div>

CSS: CSS:

.view {
    text-align: center;
    overflow: hidden;
    position: relative;
    text-align: center;
    cursor: default;
    width: 160px;
    height: 190px;
    border-image: linear-gradient(to right, rgb(139, 191, 64) 25%, rgb(230, 27, 33) 25%, rgb(230, 27, 33) 50%, rgb(124, 196, 236) 50%, rgb(124, 196, 236) 75%, rgb(254, 181, 17) 75%);
    border-image-slice: 1;
    border-image-width: 4px 0px 0px 0px;
}
.shadow_overlay {
    background: url('http://i.imgur.com/MrVzqyp.png') 0 0 no-repeat;
    background-size: cover;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin-left:auto;
    margin-right:auto;
    width:160px;
    height:190px;
}

This worked but in action border-image is wider than my div . 这行得通,但实际上, border-image比我的div宽。

Problem pic: 问题图片:

在此处输入图片说明

How do I fix this problem? 我该如何解决这个问题?

DEMO here 在这里演示

It seems like browsers assign a default width to borders when border-image is used (but the borders on the other sides are invisible because the border-image-width is 0px ). 似乎在使用border-image时,浏览器为边界分配了默认宽度(但是,由于border-image-width0px ,所以另一面的border-image-width是不可见的)。 To avoid the borders from looking like they are overflowing the div , manually set the border widths on all other sides to 0px. 为避免边框看起来好像溢出了div ,请手动将所有其他边的边框宽度设置为0px。

border-width: 4px 0px 0px 0px;

The behavior is seen in Chrome (upto v48.0.2535.0 dev-m), IE (Edge), Opera and Safari. 该行为在Chrome浏览器(最高v48.0.2535.0 dev-m),IE(Edge),Opera和Safari中可见。 The border image doesn't extend beyond the div in latest Firefox (v41.0.1) IE (v11), 在最新的Firefox(v41.0.1)IE(v11)中,边框图像不会超出div

 .view { text-align: center; overflow: hidden; position: relative; text-align: center; cursor: default; width: 160px; height: 190px; border-image: linear-gradient(to right, rgb(139, 191, 64) 25%, rgb(230, 27, 33) 25%, rgb(230, 27, 33) 50%, rgb(124, 196, 236) 50%, rgb(124, 196, 236) 75%, rgb(254, 181, 17) 75%); border-image-slice: 1; border-image-width: 4px 0px 0px 0px; border-width: 4px 0px 0px 0px; } .shadow_overlay { background: url('http://i.imgur.com/MrVzqyp.png') 0 0 no-repeat; background-size: cover; position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin-left: auto; margin-right: auto; width: 160px; height: 190px; } 
 <div class="view"> <div class="shadow_overlay"></div> </div> 


In the below snippet you can see how it looks as though all other sides have a 3px border. 在下面的代码段中,您可以看到所有其他面都有3px边框的外观。 There is no clear explanation either in the Web or in the specs about whose behavior is correct (Chrome, Edge or FF, IE11). 在Web或规范中,没有关于其行为正确(Chrome,Edge或FF,IE11)的明确解释。

 .view { text-align: center; overflow: hidden; position: relative; text-align: center; cursor: default; width: 160px; height: 190px; border-image: linear-gradient(to right, rgb(139, 191, 64) 25%, rgb(230, 27, 33) 25%, rgb(230, 27, 33) 50%, rgb(124, 196, 236) 50%, rgb(124, 196, 236) 75%, rgb(254, 181, 17) 75%); border-image-slice: 1; border-image-width: 4px 0px 0px 0px; } .view#two{ border-width: 4px 3px 3px 3px; } .shadow_overlay { background: url('http://i.imgur.com/MrVzqyp.png') 0 0 no-repeat; background-size: cover; position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin-left: auto; margin-right: auto; width: 160px; height: 190px; } 
 <div class="view"> <div class="shadow_overlay"></div> </div> <div class="view" id="two"> <div class="shadow_overlay"></div> </div> 


The W3C Specs also say the following about border-image properties but in FF and IE11 the border-image is not shown when only border-width is provided and border-image-width is avoided. W3C的规格也说一下边界图片属性,但在FF和IE11的以下border-image时仅被未示出border-width设置和border-image-width得以避免。

The border-image properties do not affect layout: layout of the box, its content, and surrounding content is based on the 'border-width' and 'border-style' properties only. 边框图像属性不会影响布局:框的布局,其内容以及周围的内容仅基于'border-width'和'border-style'属性。

So, it seems like the behavior of border-image is still not standardized. 因此,似乎border-image的行为仍未标准化。 I am leaning towards what is observed in Chrome, Edge because Microsoft, for some reason, seems to have changed the behavior from IE11 and so there must be a good reason for it. 我倾向于在Chrome,Edge中观察到的情况,因为微软出于某种原因似乎已经改变了IE11的行为,因此一定有充分的理由。

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

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