css

【CSS】背景ハイライト

ホバーすると背景がハイライトされるギミックを作成します。

実装

HTML

<a href="#" class="sample">ホーム</a>

CSS

.sample {
  color: #000000;
  font-size: 1rem;
  text-decoration: none;
  padding: 10px;
  display: inline-block;
  position: relative;
  border: 1px solid #296971;
  transition: 0.2s ease; /* 文字色が変わるスピード */
}

.sample::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 0;
  height: 100%;
  background-color: #296971;
  transition: 0.2s ease; /* 伸びるスピード */
  z-index: -1; /* テキストの後ろに配置 */
}

.sample:hover::before {
  width: 100%;
}

.sample:hover {
  color: #ffffff;
}

結果

ホーム

理解を深める為に、以下を参考して下さい。

応用パターン

パターン1

HTML

<a href="#" class="btn btn-1">ホーム</a>

CSS

a.btn {
  text-decoration: none;
  line-height: 80px;
  color: black;
  text-align: center;
}

.btn {
  position: relative;
  display: block;
  overflow: hidden;
  width: 100%;
  height: 80px;
  max-width: 250px;
  text-transform: uppercase;
  border: 1px solid #296971;
}

.btn-1 {
  color: #296971;
  transition: color 0.5s ease-in-out;
}

.btn-1:before {
  content: "";
  position: absolute;
  top: 0;
  right: -50px;
  bottom: 0;
  left: 0;
  border-right: 50px solid transparent;
  border-bottom: 80px solid #296971;
  transform: translateX(-100%);
  transition: 0.5s ease-in-out;
  z-index: -1;
}

.btn-1:hover {
  color: #d0ebee;
}

.btn-1:hover:before {
  transform: translateX(0);
}

結果

ホーム

最後に

特にありません。

© DeNnie.Lab All Rights Reserved.