2016-11-12 4 views
0

このように下線のホバー効果を持たせて、上に置かれたテキストの長さまで伸ばすことができます。私は、すべての長さをカバーする長すぎる下線を置いても、仮に想定どおりに動かすことができない場合、オーバーフローをカバーする疑似セレクタをいくつかの形で挿入しようとしました。 http://codepen.io/anon/pen/jVqqEZCSSのアンダーラインアニメーションをリンクのテキストと同じ長さにする

* { 
    background-color: blue; 
} 

li > a { 
    position: relative; 
    color: white; 
    text-decoration: none; 
} 

li > a:hover { 
    color: white; 
} 

li > a:before { 
    content: ""; 
    position: absolute; 
    width: 70%; 
    height: 1px; 
    bottom: 0; 
    left: -10; 
    background-color: white; 
    visibility: hidden; 
    transform: scaleX(0); 
    transition: all 0.2s ease-in-out 0s; 
} 

li > a:hover:before { 
    visibility: visible; 
    transform: scaleX(1); 
} 
/* 
li > a:after { 
    content: ""; 
    background-color: yellow; 
    height: 10px; 
    width: 100px; 
}*/ 

<ul class="menu"> 
    <li><a href="#about" class="slide-section">About</a></li> 
    <li><a href="#works" class="slide-section">Works and stuff</a></li> 
    <li><a href="#contact" class="slide-section">Contact</a></li> 
</ul> 

答えて

3
li > a:before { 
    width: 70%; /* initial*/ 
    width: 100%; /* changed value*/ 
} 
+0

ありがとう、私はそれを取り入れましょう。ここ

は私のcodepenへのリンクです私の男性であなたがより良いコンテキストのためにコードを投稿する必要があったと思うので、チェックしたら更新します – Smithy

0

* { 
 
    background-color: blue; 
 
} 
 

 
li > a { 
 
    position: relative; 
 
    color: white; 
 
    text-decoration: none; 
 
} 
 

 
li > a:hover { 
 
    color: white; 
 
} 
 

 
li > a:before { 
 
    content: ""; 
 
    position: absolute; 
 
    width: 100%; 
 
    height: 1px; 
 
    bottom: 0; 
 
    left: -10; 
 
    background-color: white; 
 
    visibility: hidden; 
 
    transform: scaleX(0); 
 
    transition: all 0.2s ease-in-out 0s; 
 
} 
 

 
li > a:hover:before { 
 
    visibility: visible; 
 
    transform: scaleX(1); 
 
} 
 
/* 
 
li > a:after { 
 
    content: ""; 
 
    background-color: yellow; 
 
    height: 10px; 
 
    width: 100px; 
 
}*/
<ul class="menu"> 
 
    <li><a href="#about" class="slide-section">About</a></li> 
 
    <li><a href="#works" class="slide-section">Works and stuff</a></li> 
 
    <li><a href="#contact" class="slide-section">Contact</a></li> 
 
</ul>

0

CSSで次のように使用します:

li > a:before { 
... 
width: 100%; // not 70 % 
... 
} 
関連する問題