简体   繁体   English

使用CSS在子div上应用不同的样式

[英]apply different style on child div using css

I want to apply different style on different childs of div. 我想对div的不同子项应用不同的样式。 Here is my code, but it is not working. 这是我的代码,但是不起作用。 Could anyone help me? 有人可以帮我吗? How it is possbile to change style across each div child. 如何在每个div子级中更改样式。 The selector nth-child('target') not working 选择器nth-child('target')不起作用

HTML: HTML:

<div class='background'>
 bg1
</div>

<div class='image'>
 img1
</div>
<div class='image2'>
 img21
</div>
<div class='text'>
 <p>
       1 A website, also written as web site,is a collection of related web pages, including multimedia content, typically identified with a common domain name, and published on at least one web server.
  </p>
</div>
<div class='background'>
  bg2
</div>
<div class='image'>
 img2
</div>
<div class='image2'>
 img22
</div>
<div class='text'>
  <p>
       2 A website, also written as web site,is a collection of related web  pages, including multimedia content, typically identified with a common domain name, and published on at least one web server.
  </p>
</div>

I can't access each div child seperately 我无法单独访问每个div孩子

CSS CSS

 div.background:nth-child(1)
        {
            position: absolute;
            margin-top: -200%;
            width: 100%;
            height:180%;
        }
 div.image:nth-child(1){
            background-image: url(<?php echo base_url('Images/fantasy_mountain_art_hd_wallpapers.jpg'); ?>) ;
            position: relative;
            margin-top: 150px;
            height: 40%;
            width: 56%;
            margin-left: 20%;
            align-content: center;
            will-change: scroll-position;
            pointer-events: auto;
            background-size: 100% 280px;
            border: 4px solid gainsboro;
            border-radius: 5px;
        }
 div.image2:nth-child(1){
            background-image: url(<?php echo base_url('Images/RPyvJD.jpg'); ?>) ;
            background-size: 100% 280px;
            position: relative;
            margin-top: 100px;
            margin-left: 20%;
            height: 40%;
            width: 56%;
            border: 4px solid gainsboro;
            border-radius: 5px;
        }
  div.text:nth-child(1){
            position: relative;
            color: whitesmoke;
            margin-top: -10px;
            width: 50%;
            font-size: x-large;
            font-family: Forte;
            margin-left: 20%;
        }
  div.background:nth-child(2)
        {
            position: absolute;
            margin-top: 500px;
            width: 100%;
            height: 90%;
            background-color: black;
            -webkit-animation-name: example; /* Chrome, Safari, Opera */
            -webkit-animation-duration: 20s; /* Chrome, Safari, Opera */
            -webkit-animation-iteration-count: infinite; /* Chrome, Safari, Opera */
            animation-name: example;
            animation-duration: 40s;
            animation-iteration-count: infinite;
        }
  div.image:nth-child(2)
        {
            background-image: url(<?php echo base_url('Images/tumblr_o7t5h0LV6T1tal018o1_500.gif'); ?>) ;
            position: absolute;
            width: 20%;
            height: 40%;
            margin-top:5% ;
            opacity :0.4;
        }
  div.image2:nth-child(2)
        {
            background-image: url(<?php echo base_url('Images/giphy3.gif'); ?>) ;
            position: absolute;
            margin-top: 5%;
            margin-left: 60%;
            width: 20%;
            height: 40%;
            opacity :0.4;
        }

since you're using classes you technically don't have to use nth-child() to target the element you want, however if you have/need/want to use nth-child then make sure your giving it the appropriate location to find the children in, if you write: 因为您使用的是类,从技术上讲,您不必使用nth-child()来定位所需的元素,但是,如果您有/需要/想要使用nth-child,请确保将其指定给适当的位置以查找孩子们,如果你写:

div.image:nth-child(2){}

it will look for the second instance where div.image appears as a CHILD of whatever the wrapper is, so make sure you set a div wrapper and if you want add a class of container to it, put all the html elements you want to use nth-child to find inside this new container. 它将查找第二个实例,其中div.image作为包装器的形式显示为CHILD,因此请确保设置了div包装器,如果要向其添加容器类,请放置所有要使用的html元素nth-child在此新容器中查找。

then you can use the selector like this: 那么您可以像这样使用选择器:

.container > div:nth-child(1) {}

hope it helps, gl. 希望对您有所帮助,GL。

