1

Is it possible to visually display html attributes which normally only reside in the source code, in the output of html, anchored to the element they are grouped with?

For example, here is some css which visually groups some <span></span> tags in rounded rectangles for clarity:

span{
    display:inline-block;
}

span:first-child{
    border:1px solid black;
    border-radius:15px;
    padding:10px;
}
span:last-child{
    border:1px solid black;
    border-radius:15px;
    padding:10px;
}

<span class="complete-sentence">She <span class="verb">loves</span> him a <span>lot</span>.</span>

https://jsfiddle.net/ycpw4tzz/ <-- What happens currently.

Two of the span tags have classes (global attributes) of "subject" and "verb". I want to somehow get the output display to look like this instead of what it does currently.

enter image description here

1
  • sure, I just need a working method I don't care what technology is used to achieve it. Commented Apr 9, 2015 at 5:05

1 Answer 1

3

First, don't try to use something which wasn't intended for that kind of use. Use data-* attributes instead.

<span data-type="complete-sentence">
    She <span data-type="verb">loves</span> him a <span>lot</span>.
</span>

Now, I've targeted the spans, which have data-type and using content attribute with attr, along with pseudo-selector :before (you can use :after too, if you want) applying style to them.

span[data-type]:before {
   content: attr(data-type) " | ";
}

DEMO (Also note that if you're using :after make it content: " | " attr(data-type);)

Also, user @bryc has provided an example of how you had wanted it in your image example. Do take a look at it here

Sign up to request clarification or add additional context in comments.

9 Comments

The visual effect you use is indeed cleaner, but I'm trying to avoid having to hardcode each tag, for example: what if you have <span class="tag1 tag2 tag3 tag4">She</span> and hundreds of other <span>'s with other tags, hardcoding them all is tedious, it's better if it works for any tag without being defined in the css manually
@Gallaxhar I've removed the hard coding part.
jsfiddle.net/xa3apsdc/3 here is what I mean, it breaks if the parent tag isn't complete-sentence, also complete-sentence itself should be before a | but not sure how to do that since it's the identifier here. currently you'd have to make a new css line for each parent tag
@Gallaxhar okay. Now, I have targeted only those which have that attribute without affecting others.
I'm not sure about the data-type attribute either, instead of "class", because it doesn't seem to allow multiple tags like <span class="tag1 tag2">text</span>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.