Hobbes
Hobbes

Reputation: 2115

CSS: is there a way to insert the content of an attribute as text in my output?

Normally, CSS works by matching element names in the HTML:p.heading1 {} affects all elements of type p with class heading1.

Is there a way to display an object/text that only exists as an attribute?

For example, this is my HTML:

<body var="text I want to insert">
    <div class="titlepage"> 
        <p class="titlepagetext">this should also be displayed</p>

The title page has a number of <p> children. In addition to them, I want to display the content of body/var on the title page.

Upvotes: -1

Views: 787

Answers (3)

Tony Graham
Tony Graham

Reputation: 8068

AH Formatter has an -ah-attr-from() extension function that will let you get the contents of an ancestor's attribute (see https://www.antenna.co.jp/AHF/help/en/ahf-ext.html#attr-from).

You could use -ah-attr-from() in a rule for .titlepagetext::before.

Upvotes: 2

Ram.Deepak
Ram.Deepak

Reputation: 1

When you using css, you can't target parent. There is no way to get parent selector. And content: "" can apply only for pseudo-classes.

Upvotes: -2

Temani Afif
Temani Afif

Reputation: 272582

You can probably consider CSS variables. The custom property will get inherited by all the elements inside the body and you can use pseudo element to display it where you want:

.titlepage:before {
  content:var(--var);
}
<body style="--var:'text I want to insert'">
  <div class="titlepage">
    <p class="titlepagetext">this should also be displayed</p>
  </div>
</body>

Upvotes: 4

Related Questions