コピペでできる!cssとhtmlのみで作るクリックすると横から出てくるメニュー

CSS HTML
 2018.04.02
 2018.08.08

クリックすると横から出てくるメニューです。
1個目は、PC/スマホどちらも同じ動きをしていますが、
2,3個目はPCは通常の横並びメニューでスマホでハンバーガーメニューのアイコンが出てクリックするとメニューが横から表示されます。

1個目の左から出てくるバージョンが欲しいとの声がありましたので追記します。

browser:  65 11 20 10 

クリックすると横から出てくるメニュー[4種]

クリックすると右からメニューが出てくる

ニャン易度
<div class="cp_cont">
<div class="cp_offcm01">
<input type="checkbox" id="cp_toggle01">
<label for="cp_toggle01"><span></span></label>
<div class="cp_menu">
<ul>
<li><a href="#">menu1</a></li>
<li><a href="#">menu2</a></li>
<li><a href="#">menu3</a></li>
<li><a href="#">menu4</a></li>
<li><a href="#">menu5</a></li>
</ul>
</div>
</div>
<div class="cp_contents">
<h1>contents</h1>
</div>
</div>
</div>
*, *:before, *:after {
padding: 0;
margin: 0;
box-sizing: border-box;
}
ol, ul {
list-style: none;
}
a {
text-decoration: none;
color: inherit;
}
.cp_cont {
height: 65vh;
}
.cp_offcm01 {
position: absolute;
top: 20px;
right: 20px;
display: inline-block;
}
/* menu */
.cp_offcm01 .cp_menu {
position: fixed;
top: 0;
right: -100vw;
width: 250px;
height: 100%;
cursor: pointer;
-webkit-transition: 0.53s transform;
transition: 0.53s transform;
-webkit-transition-timing-function: cubic-bezier(.38,.52,.23,.99);
transition-timing-function: cubic-bezier(.38,.52,.23,.99);
background-color: #607D8B;
opacity: 0.8;
}
.cp_offcm01 .cp_menu ul {
margin: 0;
padding: 0;
}
.cp_offcm01 .cp_menu li {
list-style: none;
}
.cp_offcm01 .cp_menu li a {
display: block;
padding: 20px;
text-decoration: none;
color: #ffffff;
border-bottom: 1px solid #ffffff;
}
.cp_offcm01 #cp_toggle01 {
position: absolute;
display: none;
opacity: 0;
}
.cp_offcm01 #cp_toggle01:checked ~ .cp_menu {
-webkit-transform: translateX(-100vw);
transform: translateX(-100vw);
}
/* menu toggle */
.cp_offcm01 #cp_toggle01 ~ label {
display: block;
padding: 0.5em;
cursor: pointer;
-webkit-transition: 0.5s transform;
transition: 0.5s transform;
-webkit-transition-timing-function: cubic-bezier(.61,-0.38,.37,1.27);
transition-timing-function: cubic-bezier(.61,-0.38,.37,1.27);
text-align: center;
color: #333333;
}
.cp_offcm01 #cp_toggle01:checked ~ label {
-webkit-transform: translateX(-250px);
transform: translateX(-250px);
}
.cp_offcm01 #cp_toggle01 ~ label::before {
font-family: 'FontAwesome';
content: '\f0c9';
font-size: 2em
}
.cp_offcm01 #cp_toggle01:checked ~ label::before {
content: '\f00d';
}
/* contents */
.cp_contents {
color: #333333;
text-align: center;
}

クリックすると左からメニューが出てくる

