CSS selection query for just parent content - html

Say I have the following.
<div class="price">$64 used
<span class="originally">$160 new</span>
<span class="you-save">You save 60%</span>
</div>
I only want to select the "$64 used", not the rest in the child spans. How would I do something like this? I have tried selecting like below and none work.
article > .price
article > .price:not(span)
article > div:not(span)
article > div:not(.originally):not(.you-save)
EDIT: For clarification..
const test = document.querySelector('section > div.price');
console.log(test.innerText);
$64 Used$160 New
You save 60%
I only want $64 Used. Is this even possible? I did not make the site, I am trying to scrape this.

div.price
do the work. For me, better is just use
.price.

Just make a small change:
<div class="price">
<span>$64 used</span>
<span class="originally">$160 new</span>
<span class="you-save">You save 60%</span>
</div>
CSS:
article > .price > span:not([class]) { color: red; }

This is not how tag:not() works. You can exclude a span tag from taking the style for span tags. But you can not select children this way.
Just apply a style to the div and overwride the properties for the children.
.price {
color: blue;
}
span.originally {
color: green;
}
span.you-save {
color: red;
}
<article>
<div class="price">$64 used
<span class="originally">$160 new</span>
<span class="you-save">You save 60%</span>
</div>
</article>

Related

How do you color HTML symbols like x's and checkmarks?

I would like to make the x's red and the checkmarks green, but confused how to since it's a dingbat HTML code.
<li><b>HD</b> Available &#10006</li>
<li><b>Commercial</b> Free &#10006</li>
<li><b>Unlimited</b> Movies/TV Shows &#10004</li>
<li><b>Cancel</b> Anytime &#10004</li>
Simply wrap each in a <span> tag and give it a .check or .cross class. Then it's just a matter of adding the color to each of the classes:
.cross {
color: #ff0000;
}
.check {
color: #00ff00;
}
<li><b>HD</b> Available <span class="cross">&#10006</span></li>
<li><b>Commercial</b> Free <span class="cross">&#10006</span></li>
<li><b>Unlimited</b> Movies/TV Shows <span class="check">&#10004</span></li>
<li><b>Cancel</b> Anytime <span class="check">&#10004</span></li>
Wrap them in a span and then color the span. Use a class name like checkmark and x.
See code snippet below:
.x{
color:red;
}
.checkmark{
color:green;
}
<ul>
<li><b>HD</b> Available <span class="x">&#10006</span></li>
<li><b>Commercial</b> Free <span class="x">&#10006</span></li>
<li><b>Unlimited</b> Movies/TV Shows <span class="checkmark">&#10004</span></li>
<li><b>Cancel</b> Anytime <span class="checkmark">&#10004</span></li>
</ul>
You could put it in a span tag and add a class like
<li><b>HD</b> Available <span class="coloring">&#10006</span></li>
<li><b>Commercial</b> Free <span class="coloring">&#10006</span></li>
then set color in css like
.coloring {
color:red;
}
See sample: https://jsfiddle.net/axz16nqe/1/

Combining CSS Selectors with :first-child

Suppose some HTML contains the following div block:
<div id="messages">
<div>
<span>from user</span>
<span>The content of the message</span>
<time datetime="2017-02-15T19:21:20.848Z">10 hours ago</time>
</div>
</div>
How could I style the "from user" text (and just that text) using CSS selectors? I tried to select the first-childelement of the child div of div #messages using #messages>div:first-child, but that didn't work.
EDIT:
There was a small mistake in my syntax.
The proper CSS selector rule should be #messages>div>:first-child rather than what I tried earlier (#messages>div:first-child). The difference is in the > I missed after div.
You're missing a space. You want the span that's the first child, not the div that's the first child.
Finds the first span:
#messages > div :first-child
Finds the first div:
#messages > div:first-child
Are you sure your div is called messages and not members?
What you're looking for is:
#members > div > span:first-of-type
#members > div > span:first-of-type {
color: #f00;
}
<div id="members">
<div>
<span>from user</span>
<span>The content of the message</span>
<time datetime="2017-02-15T19:21:20.848Z">10 hours ago</time>
</div>
</div>
Hope this helps :)
The first-of-type selector would allow you to do that:
div#members span:first-of-type {
color:#000;
}

