レスポンシブデザイン2

ナビボタンと画像をレスポンシブにする

<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>レスポンシブデザイン基礎</title>
<!--charset以外のmetaはtitleの下かつlinkの上-->
<!--スマホ対応する場合の記述-->
<meta name="viewport" content="width=device-width">
<style>
/*Reset CSS*/
html,body,ul,li {
margin: 0;
padding: 0;
}
ul {
list-style: none;
}
a {
text-decoration: none;
}
img {
vertical-align: bottom;
}
/*PC用のレイアウト*/
body {
background-color: #DDD;
}
.container {
width: 980px;
margin: 0 auto;
padding: 10px;
background-color: white;
}
.header {
width: 980px;
/*
height: 300px;
background-color: yellowgreen;
*/
height: 0;
padding-bottom: 30.6%;/*300÷980×100=30.6*/
/*h1の「大見出し」をボックスの上に表示させるためbottomを使用する
topだと文字が下に表示されてしまう*/
background: url(img/1.png) no-repeat left top /cover;
/*
background: url(../../September/0908/img/1.png) no-repeat;
background-size:contain;
*/
margin-bottom: 10px;
}
.nav {
width: 980px;
height: 50px;
background-color: yellow;
margin-bottom: 10px;
}
.nav ul {
overflow: hidden;
}
.nav ul li {
float: left;
}
.nav ul li a {
display: block;
width: 245px;
height: 50px;
text-align: center;
line-height: 50px;
box-sizing: border-box;
border-right: solid 2px #777;
}
.nav ul li:last-child a {
border-right: none;
}
.nav ul li a:hover {
background-color: #CF6;
}
.wrapper {
width: 980px;
overflow:hidden;
margin-bottom: 10px;
}
.main {
float: left;
width: 700px;
height: 400px;
background-color: blue;
margin-right: 10px;
}
.sub {
float: right;
width: 270px;
height: 400px;
background-color: red;
}
.footer {
width: 980px;
height: 100px;
background-color: black;
}
.header img {
max-width: 100%;/*フルードイメージ 原寸以上のサイズにはしない*/
}
@media screen and (max-width:959px){
/*タブレット用のレイアウト*/
.container {
width: 98%;/*980px÷1000px=98%*/
padding: 1%;/*10px÷1000px=1%*/
}
.header {
width: 100%;
margin-bottom: 1%;//親要素の横幅に対しての%
}
.nav {
width: 100%;
margin-bottom: 1%;
}
.nav ul li {
width: 25%;
}
.nav ul li a {
width: 100%;
}
.wrapper {
width: 100%;
margin-bottom: 1%;
}
.main {
width: 70%;/*700px÷980px=70%*/
}
.sub {
width: 28%;/*270px÷980px=27.6%*/
}
.footer {
width: 100%;
}
}
@media screen and (max-width:767px){
/*スマホ用のレイアウト*/
.header {
/* height: 200px;*/
}
.nav {
width: 100%;
height: 80px;
}
.nav ul li {
width: 50%;
}
.nav ul li a {
width: 100%;
height: 40px;
line-height: 40px;
}
.nav ul li:nth-child(-n+2) a {/*初めから2番目まで*/
border-bottom: solid 2px #777;
}
.nav ul li:nth-child(even) a {/*偶数番目*/
border-right: none;
}
.wrapper {
width: 100%;
}
.main {
width: 100%;
height: 300px;
float: none;
margin-bottom: 1%;
}
.sub {
width: 100%;
height: 200px;
float: none;
}
.footer {
height: 80px;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>大見出し</h1>
</div>
<nav class="nav">
<ul>
<li><a href="#">ボタン1</a></li>
<li><a href="#">ボタン2</a></li>
<li><a href="#">ボタン3</a></li>
<li><a href="#">ボタン4</a></li>
</ul>
</nav>
<div class="wrapper">
<div class="main"></div>
<div class="sub"></div>
</div><!--.wrapper-->
<footer class="footer"></footer>
</div><!--.container-->
</body>
</html>

 

PCの表示
f:id:develog:20160908204113p:plain

 

タブレットの表示

f:id:develog:20160908204119p:plain

 

スマホの表示

f:id:develog:20160908204125p:plain