search

Home  >  Q&A  >  body text

How to render different text in HTML using embedded classes

<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粉418214279P粉418214279493 days ago492

reply all(1)I'll reply

  • P粉232409069

    P粉2324090692023-09-03 00:33:42

    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.

    reply
    0
  • Cancelreply