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 3 years have passed since last update.

a[target="_blank"]::after で付加した画像の位置がおかしい時の対処法

Posted at

焦った話

いつも通り何も考えずに a[target="_blank"]に外部リンクですよー、という意味の画像をつけて確認.....
スクリーンショット 2021-05-03 19.20.30.png
※わかりやすいように、aa::afterそれぞれに色を薄くつけてあります。

謎の空白が空いているではありませんか。しかも、この画像、後続の文にがっつり被ってしまいます。

仕方ない、教えてgoogle先生!ざっと調べました。出てきません。私、焦りました。

原因

デベロッパーツールで一つずつ確認し定期ました。すると、思いもよらないやつが原因でした。そいつは、text-indentだったのです。

HTML
<style>
    p {
      text-indent: 1em;
    }
</style>
<p>
  <a href="https://google.com" target="_blank">Google</a>
</p>

以上のように、ptext-indentを適用して、 pの子要素にaを配置していました。その影響で、aにもtext-indentが継承されてしまい、謎の空白が空いてしまったのですね。

解決

ということで、atext-indentを無効にしてあげればよさそうですね!

HTML
 <style>
    p {
      text-indent: 1em;
    }

    a {
      text-indent: 0;
    }
  </style>

<p>
  <a href="https://google.com" target="_blank">Google</a>
</p>

意図した通りになりました!
スクリーンショット 2021-05-03 20.03.20.png

おわりに

text-indentは継承しないで欲しいですね。気をつけましょう!

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?