I'm trying to target the class, .textbox-fill to change the background colour and text colour for each individual box element. For example .textbox-fill's first-child should be grey. The second-child should be white. The third child I want to adjust the height slight and so on.
I have tried using the nth-child selector #About-Container .about-inner-content article:nth-child(1n+2) .textbox-fill nothing seems to work. Example here
<article class="col-sm-6 grid-tile">
<div class="textbox-fill">
<div class="about-textbox">
<h2>Some text</h2>
<p>Some text</p>
</div>
</div>
</article>
I have encounter this issue in the past and I'm not 100% sure how I resolved it. I have read several articles and posts on this subject. I understand the basics, however something more complex like this I always approached with trial and error method.
Does anyone know how I can solve this issue?
Since your .textbox-fill is inside the article, it is the article you need to start with, as target the text-fill using nth-child will not work as it can't see outside its parent
So do like this instead
article:nth-child(1) .textbox-fill {
color: red;
}
article:nth-child(2) .textbox-fill {
color: lime;
}
article:nth-child(3) .textbox-fill {
color: blue;
}
<article class="col-sm-6 grid-tile">
<div class="textbox-fill">
<div class="about-textbox">
<h2>Some text</h2>
<p>Some text</p>
</div>
</div>
</article>
<article class="col-sm-6 grid-tile">
<div class="textbox-fill">
<div class="about-textbox">
<h2>Some text</h2>
<p>Some text</p>
</div>
</div>
</article>
<article class="col-sm-6 grid-tile">
<div class="textbox-fill">
<div class="about-textbox">
<h2>Some text</h2>
<p>Some text</p>
</div>
</div>
</article>
If we talk about the .textbox-fill's .about-textbox's children, the h2 and p, do like this, where you use the global selector * in *:nth-child, as the children is of different types
.textbox-fill .about-textbox *:nth-child(1) {
color: red;
}
.textbox-fill .about-textbox *:nth-child(2) {
color: lime;
}
<article class="col-sm-6 grid-tile">
<div class="textbox-fill">
<div class="about-textbox">
<h2>Some text</h2>
<p>Some text</p>
</div>
</div>
</article>
If you have a .textbox-fill with several children, like:
div.textbox-fill
h1#header
div#firstDiv
div#secondDiv
article#someArticle
div#thirdDiv
Now, .textbox-fill div:first-child will target nothing, because there isn't any div that is a first child of it's parent (.textbox-fill). Although .textbox-fill div:first-of-type will target div#firstDiv. .textbox-fill div:nth-child(2) will also target div#firstDiv. div#thirdDiv will be .textbox-fill div:nth-of-type(3) or .textbox-fill div:nth-child(5) or .textbox-fill div:last-child or .textbox-fill div:last-of-type.
If you want to target specific .textbox-fill located on site, but with different parents, like:
div.something
div.textbox-fill
[...]
article#other
div.textbox-fill
You cannot target it using nth-child nor nth-of-type. You will need to find another way based on your site tree.
Related
Basically I want to style a p tag when it is not present inside another div.
For example
<div class="container">
<div class="row">
<p>I want to style here</p>
<div class="secondClass">
<p>I don't want to style here</p>
</div>
</div>
</div>
I tried the following but no luck
.container > :not(.secondClass) + p {
color: red;
}
Tou can change the style like this :
div > :not(.secondClass) > p {
color: red;
}
There are several ways you can do this, depending on context. See code below:
.container .row > p {
background: red;
}
<div class="container">
<div class="row">
<p>I want to style here</p>
<div class="secondClass">
<p>I don't want to style here</p>
</div>
<p>I want to style here</p>
</div>
</div>
,
I am currently preparing myself for a regional championship in web development for high-school students. Preparation tasks are one the championships website to solve. I have the following HTML code:
<h2>Task 5</h2>
<article id="task-5">
<div class="marble"></div>
<div class="marble"></div>
<section>
<div class="marble" data-target></div>
<div class="marble" data-target></div>
<section>
<div class="marble"></div>
<div class="marble"></div>
</section>
</section>
</article>
My goal is to select the divs with the marble class marked with data-target, but under following requirements:
I am not allowed to use these CSS pseudo-classes or CSS selectors:
:nth-child
:nth-last-child
:nth-of-type
:nth-last-of-type
[data-target]
nor any use of + or ~
Only one selector is allowed.
I have tried the following selector, but it still selects the third div (the one not marked with data-target):
#task-5 section > div:not(:last-child):not(:is(:first-child:is(:last-child)))
Can you please help me. Thank you very much
First you can select the section that has another section as a descendant. Then you can select the divs that are not descendants of a section which is the descendant of a section.
section:has(section) div:not(section section div) {
width: 100px;
height: 100px;
background: red;
}
<h2>Task 5</h2>
<article id="task-5">
<div class="marble">1</div>
<div class="marble">2</div>
<section>
<div class="marble" data-target>3</div>
<div class="marble" data-target>4</div>
<section>
<div class="marble">5</div>
<div class="marble">6</div>
</section>
</section>
</article>
Note: currently :has is supported on Firefox only if a flag is set.
The simplest answer, so far as I can tell from your posted constraints, would be:
/* this uses the child combinator (`>`) to select
the <div> elements which are the children of
a <section> which is in turn a child of an
<article>: */
article > section > div {
background-color: lime;
}
*,
::before,
::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
section {
margin-inline-start: 10%;
padding: 0.5em;
}
div {
border: 1px solid #000;
margin-inline-start: 10%;
min-block-size: 2em;
}
article>section>div {
background-color: lime;
}
<h2>Task 5</h2>
<article id="task-5">
<div class="marble"></div>
<div class="marble"></div>
<section>
<div class="marble" data-target></div>
<div class="marble" data-target></div>
<section>
<div class="marble"></div>
<div class="marble"></div>
</section>
</section>
</article>
References:
Child combinator: >.
You can select the first child through:
element:first-child
and you can select last child:
element :last-child
and you have some method like:
not()
first-of-type
last-of-type
Is it possible to address the first element in a div when you don't know what the first element is.
I have for example two different divs
<div class="templateOne">
<h1>Header 1</h1>
</div>
<div class="templateOne">
<h2>Header 2</h2>
</div>
That I can then say
.templateOne > * {
margin-top: 0em;
}
or something like that.
If you want to use adress the first child element, you can use the :first-child or the :nth-child(1) pseudo-selector.
.templateOne :first-child {
color: red;
}
<div class="templateOne">
<h1>Header 1</h1>
<p>Content 1</p>
</div>
<div class="templateOne">
<h2>Header 2</h2>
<p>Content 2</p>
</div>
If you want to address only the first element with a specific class name you can use :first-of-type or nth-of-type(1):
.templateOne:first-of-type {
color: red;
}
<div class="templateOne">
<h1>Header 1</h1>
<p>Content 1</p>
</div>
<div class="templateOne">
<h2>Header 2</h2>
<p>Content 2</p>
</div>
> child combinator
* universal selector
:first-child
.templateOne > *:first-child {
margin-top: 0em;
}
I have the following HTML which works:
<div class="preview">
<div class="image">
<img src="SomeFile/SomeImage.gif">
</div>
<h2>Some heading</h2>
<p>Some text</p>
</div>
And the following CSS (which works):
.preview:hover > h2 {display: none;}
.preview:hover > p {display: block;}
If I add a new <div id="newID"> around the HTML, how do I update the CSS to work with the new ID wrapper?
Also, how do I use multiple IDs in the CSS to avoid re-writing this for each ID?
Thanks
If you need the style to be applied only to the elements that are children to the new wrapper, then you can just add the ID selector like this:
#newID .preview:hover > h2 {display: none;}
Otherwise you don't need to change anything.
Is that what you looking for?
<div id="newID">
<div class="preview">
<div class="image">
<img src="SomeFile/SomeImage.gif">
</div>
<h2>Some heading</h2>
<p>Some text</p>
</div>
<div class="preview">
<div class="image">
<img src="SomeFile/SomeImage.gif">
</div>
<h2>Some heading</h2>
<p>Some text</p>
</div>
</div>
your css:
#newID .preview:hover > h2 {display: none;}
#newID .preview:hover > p {display: block;}
Use class for styling multiple elements with the same style:
.yourClass:hover {}
I didn't fully understand what you're looking for, but if you are trying to apply the css to newly added divs then you can try this
<html><head>
<style>
.preview { border: 1px solid black;width:200px}
.preview:hover > h2 {display: none;}
.preview:hover > p {display: block;}
</style>
<script src=path/to/jquery.js></script>
</head><body>
<button class=plus>Add</button>
<div class="preview">
<div class="image">
<img src="SomeFile/SomeImage.gif">
</div>
<h2>Some heading</h2>
<p>Some text</p>
</div>
<script>
// Button Adds a new div with the class 'preview'
$('button.plus').click(function () {
var new_id = '<div id="newID" class=preview>New Div <br/> <h2>Some heading</h2> <p>Some text</p></div>';
$('body').append(new_id);
});
</script>
</body></html>
If your ID use same structure, for example #newid-x, #newid-y, #newid-z, you can write you css rule targeting all ID start with newid-
div[id^='newid-']:hover > h2, div[id*=' newid-']:hover >h2 {
display: none;
}
I have a html page setup like so
<div class="row row-venue">
<div class="col-sm-12">
<h3>Venue 1</h3>
</div>
</div>
<div class="row row-venue">
<div class="col-sm-12">
<h3>Venue 2</h3>
</div>
</div>
All I want to do is make every odd h3 a different colour to every even. I have tried with the code below
div.row-venue div.col-venue h3:nth-of-type(odd){
color:white;
}
div.row-venue div.col-venue h3:nth-of-type(even){
color:black;
}
and even just tried
h3:nth-of-type(odd){
color:white;
}
h3:nth-of-type(even){
color:black;
}
I just cant seem to get my head around it, any help would be appreciated
<h3> is always the first child of <div class="col-sm-12">. Because the counting is zero base - first child = even, so the even rule that you defined applies to all <h3> elements.
To get what you ask, you need to find the nth child between the <div class="row row-venue"> items:
.row-venue:nth-child(odd) h3 {
color: white;
}
.row-venue:nth-child(even) h3 {
color: black;
}
If your divs are mixed with other elements, use :nth-of-type instead of :nth-child.
Your CSS is targeting odd/even <h3> tags within a .col-venue element, which I don't see in your markup. Even if .col-venue was in your markup It would only be targeting H3s within it - example here.
You need to control the styling from a higher level in the markup, see below.
<div class="row row-venue">
<div class="col-sm-12">
<h3>Venue 1</h3>
</div>
</div>
<div class="row row-venue">
<div class="col-sm-12">
<h3>Venue 2</h3>
</div>
</div>
.row-venue:nth-of-type(odd) h3 {
color: red;
}
.row-venue:nth-of-type(even) h3 {
color: black;
}
http://jsfiddle.net/Lvezzrnq/
With the above CSS selectors you are targeting odd and even .row-venue elements and then drilling down to the h3.