Inherit text from element

Asked

Viewed 66 times

2

Without using JS I need a simple example of how to use variables in HTML5 elements to present their content.

In the Hover action, the pointed element should be highlighted and pass the text to the element with property "inherits"

<p id="herda">Esta frase muda de acordo com apontar o mouse em cada Item</p>
<ul>
    <li>Item1 Frase 1</li>
    <li>Item2 Outra frase</li>
    <li>Item3 Frase fim</li>
</ul>

I beg for help, because it’s hard

  • Without JS there is no solution currently with CSS to change a previous element.

1 answer

2

The only difference is I put the label after the li because there is no way to use a selector for previous elements, but you can easily play up with CSS.

Important: All elements must be at the same hierarchical level.

.texto1:hover ~ #label-modificado:after {
    content: 'Item1 Frase 1';
}
.texto2:hover ~ #label-modificado:after {
    content: 'Item2 Outra frase';
}
.texto3:hover ~ #label-modificado:after {
    content: 'Item3 Frase fim'
}
#label-modificado:after {
    content: 'Esta frase muda de acordo com apontar o mouse em cada Item'
}
<li class="texto1">Item1 Frase 1</li>
<li class="texto2">Item2 Outra frase</li>
<li class="texto3">Item3 Frase fim</li>

<label id="label-modificado"></label>

~ is a brother selector, it will fetch all later elements.

+ which is also a brother selector, seeks only the later first.

More information here.

  • Thankful, this very thing you wanted... did not know symbol ~ - what name of the resource? But the problem, now that TAG "ul" disables the effect, but I need to keep it. Where error,would be the hierarchical level.? http://jsfiddle.net/8to46sbk/

  • Exactly, if you remove the ul it works because it will be on the same hierarchical level

  • But then the HTML is invalid, right? Anyway the solution is good, there is a way around it - for example by placing the label inside a <li> and tinker with the style of that item. There the <ul> does not cause problems.

  • I added some information about selectors to the answer, if you want to take a better look.

  • @dandrade How will your final HTML look?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.