ニャン易度
<div class="cp_cont">
<div class="cp_offcm04">
<input type="checkbox" id="cp_toggle04">
<label for="cp_toggle04"><span></span></label>
<div class="cp_menu">
<ul>
<li><a href="#">menu1</a></li>
<li><a href="#">menu2</a></li>
<li><a href="#">menu3</a></li>
<li><a href="#">menu4</a></li>
<li><a href="#">menu5</a></li>
</ul>
</div>
</div>
<div class="cp_contents">
<h1>contents</h1>
</div>
</div>
</div>
*, *:before, *:after {
padding: 0;
margin: 0;
box-sizing: border-box;
}
ol, ul {
list-style: none;
}
a {
text-decoration: none;
color: inherit;
}
.cp_cont {
height: 65vh;
}
.cp_offcm04 {
position: absolute;
top: 20px;
left: 20px;
display: inline-block;
}
/* menu */
.cp_offcm04 .cp_menu {
position: fixed;
top: 0;
left: -100vw;
width: 250px;
height: 100%;
cursor: pointer;
-webkit-transition: 0.53s transform;
transition: 0.53s transform;
-webkit-transition-timing-function: cubic-bezier(.38,.52,.23,.99);
transition-timing-function: cubic-bezier(.38,.52,.23,.99);
background-color: rgba(0,151,167 ,1);
opacity: 0.8;
}
.cp_offcm04 .cp_menu ul {
margin: 0;
padding: 0;
}
.cp_offcm04 .cp_menu li {
list-style: none;
}
.cp_offcm04 .cp_menu li a {
display: block;
padding: 20px;
text-decoration: none;
color: #ffffff;
border-bottom: 1px solid #ffffff;
}
.cp_offcm04 #cp_toggle04 {
position: absolute;
display: none;
opacity: 0;
}
.cp_offcm04 #cp_toggle04:checked ~ .cp_menu {
-webkit-transform: translateX(100vw);
transform: translateX(100vw);
}
/* menu toggle */
.cp_offcm04 #cp_toggle04 ~ label {
display: block;
padding: 0.5em;
cursor: pointer;
-webkit-transition: 0.5s transform;
transition: 0.5s transform;
-webkit-transition-timing-function: cubic-bezier(.61,-0.38,.37,1.27);
transition-timing-function: cubic-bezier(.61,-0.38,.37,1.27);
text-align: center;
color: #333333;
}
.cp_offcm04 #cp_toggle04:checked ~ label {
-webkit-transform: translateX(250px);
transform: translateX(250px);
}
.cp_offcm04 #cp_toggle04 ~ label::before {
font-family: 'FontAwesome';
content: '\f0c9';
font-size: 2em
}
.cp_offcm04 #cp_toggle04:checked ~ label::before {
content: '\f00d';
}
/* contents */
.cp_contents {
color: #333333;
text-align: center;
}

スマホのみクリックするとフェードインでメニューが出てくる

