ブログ

CSS 小ネタ集 Vol.2

2020.05.18 : Yamamoto Naoki
Coding

前回に引き続きCSSの小ネタになります。

文字の装飾で前回三角や矢印について書きましたが、今回はラインに ついて書きます。
ラインは見出しやリンク、文章の区切りなどに使われます。サイトのデザインに合わせて色々とCSSで装飾が可能ですのでホームページに活用ください。

アンダーライン(borderで指定)

リンクなどで標準で使われているアンダーラインですが、ひと手間加えるだけでいろいろな表現ができます。

標準はこちら

<a href=“”>リンク</a>

実際の見え方が以下になります。

リンク

上記はテキストデコレーションで装飾されていますが、これをキャンセルしてborderで表示します。わかりやすいように赤の下線に設定してみます。

a {
	text-decoration: none;
	border-bottom: solid 1px #f00;
}

リンク

通常では先の太さは変えられませんが、以下のようにCSSを指定することで太さやバターンを簡単に変えることができます。

太線

<style>
	text-decoration: none;
	border-bottom: 5px solid #f00;
</style>

リンク

破線

<style>
	text-decoration: none;
	border-bottom: 1px dashed #f00;
</style>

リンク

点線

<style>
	text-decoration: none;
	border-bottom: dotted 1px #f00;
</style>

リンク

二重線

<style>
	text-decoration: none;
	border-bottom: double 3px #f00;
</style>

リンク

疑似要素

疑似要素を利用するとさらに表現が豊かになります。

左右色違いライン

a {
	text-decoration: none;
	position: relative;
}
a::before {
	content: "";
	display: block;
	width: 100%;
	height: 3px;
	background:#f00;
	position: absolute;
	bottom: 0;
	left: 0;
}
a::after {
	content: "";
	display:block;
	width:100%;
	height:1px;
	background: #ccc;
	position:absolute;
	bottom: 4px;
	left: 0;
}

リンク

色付き破線

破線のない部分も色を付けることが可能です。

<style>
a {
	text-decoration: none;
	position: relative;
}
a::before {
	content: "";
	display: block;
	width: 100%;
	border-bottom: dashed 3px #f00;
	background: #000;
	position: absolute;
	top: -4px;
	left: 0;
}
a::after {
	content: "";
	display: block;
	width: 100%;
	border-bottom: dashed 3px #0f0;
	background: #000;
	position: absolute;
	bottom: -4px;
	left: 0;
}
</style>

リンクリンクリンクリンク

左右のライン(flexboxで指定)

flexBoxと疑似要素 を利用すると簡単に左右にラインのある見出しを作ることができます。

<style>
h1 {
	display:flex;
	justify-content: space-between;
	align-items: center;
}
h1::before,
h1::after {
	content: '';
	height: 1px;
 	background-color: #333;
	flex-grow: 1;
}
h1::before {
	margin-right: 1rem;
}
h1::after {
	margin-left: 1rem;
}
</style>
<h1>見出し</h1>

見出し

最後に

いかがだったでしょうか?
CSSを利用することで様々なラインの表現が可能だと理解していただけたかと思います。ここに表示した例はあくまでも一部に過ぎませんので、応用していろいろ試していただければと思います。

CSSをうまく利用してユーザーが見やすいホームページ制作していきましょう!

アーカイブ

新しいサイト制作や、既存サイトの運用に関するお困りごとなどございましたらご依頼、ご相談ください。

ビジネスやサービスに最適なウェブ制作、デザインをおこなっている株式会社ナナサンの会社案内をダウンロードいただけます。

制作依頼・ご相談 資料ダウンロード
イメージ:制作依頼・ご相談
イメージ:制作依頼・ご相談