*
์๋ฌด ํ์ ์์ด ์ฌ์ฉํ๋ฉด ํ์ด์ง์ ์๋ ๋ชจ๋ ์์๊ฐ ๋์
* {
margin : 0;
padding : 0;
}ํน์ ์์์ ๋ชจ๋ ์์ ์์์ ์ ์ฉํ ์๋ ์์
#container * {
border : 1px solid black;
}์ฃผ์์ : ๋จ๋ฐํ ์ ์ฑ๋ฅ์ ํ๋ฅผ ๋ถ๋ฌ์ผ์ผํด
X
id๋ฅผ ๋์์ผ๋ก ์ผ์
#container {
width : 960px;
margin : auto;
}์ฃผ์์ : id๋ ์ ์ฐ์ฑ์ด ๋ถ์กฑํ๊ธฐ ๋๋ฌธ์, ๊ผญ id๋ฅผ ์ฌ์ฉํด์ผํ๋์ง ์๋ฌธํ์.
- ๋ฐ๋ผ์ ์ฐพ๊ธฐ ์ด๋ ค์ด ์์์๋ง id๋ฅผ ์ฌ์ฉํ์
.X
class ์ ํ์
id์๋ ๋ค๋ฅด๊ฒ ์ฌ๋ฌ๊ฐ์ ์์๋ฅผ ๋์์ผ๋ก ์ ํ ์ ์์
.error {
color : red;
}X Y
X์์์ ํ์ ์์์ค ๋ชจ๋ Y์์์ ์ ์ฉ
li a {
text-decoration : none;
}X
ํ์ด์ง์ ์๋ ๋ชจ๋ X์์์ ์ ์ฉ
a {
color : red;
}
ul {
margin-left:0;
}X:visited, X:link
- :link - ํด๋ฆญํ๊ธฐ ์ a ํ๊ทธ์ ๊ฐ์ํด๋์ค
- :visited - ํด๋ฆญ ํ ์ดํ aํํฌ๊ธ ๊ฐ์ํด๋์ค
a:link {
color:red;
}
a:visited {
color : purple;
}X + Y
X ์์ ๋ฐ๋ก ๋ค์ ์๋ ์์๋ง ์ ํํจ
ul + p {
color:red;
}X > Y
X ์์์ ์ง๊ณ ์์๋ง์ ์ ํํจ
div#container > ul {
border : 1px solid black;
}X ~ Y
ํ์ ์ ํ์๋ก, X ์์์ ์๋์ ์๋ ๋ชจ๋ Y ์์์ ์ ์ฉ๋จ
ul ~ p {
color : red;
}X[title]
X ์์ ์ค title ์์ฑ์ด ์๋ ํ๊ทธ๋ง ์ ํํจ
a[title] {
color : green;
}X[bar=โfooโ]
X ์์์ bar ์์ฑ์ ๊ฐ์ด โfooโ ์ธ ํ๊ทธ๋ง ์ ํ
a[href="https://naver.com"] {
color : #1f6053;
}X[bar*=โfooโ]
X ์์์ bar ์์ฑ์ โfooโ๊ฐ ์๋ ํ๊ทธ๋ฅผ ์ ํ
a[href*="naver"] {
color : #1f6053;
}ex) https://naver.com, https:www.naver.com, http://www.naver.com ๋ชจ๋์ ์ ์ฉ๋จ
X[bar^=โfooโ]
bar ์์ฑ์ด โfooโ๋ก ์์ํ ์ ์ ์ฉ
a[href^="http"] {
background: url(path/to/external/icon.png) no-repeat;
padding-left: 10px;
}X[bar$=โfooโ]
bar ์์ฑ์ ๊ฐ์ด โfooโ ๋ก ๋๋ ์ ์ ์ฉ
a[href$=".jpg"] {
color: red;
}X[foo~=โbarโ]
foo์ ๋์ด์ฐ๊ธฐ๋ก ๊ตฌ๋ถ๋๋ ์์ฑ๊ฐ ์ค โbarโ๊ฐ ์์ผ๋ฉด ์ ์ฉ
a[data-info~="external"] {
color:red;
}
a[data-info~="image"] {
border:1px solid black;
}ex)
// ๋ ๊ฐ ๋ค ์ ์ฉ
<a href="path/to/image.jpg" data-info="external image"> Click Me, Fool </a>
// external๋ง ์ ์ฉ
<a href="path/to/image.jpg" data-info="external"> Click Me, Fool </a>
// image๋ง ์ ์ฉ
<a href="path/to/image.jpg" data-info="image"> Click Me, Fool </a>X:checked
๋ผ๋์ค๋ฒํผ์ด๋ ์ฒดํฌ๋ฐ์ค์ฒ๋ผ ์ฒดํฌ๋๋ ์ฌ์ฉ์ ์ธํฐํ์ด์ค ์์๋ง์ ๋์์ผ๋ก ํจ
input[type=radio]:checked {
border: 1px solid black;
}X::before X::after
- ::before - ์ค์ ๋ด์ฉ ๋ฐ๋ก ์์์ ์์ฑ๋๋ ์์์์
- ::after - ์ค์ ๋ด์ฉ ๋ฐ๋ก ๋ค์์ ์์ฑ๋๋ ์์์์
์ด๋ content ์์ฑ๊ฐ์ ํตํด ํน์ ๋ด์ฉ์ ์ถ๋ ฅํ ์๋ ์๋ค.

