This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions.
For example:
p + p {
/* Some declarations */
}
I don't know what the + means. What's the difference between this and just defining a style for p without + p?
See adjacent selectors on W3.org.
In this case, the selector means that the style applies only to paragraphs directly following another paragraph.
A plain p selector would apply the style to every paragraph in the page.
This will only work on IE7 or above. In IE6, the style will not be applied to any elements. This also goes for the > combinator, by the way.
See also Microsoft's overview for CSS compatibility in Internet Explorer.
It's the Adjacent sibling selector.
From Splash of Style blog.
To define a CSS adjacent selector, the
plus sign is used.
h1+p {color:blue;}
The above CSS code will format the
first paragraph after (not inside) any h1 headings
as blue.
h1>p selects any p element that is a direct (first generation) child (inside) of an h1 element.
h1>p matches <h1> <p></p> </h1> (<p> inside <h1>)
h1+p will select the first p element that is a sibling (at the same level of the dom) as an h1 element.
h1+p matches <h1></h1> <p><p/> (<p> next to/after <h1>)
The + sign means select an "adjacent sibling"
For example, this style will apply from the second <p>:
p + p {
font-weight: bold;
}
<div>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
</div>
Example
See this JSFiddle and you will understand it: http://jsfiddle.net/7c05m7tv/
(Another JSFiddle: http://jsfiddle.net/7c05m7tv/70/)
Browser Support
Adjacent sibling selectors are supported in all modern browsers.
Learn more
http://css-tricks.com/almanac/selectors/a/adjacent-sibling/
http://www.w3.org/TR/CSS2/selector.html#adjacent-selectors
https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_selectors
"+" is the adjacent sibling selector. It will select any p DIRECTLY AFTER a p (not a child or parent though, a sibling).
The + combinator is called the Adjacent sibling combinator / Next-sibling combinator.
For example, the combination of p + p selectors, selects the p elements immediately following the p elements
It can be thought of as a "looking alongside" combination that checks for the immediately following element.
Here is a sample snippet to make things more clear:
body {
font-family: Tahoma;
font-size: 12px;
}
p + p {
margin-left: 10px;
}
<div>
<p>Header paragraph</p>
<p>This is a paragraph</p>
<p>This is another paragraph</p>
<p>This is yet another paragraph</p>
<hr>
<p>Footer paragraph</p>
</div>
Since we are on the same topic, it is worth mentioning another combinator, ~, which is the General sibling combinator / Subsequent-sibling combinator
For example, p ~ p selects all the p which follows the p doesn't matter where it is, but both p should be having the same parent.
Here is what it looks like with the same markup:
body {
font-family: Tahoma;
font-size: 12px;
}
p ~ p {
margin-left: 10px;
}
<div>
<p>Header paragraph</p>
<p>This is a paragraph</p>
<p>This is another paragraph</p>
<p>This is yet another paragraph</p>
<hr>
<p>Footer paragraph</p>
</div>
Notice that the last p is also matched in this sample.
The + selector targets the one element after. On a similar note, the ~ selector targets all the elements after. Here's a diagram, if you're confused:
+ presents one of the relative selectors. Here is a list of all relative selectors:
div p - All <p> elements inside of a <div> element are selected.
div > p - All <p> elements whose direct parent is <div> are selected. It works backwards too (p < div)
div + p - All <p> elements placed immediately after a <div> element are selected.
div ~ p - All <p> elements that are preceded by a <div> element are selected.
Here is some more about selectors.
It would match any element p that's immediately adjacent to an element 'p'. See: http://www.w3.org/TR/CSS2/selector.html
p+p{
//styling the code
}
p+p{
} simply mean find all the adjacent/sibling paragraphs with respect to first paragraph in DOM body.
<div>
<input type="text" placeholder="something">
<p>This is first paragraph</p>
<button>Button </button>
<p> This is second paragraph</p>
<p>This is third paragraph</p>
</div>
Styling part
<style type="text/css">
p+p{
color: red;
font-weight: bolder;
}
</style>
It will style all sibling paragraph with red color.
final output look like this
Related
I want to provide a padding of 30px to p element inside the div element. But I don't want that padding to be applied to the img tag inside p.
<div class="desc">
<p>first paragraph</p>
<p><img src="....."></p>
<p>second paragraph</p>
</div>
I have attached the required result below.
You can use the > selector.
div > p { padding: 30px; }
It applies the styling only on the direct child elements (= direct descendants)
As #Khaaytil stated, you can use > between selectors.
Look here for more reference > direct descendant selector
For different scenarios u can see these 2 also:
Using + adjacent sibling selector
Using ~ non-adjacent sibling selector
Also you can try :only-child Selector
But as best practice you should really not wrapping images in a tag. Just put the image between the 2 tags, or wrap it in another tag.
So I've been coding for a week and I have googled for 30 min trying to find a solution. So excuse me if it's already been asked. I'm trying to write a summary of what I've learned after each lesson but it's not working!
<body> <center> h1> Module 40 </h1> </center>
<p>In this module I have learned on how to use the tag <!-- <div> ---> the purpose of this tag is to create a specific group whether it is images, headers, paragraphs, etc, which you can attribute seperate properties to so it is unaffected by tag selectors. by adding a class or ID to it. </p> <br>
<div class="p1">
<p> Like for example this paragraph is inside a div called "p1". And I have added a specific font-size for this one compared to the previous paragraph which is affected by a <strong> tag </strong> selector instead of a <strong> class </strong> selector.
</p>
</div>
</body>
And my CSS is this:
p
{
font-size: 15px;
}
/*****class selector*****/
.p1
{
font-size: 20px;
}
Shouldn't the class selector override the tag selector? Font size 15px is being applied to the whole text. It works if I add class="p1" to the second paragraph. But shouldn't this work if I add it to the div? Isn't that the purpose of having a div?
Must be .p1 p
p
{
font-size: 15px;
}
/*****class selector*****/
.p1 p
{
font-size: 20px;
}
<p>In this module I have learned on how to use the tag <!-- <div> ---> the purpose of this tag is to create a specific group whether it is images, headers, paragraphs, etc, which you can attribute seperate properties to so it is unaffected by tag selectors. by adding a class or ID to it. </p> <br>
<div class="p1">
<p> Like for example this paragraph is inside a div called "p1". And I have added a specific font-size for this one compared to the previous paragraph which is affected by a <strong> tag </strong> selector instead of a <strong> class </strong> selector.
</p>
</div>
This happens because of Specificity. Specificity is the means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied. Specificity is based on the matching rules which are composed of different sorts of CSS selectors.
You can find one of the most useful documentations here -
https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
No because your paragraph is a child of .p1
All children inherit the styling of their parent (font-size:20px), but have the ability to override this (which you did by setting the paragraph styling to font-size: 15px)
You can read more about inheritance in CSS here:
https://developer.mozilla.org/en-US/docs/Learn/CSS/Introduction_to_CSS/Cascade_and_inheritance
Your <p> tag is child of <div> tag, that's why its not working. Try adding the class to <p> tag
This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions.
For example:
p + p {
/* Some declarations */
}
I don't know what the + means. What's the difference between this and just defining a style for p without + p?
See adjacent selectors on W3.org.
In this case, the selector means that the style applies only to paragraphs directly following another paragraph.
A plain p selector would apply the style to every paragraph in the page.
This will only work on IE7 or above. In IE6, the style will not be applied to any elements. This also goes for the > combinator, by the way.
See also Microsoft's overview for CSS compatibility in Internet Explorer.
It's the Adjacent sibling selector.
From Splash of Style blog.
To define a CSS adjacent selector, the
plus sign is used.
h1+p {color:blue;}
The above CSS code will format the
first paragraph after (not inside) any h1 headings
as blue.
h1>p selects any p element that is a direct (first generation) child (inside) of an h1 element.
h1>p matches <h1> <p></p> </h1> (<p> inside <h1>)
h1+p will select the first p element that is a sibling (at the same level of the dom) as an h1 element.
h1+p matches <h1></h1> <p><p/> (<p> next to/after <h1>)
The + sign means select an "adjacent sibling"
For example, this style will apply from the second <p>:
p + p {
font-weight: bold;
}
<div>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
</div>
Example
See this JSFiddle and you will understand it: http://jsfiddle.net/7c05m7tv/
(Another JSFiddle: http://jsfiddle.net/7c05m7tv/70/)
Browser Support
Adjacent sibling selectors are supported in all modern browsers.
Learn more
http://css-tricks.com/almanac/selectors/a/adjacent-sibling/
http://www.w3.org/TR/CSS2/selector.html#adjacent-selectors
https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_selectors
"+" is the adjacent sibling selector. It will select any p DIRECTLY AFTER a p (not a child or parent though, a sibling).
The + combinator is called the Adjacent sibling combinator / Next-sibling combinator.
For example, the combination of p + p selectors, selects the p elements immediately following the p elements
It can be thought of as a "looking alongside" combination that checks for the immediately following element.
Here is a sample snippet to make things more clear:
body {
font-family: Tahoma;
font-size: 12px;
}
p + p {
margin-left: 10px;
}
<div>
<p>Header paragraph</p>
<p>This is a paragraph</p>
<p>This is another paragraph</p>
<p>This is yet another paragraph</p>
<hr>
<p>Footer paragraph</p>
</div>
Since we are on the same topic, it is worth mentioning another combinator, ~, which is the General sibling combinator / Subsequent-sibling combinator
For example, p ~ p selects all the p which follows the p doesn't matter where it is, but both p should be having the same parent.
Here is what it looks like with the same markup:
body {
font-family: Tahoma;
font-size: 12px;
}
p ~ p {
margin-left: 10px;
}
<div>
<p>Header paragraph</p>
<p>This is a paragraph</p>
<p>This is another paragraph</p>
<p>This is yet another paragraph</p>
<hr>
<p>Footer paragraph</p>
</div>
Notice that the last p is also matched in this sample.
The + selector targets the one element after. On a similar note, the ~ selector targets all the elements after. Here's a diagram, if you're confused:
+ presents one of the relative selectors. Here is a list of all relative selectors:
div p - All <p> elements inside of a <div> element are selected.
div > p - All <p> elements whose direct parent is <div> are selected. It works backwards too (p < div)
div + p - All <p> elements placed immediately after a <div> element are selected.
div ~ p - All <p> elements that are preceded by a <div> element are selected.
Here is some more about selectors.
It would match any element p that's immediately adjacent to an element 'p'. See: http://www.w3.org/TR/CSS2/selector.html
p+p{
//styling the code
}
p+p{
} simply mean find all the adjacent/sibling paragraphs with respect to first paragraph in DOM body.
<div>
<input type="text" placeholder="something">
<p>This is first paragraph</p>
<button>Button </button>
<p> This is second paragraph</p>
<p>This is third paragraph</p>
</div>
Styling part
<style type="text/css">
p+p{
color: red;
font-weight: bolder;
}
</style>
It will style all sibling paragraph with red color.
final output look like this
Is it possible to use the CSS3 selector :first-of-type to select the first element with a given class name? I haven't been successful with my test so I'm thinking it's not?
The Code (http://jsfiddle.net/YWY4L/):
p:first-of-type {color:blue}
p.myclass1:first-of-type {color:red}
.myclass2:first-of-type {color:green}
<div>
<div>This text should appear as normal</div>
<p>This text should be blue.</p>
<p class="myclass1">This text should appear red.</p>
<p class="myclass2">This text should appear green.</p>
</div>
No, it's not possible using just one selector. The :first-of-type pseudo-class selects the first element of its type (div, p, etc). Using a class selector (or a type selector) with that pseudo-class means to select an element if it has the given class (or is of the given type) and is the first of its type among its siblings.
Unfortunately, CSS doesn't provide a :first-of-class selector that only chooses the first occurrence of a class. As a workaround, you can use something like this:
.myclass1 { color: red; }
.myclass1 ~ .myclass1 { color: /* default, or inherited from parent div */; }
Explanations and illustrations for the workaround are given here and here.
The draft CSS Selectors Level 4 proposes to add an of <other-selector> grammar within the :nth-child selector. This would allow you to pick out the nth child matching a given other selector:
:nth-child(1 of p.myclass)
Previous drafts used a new pseudo-class, :nth-match(), so you may see that syntax in some discussions of the feature:
:nth-match(1 of p.myclass)
This has now been implemented in WebKit, and is thus available in Safari, but that appears to be the only browser that supports it. There are tickets filed for implementing it Blink (Chrome), Gecko (Firefox), and a request to implement it in Edge, but no apparent progress on any of these.
This it not possible to use the CSS3 selector :first-of-type to select the first element with a given class name.
However, if the targeted element has a previous element sibling, you can combine the negation CSS pseudo-class and the adjacent sibling selectors to match an element that doesn't immediately have a previous element with the same class name :
:not(.myclass1) + .myclass1
Full working code example:
p:first-of-type {color:blue}
p:not(.myclass1) + .myclass1 { color: red }
p:not(.myclass2) + .myclass2 { color: green }
<div>
<div>This text should appear as normal</div>
<p>This text should be blue.</p>
<p class="myclass1">This text should appear red.</p>
<p class="myclass2">This text should appear green.</p>
</div>
I found a solution for your reference. from some group divs select from group of two same class divs the first one
p[class*="myclass"]:not(:last-of-type) {color:red}
p[class*="myclass"]:last-of-type {color:green}
BTW, I don't know why :last-of-type works, but :first-of-type does not work.
My experiments on jsfiddle... https://jsfiddle.net/aspanoz/m1sg4496/
This is an old thread, but I'm responding because it still appears high in the list of search results. Now that the future has arrived, you can use the :nth-child pseudo-selector.
p:nth-child(1) { color: blue; }
p.myclass1:nth-child(1) { color: red; }
p.myclass2:nth-child(1) { color: green; }
The :nth-child pseudo-selector is powerful - the parentheses accept formulas as well as numbers.
More here: https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child
You can do this by selecting every element of the class that is the sibling of the same class and inverting it, which will select pretty much every element on the page, so then you have to select by the class again.
eg:
<style>
:not(.bar ~ .bar).bar {
color: red;
}
<div>
<div class="foo"></div>
<div class="bar"></div> <!-- Only this will be selected -->
<div class="foo"></div>
<div class="bar"></div>
<div class="foo"></div>
<div class="bar"></div>
</div>
As a fallback solution, you could wrap your classes in a parent element like this:
<div>
<div>This text should appear as normal</div>
<p>This text should be blue.</p>
<div>
<!-- first-child / first-of-type starts from here -->
<p class="myclass1">This text should appear red.</p>
<p class="myclass2">This text should appear green.</p>
</div>
</div>
Not sure how to explain this but I ran into something similar today.
Not being able to set .user:first-of-type{} while .user:last-of-type{} worked fine.
This was fixed after I wrapped them inside a div without any class or styling:
https://codepen.io/adrianTNT/pen/WgEpbE
<style>
.user{
display:block;
background-color:#FFCC00;
}
.user:first-of-type{
background-color:#FF0000;
}
</style>
<p>Not working while this P additional tag exists</p>
<p class="user">A</p>
<p class="user">B</p>
<p class="user">C</p>
<p>Working while inside a div:</p>
<div>
<p class="user">A</p>
<p class="user">B</p>
<p class="user">C</p>
</div>
I found something that works
If you have a bigger class which contains something like grid, all of elements of your another class
You can do like that
div.col-md-4:nth-child(1).myclass{
border: 1px solid #000;
}
Simply :first works for me, why isn't this mentioned yet?
If I want to add styling to all p elements inside of a div, why should I use
div > p{
*style here*
}
as opposed to just
div p{
*style here*
}
furthermore, if I want to use a pseudo class, why would I then choose to use ">"
div > p:first-child{
*style here*
}
instead of
div p:first-child{
*style here*
}
Are there any benefits or drawbacks?
what does that operator do?
It's the direct child, not a recursive match.
CSS
div > p {
}
HTML
<div>
<p>Match</p>
<span>
<p>No match</p>
</span>
</div>
CSS
div p {
}
Markup
<div>
<p>Match</p>
<span>
<p>Match</p>
</span>
</div>
Because it means direct child.
Your second example would match the p in this example
<div>
<header>
<p>
</p>
</header>
</div>
> and (space) are relationship selectors meaning "child" and "descendant" respectively. On top of the semantic differences others have pointed out, a child selector computes faster as it avoids redundant DOM tree traversal on non-matching elements.