CSS all classes starts with and ignore some child class - html

I have a nested HTML element structure where I need to apply css styles for some condition.
Apply styles to class starts with "a-" in the .a-comp element
Ignore styles to .a-col class and its child elements which have "a-*" class
The code below works for the above scenario. But it doesn't work for other child elements which have "a-*" class.
How can I achieve that?
.a-comp :not(.a-col) [class^="a-"],
.a-comp :not(.a-col) [class*="a-"] {
color: red;
}
<div class="a-comp">
<div class="a-one">
<div class="a-col">
<div class="a-text">
Text1
</div>
</div>
</div>
<div class="a-row">
<div class="a-text">
Text 2
</div>
</div>
</div>
View on JSFiddle

Your selector matches .a-* elements that have an .a-comp ancestor, with some intermediate element that is not .a-col. However, both of your examples match that selector.
The first one has .a-one as a descendant of .a-comp and ancestor of a-text. The second one has .a-row as a descendant of .a-comp and ancestor of a-text.
One solution might be to set the appropriate children of .a-col elements not to be red.
.a-comp [class^="a-"] {
color: red;
}
.a-comp .a-col [class^="a-"] {
color: black;
}
<div class="a-comp">
<div class="a-row">
<div class="a-two">
<div class="a-text">
In a ROW
</div>
</div>
</div>
<div class="a-one">
<div class="a-col">
<div class="a-text">
In a COL
</div>
</div>
</div>
<div class="a-row">
<div class="a-text">
In a ROW
</div>
</div>
<div class="a-one">
<div class="a-something">
<div class="a-text">
In something else
</div>
</div>
</div>
</div>

Related

How can i add CSS on that DIV

I have a below div structure and I want to add css on first .column element, not its sibling
<div class="row" id="team">
<div class="column">
<div class="row">
<div class="column">
A
</div>
<div class="column">
B
</div>
<div class="column">
C
</div>
</div>
</div>
</div>
I want to add CSS only first .column that comes just after #team div. So how can I select a class for that .column not for the inner .column?
You would use the direct descendant / child combinator ">" which in effect says - target the .column class that DIRECTLY descends from the #team parent div.
In the following - I am placing a border around the targetted .column div and not around the nested children .column divs.
and if there are other divs that are siblings of that particvular div - then you could use the :first-child pseudo selector as well..
#team > .column:first-child {...}
which says - target the .column div that is a direct descendant AND the first child of the #team div.
#team > .column {
border: solid 1px red;
}
<div class="row" id="team">
<div class="column">
<div class="row">
<div class="column">
A
</div>
<div class="column">
B
</div>
<div class="column">
C
</div>
</div>
</div>
</div>
The most specific selector in this case is #team>.column, with > between parent and child to make sure the nested divs which also have the .column class are not affected.
#team .column would not work in this case, since it also selects the .column divs which are nested in lower instances.
BTW: You mention "siblings", which is a bit confusing, since there are not any siblings to that element...
#team>.column {
background: yellow;
}
<div class="row" id="team">
<div class="column">
<div class="row">
<div class="column">
A
</div>
<div class="column">
B
</div>
<div class="column">
C
</div>
</div>
</div>
</div>
Ok, so I think you may have confused your HTML 'parent/child' structure.
You could use
#team > .column:first-child {
}
However, I don't know if you are aware that you can add any number of classes to HTML elements. You could have many classes to easily distinguish between your components and to be able to grab hold of them with CSS or JS.
For the sake of ease, you could just add another class to the element you want to add another separate class style, as I have below.
Then you could just add CSS styling for that class.
<div class="row" id="team"> //this is parent
<div class="column main"> // a child that I've added the
// class of .main to
<div class="row"> // a grandchild
<div class="column"> // then great grandchildren
A //these are siblings
</div>
<div class="column"> //these are siblings
B
</div>
<div class="column"> //these are siblings
C
</div>
</div>
</div>
</div>
/*Then you would just add stylings for*/
.main {
}

How to hide element if it contain specific div with a class [duplicate]