ニャン易度
<div class="cp_cont">
<input type="checkbox" id="cp_toggle02">
<div class="cp_mobilebar">
<label for="cp_toggle02" class="cp_menuicon">
<span></span>
</label>
</div>
<header class="cp_offcm02">
<nav>
<ul>
<li><a href="#">menu1</a></li>
<li><a href="#">menu2</a></li>
<li><a href="#">menu3</a></li>
<li><a href="#">menu4</a></li>
<li><a href="#">menu5</a></li>
</ul>
</nav>
</header>
<div class="cp_container">
<div class="cp_content">
<h1>contents</h1>
</div>
</div>
</div>
*, *:before, *:after {
padding: 0;
margin: 0;
box-sizing: border-box;
}
ol, ul {
list-style: none;
}
a {
text-decoration: none;
color: inherit;
}
.cp_cont {
height: 65vh;
}
/* menu */
.cp_offcm02 {
position: relative;
z-index: 5;
left: 0;
visibility: visible;
overflow: auto;
width: 100%;
height: auto;
padding-top: 0;
text-align: center;
opacity: 1;
}
.cp_offcm02 nav {
background: #FFB74D;
}
.cp_offcm02 nav, .cp_offcm02 ul {
height: 100%;
}
.cp_offcm02 li {
display: inline-block;
margin-right: -6px;
border: none;
color: #ffffff;
}
.cp_offcm02 a {
display: block;
padding: 12px 45px;
-webkit-transition: background-color .3s ease-in;
transition: background-color .3s ease-in;
}
.cp_offcm02 a:hover {
background-color: #FF9800;
}
/* menu toggle */
#cp_toggle02 {
display: none;
}
.cp_mobilebar {
display: none;
}
/* content */
.cp_container {
position: relative;
top: 0px;
margin-top: 0;
padding: 35px auto;
-webkit-transition: transform .3s ease-in;
transition: transform .3s ease-in;
}
.cp_content {
margin: 0 auto;
padding: 20px;
height: 65vh;
text-align: center;
}
@media (max-width: 767px) {
/* menu */
.cp_offcm02 {
position: fixed;
z-index: 2;
top: 0;
/* overflow-y: scroll; */
overflow: hidden;
height: 100%;
padding-top: 60px;
-webkit-transition: opacity 0.3s ease-in, visibility 0.3s ease-in 0.3s;
transition: opacity 0.3s ease-in, visibility 0.3s ease-in 0.3s;
opacity: 0;
background-color: #ffb74d;
}
.cp_offcm02 nav {
background: #FFE0B2;
}
.cp_offcm02 li {
display: block;
color: #FB8C00;
border-bottom: 1px solid #FB8C00;
}
.cp_offcm02 a {
padding: 1.2em;
}
.cp_offcm02 a:hover {
background-color: #FFE082;
}
/* menu toggle */
.cp_mobilebar {
display: block;
z-index: 10;
position: fixed;
top: 0;
left: 0;
padding: 0 25px;
width: 100%;
height: 60px;
background-color: #FFB74D;
-webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
}
.cp_menuicon {
display: block;
position: relative;
width: 25px;
height: 100%;
cursor: pointer;
-webkit-transition: transform .3s ease-in;
transition: transform .3s ease-in;
}
.cp_menuicon > span {
display: block;
position: absolute;
top: 55%;
margin-top: -0.3em;
width: 100%;
height: 0.2em;
border-radius: 1px;
background-color: #eeeeee;
-webkit-transition: transform .3s ease-in;
transition: transform .3s ease-in;
}
.cp_menuicon > span:before, .cp_menuicon > span:after {
content: '';
position: absolute;
width: 100%;
height: 100%;
border-radius: 1px;
background-color: #eeeeee;
-webkit-transition: transform .3s ease;
transition: transform .3s ease;
}
.cp_menuicon > span:before {
-webkit-transform: translateY(-0.6em);
transform: translateY(-0.6em);
}
.cp_menuicon > span:after {
-webkit-transform: translateY(0.6em);
transform: translateY(0.6em);
}
#cp_toggle02:checked + .cp_mobilebar .cp_menuicon {
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
#cp_toggle02:checked + .cp_mobilebar span:before,
#cp_toggle02:checked + .cp_mobilebar span:after {
-webkit-transform: rotate(90deg);
transform: rotate(90deg);
}
#cp_toggle02:checked ~ .cp_offcm02 {
visibility: visible;
opacity: 1;
-webkit-transition-delay: 0s;
transition-delay: 0s;
}
/* content */
.cp_container {
top:60px;
}
}

スマホのみクリックすると左からメニューが出てくる

