0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

HTMLでテキストに異なる装飾を重ねたいときに困った話

Posted at

はじめに(困ったこと)

こういうHTMLがあったとして、

<span class="underline">
  undeline
  <span class="bold">bold and underline</span>
  bold
</span>

本当はこのように表示されてほしいのですが、
expected.png

実際には、下記のようになってしまいます。
unexpected.png

末尾の bold は下線なしで太字になってほしいのですが、</span><span class="underline"> の終了なのか、<span class="bold"> の終了なのか判断できないせいか、期待どおりに表示されません。

回避策

下記のように、spanタグだけで区別せず、タグを分けます。

<u class="underline">
  undeline
  <b class="bold">bold and underline</u>
  bold
</b>

すると、下記のように期待どおり表示されます。
expected.png

おわりに

そもそも上記のように開始タグと終了タグがたがい違いになるHTML的にOKなのか?という疑問はありますが、リッチエディタみたいにテキストに装飾を重ねるのは、普通にありえますし、直感的に操作することを考えれば、むしろ自然なことに思えます。
そんなときは、区別したい単位でタグを変えてあげればいいよ、という短い話でした。

ではでは。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?