Can I use a pseudo-selector to style individual words? - html

If i have something like
<p id="something">My Text Here</p>
Is it possible to use some kind of pseudo-selector in CSS to apply a style only to certain text elements from my text?
Example, i want:
"My" to be color: red;
and "Text" color: blue;

No, this is not possible.
There are pseudo-selectors such as :first-letter and :first-line, but you cannot select invidual words without wrapping them in elements that you can individually style:
<p id="something">
<span class="foo">My</span>
<span class="bar">Text</span>
<span class="baz">Here</span>
</p>
You'd then be able to style them individually:
.foo {
color: #ff0000;
}
.bar {
color: #00ff00;
}
.baz {
color: #0000ff;
}

No. If you want parts of the text to have different colors, you 'll have to wrap them inside separate elements (most likely, ''s). For example:
<p id="something"><span class="red">My</span> <span class="blue">Text</span> Here</p>
And of course this will also need the appropriate CSS for .red and .blue.
The exception to the above are the pseudo-selectors :first-letter and :first-line, but these do not offer any flexibility.

You can use first letter pseudo class for styling first letter
p:first-letter
{
font-size:200%;
color:#8A2BE2;
}
But you cant use pseudo class for 2nd "text" you have to wrap this in a html tag & style it separately.
Not possible with Styling words differently.

Other users already told you that it is not possible natively.
If you use jQuery, as I assume from your tag, you can do a little trickery from JavaScript and "tokenize" the text in the P tag with:
$("p").html($.map($("p").text().split(" ")​,function (i,e) {return "<span id='w"+e+"'>"+i+"</span>";}).join(" "));
This command takes a selector (p in this case, but you can use p#myId and so on) and substitutes every word:
This is a sentence
becomes
<span id='w0'>This</span>
<span id='w1'>is</span>
<span id='w2'>a</span>
<span id='w3'>sentence</span>
So you could use a CSS like:
p span.w2 {color: red;}
to color the third word.

Related

:nth-of-type(3n+1) not working when different sibling present [duplicate]

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?

How can I change this 2 lines of HTML so that i can put it into my CSS?

How can i target the red text in CSS? I need to change the color of all the red text to orange:
<span style="color: #dc143c;"><strong>Red text that must be Orange</strong></span>
I tried with the following, but i don't really know how should I write it to make it work..
span[style="color: #dc143c;"] {
color:#f7941d !important;
}
Thanks for your help!
Place the all red code in <span> tag then in css target the <span> and give them class tag to change it color to red code given below
<span class="red-text">this is red text</span>
css code:
.red-text {color:#f7941d;}
You should remove the style tag from your html completely and use classes instead.
<span class="colored"><strong>colored text</strong></span>
then select the class in your css instead
.colored {
color: orange;
}
Give your Red Text an ID or Class of color-change etc.
<span class="color-change" style="color: #dc143c;"><strong>Red text that must be Orange</strong></span>
now in your css
.color-change {
color:#f7941d !important;
}
One More Thing not use inline css because inline css has maximum priority if you use inline css must use !important keyword to change color.
<span id="change">this is red text</span>
css code:
#change {
color:#f7941d;
}
Try this Hope this help you
Add the class orange at the strong tag:
<span style="color: #dc143c;"><strong class="orange">Red text that must be Orange</strong></span>
In the .css file:
.orange {
color: #f7941d !important;
}
I think it would be easier to use a class or id for the span and target it whenever you want using javascript or jquery. example
Red text must be orange
with jquery you can do this:
$('.OrangeRedSpan').css('color': #FE9A2E);

Is it possible to override <strong> properties within a CSS class only?

I want to create a div that contains three words, and I want one of the words in the div to be emphasized in a different font and size. Is it possible to override the default <strong> in that div's class so that I can just use, for example, hello there <strong> world for the word "world" to be emphasized differently to the other "strong-ed" words that aren't in the div?
You can set styles to strong tags:
strong{
font-weight:normal;
}
Use another selector before strong to apply it to strong tags inside certain tags.
I would recommend changing it from strong to the inline element <span>. This will give you all the control you need.
<div id="myid">hello there <span>world</span></div>
#myid span {properties:values}
Don't forget to ad in which container you are. So you can still use <strong> element elsewhere.
<div id="container">
hello there <strong> world </strong>
</div>
#container strong
{
font-weight: normal;
color: red;
}

Repetitive color pattern in paragraph in CSS

I have a paragraph tag and I want the text in it to have every other letter orange and the rest of the letters dark orange. The lazy code looks like this:
Html
<p>
<span class='orange'>L</span>
<span class='yellow'>o</span>
<span class='orange'>r</span>
<span class='yellow'>e</span>
<span class='orange'>m</span>
</p>
Css
.yellow {
color: darkorange;
}
.orange {
color: orange;
}
Fiddle (Second color turned to blue for noticeability)
How can I make this code look less lazy? I know when you read this question the first time you probably think "Impossible. Can only be done with sorcery.", but I think this should probably be very simple. I'll cope with anything, even a way to make an image repeat across the text.
In CSS you cannot target individual letters of character data content, only elements and a small subset of pseudoelements. You do need markup therefore. You should however never use presentational names in classes, that is the job of CSS. So use the anonymous semanticless inline element <span> without further details, and then just use advanced CSS selectors:
<p>
<span>L</span>
<span>o</span>
<span>r</span>
<span>e</span>
<span>m</span>
</p>
And CSS:
p {
color:orange;
}
p span:nth-child(odd) {
color:yellow;
}
Seen here in action.

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