1. homeブログTOP
  2. [CSS] 途中で色が変わるカラフルなborderの作り方

[CSS] 途中で色が変わるカラフルなborderの作り方

[CSS] 途中で色が変わるカラフルなborderの作り方

1色まで:borderを使う

見出し
<div class="underline">見出し</div>
.underline{
  padding-bottom: 5px;
  font-weight: bold;
  font-size: 16px;
  border-bottom: 3px solid #f98469;/*ピンク*/
}

3色まで:疑似要素「::before」「::after」を使う

疑似要素::before,::afterを使うと、3色までの下線を作ることができます。

  • border-bottom-colorで1色
  • 疑似要素::beforeのbackground-colorで1色
  • 疑似要素::afterのbackground-colorで1色

2色(1:1)

見出し
<div class="underline">見出し</div>
.underline{
  padding-bottom: 5px;
  font-weight: bold;
  font-size: 16px;
  position: relative;
  border-bottom: 3px solid #fbd8d0;/*右:薄ピンク*/
}
.underline::before{
  content: '';
  position: absolute;
  bottom: -3px;
  width: 50%;
  height: 3px;
  background: #f98469;/*左:ピンク*/
}

3色(1:1:1)

見出し
<div class="underline">見出し</div>
.underline{
  padding-bottom: 5px;
  font-weight: bold;
  font-size: 16px;
  position: relative;
  border-bottom: 3px solid #ffd12a;/*真ん中:黄色*/
}
.underline::before{
  content: '';
  position: absolute;
  bottom: -3px;
  width: 33.3%;
  height: 3px;
  background: #f98469;/*左:ピンク*/
}
.underline::after{
  content: '';
  position: absolute;
  right: 0;
  bottom: -3px;
  width: 33.3%;
  height: 3px;
  background: #a4de32;/*右:緑*/
}

2色の場合(片方はテキストの長さ分だけ)

見出し

テキストが長い見出し
<div class="underline">見出し</div>
<div class="underline">テキストが長い見出し</div>
.underline{
  padding-bottom: 8px;
  font-weight: bold;
  font-size: 16px;
  position: relative;
  overflow: hidden;
}
.underline::before{
  content: '';
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 3px;
  background: #f98469;/*ピンク*/
}
.underline::after{
  content: '';
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 3px;
  background: #fbd8d0;/*薄ピンク*/
}

4色以上:linear-gradientを使う

見出し
<div class="underline">見出し</div>
.underline{
  padding-bottom: 5px;
  font-weight: bold;
  font-size: 16px;
  position: relative;
}
.underline::before{
  content: '';
  position: absolute;
  bottom: -3px;
  width: 100%;
  height: 3px;
  background: linear-gradient(
    to right, 
    #f98469 0%, #f98469 25%, /*ピンク*/
    #ffd12a 25%, #ffd12a 50%, /*黄色*/
    #a4de32 50%, #a4de32 75%, /*緑*/
    #91c0f1 75%, #91c0f1 100% /*青*/
  );
}
  1. homeブログTOP
  2. [CSS] 途中で色が変わるカラフルなborderの作り方