コピペでできる!cssとhtmlのみで作る擬似要素とSVGを使用した斜め切り替えヘッダー

CSS HTML
 2018.03.28

ブログやランディングページでよく見かける切り替え部分を斜めにしたヘッダーです。
コンテンツとコンテンツの切り替えで使用するとシュッとした感じのサイトになりますね。
毎度毎度SVGを用意して作るのが面倒なので、良く使いそうなパターンをご用意しました。
こちらは擬似要素にSVGを指定するなのでサクッと設定できますね。
レスポンシブにも対応していますが、好みに合わせて微調整した方がいいかもしれません。
*SVGを[background-image:]で使用するにはエンコードが必要です。

browser:  65 11 20 10 

擬似要素とSVGを使用した斜め切り替えヘッダー[12種]

右斜め上にカットされたヘッダー(ヘッダーの背景に画像)

ニャン易度

Header

Content

<header>
<h1>Header</h1>
</header>
<article>
<section>
<h2>Content</h2>
</section>
</article>
header {
	height: 200px;
	background: url(image.jpg) no-repeat top center;
	background-size: cover;
}
section {
	position: relative;
	height: 100px;
	background: #80deea;
}
section::before {
	position: absolute;
	top: -100px;
	display: block;
	width: 100%;
	height: 100px;
	content: '';
	background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100' preserveAspectRatio='none'%3E%3Cpolygon fill='%2380deea' points='0,100 100,100 100,0'%3E%3C/polygon%3E%3C/svg%3E");
	/* ↑[fill='%2380deea']部分がカラー指定[80deea]部分を変更 */
	/* svgファイルを外部読み込みする場合は下記を[ファイル名.svg]として保存してurl(ファイル名.svg)で指定 */
	/* <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="none">
	<polygon fill="#80deea" points="0,100 100,100 100,0"></polygon>
	</svg>*/
}
h1, h2 {
	font-size: 2em;
	margin: 0;
	padding: 1em 0;
	text-align: center;
	color: #ffffff;
}
h2 {
	padding: 0 0 1em;
}

右斜め上にカットされたヘッダー(コンテンツの背景に画像)

ニャン易度

Header

Content

<header>
<h1>Header</h1>
</header>
<article>
<section>
<h2>Content</h2>
</section>
</article>
header {
	position: relative;
	height: 100px;
	background: #80deea;
}
header::after {
	position: absolute;
	bottom: -100px;
	display: block;
	width: 100%;
	height: 100px;
	content: '';
	background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100' preserveAspectRatio='none'%3E%3Cpolygon fill='%2380deea' points='0,0 0,100 100,0'%3E%3C/polygon%3E%3C/svg%3E");
	/* ↑[fill='%2380deea']部分がカラー指定[80deea]部分を変更 */
	/* svgファイルを外部読み込みする場合は下記を[ファイル名.svg]として保存してurl(ファイル名.svg)で指定 */
	/* <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="none">
	<polygon fill="#80deea" points="0,0 0,100 100,0"></polygon>
	</svg>*/
}
section {
	height: 200px;
	background: url(image.jpg) no-repeat top center;
	background-size:cover;
}
h1,h2 {
	font-size: 2em;
	margin: 0;
	padding: 1em 0;
	text-align: center;
	color: #ffffff;
}
h2 {
	padding: 3em 0;
}

左斜め上にカットされたヘッダー(ヘッダーの背景に画像)

ニャン易度

Header

Content

<header>
<h1>Header</h1>
</header>
<article>
<section>
<h2>Content</h2>
</section>
</article>
header {
	height: 200px;
	background: url(image.jpg) no-repeat top center;
	background-size:cover;
}
section {
	position: relative;
	height: 100px;
	background: #80deea;
}
section::before {
	position: absolute;
	top: -100px;
	display: block;
	width: 100%;
	height: 100px;
	content: '';
	background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100' preserveAspectRatio='none'%3E%3Cpolygon fill='%2380deea' points='0,0 100,100 0,100'%3E%3C/polygon%3E%3C/svg%3E");
	/* ↑[fill='%2380deea']部分がカラー指定[80deea]部分を変更 */
	/* svgファイルを外部読み込みする場合は下記を[ファイル名.svg]として保存してurl(ファイル名.svg)で指定 */
	/* <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="none">
	<polygon fill="#80deea" points="0,0 100,100 0,100"></polygon>
	</svg>*/
}
h1,h2 {
	font-size: 2em;
	margin: 0;
	padding: 1em 0;
	text-align: center;
	color: #ffffff;
}
h2 {
	padding: 0 0 3em 0;
}

