언어/HTML&CSS

[HTML&CSS]CSS 선언 방식들과 우선 순위

GAEBAL 2022. 3. 6. 00:12
728x90

CSS 선언 방식들과 우선 순위

1. 인라인 스타일 시트

- 해당 HTML 태그 안에 style이라는 속성을 이용해서 선언

<div style="background: black; color: white; border: red solid 1px;">HELLO</div>

 

2. 내부(임베디드, Embedded) 스타일 시트

- <head></head> 태그 안에 <style></style> 태그를 사용하여 선언

- 해당 html 파일 안에서만 사용 가능

<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>Document</title>
    <style>
        div {
            width: 50px;
            height: 50px;
            margin-bottom: 20px;
            background: green;
        }

        .book_box {
            background: pink;
            left: 20px;
            top: 20px;
            position: sticky;
        }
        /* reletive, absolute, fixed, sticky 차이 */

        .main_box {
            background-color: beige;
            left: 20px;
            top: 20px;
            position: relative;
        }
    </style>
</head>

 

3. 외부(링크, Link) 스타일 시트

- 별도의 .css 파일을 외부에서 만들어서 그 파일을 html 파일의 <head></head> 태그 안에 <link></link> 태그를 이용하여 선언

- 작업하는 html 파일이 많거나, 공통적으로 사용하는 css에 관해서는 이 방식으로 많이 처리한다고 함

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>SSAFY BOOK CAFE</title>
  <link rel="stylesheet" href="../css/main.css">
</head>

 

우선 순위

- 인라인, 내부, 외부 순으로 우선 순위가 낮아짐

-> 인라인 스타일 시트의 우선 순위가 제일 높음!

 

728x90