요즘 사이트들은 대부분 낮/밤 모드가 있습니다.
이 부분을 구현하는 방법은 여러가지가 있는데,
저는 아래와 같은 방식으로 구현해 봤어요.
다크모드 설정 방법
1. 다크모드 버튼 클릭시 dark 클래스 추가 - 스크립트로 제어
2. 다크모드 버튼 아이콘 이미지는 백그라운드 이미지로 제어 - 클릭 시 css로 아이콘 변경 가능
3. root 속성으로 작성된 css를 활용 - 다크모드에서 일괄 수정
4. 아이콘 이미지의 경우(SVG) 반전 효과 사용 : css의 filter:invert(1) 속성 활용
위의 사항들을 순서로 하여 마크업과 CSS 제작을 진행해 봅니다.
최종 view 모습 (하단에서 확인)
라이트 모드
다크모드
HTML
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1, width=device-width" />
<link href="https://cdn.jsdelivr.net/npm/reset-css@5.0.2/reset.min.css" rel="stylesheet">
<link rel="stylesheet" as="style" crossorigin href="https://cdn.jsdelivr.net/gh/orioncactus/pretendard@v1.3.9/dist/web/variable/pretendardvariable.min.css" />
</head>
<body>
<div class="main">
<div class="gnb">
<div class="user-wrap">
<div class="mode" onclick="toggleTheme()">
<div class="mode-change" alt="night아이콘"></div>
</div>
<div class="user">
<b>SU</b>
</div>
</div>
</div>
<div class="wrap">
<div class="contents">
<div class="txt">
<div class="txt-sample">
<div class="bd-wrap">
<div class="bd-row">
<div class="bd">
<div class="bd-tit">What is Lorem Ipsum?</div>
<div class="bd-txt">
<ul class="ul">
<li>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.</li>
</ul>
</div>
</div>
<div class="bd">
<div class="bd-tit">Why do we use it?</div>
<div class="bd-txt">
<ul class="ul">
<li>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).</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Javascript
function toggleTheme() {
document.documentElement.classList.toggle('dark');
}
CSS
root로 공통 속성인 애들을 한꺼번에 관리하여, 다크모드 변경시 일괄 변경 가능하도록
body, html {
height: 100%;
}
body {
margin: 0;
line-height: normal;
font-family: 'Pretendard';
}
:root {
--font-pretendard: 'Pretendard Variable';
--white: #fff;
--black: #000;
--element1: #f3f3f3;
--element2: #ddd;
--element3: #666;
--element4: #404040;
--element5: #f0ecee;
--element6: #aaa;
--element7: #222;
--element8: #0c1421;
--element9: #26272b;
--opacity2: rgba(255, 255, 255, 0.4);
--puple:#7d56c1;
--point_bg: #deebf6;
--basic-shadow: 3px 3px 15px rgba(0, 0, 0, 0.15);
}
/* 다크모드 설정 */
:root.dark {
--white: #0f0f0f;
--black: #fff;
--element1: #0c0c0c;
--element2: #222222;
--element3: #999999;
--element4: #eee;
--element5: #0f1311;
--element6: #555555;
--element7: #dddddd;
--element8: #f3ebde;
--element9: #d9d7d4;
--opacity2: rgba(0, 0, 0, 0.4);
--puple:#7d56c1;
}
/* 다크모드 커스텀 변경 */
html.dark .main {
background-color: #181818;
background-image: none;
}
html.dark .gnb .mode-change{
background: url(https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FcxNtnk%2FbtsHZtE3TeE%2FWyYQ7jeDUpKwAKUpntbbZk%2Ftfile.svg) no-repeat center;
filter : invert(1);
-webkit-user-drag: none;
}
/* contents css */
.main {
width: 100%;
height: 100%;
position: relative;
display: flex;
flex-direction: column;
align-items: flex-start;
background-color:var(--element1);
background-size: cover;
background-repeat: no-repeat;
background-position: top;
text-align: left;
font-size:20px;
color: var(--white);
font-family: var(--font-pretendard);
overflow: auto;
}
.gnb {
align-self: stretch;
background-color: var(--white);
border-bottom: 1px solid var(--element2);
box-sizing: border-box;
height: 65px;
justify-content: flex-end;
padding: 0 20px;
display: flex;
flex-direction: row;
align-items: center;
position: fixed;
top: 0;
width: 100%;
z-index: 99;
}
.gnb .user-wrap, .gnb .user, .gnb .mode {
display: flex;
align-items: center;
justify-content: center;
gap:10px;
}
.gnb .mode {
width: 40px;
border-radius: 30px;
background-color: var(--element5);
height: 40px;
overflow: hidden;
font-size: 18px;
transition: all 0.3s;
border: 1px solid rgba(138, 138, 138, 0.6);
opacity: 0.6;
}
.gnb .mode-change {
width: 20px;
height: 20px;
background: url(https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FcykOSx%2FbtsHYL0F1EJ%2FXR1GMNocnk7gX7T04jM8K1%2Ftfile.svg) no-repeat center;
background-size: contain;
}
.gnb .user {
width: 40px;
border-radius:20px;
background-color: var(--puple);
height: 40px;
overflow: hidden;
font-size: 18px;
color:white;
}
.wrap {
display: flex;
align-items: center;
flex: 1;
flex-direction: row;
font-size: 14px;
color: var(--element3);
height: 100%;
margin-top: 65px;
align-self: stretch;
justify-content: center;
overflow: hidden;
}
.contents {
flex: 1;
flex-direction: row;
font-size: 24px;
color: var(--element7);
overflow-y: scroll;
align-self: stretch;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
gap: 20px;
}
.txt {
width: 980px;
align-self: stretch;
flex-direction: column;
position: relative;
display: flex;
}
.txt-sample {
display: flex;
align-items: center;
justify-content: center;
align-self: stretch;
flex-direction: column;
gap: 20px;
z-index: 2;
font-size: 20px;
color: var(--black);
flex-grow: 1;
}
.txt-sample .bd-wrap {
align-self: stretch;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 10px;
}
.txt-sample .bd-row {
align-self: stretch;
flex-direction: row;
gap:30px;
display: flex;
}
.txt-sample .bd {
flex: 1;
border-radius: 20px;
background-color: var(--opacity2);
display: flex;
flex-direction: column;
padding: 20px;
gap:10px;
}
.txt-sample .bd-tit {
font-weight: 700;
padding-left: 17px;
}
.txt-sample .bd-txt {
font-size: 18px;
letter-spacing: -0.01em;
line-height: 160%;
font-weight: 500;
color: var(--element3);
}
.txt-sample .bd-txt .ul {
margin: 0;
font-family: inherit;
font-size: inherit;
padding-left: 5px;
}
다크모드 설정 관련 참고 사이트
더보기
사용 이미지
728x90
반응형
'퍼블리싱' 카테고리의 다른 글
[Javascript, css] 스크롤 내리면 카운팅 시작 되는 소스 (0) | 2024.06.25 |
---|---|
[CSS]마우스 호버시 아이콘 모양 변경(사이드바 보기/감추기역할) (0) | 2024.06.19 |
[jQuery]셀렉트박스+제이쿼리, 셀렉트 선택 시 내용 보여지게 (0) | 2024.02.22 |
[jQuery] mouseenter , mouseleave 마우스 호버 효과 - 마우스 엔터 (0) | 2024.01.31 |
[javaScript] 클릭 시 클래스 명 변경 되는 자바스크립트 (0) | 2023.11.29 |
댓글