左斜め上にカットされたヘッダー(コンテンツの背景に画像)

ニャン易度

Header

Content

<header>
<h1>Header</h1>
</header>
<article>
<section>
<h2>Content</h2>
</section>
</article>
header {
	position: relative;
	height: 100px;
	background: #80deea;
}
header::after {
	position: absolute;
	bottom: -100px;
	display: block;
	width: 100%;
	height: 100px;
	content: '';
	background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100' preserveAspectRatio='none'%3E%3Cpolygon fill='%2380deea' points='0,0 100,100 100,0'%3E%3C/polygon%3E%3C/svg%3E");
	/* ↑[fill='%2380deea']部分がカラー指定[80deea]部分を変更 */
	/* svgファイルを外部読み込みする場合は下記を[ファイル名.svg]として保存してurl(ファイル名.svg)で指定 */
	/* <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="none">
	<polygon fill="#80deea" points="0,0 100,100 100,0"></polygon>
	</svg>*/
}
section {
	height: 200px;
	background: url(image.jpg) no-repeat top center;
	background-size: cover;
}
h1, h2 {
	font-size: 2em;
	margin: 0;
	padding: 1em 0;
	text-align: center;
	color: #ffffff;
}
h2 {
	padding: 3em 0;
}

Vの字にカットされたヘッダー(ヘッダーの背景に画像)

ニャン易度

Header

Content

<header>
<h1>Header</h1>
</header>
<article>
<section>
<h2>Content</h2>
</section>
</article>
header {
	position: relative;
	height: 150px;
	background: url(image.jpg) no-repeat top center;
	background-size:cover;
}
header::after {
	position: absolute;
	content: '';
	bottom: -50px;
	display: block;
	width: 100%;
	height: 100px;
	background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100' preserveAspectRatio='none'%3E%3Cpolygon fill='%2380deea' points='100,50 0,50 0,0 50,50 100,0 '/%3E%3C/svg%3E");
	/* ↑[fill='%2380deea']部分がカラー指定[80deea]部分を変更 */
	/* svgファイルを外部読み込みする場合は下記を[ファイル名.svg]として保存してurl(ファイル名.svg)で指定 */
	/* <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="none">
	<polygon fill="#80deea" points="100,50 0,50 0,0 50,50 100,0 "/>
	</svg> */
}
section {
	height: 150px;
	background: #80deea;
}
h1,h2 {
	font-size: 2em;
	margin: 0;
	padding: 1em 0;
	text-align: center;
	color: #ffffff;
}

Vの字にカットされたヘッダー(コンテンツの背景に画像)

ニャン易度

Header

Content

<header>
<h1>Header</h1>
</header>
<article>
<section>
<h2>Content</h2>
</section>
</article>
header {
	position: relative;
	height: 100px;
	background: #80deea;
}
header::after {
	position: absolute;
	content: '';
	bottom: -100px;
	display: block;
	width: 100%;
	height: 100px;
	background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100' preserveAspectRatio='none'%3E%3Cpolygon fill='%2380deea' points='0,0 50,50 100,0'%3E%3C/polygon%3E%3C/svg%3E");
	/* ↑[fill='%2380deea']部分がカラー指定[80deea]部分を変更 */
	/* svgファイルを外部読み込みする場合は下記を[ファイル名.svg]として保存してurl(ファイル名.svg)で指定 */
	/* <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="none">
	<polygon fill="#80deea" points="0,0 50,50 100,0"></polygon>
	</svg> */
}
section {
	height: 200px;
	background: url(image.jpg) no-repeat top center;
	background-size:cover;
}
h1,h2 {
	font-size: 2em;
	margin: 0;
	padding: 1em 0;
	text-align: center;
	color: #ffffff;
}
h2 {
	padding: 3em 0;
}

逆Vの字にカットされたヘッダー(ヘッダーの背景に画像)

ニャン易度

Header

Content

