How to render different text in HTML using embedded classes
P粉418214279
2023-09-02 10:34:50
<p>I want to display different text based on the category/category the code is embedded in, i.e. when the category is .category-rabbitmq. </p>
<p>This changes the background when the category is .category-rabbitmq. </p>
<pre class="brush:php;toolbar:false;"><style>
.category-rabbitmq
{
background-image: url('https://www.nastel.com/wp-content/uploads/2022/04/nastel_navigator_xpress.png') !important;
background-size: cover;
}
</style>
<themainbody>Read more about</themainbody><br></pre>
<p>This always displays a variable. </p>
<pre class="brush:php;toolbar:false;"><style>
themainbody::after {
content: "RabbitMQ";
}
</style>
<themainbody>Read more about</themainbody><br></pre>
<p>However, this does not work when only displaying the variable when setting the category: </p>
<pre class="brush:php;toolbar:false;"><style>
.category-rabbitmq
{
themainbody::after {
content: "RabbitMQ";
}
</style>
<themainbody>Read more about</themainbody><br></pre>
<p>Can you help? </p>
Cannot nest rules in CSS (can in SCSS). There is a first public working draft that allows nesting in CSS, so maybe in the future you will be able to.
So you need to do something like this:
I'm not sure how many levels the
.category-rabbitmq
element has relative tothemainbody
. If you know thatthemainbody
is a direct child of.category-rabbitmq
, then you can be more specific and use the child combinator and optimize:>
See CSS Descendant Combinators and CSS Child Combinators.