22/09/28
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DreamCoding</title>
<link rel="stylesheet" href="asset/style.css">
</head>
<body>
<div class="container">
<header>
<div class="logo">
<div class="logo_left">
<i class="fa-solid fa-star"></i>
<span>DreamCoding</span>
</div>
<i class="fa-solid fa-bars" id="toggle"></i>
</div>
<nav>
<ul>
<li>Home</li>
<li>Gallery</li>
<li>Weddings</li>
<li>FAQ</li>
<li>Bookings</li>
</ul>
</nav>
<div class="contact">
<i class="fa-brands fa-twitter"></i>
<i class="fa-brands fa-facebook-f"></i>
</div>
</header>
</div>
<script src="https://kit.fontawesome.com/1045643a9e.js" crossorigin="anonymous"></script>
</body>
</html>
CSS
* {
margin: 0;
padding: 0;
}
ul {
list-style: none;
}
header {
background-color: #00004f;
color: white;
}
.logo i {
color: rgb(255, 145, 0);
}
.logo_left span {
font-weight: bold;
font-size: large;
}
@media screen and (max-width:480px) {
header {
display: flex;
flex-direction: column;
padding: 10px;
}
.logo {
display: flex;
justify-content: space-between;
}
.logo > * {
padding: 0px 5px 0px 5px;
}
.logo_left :first-child {
padding-right: 3px;
}
nav {
padding: 8px 0;
}
ul {
display: flex;
flex-direction: column;
}
li {
padding: 4px 0 4px 0;
font-size: 0.8em;
width: 100%;
text-align: center;
}
nav ul li:hover{
background-color: rgb(218, 123, 0);
border-radius: 3px;
}
.contact {
display: flex;
justify-content: center;
font-size: 0.75em;
padding: 3px;
}
.contact > i {
padding: 0 3px
}
}
@media screen and (min-width:480px) {
header {
display: flex;
justify-content: space-between;
padding: 15px;
align-items: center;
}
.logo {
flex: 1;
}
.logo_left {
display: flex;
align-items: center;
}
.logo_left > * {
padding-right: 5px;
}
.logo i:last-child {
display: none;
}
nav {
flex: 1;
}
nav ul {
display: flex;
}
nav ul li {
padding: 0px 13px;
width: 100%;
text-align: center;
}
.contact {
flex: 1;
display: flex;
justify-content: flex-end;
}
.contact i {
padding: 0 10px 0 0;
font-size: 1em;
padding: 0 7px;
}
}
22/11/24
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive Header</title>
<link rel="stylesheet" href="style.css">
<script src="https://kit.fontawesome.com/1045643a9e.js" crossorigin="anonymous"></script>
</head>
<body>
<header>
<div class="header__logos">
<div class="header__logo">
<i class="fa-solid fa-house"></i>
<h2>Dream Coding</h2>
</div>
<a href="#"><i class="fa-solid fa-bars"></i></a>
</div>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Gallery</a></li>
<li><a href="#">Weddings</a></li>
<li><a href="#">FAQ</a></li>
<li><a href="#">Bookings</a></li>
</ul>
</nav>
<div class="header__icons">
<ul>
<li>
<a href="#"><i class="fa-brands fa-twitter"></i></a>
</li>
<li>
<a href="#"><i class="fa-brands fa-facebook-f"></i></a>
</li>
</ul>
</div>
</header>
</body>
</html>
CSS
/* 초기화 */
* {
margin: 0;
padding: 0;
}
body,
html {
font-family: sans-serif;
}
li {
list-style: none;
}
a {
text-decoration: none;
}
/* 모바일 - 헤더 */
header {
background-color: #253245;
color: white;
}
.header__logos {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0.3em;
}
.header__logo {
display: flex;
align-items: center;
padding-left: 1.2em;
}
.fa-house {
font-size: 1.4em;
margin-right: 0.3em;
color: #D79462;
}
.fa-bars {
font-size: 1.4em;
padding-right: 1.2em;
color: #D79462;
}
/* 모바일 - 네비 */
nav {
text-align: center;
padding: 0.7em;
}
nav li {
padding: 0.4em;
border-radius: 0.2em;
}
nav li:hover {
background-color: #D79462;
}
nav ul li a{
color: white;
display: inline-block;
width: 100%;
}
/* 모바일 - 아이콘 */
.header__icons {
padding-bottom: 0.7em;
}
.header__icons ul {
display: flex;
justify-content: center;
align-items: center;
}
.header__icons li {
padding: 0.5em;
}
.header__icons a {
color: white;
text-align: center;
}
/* 피시버전 - 헤더 */
@media screen and (min-width: 768px) {
/* 헤더 */
header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0.7em;
}
.fa-bars {
display: none;
}
.header__logos {
padding: 0;
}
/* 네비 */
nav ul {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
nav ul li {
margin-right: 0.2em;
}
/* 아이콘 */
.header__icons {
padding-bottom: 0;
}
}
-> 9월과 비교하면 굉장히 발전했다! 하지만...
pc 버전에서 처음 HTML 파일을 열때마다, 토글 바의 display: none; 속성이 사라진다. 이유가 무엇일까? 더 찾아보자.
-해결했다!
before
.fa-bars {
display: none;
}
after
.header__logos :last-child i {
display: none;
}
위 코드처럼 바꾸니까 문제가 해결됐다.
다시 작성한 HTML코드
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive Header</title>
<link rel="stylesheet" href="style.css">
<link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro&display=swap" rel="stylesheet">
<script src="https://kit.fontawesome.com/1045643a9e.js" crossorigin="anonymous"></script>
<script src="main.js" defer></script>
</head>
<body>
<header>
<div class="header__logos">
<div class="header__logo">
<i class="fa-brands fa-accusoft"></i>
<a href="#"><p>DreamCoding</p></a>
</div>
<a href="#" class="toggleBtn"><i class="fa-solid fa-bars"></i></a>
</div>
<ul class="header__nav">
<li><a href="#">Home</a></li>
<li><a href="#">Gallery</a></li>
<li><a href="#">Weddings</a></li>
<li><a href="#">FAQ</a></li>
<li><a href="#">Bookings</a></li>
</ul>
<ul class="header__icons">
<li><a href="#"><i class="fa-brands fa-twitter"></i></a></li>
<li><a href="#"><i class="fa-brands fa-facebook-f"></i></a></li>
</ul>
</header>
</body>
</html>
다시 작성한 CSS 코드
/* 초기화 */
* {
margin: 0;
padding: 0;
}
body {
font-family: 'Source Sans Pro', sans-serif;
}
li {
list-style: none;
}
a {
text-decoration: none;
}
/* 모바일 - 헤더 */
header {
background-color: #253245;
}
.header__logos {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0.5em;
}
.header__logo {
display: flex;
align-items: center;
padding-left: 1.0em;
font-size: 1.4em;
}
.header__logo :first-child {
margin-right: 0.3em;
color: #D79462;
}
.header__logo :last-child {
color: white;
}
.header__logos :last-child i {
font-size: 1.4em;
padding-right: 1.0em;
color: #D79462;
}
/* 모바일 - 네비 */
.header__nav {
display: none;
text-align: center;
padding: 0.5em 0 0 0;
}
.header__nav li {
padding: 0.6em;
margin: 0 0.6em ;
}
.header__nav li:hover {
background-color: #D79462;
border-radius: 0.2em;
}
.header__nav li a {
color: white;
display: inline-block;
width: 100%;
}
/* 모바일 - 아이콘 */
.header__icons {
display: none;
padding: 1.5em 0;
}
.header__icons li {
padding: 0.7em;
}
.header__icons i {
color: white;
}
/* 토글 버튼 */
.header__nav.active {
display: block;
}
.header__icons.active {
display: flex;
justify-content: center;
}
/* 피시버전 - 헤더 */
@media screen and (min-width: 768px) {
/* 헤더 */
header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0.7em;
}
.header__logos :last-child i {
display: none;
}
.header__logos {
padding: 0;
}
/* 네비 */
.header__nav {
padding: 0;
display: flex;
}
.header__nav li {
margin-right: 0.2em;
}
/* 아이콘 */
.header__icons {
display: flex;
padding: 0;
}
.header__nav.active {
display: flex;
}
}
JS 코드
const toggleBtn = document.querySelector('.toggleBtn');
const menu = document.querySelector('.header__nav');
const icons = document.querySelector('.header__icons');
toggleBtn.addEventListener('click', () => {
menu.classList.toggle('active');
icons.classList.toggle('active');
});