高さ可変のモーダルの中央配置のやり方メモです。
width: 80%; margin: auto; right: 0; left: 0; で左右を真ん中に配置。
max-height: 300px; top: 50%; transform: translateY(-50%); で、高さ可変の縦位置の中央配置。
max-height: 200px; overflow: scroll; さらに量が増えすぎた場合にスクロールさせる。
簡単なモーダルウィンドウのサンプル | Proglad
それ以外はこちらの記事と同じコードです。
コンテンツ量の差を許容できるので、使い勝手はいいかなと思います。
See the Pen 高さ可変のモーダルの中央配置 by takapen (@takapen) on CodePen.
btn中身
btn2中身
中身
中身
中身
中身
中身
中身
中身
中身
中身
中身
中身
/*とりあえずのリセット*/
div,p{margin: 0;padding: 0;}
/*ここからモーダル用*/
.js-modal__bg {
width: 100%;
height: 100%;
display: none;
background-color: rgba(0,0,0,0.6);
position: fixed;
top: 0;
left: 0;
z-index: 100;
}
.js-modal__main {
width: 80%;
max-height: 300px;
margin: auto;
padding: 20px;
top: 50%;
right: 0;
left: 0;
transform: translateY(-50%);
background: #fff;
border-radius: 5px;
position: absolute;
}
.js-modal__main__scroll {
max-height: 200px;
margin-bottom: 20px;
overflow: scroll;
}
.js-modal__btn {
cursor: pointer
}
.js-modal__btn--close {
width: 30px;
height: 30px;
background: #333;
border-radius: 50%;
position: absolute;
top: -12px;
right: -12px;
z-index: 101;
cursor:pointer;
}
.js-modal__btn--close:before{
content:"";
width: 20px;
height: 20px;
border-right:2px solid #fff;
transform: rotate(-45deg);
position: absolute;
top:12px;
left:-3px;
}
.js-modal__btn--close:after{
content:"";
width: 20px;
height: 20px;
border-right:2px solid #fff;
transform: rotate(45deg);
position: absolute;
top:-2px;
left:-3px;
}
$(function(){
var modalBtn = $('.js-modal__btn');
var modalBtnClose = $('.js-modal__btn--close');
var modalBg = $('.js-modal__bg');
var modalMain = $('.js-modal__main');
modalBtn.on('click', function (e) {
$(this).next(modalBg).fadeToggle();
});
modalBtnClose.on('click', function (e) {
modalBg.fadeOut();
});
modalMain.on('click', function (e) {
e.stopPropagation();
});
modalBg.on('click', function () {
$(this).fadeOut();
});
});