简体   繁体   English

CSS:“背景尺寸”和“背景重复:重复”拉伸问题

[英]CSS: 'Background-size' and 'background-repeat: repeat' stretch issue

I'm sure this is correct behavior for the implementation I have, but I'm wondering if theres an easy way to do what I want to accomplish. 我确定这对于我的实现来说是正确的行为,但是我想知道是否有一种简单的方法可以完成我想完成的事情。

I have a background image that is a 3px x 3px pattern. 我的背景图片是3px x 3px的图案。

I want this pattern to repeat-x the full width (100%) of the element its set in, however I only want it to repeat-y for half of the width of the element its in (50%). 我希望此模式重复-x设置元素的整个宽度(100%),但是我只希望它重复-y重复元素的一半(50%)。

I have this implementation: 我有这个实现:

.element {
    width: 100%;
    background-image: url('/path/to/pattern.png');
    background-repeat: repeat;
} 

which successefully repeats the pattern throughout the entire element. 成功地在整个元素中重复该模式。 To attempt to achieve the 50% repeat-y height, which is what I want, i tried: 为了达到我想要的50%重复Y高度,我尝试了以下方法:

.element {
    width: 100%;
    background-image: url('/path/to/pattern.png');
    background-repeat: repeat;
    background-size: 100% 50%;
} 

However, the background-size skews the pattern image to 100%/50% height/width instead of keeping the desired repeat effect. 但是,背景尺寸会使图案图像倾斜到100%/ 50%的高度/宽度,而不是保持所需的重复效果。

Is there any way to simply accomplish this? 有什么方法可以简单地做到这一点?

Thanks 谢谢

Make a graphic 3px wide and really tall with the different background below. 制作一个3像素宽的图形,并在下面使用不同的背景,确实很高。 Or, though more code, make a 'unit' of three divs: the base is a div with whatever other color/pattern you want that will be the 50% of the y. 或者,尽管编写了更多代码,但使“ div”成为三个div:基数是一个div,它具有您想要的任何其他颜色/图案,它们将是y的50%。 Next in that div is the background repeating to a fixed height and that one is positioned relative to the top of the base. 该div中的下一个是重复到固定高度的背景,并且背景相对于底部的顶部定位。 The last div is just the content. 最后一个div只是内容。 Not as pretty as a simple CSS declaration, but it works across platforms and most browsers, even IE6. 不像简单的CSS声明那样漂亮,但是它可以跨平台和大多数浏览器(甚至IE6)运行。

How does your pattern look like? 您的图案看起来如何? This may fulfill your requirements. 这样可以满足您的要求。 Instead of using a background to display the PNG, you now use an img element, and set the width to 100% and the height to 50%. 现在,您不再使用背景来显示PNG,而是使用img元素,并将宽度设置为100%,将高度设置为50%。 Or use a div to benefit from background: 或使用div来受益于背景:

<div id="element">
    <div id="pattern"/>
    <div>I'm at the top!<div>
</div>    

The rules: 规则:

* {
  margin: 0;
  padding: 0;
}

html, body {
    height: 100%;
}

#element {
    position: relative;
    min-height: 100%;
}

#element #pattern {
    background: url(path/to/pattern.png);
    height: 50%;
    left: 0px;
    position: absolute;
    z-index: -1;
    top: 0px;
    width: 100%;
}
Add another container div

You can create another div inside the container div & set its width to 50% of parent container div. 您可以在容器div中创建另一个div并将其宽度设置为父容器div的50%。 Inside this div, you can fill your pattern. 在此div内,您可以填充图案。

<div id="container">
<div id="myPattern"></div>

#container{
        width:200px;
        height:400px;
        background-color:black;
    }      

#myPattern
{
    background-color:yellow;
    height:50%;
    width:100%;
    /* fill pattern here */
    background-image: url(tt.png);
    background-repeat: repeat-x repeat-y;
}

JSFiddle 的jsfiddle

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

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