ニャン易度
<div class="cp_cont">
<input type="checkbox" id="cp_toggle03">
<div class="cp_mobilebar">
<label for="cp_toggle03" class="cp_menuicon">
<span></span>
</label>
</div>
<header class="cp_offcm03">
<nav>
<ul>
<li><a href="#">menu1</a></li>
<li><a href="#">menu2</a></li>
<li><a href="#">menu3</a></li>
<li><a href="#">menu4</a></li>
<li><a href="#">menu5</a></li>
</ul>
</nav>
</header>
<div class="cp_container">
<div class="cp_content">
<h1>contents</h1>
</div>
</div>
</div>
*, *:before, *:after {
padding: 0;
margin: 0;
box-sizing: border-box;
}
ol, ul {
list-style: none;
}
a {
text-decoration: none;
color: inherit;
}
.cp_cont {
height: 65vh;
}
/* menu */
.cp_offcm03 {
position: relative;
z-index: 5;
top: 0;
left: 0;
overflow: auto;
width: 100%;
height: auto;
padding-top: 0;
-webkit-transition: transform 0.3s ease-in;
transition: transform 0.3s ease-in;
text-align: center;
color: #ffffff;
background-color: #039be5;
}
.cp_offcm03 nav,
.cp_offcm03 ul {
height: 100%;
}
.cp_offcm03 li {
display: inline-block;
margin-right: -6px;
border: none;
}
.cp_offcm03 a {
display: block;
padding: 12px 45px;
-webkit-transition: background-color .3s ease-in;
transition: background-color .3s ease-in;
}
.cp_offcm03 a:hover {
background-color: #1565C0;
}
/* menu toggle */
#cp_toggle03 {
display: none;
}
#cp_toggle03:checked ~ .cp_offcm03 {
-webkit-transform: translateX(0);
transform: translateX(0);
}
#cp_toggle03:checked ~ .cp_container {
-webkit-transform: translateX(0);
transform: translateX(0);
}
.cp_mobilebar {
display: none;
}
/* content */
.cp_container {
position: relative;
top: 0;
padding: 35px auto;
-webkit-transition: transform .3s ease-in;
transition: transform .3s ease-in;
}
.cp_content {
margin: 0 auto;
padding: 20px;
height: 65vh;
text-align: center;
}
@media (max-width: 767px) {
/* menu */
.cp_offcm03 {
position: fixed;
left: -250px;
overflow-y: hidden;
width: 250px;
height: 100%;
padding-top: 60px;
color: #ffffff;
background-color: #039be5;
}
.cp_offcm03 nav {
background: #29B6F6;
}
.cp_offcm03 li {
display: block;
margin-right: 0;
border-bottom: 1px solid #ffffff;
}
.cp_offcm03 a {
padding: 20px;
}
/* menu toggle */
.cp_mobilebar {
display: block;
z-index: 10;
position: fixed;
top: 0;
left: 0;
padding: 0 25px;
width: 100%;
height: 60px;
background-color: #039BE5;
}
.cp_menuicon {
display: block;
position: relative;
width: 25px;
height: 100%;
cursor: pointer;
-webkit-transition: transform .3s ease-in;
transition: transform .3s ease-in;
}
.cp_menuicon > span {
display: block;
position: absolute;
top: 55%;
margin-top: -0.3em;
width: 100%;
height: 0.2em;
border-radius: 1px;
background-color: #eeeeee;
-webkit-transition: transform .3s ease;
transition: transform .3s ease;
}
.cp_menuicon > span:before,
.cp_menuicon > span:after {
content: '';
position: absolute;
width: 100%;
height: 100%;
border-radius: 1px;
background-color: #eeeeee;
-webkit-transition: transform .3s ease-in;
transition: transform .3s ease-in;
}
.cp_menuicon > span:before {
-webkit-transform: translateY(-0.6em);
transform: translateY(-0.6em);
}
.cp_menuicon > span:after {
-webkit-transform: translateY(0.6em);
transform: translateY(0.6em);
}
#cp_toggle03:checked + .cp_mobilebar .cp_menuicon {
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
#cp_toggle03:checked + .cp_mobilebar span:before,
#cp_toggle03:checked + .cp_mobilebar span:after {
-webkit-transform: rotate(90deg);
transform: rotate(90deg);
}
#cp_toggle03:checked ~ .cp_offcm03 {
-webkit-transform: translateX(100%);
transform: translateX(100%);
}
#cp_toggle03:checked ~ .cp_container {
-webkit-transform: translateX(250px);
transform: translateX(250px);
}
/* content */
.cp_container {
top: 60px;
height: 92vh;
}
}

copypet.jp

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

More Info

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