<header>
<h1>Header</h1>
</header>
<article>
<section>
<h2>Content</h2>
</section>
</article>
header {
	position: relative;
	height: 150px;
	background: url(image.jpg) no-repeat top center;
	background-size:cover;
}
header::after {
	position: absolute;
	content: '';
	bottom: -50px;
	display: block;
	width: 100%;
	height: 100px;
	background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100' preserveAspectRatio='none'%3E%3Cpolygon fill='%2380deea' points='100,50 0,50 0,0 50,50 100,0 '/%3E%3C/svg%3E");
	/* ↑[fill='%2380deea']部分がカラー指定[80deea]部分を変更 */
	/* svgファイルを外部読み込みする場合は下記を[ファイル名.svg]として保存してurl(ファイル名.svg)で指定 */
	/* <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="none">
	<polygon fill="#80deea" points="100,50 0,50 0,0 50,50 100,0 "/>
	</svg> */
}
section {
	height: 150px;
	background: #80deea;
}
h1,h2 {
	font-size: 2em;
	margin: 0;
	padding: 1em 0;
	text-align: center;
	color: #ffffff;
}

逆Vの字にカットされたヘッダー(コンテンツの背景に画像)

ニャン易度

Header

Content

<header>
<h1>Header</h1>
</header>
<article>
<section>
<h2>Content</h2>
</section>
</article>
header {
	position: relative;
	height: 150px;
	background: #80deea;
}
header::after {
	position: absolute;
	content: '';
	bottom: -100px;
	display: block;
	width: 100%;
	height: 100px;
	background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100' preserveAspectRatio='none'%3E%3Cpolygon fill='%2380deea' points='100,50 50,0 0,50 0,0 100,0 '/%3E%3C/svg%3E");
	/* ↑[fill='%2380deea']部分がカラー指定[80deea]部分を変更 */
	/* svgファイルを外部読み込みする場合は下記を[ファイル名.svg]として保存してurl(ファイル名.svg)で指定 */
	/* <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="none">
	<polygon fill="#80deea" points="100,50 50,0 0,50 0,0 100,0 "/>
	</svg> */
}
section {
	height: 150px;
	background: url(image.jpg) no-repeat top center;
	background-size:cover;
}
h1,h2 {
	font-size: 2em;
	margin: 0;
	padding: 1em 0;
	text-align: center;
	color: #ffffff;
}

上向きでラウンドにカットされたヘッダー(ヘッダーの背景に画像)

ニャン易度

Header

Content

<header>
<h1>Header</h1>
</header>
<article>
<section>
<h2>Content</h2>
</section>
</article>
header {
	height: 150px;
	background: url(image.jpg) no-repeat top center;
	background-size:cover;
}
section {
	position: relative;
	background: #80deea;
}
section::before {
	position: absolute;
	top: -50px;
	display: block;
	width: 100%;
	height: 100px;
	content: '';
	background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100' preserveAspectRatio='none'%3E%3Cpath fill='%2380deea' d='M0,50 Q50,0 100,50'%3E%3C/path%3E%3C/svg%3E");
	/* ↑[fill='%2380deea']部分がカラー指定[80deea]部分を変更 */
	/* svgファイルを外部読み込みする場合は下記を[ファイル名.svg]として保存してurl(ファイル名.svg)で指定 */
	/* <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="none">
	<path fill="#80deea" d="M0,50 Q50,0 100,50"></path>
	</svg> */
}
h1,h2 {
	font-size: 2em;
	margin: 0;
	padding: 1em 0;
	text-align: center;
	color: #ffffff;
}
h2 {
	padding: 2em 0;
}

上向きでラウンドにカットされたヘッダー(コンテンツの背景に画像)

ニャン易度

Header

Content

