[JS] JavaScript 프로젝트에서 스타일 설정 방법 3가지 코드 예시

2024. 3. 18. 21:07·개발공부/Javascript

1. 인라인 스타일


app.style.background = '#000';
app.style.setProperty('background', '#f00');
cssText는 기존에 설정된 inline이 날아감
app.style.cssText = `            
    background: #fff;
    max-width: 500px;
    width: 100%;
    margin: 100px auto;
    padding: 20px;
    border-radius: 6px;
    box-shadow: 5px 5px 10px rgba(0, 0, 0, 0.1);
`;

app.style.removeProperty('box-shadow');

 

 

2. 스타일 시트


document.styleSheets[0].insertRule(`
#app {
    background: #fff;
    max-width: 500px;
    width: 100%;
    margin: 100px auto;
    padding: 20px;
    border-radius: 6px;
    box-shadow: 5px 5px 10px rgba(0, 0, 0, 0.1);
}
`);
document.styleSheets[0].deleteRule(0);

app.style.background = '#000';
console.log(app.style.background);

console.log(getComputedStyle(app));

 

 

3. 스타일 시트 동적 생성


const styleSheet = new CSSStyleSheet();
styleSheet.replace(`* {
    margin: 0;
    padding: 0;
}

body {
    height: 100vh;
    background: linear-gradient(180deg,
            #3ac6f3 0%,
            rgba(200, 180, 180, 0.4) 40%,
            rgba(255, 0, 0, 0.56) 100%);
    overflow: hidden;
}


#app {
    background: #fff;
    max-width: 500px;
    width: 100%;
    margin: 100px auto;
    padding: 20px;
    border-radius: 6px;
    box-shadow: 5px 5px 10px rgba(0, 0, 0, 0.1);
}

#header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

#counter span {
    margin-right: 0.5rem;
}

#input-field {
    margin: 20px 0;
    width: 100%;
    display: flex;
    height: 2.5rem;
}

#input-field input {
    width: 85%;
    height: 100%;
    border-radius: 3px;
    border: 1px solid #ccc;
    font-size: 0.8rem;
    padding-left: 15px;
    outline: none;
}

#input-field input:focus {
    border-color: #ff0051;
}

#input-field button {
    width: 50px;
    height: 100%;
    border: none;
    color: #fff;
    margin-left: 5px;
    font-size: 21px;
    outline: none;
    background: #3ac6f3;
    border-radius: 3px;
}

#item-list-container {
    max-height: 250px;
    overflow: scroll;
    scrollbar-width: none;
}

#item-list-container li {
    position: relative;
    display: flex;
    justify-content: space-between;
    list-style: none;
    height: 3rem;
    line-height: 3rem;
    margin-bottom: 0.5rem;
    overflow: hidden;
    border-radius: 5px;
}

#item-list-container div {
    width: 100%;
    display: flex;
    justify-content: space-between;
}

#item-list-container input {
    margin: 1rem;
    transition-duration: 0.5s;
    opacity: 1;
}

#item-list-container button {
    width: 3rem;
    margin: 0.3rem 1rem;
    width: 3rem;
    border: none;
    color: #fff;
    margin-left: 5px;
    font-size: 0.8rem;
    font-weight: bold;
    outline: none;
    background: #f97;
    transition-duration: 0.5s;
    opacity: 1;
}

#item-list-container button:hover {
    background-color: #ddd;
}

#item-list-container span {
    width: 100%;
    text-align: left;
}`);

document.adoptedStyleSheets = [styleSheet];

'개발공부 > Javascript' 카테고리의 다른 글

[JS] 이벤트 버블링과 캡처링  (0) 2024.04.04
[JS] 호이스팅  (0) 2024.03.28
[JS] 기본형 데이터와 참조형 데이터  (0) 2024.03.24
[JS] 실행 컨텍스트  (1) 2024.03.13
[JS] 클로저  (0) 2024.03.11
'개발공부/Javascript' 카테고리의 다른 글
  • [JS] 호이스팅
  • [JS] 기본형 데이터와 참조형 데이터
  • [JS] 실행 컨텍스트
  • [JS] 클로저
유채주
유채주
  • 유채주
    채블로그
    유채주
  • 전체
    오늘
    어제
    • 유블로그 (27)
      • 개발공부 (27)
        • Javascript (12)
        • HTML&CSS (1)
        • 알고리즘 (3)
        • React (8)
        • 프론트엔드 (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    알고리즘
    Flex
    이벤트버블링
    javascript
    typescript
    이벤트루프
    ESM
    이벤트캡처링
    컴파일링
    이벤트위임
    기본형
    yarn
    호이스팅
    실행컨텍스트
    testing
    프론트테스트
    CSS
    번들링
    await
    Mocking
    dfs
    참조형
    react테스트
    TDD
    Async
    CJS
    JEST
    Rendering
    PnP
    react
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
유채주
[JS] JavaScript 프로젝트에서 스타일 설정 방법 3가지 코드 예시
상단으로

티스토리툴바