Utilize the 'content' attribute to acquire the value of the node

Is it possible to pass the value of a node into the content attribute?

For example, if the value of an <h2/> tag is "Random title", can I use this value inside the content attribute in CSS?

I have attempted using content: inherit;, but it does not appear to be effective.

Here is an illustration of my concept:

HTML:

<h2>Random title</h2>

CSS:

h2:after {
    content: inherit; /* should display as "Random title" */
}

Thank you in advance!

Answer №1

Unfortunately, it is not possible to achieve that using the inherit property.

A workaround would be to use the following code:

<h2 data-title="Random title">Random title</h2>

h2:after {
    content: attr(data-title);
}

However, this method involves duplication and is not ideal.

Answer №2

CSS3 introduces the content: contents; property, but its utility is questionable and it may not be widely implemented yet. More information can be found at http://dev.w3.org/csswg/css3-content/#contents0

For now, @thirtydot's answer remains valid.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for personalizing the color scheme of Material UI Stepper Step?

I need help customizing the disabled Step color for Material UI Steppers. The default colors are Blue for enabled steps and Grey for disabled steps, but I want to change it to a different color as shown in this example: https://i.stack.imgur.com/HGGxp.png ...

The alignment of the Bootstrap 4 row is off-center

My bootstrap structure includes a row with 4 columns, initially set up with 3 columns containing icons that aligned perfectly. Upon adding a fourth column, the new addition appears slightly lower on the page compared to the rest, resulting in misalignment. ...

Scroll down the box with the page

I need my box to scroll down with the page, but the script I'm using doesn't seem to be working. Here is my code: CSS /* Print and Download Buttons */ #box {top: 271px;} a.print{ background:#ffffff url(images/bg-print.jpg) no-repeat top center; ...

The CSS property "text-decoration: none" doesn't appear to be functioning as expected

I have looked through many questions on StackOverflow, but I haven't been able to find a solution to this specific issue. Can someone help me understand why the code below is not functioning as expected? <pre style="text-decoration: underline;"& ...

The classes from TailwindCSS being passed as props in Next.js are not displaying properly when used within a map

const DummyData = [ { _id: 1, title: "lorem ipsum", desc: " ", cta: "Explore Service", ctaUrl: "/", theme: "#E4F8ED", btnTheme: "#4F9D73", links: [ { ...

display the information stored within the sports attribute using React

I am attempting to display the values stored in the sports property. So, I inserted a console log within the sports property. However, an error is being thrown: Syntax error: C:/codebase/src/containers/sports.js: Unexpected token, expec ...

Having trouble with CSS Locator identifying an element in your Protractor Test?

In the Protractor test snippet below, I am attempting to calculate the quantity of div elements using the CSS locator: let list = element.all(by.css('div[class="ag-header-cell ag-header-cell-sortable"]')); expect(list.count()).toBe(11); ...

Does the 'span' element negatively impact the vertical alignment of text?

While working on my code, I noticed an issue with vertical alignment when using span tags to change the background of the text based on its content. The problem occurs specifically when viewing the code in the Android Chrome browser. You can view the initi ...

What is the best way to split a Bootstrap row into five equal-sized columns?

Trying to divide a Bootstrap row into five columns can be tricky when you only have 12 units available on the device. How can this division be achieved successfully? ...

Stop flexbox list items from altering the width of their parent container when the children are hovered over

In my project, I am dealing with a series of list items where some have child lists containing sub navigation links. To create a hover effect using CSS, I set display: none on the child <ul> and then change it to display: block when hovering over the ...

Stable navigation bar implemented with a grid design

I'm struggling with creating Navbars in Grid Layout. https://codepen.io/Aeshtray/pen/BdqeZL For mobile view, I want the Navbar to be fixed horizontally (as currently coded), but once reaching a width of 500px, I need it to become vertically fixed on ...

Adding and removing attributes with Jquery upon clicking a button

How can I make my listed items add an ID when clicked, or what am I doing incorrectly? $('.ex-menuLi #tt').attr('id', 'test'); $('.ex-menuLi').on('click', function(){ $(this).attr('id', &apos ...

Why does the JavaScript background color change take precedence over CSS selectors for my object?

Can someone explain why my list elements are not turning blue when I hover over them, even with the CSS and JS provided below? li { background:red; } li:hover { background:blue; } Here is the JS I am using: document.getElementsByTagName("li")[0].style.b ...

Modify the color of a set of div elements when clicked

Is there a way to change the background color of all divs with a common attribute value (#id for example) after clicking on one of the divs that shares the same attribute value? This is what I have tried so far: $('.group').click(function(){ ...

Tips for adjusting the button spacing on a bootstrap navbar

As someone who doesn't work on front-end tasks frequently, I have encountered an issue while creating a navbar using Bootstrap. The problem arises when trying to add multiple buttons as it causes the spacing between them to appear strange. Does anyon ...

``Stylishly Designed CSS Tabs inspired by Google Chrome's Modern

Utilizing Material UI Tabs in React to create curved tabs resembling those seen in Google Chrome. The design requires the parent element to have a bottom border that extends across the entire row, appearing on both the tabs and other content on the right. ...

Displaying an element outside with reduced opacity using fabric js and controls placed above overlay

The property controlsAboveOverlay in Fabric.js is a boolean that, when set to true, will display the controls (borders, corners, etc.) of an object above the overlay image. The overlay image is an image that can be placed on top of the canvas. Currently, ...

Leveraging $_POST data in embedded CSS styling

Can I use a PHP variable in inline CSS code? In the CSS section below, I have tried to incorporate a PHP variable: <style> p { text-align: center; } img{ -moz-animation:<?php $_POST["rmp"]; ?>s rotateRight infinite li ...

Change the style of the opacity_OVERRIDE

Looking for some help here. I have a webpage located at . On this page, there is a semi-transparent div with an opacity set to 0.7. I want to change the opacity of the images on this page, but I am having trouble creating a style that will override the exi ...

What is the reason for the lack of an applied CSS selector?

.colored p{ color: red; } article > .colored{ color:powderblue; } .blue{ color: white; } <!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8> <meta name="viewport" content="width=device-width, initi ...