<header>
<h1>Header</h1>
</header>
<article>
<section>
<h2>Content</h2>
</section>
</article>
header {
	height: 150px;
	background: #80deea;
}
section {
	position: relative;
	height: 150px;
	background: url(image.jpg) no-repeat top center;
	background-size:cover;
}
section::before {
	position: absolute;
	top: 0px;
	display: block;
	width: 100%;
	height: 100px;
	content: '';
	background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100' preserveAspectRatio='none'%3E%3Cpath fill='%2380deea' d='M100 0H0v25C33.333-8.333 66.666-8.333 100 25V0z'/%3E%3C/svg%3E");
	/* ↑[fill='%2380deea']部分がカラー指定[80deea]部分を変更 */
	/* svgファイルを外部読み込みする場合は下記を[ファイル名.svg]として保存してurl(ファイル名.svg)で指定 */
	/* <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="none">
	<path fill="#80deea" d="M100 0H0v25C33.333-8.333 66.666-8.333 100 25V0z"/>
	</svg> */
}
h1,h2 {
	font-size: 2em;
	margin: 0;
	padding: 1em 0;
	text-align: center;
	color: #ffffff;
}
h2 {
	padding: 2em 0;
}

下向きでラウンドにカットされたヘッダー(ヘッダーの背景に画像)

ニャン易度

Header

Content

<header>
<h1>Header</h1>
</header>
<article>
<section>
<h2>Content</h2>
</section>
</article>
header {
	position: relative;
	height: 150px;
	background: url(img/sample.jpg) no-repeat top center;
	background-size:cover;
}
header::after {
	position: absolute;
	content: '';
	bottom: -75px;
	display: block;
	width: 100%;
	height: 100px;
	background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100' preserveAspectRatio='none'%3E%3Cpath fill='%2380deea' d='M100,25H0V0h0c33.333,33.333,66.666,33.333,100,0l0,0V25z'%3E%3C/path%3E%3C/svg%3E");
	/* ↑[fill='%2380deea']部分がカラー指定[80deea]部分を変更 */
	/* svgファイルを外部読み込みする場合は下記を[ファイル名.svg]として保存してurl(ファイル名.svg)で指定 */
	/* <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="none">
	<path fill="#80deea" d="M100,25H0V0h0c33.333,33.333,66.666,33.333,100,0l0,0V25z"></path>
	</svg> */
}
section {
	height: 150px;
	background: #80deea;
}
h1,h2 {
	font-size: 2em;
	margin: 0;
	padding: 1em 0;
	text-align: center;
	color: #ffffff;
}
h2 {
	padding: 2em 0;
}

下向きでラウンドにカットされたヘッダー(コンテンツの背景に画像)

ニャン易度

Header

Content

<header>
<h1>Header</h1>
</header>
<article>
<section>
<h2>Content</h2>
</section>
</article>
header {
	position: relative;
	height: 150px;
	background: #80deea;
}
header::after {
	position: absolute;
	content: '';
	bottom: -100px;
	display: block;
	width: 100%;
	height: 100px;
	background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100' preserveAspectRatio='none'%3E%3Cpath fill='%2380deea' d='M0,0 Q50,50 100,0'%3E%3C/path%3E%3C/svg%3E");
	/* ↑[fill='%2380deea']部分がカラー指定[80deea]部分を変更 */
	/* svgファイルを外部読み込みする場合は下記を[ファイル名.svg]として保存してurl(ファイル名.svg)で指定 */
	/* <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="none">
	<path fill="#80deea" d="M0,0 Q50,50 100,0"></path>
	</svg> */
}
section {
	height: 150px;
	background: url(img/sample.jpg) no-repeat top center;
	background-size:cover;
}
h1,h2 {
	font-size: 2em;
	margin: 0;
	padding: 1em 0;
	text-align: center;
	color: #ffffff;
}
h2 {
	padding: 2em 0;
}

SVGをエンコードするのに便利なサイト


[Insert your SVG:]にSVGタグを入れると[Ready for CSS:]に[background-image:]のcssを吐き出してくれます。
プレビューまで出てとっても便利!

*イラストレーターなどで作成したSVGはSVGOMGなどで最適化してからエンコードするとよりシンプルです。
*IEに対応するためには頭の[data:image/svg+xml,]を[data:image/svg+xml;charset=utf8,]に変更する必要があります。

copypet.jp

CSS3などで新たに追加された要素・装飾方法など、日々コードを書いていないと忘れてしまったり、ささっとプロトタイプを作る時などちょっとしたことに時間をかけている暇はない。そんな時に「あ〜、あれストックしときゃよかったなぁ」って困った自分用のストックブログです。カスタマイズなどがしやすいよう、昨今のweb制作に取り入れられる一般的なコードを中心に掲載しています。

More Info

こんな記事はいかがですか?