0

On css I want to add a rule for a selfclosed tag like this: <unclear/> (exacly convert content to one symbol)

I already do it to add a different style for simple open-close tag <unclear>lorem ipsum</unclear> (I want to convert open tag <unclear> and closed tag </unclear> to a symbol so enclose a phrase inside symbols)

In css:

unclear:before{ content:'\2020'; }
unclear:after{ content:'\2020'; }

But with this rule for selfclosed tag I need to get one symbol not two.

unclear:before{
 content:'\2020';
}
unclear:after{
 content:'\2020';
}
<h1>Example1</h1>
<div>Somno contentus exiguo, cum id posceret tempus et ratio, perque spatia ita longissima impendio castus, ut nec <unclear/> mare ministro saltem suspicione tenus posset redargui, quod crimen, etiamsi non invenit, malignitas fingit in summarum licentia potestatum.</div>
...
<br />
<br />
<h1>Example2</h1>
<div>Intellectus autem naturae et <unclear>qualitate</unclear> sensus mundi ex omnibus, quae in mundo sensibilia sunt, poterit peruideri.</div>
...
<br />
<br />
<h1>Example3</h1>
<div>On my website this is display two cruces before finiruris, but I want to display one cruces beforefiniruris, how to do it?
Ager est <unclear/>finiruris <gap/> non praetermittimus nomina consent<supplied>i</supplied>entia condicionibus possessionum.</div>
steplab
  • 11
  • 6
  • 2
    It's quite unclear what you're asking. Perhaps posting some HTML would help. – Mitya Jul 24 '17 at 10:03
  • Welcome to StackOverflow. Please take the tour have a look around, and read through the HELP center(https://stackoverflow.com/help), then read How to Ask Question(https://stackoverflow.com/help/asking), What types of questions should I avoid asking? and provide a MCVE : Minimal, Complete, and Verifiable Example. If people around can easily read and understand what you mean, or what the problem is, they'll be more likely willing to help :) – Naeem Ul Wahhab Jul 24 '17 at 10:05

1 Answers1

1

CSS does not differentiate between self-closing and not-self-closing tags. So if you have <unclear></unclear> and then <unclear/> later on, that's the same to CSS.

In fact, even the XML parser doesn't differentiate: when building the document tree, the node in memory for <unclear></unclear> looks exactly the same as it does for <unclear/>.

So that's the answer: it's impossible; find another way.

One partial solution is to use the :empty pseudo class. You can use

unclear:before{ content:'\2020'; }
unclear:not(:empty):after{ content:'\2020'; }

to assign the after content only to the <unclear> that contains something (in your particular example, the <unclear>lorem ipsum</unclear> one).
<unclear>s which do not contain anything (here, the one that's written <unclear/>) only get the before content.

Would that help?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • I used exactly your code, and it resolved the issue. Many thanks Mr Lister! – steplab Jul 24 '17 at 10:38
  • I know you are talking about CSS but one needs to be careful cause in his example he uses `
    ` which is not in any HTML specification. HTML parsers are instructed to ignore the slash and it does nothing. I'm only bringing this up to avoid confusion for some who may read this.
    – Rob Jul 24 '17 at 10:44
  • 1
    @Rob:
    is in HTML5, by virtue of /> being [specced to mean the same thing as > for void elements, and to mark a foreign element as self-closing a la XHTML](https://www.w3.org/TR/html/syntax.html#start-tags). It does mean nothing in the case of other elements, though, which can indeed lead to unexpected results, see https://stackoverflow.com/questions/5455768/why-do-browsers-think-this-div-tag-isnt-immediately-ended/5455770#5455770
    – BoltClock Jul 24 '17 at 10:47
  • @BoltClock Yes, you can do that, but [it is unspecified for the element itself](https://html.spec.whatwg.org/dev/text-level-semantics.html#the-br-element), it means nothing, it does nothing, and browsers are instructed to ignore it, which makes it useless and pointless. – Rob Jul 24 '17 at 10:58
  • @Rob: Saying /> is useless and pointless in the HTML syntax is one thing (that I happen to agree with). Saying it is not in the spec is another. I don't understand why it has to be specified "for the element itself", when br is listed as one of the void elements, and as I've cited it *is* specified - for void elements in general (also in the [living spec](https://html.spec.whatwg.org/dev/syntax.html#start-tags), which I'm happy to proceed with). – BoltClock Jul 24 '17 at 11:10
  • @BoltClock It is not specified for the element/tag directly which implies, to me, that it isn't important or needed. That void elements used like this are ignored indicates, again to me, that it may be removed from the specification altogether. My real point is, one shouldn't be using the slash because the specification does not call for its usage at all. Void elements, if anything, are weird exceptions to the rule but some people use them anyway. – Rob Jul 24 '17 at 11:14
  • @Rob The original question (which I posted the answer to) didn't contain any HTML at all, nor the [tei] tag, so my answer assumes plain XML. And in XML, the slash does matter, just as it does in HTML served up with an XHTML file type. – Mr Lister Jul 24 '17 at 11:50
  • I didn't notice that tag but I wouldn't have noticed it was XML either. – Rob Jul 24 '17 at 11:52