HTML Styles - CSS


CSS = Styles and Colors

Manipulate Text
Colors,  Boxes

Example

<!DOCTYPE html>
<html>

<head>
<style>
  body {background-color:lightgray}
  h1   {color:blue}
  p    {color:green}
</style>
</head>

<body>
  <h1>This is a heading</h1>
  <p>This is a paragraph.</p>
</body>

</html>

Try it Yourself »

Styling HTML with CSS

CSS 는 Cascading Style Sheets 을 의미한다.

HTML 요소들에 스타일을 적용하는 방법이 3가지 있다:

  • 인라인(Inline) - HTML 요소 안의 style 속성을 사용
  • 내부(Internal) - <head> 섹션 안의 <style> 요소를 사용
  • 외부(External) - 외부의 CSS 파일을 사용

HTML에 CSS를 추가하는 가장 일반적인 방법은, 외부의 별도 CSS 파일에 CSS 문법을 넣는 것이다. 하지만, 이 HTML 튜토리얼에서는 내부(internal) 스타일링을 사용한다. 이는 설명하기 쉽고, 직접 실험을 쉽게 할 수 있게 해 주기 때문이다.

Note You can learn much more about CSS in our CSS Tutorial.

CSS 문법

CSS 스타일링은 다음의 문법을 따른다:

element { property:value ; property:value }

element 는 HTML element name.  property 는 CSS property. value 는 CSS value.

다수의 styles 들은 semicolon으로 분리된다.


인라인(Inline) Styling (Inline CSS)

인라인 스타일링(Inline styling)은 하나의 HTML 요소에 독특한 스타일을 적용 할 경우 유용하다.

인라인 스타일링은 style 속성을 사용합니다.

아래의 예는 하나의 제목의 텍스트 색상을 변경한다:

Example

<h1 style="color:blue;">This is a Blue Heading</h1>
Try it Yourself »

내부(Internal) Styling (내부 CSS)

하나의 페이지에 공통의 스타일을 정의 하려면 내부(internal) 스타일 시트를 사용할 수 있다.

내부 스타일링(Internal styling)은 HTML 페이지의 <head> 섹션 안에 다음과 같이 <style> 태그를 사용하여 정의된다.:

Example

<!DOCTYPE html>
<html>
<head>
<style>
body {background-color:lightgrey;}
h1   {color:blue;}
p    {color:green;}
</style>
</head>
<body>

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

</body>
</html>
Try it Yourself »

External Styling (External CSS)

스타일을 많은 페이지에 적용할 때 외부 스타일 시트(External style sheet)가 이상적이다.

외부 스타일 시트를 사용하면 하나의 파일을 변경하여 전체 웹 사이트의 모양을 변경할 수 있습니다.

외부 스타일(External styles)은 페이지의 <head> 섹션 안에 <link> 요소로 정의된다. :

Example

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="styles.css">
</head>
<body>

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

</body>
</html>
Try it Yourself »

CSS Fonts

CSS property color 는 HTML 요소에 사용될 텍스트 컬러를 정의한다. 

CSS property font-family 는 HTML 요소에 사용될 폰트를 정의한다. 

CSS property font-size 는 HTML 요소에 사용될 텍스트 크기를 정의한다. 

Example

<!DOCTYPE html>
<html>
<head>
<style>
h1 {
    color: blue;
    font-family: verdana;
    font-size: 300%;
}
p  {
    color: red;
    font-family: courier;
    font-size: 160%;
}
</style>
</head>
<body>

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

</body>
</html>
Try it Yourself »

Note    HTML 의 옛 버전에서 지원되던 <font> 태그는 HTML5에서는 유효하지 않다.

The CSS Box Model

모든 시각적 HTML element 는 보이지 않을 지라도 주위를 박스가 감싼다. 

CSS border property 는 HTML 요소 주위의 보이는 테두리를 정의한다. :

Example

p {
    border: 1px solid black;
}
Try it Yourself »

CSS padding property 는 테두리 내부의 패딩(공간)을 정의한다.:

Example

p {
    border: 1px solid black;
    padding: 10px;
}
Try it Yourself »

CSS margin property 은 테두리 바깥의 여백(공간)을 정의 한다. :

Example

p {
    border: 1px solid black;
    padding: 10px;
    margin: 30px;
}
Try it Yourself »

Note  위의 CSS 예재는 크기를 pixel 로 정의 하는데 px 를 사용한다. 

id 속성

위의 모든 예재는 일반적인 방법으로 HTML 요소를 스타일하는 CSS 를 사용하였다.

CSS styles 은 모든 동일한 요소에 대한 동일한 스타일을 정의 한다. 

특별한 요소에 특별한 스타일을 정의 하려면, 그 요소에 id 속성을 추가 한다.:

<p id="p01">I am different</p>

그 다음, 해당 요소에 대한 다른 스타일을 정의 한다. :

Example

#p01 {
    color: blue;
}
Try it Yourself »

class 속성

요소의 특별한 타입(클래스:class)에 스타일을 정의 하려면, 그 요소에 class 속성을 추가한다.:

<p class="error">I am different</p>

이제, 이 타입(클래스:class)의 요소의 다른 스타일을 정의할 수 있다. :

Example

p.error {
    color: red;
}
Try it Yourself »

Note   단일 요소를 언급할 때 id 를 사용하고, 요소의 그룹을 언급하고자 할때 class 를 사용한다. 

사용되지 않는(Deprecated) Tags 와 Attributes in HTML5

In older HTML versions, several tags and attributes were used to style documents.

These tags are not supported in HTML5.

Avoid using the elements: <font>, <center> and <strike>.

Avoid using the attributes: color and bgcolor.


Chapter Summary

  • Use the HTML style attribute for inline styling
  • Use the HTML <style> element to define internal CSS
  • Use the HTML <link> element to define external CSS
  • Use the HTML <head> element to store <style> and <link> elements
  • Use the CSS color property for text colors
  • Use the CSS font-family property for text fonts
  • Use the CSS font-size property for text sizes
  • Use the CSS border property for visible element borders
  • Use the CSS padding property for space inside the border
  • Use the CSS margin property for space outside the border

Test Yourself with Exercises!

Exercise 1 »  Exercise 2 »  Exercise 3 »  Exercise 4 »  Exercise 5 »  Exercise 6 »


HTML Style Tags

Tag Description
<style> Defines style information for a document
<link> Defines a link between a document and an external resource