ホームページ  >  記事  >  ウェブフロントエンド  >  CSSを使って三角矢印パターンを描画するテクニックを詳しく解説

CSSを使って三角矢印パターンを描画するテクニックを詳しく解説

高洛峰
高洛峰オリジナル
2017-03-08 14:14:451583ブラウズ

最近このウェブサイトを変更したいので、プロンプトボックスを設置したいと考えています。これは非常に簡単ですが、ツールチップに三角形の矢印を表示したいと思います。しかし、写真を使用し、さまざまな色や方向の矢印を無数に用意する必要があることを考えると、ほとんど災害です。幸いなことに、MooTools の中心開発者である Darren Waddell が、CSS で三角形の矢印を描くという素晴らしいテクニックについて教えてくれました。純粋な CSS を使用すると、非常に少ないコードだけで、さまざまなブラウザと互換性のある三角形の矢印を作成できます。

CSS コード

/* create an arrow that points up */
p.arrow-up {   
 width: 0;    
 height: 0;    
 border-left: 5px solid transparent;  /* left arrow slant */
 border-right: 5px solid transparent; /* right arrow slant */
 border-bottom: 5px solid #2f2f2f; /* bottom, add background color here */
 font-size: 0;   
 line-height: 0;   
}   

/* create an arrow that points down */
p.arrow-down {   
 width: 0;    
 height: 0;    
 border-left: 5px solid transparent;   
 border-right: 5px solid transparent;   
 border-top: 5px solid #2f2f2f;   
 font-size: 0;   
 line-height: 0;   
}   

/* create an arrow that points left */
p.arrow-left {   
 width: 0;    
 height: 0;    
 border-bottom: 5px solid transparent;  /* left arrow slant */
 border-top: 5px solid transparent; /* right arrow slant */
 border-right: 5px solid #2f2f2f; /* bottom, add background color here */
 font-size: 0;   
 line-height: 0;   
}   

/* create an arrow that points right */
p.arrow-rightright {   
 width: 0;    
 height: 0;    
 border-bottom: 5px solid transparent;  /* left arrow slant */
 border-top: 5px solid transparent; /* right arrow slant */
 border-left: 5px solid #2f2f2f; /* bottom, add background color here */
 font-size: 0;   
 line-height: 0;   
}

これらの三角形を描く鍵は、矢印の方向の 2 つの辺に太い境界線を持たせることです。矢印の反対側にも同じ太い枠線があり、この側の色が三角形の色になります。境界線が太くなるほど、三角形は大きくなります。このようにして、さまざまな色、サイズ、方向の矢印を描画できます。最も良い点は、この効果を実現するために必要な CSS コードは数行だけであるということです。

CSS 三角形を描画するには :before と :after を使用します

上記の CSS の例では実際のページ要素を使用して描画しますが、この実際の要素には他の用途があり、その上で直接操作できない場合があります。純粋な CSS 三角形は、実際には疑似要素を使用して描画できます。描き方はこちらです:

p.tooltip {   
 /* tooltip content styling in here; nothing to do with arrows */
}   

/* shared with before and after */
p.tooltip:before, p.tooltip:after {   
 content: ' ';   
 height: 0;   
 position: absolute;   
 width: 0;   
 border: 10px solid transparent; /* arrow size */
}   

/* these arrows will point up */

/* top-stacked, smaller arrow */
p.tooltip:before {   
 border-bottom-color: #fff;  /* arrow color */

 /* positioning */
 position: absolute;   
 top: -19px;   
 left: 255px;   
 z-index: 2;   
}   

/* arrow which acts as a background shadow */
p.tooltip:after {   
 border-bottom-color: #333;  /* arrow color */

 /* positioning */
 position: absolute;   
 top: -24px;   
 left: 255px;   
 z-index: 1;   
}

矢印の反対側の枠線の色は三角矢印の色です。この矢印を描画するために :before と :after の両方の疑似要素を使用する必要はありません。1 つで十分です。もう 1 つは、前のものの背景の影または背景のエッジとして使用できます。

このテクノロジーについてもっと早く知っておくべきでした!このシンプルで手間のかからない技術は、今後インターフェースを改善する際に役立つと思います。

以上がこの記事の全内容です。皆さんの学習に役立つことを願っています。また、皆さんも PHP 中国語 Web サイトをサポートしていただければ幸いです。

以上がCSSを使って三角矢印パターンを描画するテクニックを詳しく解説の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。