css - How do I defind style for this structure

<h3 class="st-module-heading">
<span>
<span>Lastest Products</span>
</span>
</h3>
I want to defind css for span element contain content "Latest Products".
How will do do? Thanks so much.
One option would be to give the span a class:
<h3 class="st-module-heading">
<span>
<span class='myspan'>Lastest Products</span>
</span>
</h3>
Then in CSS, depending on how specific or general you need to be:
.myspan { ... }
/*or*/
span.myspan { ... }
/*or*/
h3.st-module-heading span.myspan { ... }
Without a specific class defined, you would need to do this:
h3.st-module-heading span span { ... }
Which selects the <span> inside the <span> inside <h3 class=st-module-heading>.
But why the extra <span>? In your current code, it is not doing anything. You could just as easily remove it all together unless you are going to need it for something.
Either way, here's a Fiddle to play around with.
the selector should be:
h3.st-module-heading span {
}
html:
<h3 class="st-module-heading">
<span>Lastest Products</span>
</h3>
Assuming that exact structure (the two nested spans), you can use the following css to only select the second nested span:
HTML:
<h3 class="st-module-heading">
<span>
<span>Lastest Products</span>
</span>
</h3>
CSS:
.st-module-heading>span>span {
/* Your css here */
}
The > is the child selector - so .st-module-heading>span>span literally means 'select the span which is directly inside another span, which is directly inside the element with the class st-module-heading'.
You could simply use .st-module-heading span span if need be - but that may not suit if you have additional nested spans.
Link to JS Fiddle.

how to select a class which is children of many elements

<div class="rightsidebox">
<div class="item-info-list">
<p>Model: AIDCU</p>
<div class="product-details">
<p></p>
<div class="price-box"> <span class="regular-price" id="product-price-1617-related">
<span class="price">$8.99</span></span>
</div>
<p></p>
</div>
</div>
I want to make a style for price and make the color green just in a case it is in the rightbox div and I want to use css , I cannot change the structure because it is a theme and it should not have conflict with other prices in other themes
I can use div.rightsidebox>div.item-info-list
but I cannot go further because of the paragraph in there
how can I solve it? I have weakness in using ">" and multiple classes in each other
This I believe is what you are looking for:
div.rightsidebox>div.item-info-list>div.product-details {
background:#ff0000;
}
JSFiddle: http://jsfiddle.net/RF5e7/
If you merely just want to select the price and make it green if it is contained by rightbox:
.rightsidebox .price {
color: green !important;
}
.rightsidebox .price { color: green !important; } // important to override other styles
EDIT: Usage of > - selectorr
The element>element selector is used to select elements with a specific parent. Note: Elements that are not directly a child of the specified parent, are not selected. More info
div.rightsidebox>div.item-info-list .price{
color: green;
}
JSFiddle example.
.rightsidebox .item-info-list p {
/* code */
}
This would go down to the paragraph element inside the classes defined there inside the stylesheet (above off course).
You don't need to be using div.rightsidebox that is required only if you're having class names for multiple elements. Otherwise only .rightsidebox is OK.
You can learn more about the CSS child selectors here: https://developer.mozilla.org/en-US/docs/Web/CSS/Child_selectors

How to access this element with CSS

I have this autogenerated code in HTML.
How can I get the span class icon with CSS to modify it when hover.
I can not call .icon because I will change all the icons on the page and I just want to change this one.
<li class="level1 item156">
<a class="carpo1 level1" href="/carpooling" title="carpoT">
<span>
<span class="icon" style="background-image: url('http://thebackpackerweb.estrategic.es/images/EntregaProyecto/Icon_carpooling_h.png');"></span>
carpooling
</span>
</a>
</li>
I would maybe go with
.item156 .icon {
background: red;
}
This will work as you expect :
.item156 .icon:hover {
background: blue;
}
Give this particular span class an id and then change it according to your requirement.