X:hover
์ฌ์ฉ์๊ฐ ์์ ์์ ๋ง์ฐ์ค๋ฅผ ์ฌ๋ ธ์๋ ์ ์ฉ
div:hover {
background : #e3e3e3;
}X:not(์ ํ์)
๋ชจ๋ X์ ์ ์ฉํ๊ณ ์ถ์๋ฐ, (์ ํ์)๋ก ์ง์ ๋ ๊ฒ๋ง ๋นผ๊ณ ์ถ์๋ ์ฌ์ฉ
div:not(#container) {
color : blue;
}
*:not(p) {
color:green;
}X::๊ฐ์์์
์ฒซ๋ฒ์งธ ์ค์ด๋ ์ฒซ ๊ธ์์ ์คํ์ผ์ ์ ์ฉํ ๋ ์ฌ์ฉ๊ฐ๋ฅ.
๋ฐ๋์ ๋ธ๋ก ๋ ๋ฒจ ์์์ ์ ์ฉํด์ผํ๋ค.
p::first-line {
font-weight :bold;
font-size:1.2em;
}
p::first-letter {
float:left;
font-size:2em;
font-weight:bold;
font-family : cursive;
padding-right:2px;
}::after, ::before ๋ ๊ฐ์์์์ด๋ค.
X:nth-child(n)
X์ n๋ฒ์งธ ์์์๋ง ์ ์ฉ
li:nth-child(3) {
color:red;
}X:nth-last-child(n)
X์ ๋ง์ง๋ง n๋ฒ์งธ ์์์๋ง ์ ์ฉ
li:nth-last-child(2) {
color:red;
}X:nth-of-type(n)
X์ ๊ฐ์ ์์์ n๋ฒ์งธ ํ์ ๋ฅผ ์ ํํ๋ค.
li:nth-of-type(3) {
border:1px solid black;
}X:nth-last-of-type(n)
X์ ๊ฐ์ ์์์ ํ์ ์ค ๋ง์ง๋ง n๋ฒ์งธ๋ฅผ ์ ํํจ
ul:nth-last-of-type(3) {
border: 1px solid black;
}X:first-child, X:last-child
๋ถ๋ชจ ์์์ ์ฒซ๋ฒ์งธ/๋ง์ง๋ง ์์์๋ง ์ ์ฉ ๊ฐ๋ฅ
ul li:first-child {
border-top : none;
}
ul > li:last-child {
border-top : none;
}X:only-child
๋ถ๋ชจ ์์์ ๋จ ํ๋์ ์์์์๋ฅผ ์ง์ ํ ์ ์๋ค.
๋ง์ฝ ๋ถ๋ชจ์ ์ฌ๋ฌ X๊ฐ ์์๋ ์ ์ฉ์ด ๋์ง ์์
div p:only-child {
color :red
}<div>
<p>
์ ์ฉ๋จ
</p>
</div>
<div>
<p>
์ ์ฉ์๋จ
</p>
<p>
์ ์ฉ์๋จ
</p>
</div>X:only-of-type
๋ถ๋ชจ ์ปจํ ์ด๋์ ํ์ ์์๊ฐ ์๋ ์์๋ฅผ ๋์์ผ๋ก ํจ
li:only-of-type {
font-weight:bold;
}
ul > li:only-of-type {
font-weight:bold;
}X:first-of-type
ํด๋น type์ ์ฒซ๋ฒ์งธ ํ์ ์ ํ์๋ฅผ ์ ํํจ
<div>
<p> My paragraph here. </p>
<ul>
<li> List Item 1 </li>
<li> List Item 2 </li>
</ul>
<ul>
<li> List Item 3 </li>
<li> List Item 4 </li>
</ul>
</div>์ html์์ List Item 2 ์๋ง ์คํ์ผ์ ์ ์ฉํ๋ ๋ฐฉ๋ฒ์ ์๋์ ๊ฐ์ด 3๊ฐ์ง๊ฐ ์์
ul:first-of-type > li:nth-child(2) {
font-weight: bold;
}
p + ul li:last-child {
font-weight: bold;
}
ul:first-of-type li:nth-last-child(1) {
font-weight: bold;
}์ถ์ฒ
https://code.tutsplus.com/ko/tutorials/the-30-css-selectors-you-must-memorizeโnet-16048