I try to create styles with first-child and last-child items but I encountered a problem.
When I use first-child, because there is strong item just before, the style isn't apply. But my last-child work fine.
HTML:
<br />
<h2 class="title_block">Info <strong>1</strong>
<span class="title_block_info">+2</span>
<span class="title_block_info">+1</span>
</h2>
CSS:
h2 .title_block_info,
h2 strong {
border: 1px solid #000;
}
h2 .title_block_info:first-child {
border-radius: 10px 0 0 10px;
}
h2 .title_block_info:last-child {
border-radius: 0 10px 10px 0;
}
Example here : http://jsfiddle.net/mYKRW/
Anyone know why this came about?
Thanks,
It's because you have a "strong" tag as the first child, not the title_block_info class you were going for. first-child only works if it is in fact the first child of an element.
This works
<h2 class="title_block">
<span class="title_block_info">+2</span>
<span class="title_block_info">+1</span>
</h2>
http://jsfiddle.net/mYKRW/1/
If you need that strong text in there, you could try this, notice how I wrapped your two span tages in another span tag. This will allow you to use first-child and last-child
h2 .title_block_info,
h2 strong {
border: 1px solid #000;
}
h2 span .title_block_info:first-child {
border-radius: 10px 0 0 10px;
}
h2 span .title_block_info:last-child {
border-radius: 0 10px 10px 0;
}
<h2 class="title_block">
Info <strong>1</strong>
<span>
<span class="title_block_info">+2</span>
<span class="title_block_info">+1</span>
</span>
</h2>
http://jsfiddle.net/mYKRW/6/
Lastly you could use the first-of-type pseudo class if you want to keep your html exactly as you want, and just change your css.
h2 .title_block_info,
h2 strong {
border: 1px solid #000;
}
h2 .title_block_info:first-of-type {
border-radius: 10px 0 0 10px;
}
h2 .title_block_info:last-of-type {
border-radius: 0 10px 10px 0;
}
http://jsfiddle.net/mYKRW/9/
The :first-child pseudo-class selects the first matching element from the selector .title_block_info if it's also the :first-child of the parent element; as you note this doesn't work because there's another element that's the first-child of the parent element.
In your case you could either remove the strong element that's taking the :first-child position in the DOM, or you could use, instead, the :first-of-type pseudo-class:
h2 .title_block_info:first-of-type {
border-radius: 10px 0 0 10px;
}
JS Fiddle demo.
If your HTML is going to remain similarly predictable (the .title_block_info element will always follow the :first-child element) you could, instead:
h2 :first-child + .title_block_info {
border-radius: 10px 0 0 10px;
}
JS Fiddle demo.
References:
:first-of-type pseudo class.
:first-of-type compatibility.
Related
So i decided to nest three divs as follows for experimental purposes
<div>
<div>
<div>
</div>
</div>
</div>
I then went ahead and decided to target the inner divs as follows using css:
div {
border: 1px solid black;
padding: 40px;
background: white;
}
div > div:hover {
box-shadow: 0 0 0 40px white;
}
div > div:hover{
box-shadow: 0 0 0 80px white;
}
What I expected to happen, is either for the page to break, or for it to select all of the divs at once. However, oddly enough it is targeting the divs one by one, propagating if you will.
div {
border: 1px solid black;
padding: 40px;
background: white;
}
div > div:hover {
box-shadow: 0 0 0 40px white;
}
div > div:hover{
box-shadow: 0 0 0 80px white;
}
<div>
<div>
<div>
</div>
</div>
</div>
Actually only this last class is being used (CSS in general uses the last-defined class properties, with some exceptions, such as ID selectors, !important declarations, etc.):
div > div:hover {
box-shadow: 0 0 0 80px white;
}
You have three div elements nested. So the second div in the hierarchy obeys the rule (it is a direct descendant of the first div), and the third div in the hierarchy also obeys the rule... It is a direct descendant of the second div in the hierarchy.
Nothing is broken or unexpected about it. The direct descendant operator > is working exactly as it should in your case.
The code is working perfect.In css the last div selector will of div element will be considered.
If you want to access each div either you can use id or you can use parent-child
like:
div:nth-child(2)
{
}
How I can target first child of span after some html tag? My markup is is like this:
<div class="gad-help">
AdWords vodič<br><span>osigurajte najbolje rezultate</span>
</div>
And my CSS:
.gad-help span:nth-child(1) {
font-size: 12px;
text-align: center;
border: 2px solid red;
padding: 0px 10px;
}
But it won't select it when I use <br> tag in front of tag.
I think span:first-of-type or span:nth-of-type(1) are what you're looking for. But note that they both will select the first span child element of their parents. in this case the anchor tag, not the div element.
Therefore if you want to target the first anchor tag, you should do that by .glad-help > a:first-of-type instead (or :first-child in this case).
Demo
css
.gad-help a span:first-of-type {
font-size: 12px;
text-align: center;
border: 2px solid red;
padding: 0px 10px;
color:red;
}
I am trying to apply a css style to the first children of an element. So say I have a div, with two divs, which are the children, and within each child is their own child, which are the grandchildren.
This JSFiddle, I hope is what I've done: http://jsfiddle.net/o8xhba9u/
#parent {
border: 1px solid;
padding: 10px;
}
#child-one {
text-indent: 5px;
padding: 10px;
}
#child-two {
text-indent: 5px;
padding: 10px;
}
#parent * {
border-top: 1px solid red;
}
My goal is to only have the children (child-one and child-two) to only be the ones with the red border-top. The paragraph elements (grandchildren) shouldn't have the red outline. I am trying to accomplish this dynamically, as if I were to have different elements, and add new ones later and have the effect applied without having to edit the css. How can I accomplish that?
You are looking for the direct child combinator, >.
Example Here
#parent > * {
border-top: 1px solid red;
}
I need select some nodes in sequences via CSS. Is basically something like .button:first-of-sequence. Currently it doesn't exists, so I'm searching for an alternative method. See this case:
<div class="paginator-widget">
<div class="page">First</div>
<div class="page">Previous</div>
<div class="separator"></div>
<div class="page">1</div>
<div class="page">2</div>
<div class="page">3</div>
<div class="page">4</div>
<div class="separator"></div>
<div class="page">Next</div>
<div class="page">Last</div>
</div>
It's a paginator, and I need style each group of .page turning the first-of-sequence left border rounded, the middle-sequence (default) without border rounded and the last-of-sequence right border rounded (note that .separator breaks the sequences). Something like:
.page { background-color: black; color: white; }
.page:first-of-sequence { border-radius: 5px 0 0 5px; }
.page:last-of-sequence { border-radius: 0 5px 5px 0; }
Is it possible with pure CSS, or I need specify a new class to match this specific elements? (like this example)
Nothing like that exists, but you can use containing elements to achieve the same result.
HTML:
<div class="paginator-widget">
<section>
<div class="page">First</div>
<div class="page">Previous</div>
</section>
<section>
<div class="page">1</div>
<div class="page">2</div>
<div class="page">3</div>
<div class="page">4</div>
</section>
<section>
<div class="page">Next</div>
<div class="page">Last</div>
</section>
</div>
CSS:
.page { background-color: black; color: white; }
section div:first-child { border-radius: 5px 0 0 5px; }
section div:last-child { border-radius: 0 5px 5px 0; }
jsFiddle Link
You can use sibling combinators to simulate :first-of-sequence, but not :last-of-sequence.
For example, even if the only elements in your parent element were .page and .separator, you could match .page:first-of-sequence using .page:first-child, .separator + .page, but you wouldn't be able to select .page elements directly preceding a .separator. That's because CSS doesn't provide a previous sibling selector.
This is as far as you could go in replicating those selectors with pure CSS:
.page { background-color: black; color: white; }
.page:first-child, .separator + .page { border-radius: 5px 0 0 5px; }
.page:last-child { border-radius: 0 5px 5px 0; }
You can see in this example that the page 4 link doesn't have rounded corners like it's supposed to.
However, in your specific case, if you can rely on the first, second, second last and last elements being your first-previous and next-last pagination links, you could simply use combinations of :nth-child() and :nth-last-child():
.page { background-color: black; color: white; }
/* [First], [Next], [1] */
.page:first-child, .page:nth-last-child(2), .page:nth-child(4) { border-radius: 5px 0 0 5px; }
/* [Last], [Previous], [4] (or whatever ends up being the last page number) */
.page:last-child, .page:nth-child(2), .page:nth-last-child(4) { border-radius: 0 5px 5px 0; }
Notes:
:nth-child(3) and :nth-last-child(3) are .separator elements, so we skip those and count to 4 instead.
I think Chrome has a bug with :nth-last-child() which may force you to have to use :nth-last-of-type() instead, but I don't know if that's been fixed yet.
If all of this is too complex, the simplest alternative would be to group your end links (first-previous, next-last) and your page number links into their own parent elements separately if possible, which makes it easy for you to just target .page:first-child and .page:last-child, as Lochlan's answer shows.
I think you can use a pseudo selector available in css3 for first and last elements.
You can do something like this:
.page:first-child {
border-radius: 5px 0 0 5px;
}
.page:last-child {
border-radius: 0 5px 5px 0;
}
And the nth-child pseudo selector for select elements using the index.
for example:
.page:nth-child(2){
/* your custom style for second element */
}
Keep in mind, all css3 pseudo selectors don't work in old browsers, if you want compatibility, you should use classes for your particular elements.
you can do this by :first-child, :last-child, :nth-child
.paginator-widget div.page:first-child,
.paginator-widget div.page:nth-child(4),
.paginator-widget div.page:nth-child(9){
padding-left: 15px;
border-radius: 10px 0 0 10px;
}
.paginator-widget div.page:last-child,
.paginator-widget div.page:nth-child(2),
.paginator-widget div.page:nth-child(7){
border-radius: 0 10px 10px 0;
padding-right: 15px;
}
updated jsFiddle File
Use like this
.page:first-child { border-radius: 5px 0 0 5px; }
.page:last-child { border-radius: 0 5px 5px 0; }
and verify it with compatibility chart.
:first-child is supported IE9 properly, and IE7 and IE8 sort of (see chart).
:last-child is supported by IE9+ only.
Both of them are supported well by the good browsers.
You can also use nth-child(even) AND nth-child(odd)
What is so annoying about CSS when you add style in the css class, it may apply other element/class by itself.
What the best way to prevent that?
For example:
HTML:
<div class='main-content'>
<p> Hello World </p>
<span> Test One </span>
<div class='column'>
<span> Test Two</span>
</div>
</div>
CSS:
.main-content span {
background: #921192;
color: white;
padding: 3px 4px;
margin: 0 5px;
}
.column span {
font-size:20px;
text-transform:none;
display:inline-block;
}
I do not want "Test Two" <span> to have a background color.
Test: http://jsfiddle.net/szm9c/1/
Use a selector that actually selects the elements you want. In this case >, the child selector, will suffice.
.main-content > span {
background: #921192;
color: white;
padding: 3px 4px;
margin: 0 5px;
}
http://jsfiddle.net/mattball/mQFz2/
Use .main-content > span, that selects only directly descendent elements.
This has nothing to do with inheritance.
To use CSS properly, assign properties to elements using selectors that match only the elements that you wish to affect. (The example given is far too artificial for a useful analysis and for constructive suggestions on a better approach.)
You can use this
.main-content > span {
background: #921192;
color: white;
padding: 3px 4px;
margin: 0 5px;
}
If you use like .main-content > span that style will only affect to the immediate child spans of .main-content class
Just use all: initial at your root element.
.selector
{
all: initial;
}