【问题标题】:How to apply a border image troughout box-sizing on a image that dynamically changes it's size according to parent's size如何在根据父级大小动态更改其大小的图像上应用边框图像 troughout box-sizing
【发布时间】:2014-01-22 09:52:29
【问题描述】:

尊敬的 stackoverflow 用户,

我有一个包含图像的 div(由 php 在 wordpress 中生成)。此图像将根据父 div 大小为 100% 宽度和自动高度。

我想应用一个覆盖该图像顶部和底部的边框图像。这可以通过 box-sizing:border-box 来完成。所以我做到了。

但由于某种原因,边框没有覆盖图像,并且图像的高度尺寸减小了,或者父级的高度尺寸增加了。

我已经坚持了很久。无论如何,这可以只用 css 完成吗?

jsfiddle:http://jsfiddle.net/SE3Na/

HTML

<div class="box1">
<div class="box2">
    <img src="http://s29.postimg.org/oj8fdc5nb/example_image.png"/>
</div>
</div>

CSS

.box1{
position:relative;
width:400px;
height:auto;
border:1px solid grey;}

.box2{
width:100%;
height:auto;}

.box2 img{
width:100%;
height:auto;
border-image:url(http://s30.postimg.org/atduh061p/border_banner_3.png) 12 12 12 12 repeat;
-webkit-border-image:url("http://s30.postimg.org/atduh061p/border_banner_3.png") 12 12 12 12 repeat;
-moz-border-image:url("http://s30.postimg.org/atduh061p/border_banner_3.png") 12 12 12 12 repeat;   
border-width: 12px 0px 12px 0px;
box-sizing:border-box;
-webkit-box-sizing:border-box;}

【问题讨论】:

    标签: html css border-box


    【解决方案1】:

    我发现 :before 和 :after 对于这种努力很有用:

    .box1:before,
    .box1:after
    {
        position: absolute;
        z-index: 1;
        display: block;
        content: "";
        height: 12px;
        width: 100%;
        background-color: yellow;
    }
    .box1:after
    {
        bottom: 0;
        background-color: green;
    }
    

    JSFiddle查看它的实际应用

    【讨论】:

    • 谢谢,我对前后元素没有经验。我以您的代码为基础,并按照我希望它的工作方式对其进行了微调。我希望图像根据其父级 (box1) 100% 传播。所以我添加了这个。然后我添加了盒子大小而不是背景图像。这给了我一些麻烦,它压缩了边框图像并在此压缩中重复了它。我将其固定为 110% 的高度和溢出:隐藏。不是最美丽的方式,但它可以解决问题。示例:jsfiddle.net/SE3Na/3
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    相关资源
    最近更新 更多