This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 3 years ago.
I have this html structure:
<div>
<div class="parent">
<div>
<div class="label">
<div class="xyz"></div>
</div>
</div>
</div>
<div class="parent">
<div>
<div class="label">
</div>
</div>
</div>
</div>
I want to hide the parent if it is contains div with class="xyz"
I tried that:
.parent div.xyz {
display: none;
}
but seems like my selector does not work.
You can Hide the parent element with the following jquery
jQuery('.parent').each(function(){
if (jQuery(this).find('div.xyz').length != 0) {
jQuery(this).closest('.parent').hide();
}
});
Hope it will helpful.
In css, there's no parent element selector. In your case, you can use jquery to achieve the solution.
$('.parent:has(.xyz)').addClass('hide');
.hide {
visibility: hidden;
}
div.xyz .parent {
display: none;
}
<div>
<div class="xyz">
<div class="parent">
<div>
<div class="label">
<div class="xyz"></div>
</div>
</div>
</div>
</div>
<div class="parent">
<div>
<div class="label">
</div>
</div>
</div>
</div>
You can not select an ancestor, just descendants, an example of how you could do is like this:
<div class="xyz">
<div class="parent">
<div class="label">
<div class="xyz"></div>
</div>
</div>
<div class="parent">
<div class="label">
</div>
</div>
</div>
div.xyz .parent {
display: none;
}
in this case you will hide both .parent class
See more about descending selector
to select a parent or element inside another wapper you will need to use javascript

How to hide all elements except a grandchild with a specific class with CSS? [duplicate]

This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 5 years ago.
I'm not sure if this can be done entirely with CSS (imperative), but it's halfway working at the moment. I have this current HTML setup:
<div class="content">
<div>
<div class="image"></div>
</div>
<div class="text"></div>
<div class="text"></div>
<div>
<div class="button"></div>
</div>
<div>
<div class="image"></div>
</div>
</div>
My current CSS hides all of the child elements of ".content" that don't have a class.
.content > *:not([class]):first-child {
display:block;
}
Of the remaining 3 visible class child elements of ".content", I need to hide them all except the first child element that has the grandchild element with the ".image" class. This is the CSS I have, but it's not working:
.content > *:not([class]):not(.image):first-child {
display:block;
}
It's imposible on CSS. You tryed not show parent element by attribute of child. CSS so does not work. But you can small js for this:
document.querySelector(".image").parentNode.style.display = "block";
.content>div {
display: none;
}
<div class="content">
<div>
<div class="image">1</div>
</div>
<div class="text">2</div>
<div class="text">3</div>
<div>
<div class="button">4</div>
</div>
<div>
<div class="image">5</div>
</div>
</div>
Andrey’s answer is good, however if you don’t want to use JS I think you will need to have a class on the intermediary children as well since the entire tree to the element you want must be visible. That is, if any parent of the element you want to show is hidden then the children will be too. Something like this might do:
<div class="content">
<div>
<div class="image"></div>
</div>
<div class="text"></div>
<div class="text"></div>
<div>
<div class="button"></div>
</div>
<div class="visible">
<div class="image"></div>
</div>
</div>
.content > * {
display: none;
}
.content > .visible {
display: block;
}

How can I exclude last two child elements with CSS selector?

I want to draw bottom-border under some div elements inside their parent but except the last two, knowing that number of child element is changeable:
<div id="parent">
<div>
One
</div>
<div>
Two
</div>
<div>
Three
</div>
<div>
Four
</div>
</div>
Is it possible to select all child elements except the last two?
In this case, something like
#parent > div:not(:nth-last-of-type(-n+2)) {
border-bottom: 1px solid red;
}
JSFiddle
:nth-last-child should do the job (combined with :not).
#parent >:not(:nth-last-child(-n+2)) {
border-bottom: solid black 1px;
}
<div id="parent">
<div>
One
</div>
<div>
Two
</div>
<div>
Three
</div>
<div>
Four
</div>
<div>
Five
</div>
<div>
Six
</div>
<div>
Seven
</div>
</div>

CSS Selector nth-child() for different colors

I have a parent DIV with 5 Child DIVs, each Child DIV has the same class and that appears to be the problem. I am trying to put a different background color for each child DIV? But something seems to be going wrong with the CSS. Thoughts?
CSS
.rsThumbsContainer:nth-child(2){
background: rgb(184,84,84);
}
HTML
<div class="rsThumbsContainer">
<div class="rsNavItem rsThumb">
<div class="rsTmb">Frugobee video</div>
</div>
<div class="rsNavItem rsThumb">
<div class="rsTmb">Post a job</div>
</div>
<div class="rsNavItem rsThumb">
<div class="rsTmb">Get a quote</div>
</div>
<div class="rsNavItem rsThumb">
<div class="rsTmb">Make a hire</div>
</div>
<div class="rsNavItem rsThumb">
<div class="rsTmb">Pay with ease</div>
</div>
</div>
Output:
I know it can be done using ID's and JQuery.
See example.
You need set :nth-child on
<div class="rsNavItem rsThumb">
JSFIDDLE DEMO
You need to apply nth-child to the child selector, not the parent:
.rsNavItem:nth-child(2) {
background: rgb(184,84,84);
}