LoginSignup
1
0

More than 5 years have passed since last update.

hover時にテキストを切り替える方法

Last updated at Posted at 2018-10-25

実装すること

マウスポインタが指定範囲に入った時、つまりhover時に表示を切り替える

方法

buttonの子要素としてspan要素を2つ配置、hover時に2つのspancssを切り替えることによって実装します。

Oct-25-2018 18-21-49.gif

HTML

<button>
  <span>おはよう</span>
  <span>こんばんは</span>
</button>

CSS

button {
  margin: 100px;
  display: inline-flex;
  justify-content: center;
  padding: 1em;
  border: solid 1px black;
}
span:nth-child(1) {
  position: absolute;
  display: none;
}
button:hover span:nth-child(1) {
  display: inline-block;
}
button:hover span:nth-child(2) {
  opacity: 0;
}
1
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
1
0