How to render different text in HTML using embedded classes
P粉418214279
P粉418214279 2023-09-02 10:34:50
0
1
469
<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>
P粉418214279
P粉418214279

reply all(1)
P粉232409069

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:

<style>
.category-rabbitmq themainbody::after {
  content: " RabbitMQ";
}
</style>

I'm not sure how many levels the .category-rabbitmq element has relative to themainbody. If you know that themainbody is a direct child of .category-rabbitmq, then you can be more specific and use the child combinator and optimize: >

<style>
.category-rabbitmq > themainbody::after {
  content: " RabbitMQ";
}
</style>

See CSS Descendant Combinators and CSS Child Combinators.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template