Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 仅为HTML元素的一部分着色_Javascript_Css - Fatal编程技术网

Javascript 仅为HTML元素的一部分着色

Javascript 仅为HTML元素的一部分着色,javascript,css,Javascript,Css,我将如何使用像背景剪辑这样的东西来为HTML元素的一部分着色?仅使用CSS 我看起来像: div { background-image: url('mi_image'); ***: 50% 30em; /* Background only covering 50% height and 30em width */ } <div class="foo">This is the element. This is the element. This is the elemen

我将如何使用像
背景剪辑这样的东西来为HTML元素的一部分着色?仅使用CSS

我看起来像:

div {
   background-image: url('mi_image');
   ***: 50% 30em; /* Background only covering 50% height and 30em width */
}
<div class="foo">This is the element. This is the element. This is the element. This is the element. This is the element. This is the element. This is the element. This is the element. This is the element. This is the element. </div>

.foo {
    width: 200px;
    height: 200px;
    position: relative;
    border: 1px solid green;
    margin: 10px;
    float: left;
}
.foo:before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 50%;
    background-color: yellow;
    z-index: -1;
}
如果有必要,我也愿意使用JavaScript解决方案,但纯CSS会更好。

您可以使用伪
:before
元素为元素的部分背景着色

演示

代码:

div {
   background-image: url('mi_image');
   ***: 50% 30em; /* Background only covering 50% height and 30em width */
}
<div class="foo">This is the element. This is the element. This is the element. This is the element. This is the element. This is the element. This is the element. This is the element. This is the element. This is the element. </div>

.foo {
    width: 200px;
    height: 200px;
    position: relative;
    border: 1px solid green;
    margin: 10px;
    float: left;
}
.foo:before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 50%;
    background-color: yellow;
    z-index: -1;
}
这是元素。这就是元素。这就是元素。这就是元素。这就是元素。这就是元素。这就是元素。这就是元素。这就是元素。这就是元素。
傅先生{
宽度:200px;
高度:200px;
位置:相对位置;
边框:1px纯绿色;
利润率:10px;
浮动:左;
}
傅:以前{
内容:“;
位置:绝对位置;
排名:0;
左:0;
身高:100%;
宽度:50%;
背景颜色:黄色;
z指数:-1;
}

<代码> > p>我创建一个后台div。考虑这个HTML:

<div id="outer-container">
    <div id="container-background"></div>
    <div id="container">
        <p>
            Here's some of my interesting text        
        </p>    
    </div>
</div>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​


这是我的一些有趣的文章

​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
使用此CSS:

<style type="text/css">
    #outer-container {
        height: 300px;
        width: 300px;
        border: 1px solid black;
        position: relative;
        overflow: hidden;
    }
    #container-background {
        position: absolute;
        top: 0px;
        left: 0px;
        background-color: #FF0000; /* Use background-image or whatever suits you here */
        width: 50%; /* Your width */
        height: 200px; /* Your height */
        z-index: -1;
    }​
</style>

#外容器{
高度:300px;
宽度:300px;
边框:1px纯黑;
位置:相对位置;
溢出:隐藏;
}
#容器背景{
位置:绝对位置;
顶部:0px;
左:0px;
背景颜色:#FF0000;/*使用背景图像或任何适合您的颜色*/
宽度:50%;/*您的宽度*/
身高:200px;/*您的身高*/
z指数:-1;
}​
要创建如下结果:


我不确定我是否完全理解你的问题,但看看这把小提琴:看看这是不是你想要的

HTML

<div>
    This is my div and I love my divs. I take good care of my divs; they are my babies.
</div>​
<div>
    This is my first div.
    <div>
        This is my sub-div.        
    </div>
<div>​
但是,如果只想非对称地为div的一部分着色,那么最好使用sub div并为该div着色。这种方法是首选的,因为它比我不知道的更新的CSS3技术具有更好的浏览器支持。对于后一个示例,请查看此小提琴:

HTML

<div>
    This is my div and I love my divs. I take good care of my divs; they are my babies.
</div>​
<div>
    This is my first div.
    <div>
        This is my sub-div.        
    </div>
<div>​

mi_image
是纯色图像还是有某种图案?是重复的正方形我必须用javascript制作,因为我需要将几个元素的颜色分开,我想我要设置填充元素的宽度和高度来设置style属性。@juanpstatas我不确定我是否理解-这将适用于无限多的元素。如果您想要一些背景,请查看我更新的JSFIDLE:I明白了。在不同的元素中,我的意思不是一个容器中有几个填充物,而是每个容器中有一个填充物,但我明白了。谢谢