将 CSS 样式应用于<div></div><div id="text_translate"><p>我的问题是下面的 html</p><pre> &lt;div class="editor-container"&gt; &lt;div class="editor-row curFocus"&gt; &lt;div class="editor-label"&gt; &lt;label for="FirstName"&gt;First Name&lt;/label&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;input class="text-box single-line valid" id="FirstName" name="FirstName" type="text" value="Nancy" maxlength="20"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> 当用户选择输入字段时,我通过一些 javascript 将 class "curFocus" 添加到外部 div 以突出显示 label 和输入字段。</p><p> 我的 css 是 -</p><pre> .editor-container { border: thin solid #444444; display: table; width: 100%; }.editor-row { width: 100%; display: table-row; }.editor-label { padding-left: .4em; width: 40%; }.editor-label, .editor-field { padding-right: .4em; padding-bottom: .2em; padding-top: .2em; display: table-cell; }.curFocus { border: 2px solid #05365b; background-color: #d3e5f2; margin: 3px; padding: 3px; }</pre><p> 我的问题是,在 Chrome 12 和 IE9 中使用调试器时,它们都显示了应用于外部 div 的边框设置。 但是,在查看表单时,浏览器都不会显示指定的边框。 所有其他 css 设置工作正常。 我还尝试将“.curFocus”的定义更改为“.curFocus div”。 但这也将样式应用于每个嵌套的 div,但确实在所有 div 上显示了边框。</p><p> 虽然我不是 CSS 专家,但不清楚为什么这不起作用。</p><hr><p> 编辑这里是 jsfiddle 链接 - <a href="http://jsfiddle.net/photo_tom/KmsF5/1/" rel="nofollow">http://jsfiddle.net/photo_tom/KmsF5/1/</a> 。 在进行测试时,如果在 IE7 兼容模式下,它确实可以在 IE9 中正常工作。 否则,它不能正确显示。 很抱歉不包括链接,仍然习惯了 jsfiddle 甚至存在的事实。</p></div> - Apply CSS style to <div>

暂无
暂无

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

相关问题 将 CSS 样式应用于<div></div><div id="text_translate"><p>我的问题是下面的 html</p><pre> &lt;div class="editor-container"&gt; &lt;div class="editor-row curFocus"&gt; &lt;div class="editor-label"&gt; &lt;label for="FirstName"&gt;First Name&lt;/label&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;input class="text-box single-line valid" id="FirstName" name="FirstName" type="text" value="Nancy" maxlength="20"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> 当用户选择输入字段时,我通过一些 javascript 将 class "curFocus" 添加到外部 div 以突出显示 label 和输入字段。</p><p> 我的 css 是 -</p><pre> .editor-container { border: thin solid #444444; display: table; width: 100%; }.editor-row { width: 100%; display: table-row; }.editor-label { padding-left: .4em; width: 40%; }.editor-label, .editor-field { padding-right: .4em; padding-bottom: .2em; padding-top: .2em; display: table-cell; }.curFocus { border: 2px solid #05365b; background-color: #d3e5f2; margin: 3px; padding: 3px; }</pre><p> 我的问题是,在 Chrome 12 和 IE9 中使用调试器时,它们都显示了应用于外部 div 的边框设置。 但是,在查看表单时,浏览器都不会显示指定的边框。 所有其他 css 设置工作正常。 我还尝试将“.curFocus”的定义更改为“.curFocus div”。 但这也将样式应用于每个嵌套的 div,但确实在所有 div 上显示了边框。</p><p> 虽然我不是 CSS 专家,但不清楚为什么这不起作用。</p><hr><p> 编辑这里是 jsfiddle 链接 - <a href="http://jsfiddle.net/photo_tom/KmsF5/1/" rel="nofollow">http://jsfiddle.net/photo_tom/KmsF5/1/</a> 。 在进行测试时,如果在 IE7 兼容模式下,它确实可以在 IE9 中正常工作。 否则,它不能正确显示。 很抱歉不包括链接,仍然习惯了 jsfiddle 甚至存在的事实。</p></div> - Apply CSS style to <div> 将样式应用于 div 但不是<strong>子级</strong> - Apply style to div but not <strong> child 如何将两种不同的CSS样式应用于同一页面上的同一div? - How to apply two different CSS style to the same div on the same page? 将css样式应用于子元素 - Apply css style to child element 将CSS样式应用于子节点 - Apply CSS style to child nodes CSS无法在子div上申请 - Css fails to apply on a child div 仅将CSS应用于子DIV - Apply CSS to child DIV only 覆盖子div中的CSS样式 - Override CSS style in child div 在包装上应用不同的CSS样式 - Apply different CSS style on wrapping 如何使用CSS或JS从子div中删除样式? - How to remove style from child div using css or js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM