Study/반응형 웹
반응형 웹 : float, flex, grid로 만들어보기
posey
2022. 12. 28. 09:28
- UI/UX 반응형 웹디자인&웹퍼블리셔 개발자 과정 - DAY 74
Review
- UI/UX 반응형 웹디자인&웹퍼블리셔 개발자 과정 - DAY 74
<float으로 구조 만들기>
- HTML
<div id="wrap">
<header>header</header>
<section>
<article class="left">left</article>
<article class="main">center</article>
<article class="right">right</article>
</section>
<footer>footer</footer>
</div>
- CSS
#wrap {
width: 100%;
max-width: 1280px;
margin: 0 auto;
text-align: center;
}
header {
background: #eee;
padding: 20px;
}
section > article {
float: left;
padding: 200px 0;
box-sizing: border-box;
}
.left {
width: 10%;
background: #bbb;
}
.main {
width: 80%;
background: #ccc;
}
.right {
width: 10%;
background: #bbb;
}
footer {
clear: both;
padding: 20px;
background: #eee;
}
@media screen and (max-width: 768px) {
section > article {
float: none;
}
.left,
.main,
.right {
width: 100%;
padding: 50px 20px;
}
}
<flex로 구조 만들기>
- HTML
<div id="wrap">
<header>header</header>
<section>
<article class="main">center</article>
<article class="right">right</article>
<article class="left">left</article>
</section>
<footer>footer</footer>
</div>
- CSS
#wrap {
width: 100%;
max-width: 1280px;
margin: 0 auto;
text-align: center;
}
header {
background: #eee;
padding: 20px;
}
section {
display: flex;
}
section > article {
padding: 200px 20px;
}
.left {
flex: 1;
background: #bbb;
}
.main {
flex: 8;
background: #ccc;
}
.right {
flex: 1;
background: #bbb;
}
footer {
background: #eee;
padding: 20px;
}
@media screen and (max-width: 768px) {
section {
flex-direction: column;
}
section > article {
padding: 50px 0;
}
.left {
order: 1;
}
.main {
order: 2;
}
.right {
order: 3;
}
}
여기서는 flex와 order 속성을 이용했다.
flex로 가로 크기를 어느정도 할 것인지 비율로 지정해주고, order로 순서를 지정했다.
가로 768px이 되면 flex-direction을 세로 방향으로 되도록 했기 때문에 세로로 나란히 배열된 것을
볼 수 있다. 순서도 각각 지정한 대로 left - center - right 순서로 오게 된다.
<grid로 구조 만들기>
- HTML
<div id="wrap">
<header>header</header>
<section>
<article class="left">left</article>
<article class="main">center</article>
<article class="right">right</article>
</section>
<footer>footer</footer>
</div>
- CSS
#wrap {
width: 100%;
max-width: 1280px;
margin: 0 auto;
text-align: center;
}
header {
background: #eee;
padding: 20px;
}
section {
display: grid;
grid-template-columns: 1fr 8fr 1fr;
}
section > article {
padding: 200px 0;
box-sizing: border-box;
}
.left {
background: #bbb;
}
.main {
background: #ccc;
}
.right {
background: #bbb;
}
footer {
background: #eee;
padding: 20px;
}
@media screen and (max-width: 768px) {
section {
grid-template-columns: auto;
}
.left,
.main,
.right {
width: 100%;
padding: 50px 0px;
}
}
<float으로 반응형 웹 만들어보기 예제 + 햄버거 버튼>
- HTML
<div id="wrap">
<header>
<h1>Media Query</h1>
</header>
<nav>
<ul>
<li><a href="#">menu1</a></li>
<li><a href="#">menu2</a></li>
<li><a href="#">menu3</a></li>
<li><a href="#" style="border-right: none">menu4</a></li>
</ul>
</nav>
<section>
<main>
What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing
and typesetting industry. Lorem Ipsum has been the industry's standard
dummy text ever since the 1500s, when an unknown printer took a galley
of type and scrambled it to make a type specimen book. It has survived
not only five centuries, but also the leap into electronic
typesetting, remaining essentially unchanged. It was popularised in
the 1960s with the release of Letraset sheets containing Lorem Ipsum
passages, and more recently with desktop publishing software like
Aldus PageMaker including versions of Lorem Ipsum. Why do we use it?
It is a long established fact that a reader will be distracted by the
readable content of a page when looking at its layout. The point of
using Lorem Ipsum is that it has a more-or-less normal distribution of
letters, as opposed to using 'Content here, content here', making it
look like readable English. Many desktop publishing packages and web
page editors now use Lorem Ipsum as their default model text, and a
search for 'lorem ipsum' will uncover many web sites still in their
infancy. Various versions have evolved over the years, sometimes by
accident, sometimes on purpose (injected humour and the like). Where
does it come from? Contrary to popular belief, Lorem Ipsum is not
simply random text. It has roots in a piece of classical Latin
literature from 45 BC, making it over 2000 years old. Richard
McClintock, a Latin professor at Hampden-Sydney College in Virginia,
looked up one of the more obscure Latin words, consectetur, from a
Lorem Ipsum passage, and going through the cites of the word in
classical literature, discovered the undoubtable source. Lorem Ipsum
comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et
Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC.
This book is a treatise on the theory of ethics, very popular during
the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit
amet..", comes from a line in section 1.10.32. The standard chunk of
Lorem Ipsum used since the 1500s is reproduced below for those
interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et
Malorum" by Cicero are also reproduced in their exact original form,
accompanied by English versions from the 1914 translation by H.
Rackham.
</main>
<article>
What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing
and typesetting industry. Lorem Ipsum has been the industry's standard
dummy text ever since the 1500s, when an unknown printer took a galley
of type and scrambled it to make a type specimen book. It has survived
not only five centuries, but also the leap into electronic
typesetting, remaining essentially unchanged. It was popularised in
the 1960s with the release of Letraset sheets containing Lorem Ipsum
passages, and more recently with desktop publishing software like
Aldus PageMaker including versions of Lorem Ipsum. Why do we use it?
It is a long established fact that a reader will be distracted by the
readable content of a page when looking at its layout. The point of
using Lorem Ipsum is that it has a more-or-less normal distribution of
letters, as opposed to using 'Content here, content here', making it
look like readable English. Many desktop publishing packages and web
page editors now use Lorem Ipsum as their default model text, and a
search for 'lorem ipsum' will uncover many web sites still in their
infancy. Various versions have evolved over the years, sometimes by
accident, sometimes on purpose (injected humour and the like). Where
does it come from? Contrary to popular belief, Lorem Ipsum is not
simply random text. It has roots in a piece of classical Latin
literature from 45 BC, making it over 2000 years old. Richard
McClintock, a Latin professor at Hampden-Sydney College in Virginia,
looked up one of the more obscure Latin words, consectetur, from a
Lorem Ipsum passage, and going through the cites of the word in
classical literature, discovered the undoubtable source. Lorem Ipsum
comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et
Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC.
This book is a treatise on the theory of ethics, very popular during
the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit
amet..", comes from a line in section 1.10.32. The standard chunk of
Lorem Ipsum used since the 1500s is reproduced below for those
interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et
Malorum" by Cicero are also reproduced in their exact original form,
accompanied by English versions from the 1914 translation by H.
Rackham.
</article>
<article>
What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing
and typesetting industry. Lorem Ipsum has been the industry's standard
dummy text ever since the 1500s, when an unknown printer took a galley
of type and scrambled it to make a type specimen book. It has survived
not only five centuries, but also the leap into electronic
typesetting, remaining essentially unchanged. It was popularised in
the 1960s with the release of Letraset sheets containing Lorem Ipsum
passages, and more recently with desktop publishing software like
Aldus PageMaker including versions of Lorem Ipsum. Why do we use it?
It is a long established fact that a reader will be distracted by the
readable content of a page when looking at its layout.
</article>
</section>
<div class="btn">
<span></span>
<span></span>
<span></span>
</div>
</div>
- CSS
@font-face {
font-family: montserrat;
src: url(../fontawesome-free-6.2.0-web/webfonts/Montserrat-Medium.woff)
format("woff");
}
* {
margin: 0;
padding: 0;
font-family: montserrat, sans-serif;
}
ul,
li {
list-style: none;
}
a {
text-decoration: none;
color: #222222;
}
body {
margin-bottom: 30px;
}
#wrap {
position: relative;
width: 100%;
max-width: 1280px;
margin: 0 auto;
border: 1px solid #222222;
}
header {
background: #eee;
padding: 20px;
}
nav {
background: pink;
}
nav ul li {
float: left;
width: 25%;
text-align: center;
}
nav ul li a {
display: block;
width: 100%;
height: 50px;
line-height: 50px;
border-top: 1px solid #222222;
border-bottom: 1px solid #222222;
border-right: 1px solid #222222;
box-sizing: border-box;
background: pink;
}
nav ul li a:hover {
background: rgb(236, 123, 180);
}
section {
clear: left;
width: 100%;
}
section > main,
section > article {
padding: 20px;
text-align: justify;
}
.btn {
position: absolute;
top: 30px;
right: 30px;
width: 23px;
height: 18px;
display: none;
cursor: pointer;
}
.btn span {
position: absolute;
width: 100%;
height: 3px;
background: #222222;
border-radius: 5px;
display: block;
transition: transform 0.5s;
}
.btn span:first-child {
top: 0;
}
.btn span:nth-child(2) {
top: 50%;
transform: translateY(-50%);
}
.btn span:last-child {
bottom: 0;
}
.btn.on span:first-child {
top: 50%;
transform: translateY(-50%) rotate(45deg);
}
.btn.on span:nth-child(2) {
opacity: 0;
}
.btn.on span:last-child {
bottom: 50%;
transform: translateY(50%) rotate(-45deg);
}
@media screen and (max-width: 768px) {
#wrap nav ul li {
width: 100%;
float: none;
}
#wrap nav ul li a {
border-top: none;
border-right: none;
}
#wrap nav ul li:first-child a {
border-top: 1px solid #222222;
}
}
@media screen and (max-width: 480px) {
nav {
display: none;
}
.btn {
display: block;
}
}
- jQuery
$(function () {
$(".btn").click(function () {
$(this).toggleClass("on");
$("nav").slideToggle();
});
});
0
<flex로 반응형 웹 만들어보기 예제 + 햄버거 버튼>
- HTML
<div id="wrap">
<header>
<h1>대한민국</h1>
<br />
<h2>大韓民國 / Republic of Korea</h2>
</header>
<section>
<div class="nav">
<ul>
<li><a href="#">서울</a></li>
<li><a href="#">대전</a></li>
<li><a href="#">대구</a></li>
<li><a href="#">부산</a></li>
<li><a href="#">광주</a></li>
</ul>
</div>
<article>
What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing
and typesetting industry. Lorem Ipsum has been the industry's standard
dummy text ever since the 1500s, when an unknown printer took a galley
of type and scrambled it to make a type specimen book. It has survived
not only five centuries, but also the leap into electronic
typesetting, remaining essentially unchanged. It was popularised in
the 1960s with the release of Letraset sheets containing Lorem Ipsum
passages, and more recently with desktop publishing software like
Aldus PageMaker including versions of Lorem Ipsum. Why do we use it?
It is a long established fact that a reader will be distracted by the
readable content of a page when looking at its layout. The point of
using Lorem Ipsum is that it has a more-or-less normal distribution of
letters, as opposed to using 'Content here, content here', making it
look like readable English. Many desktop publishing packages and web
page editors now use Lorem Ipsum as their default model text, and a
search for 'lorem ipsum' will uncover many web sites still in their
infancy. Various versions have evolved over the years, sometimes by
accident, sometimes on purpose (injected humour and the like). Where
does it come from? Contrary to popular belief, Lorem Ipsum is not
simply random text. It has roots in a piece of classical Latin
literature from 45 BC, making it over 2000 years old. Richard
McClintock, a Latin professor at Hampden-Sydney College in Virginia,
looked up one of the more obscure Latin words, consectetur, from a
Lorem Ipsum passage, and going through the cites of the word in
classical literature, discovered the undoubtable source. Lorem Ipsum
comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et
Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC.
This book is a treatise on the theory of ethics, very popular during
the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit
amet..", comes from a line in section 1.10.32. The standard chunk of
Lorem Ipsum used since the 1500s is reproduced below for those
interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et
Malorum" by Cicero are also reproduced in their exact original form,
accompanied by English versions from the 1914 translation by H.
Rackham.
</article>
<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>
<li><a href="#">menu6</a></li>
<li><a href="#">menu7</a></li>
</ul>
</nav>
</section>
<div class="btn">
<span></span>
<span></span>
<span></span>
</div>
</div>
- CSS
@font-face {
font-family: montserrat;
src: url(../fontawesome-free-6.2.0-web/webfonts/Montserrat-Medium.woff)
format("woff");
}
@font-face {
font-family: notosans;
src: url(../fontawesome-free-6.2.0-web/webfonts/NotoSansKR-Regular.woff)
format("woff");
}
* {
margin: 0;
padding: 0;
font-family: montserrat, notosans;
}
ul,
li {
list-style: none;
}
a {
text-decoration: none;
color: #222222;
}
body {
margin-bottom: 30px;
}
#wrap {
position: relative;
width: 100%;
max-width: 1280px;
margin: 0 auto;
}
header {
background: skyblue;
padding: 20px;
text-align: center;
}
section {
display: flex;
width: 100%;
border: 1px solid #222222;
box-sizing: border-box;
margin-top: 20px;
}
.nav {
flex: 2;
height: 300px;
}
.nav ul li {
margin: 0 8px 10px 5px;
text-align: center;
}
.nav ul li a {
display: block;
background: skyblue;
box-shadow: 3px 3px 1px #d9d9d9;
padding: 10px 0px;
}
nav {
flex: 2;
height: 225px;
background: pink;
}
nav ul {
padding: 10px 0;
}
nav ul li {
text-align: center;
cursor: pointer;
}
nav ul li a {
display: block;
width: 100%;
padding: 5px 0;
background: pink;
}
nav ul li a:hover {
background: rgb(236, 123, 180);
}
section > article {
flex: 5;
padding: 20px;
background: #eee;
text-align: justify;
margin-right: 5px;
}
.btn {
position: absolute;
top: 30px;
right: 30px;
width: 23px;
height: 18px;
display: none;
cursor: pointer;
}
.btn span {
position: absolute;
width: 100%;
height: 3px;
background: #222222;
border-radius: 5px;
display: block;
transition: transform 0.5s;
}
.btn span:first-child {
top: 0;
}
.btn span:nth-child(2) {
top: 50%;
transform: translateY(-50%);
}
.btn span:last-child {
bottom: 0;
}
.btn.on span:first-child {
top: 50%;
transform: translateY(-50%) rotate(45deg);
}
.btn.on span:nth-child(2) {
opacity: 0;
}
.btn.on span:last-child {
bottom: 50%;
transform: translateY(50%) rotate(-45deg);
}
@media screen and (max-width: 768px) {
#wrap {
width: 99%;
}
section {
display: flex;
flex-direction: column;
align-content: space-between;
}
section nav {
display: none;
order: 1;
margin: 0 0 10px 0;
}
section .nav {
order: 3;
}
section article {
order: 2;
margin: 0 0 10px 0;
}
.btn {
display: block;
}
}
- jQuery
$(function () {
$(".btn").click(function () {
$(this).toggleClass("on");
$("nav").slideToggle();
});
});
0
반응형