jQuery / Reference / .css()

개요

.css()로 선택한 요소의 css 속성값을 가져오거나 style 속성을 추가할 수 있습니다.

문법 1 - 속성의 값 가져오기

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
.css( propertyName )
.css( propertyName )
.css( propertyName )
  • propertyName의 속성값을 가져옵니다. 예를 들어 다음은 h1 요소의 스타일 중 color 속성의 값을 가져옵니다.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$( "h1" ).css( "color" );
$( "h1" ).css( "color" );
$( "h1" ).css( "color" );

문법 2 - 속성 추가하기

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
.css( propertyName, value )
.css( propertyName, value )
.css( propertyName, value )
  • style 속성을 추가합니다. 예를 들어 다음은 h1 요소에 style 속성을 추가하여 글자색을 녹색으로 바꿉니다.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$( "h1" ).css( "color", "green" );
$( "h1" ).css( "color", "green" );
$( "h1" ).css( "color", "green" );
  • 여러 속성을 변경하고 싶다면 다음과 같이 합니다.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
.css( {
propertyName1: value1,
propertyName2: value2,
propertyName3: value3
} )
.css( { propertyName1: value1, propertyName2: value2, propertyName3: value3 } )
.css( {
  propertyName1: value1,
  propertyName2: value2,
  propertyName3: value3
} )

예제 1 - 속성의 값 가져오기

  • h1 요소의 color 속성의 값을 출력합니다.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!doctype html>
<html lang="ko">
<head>
<meta charset="utf-8">
<title>jQuery</title>
<style>
h1 {color: red;}
</style>
<script src="//code.jquery.com/jquery-3.3.1.js"></script>
<script>
$(document).ready(function() {
var color = $( "h1" ).css( "color" );
$( "p" ).html( "Color is " + color );
});
</script>
</head>
<body>
<h1>Lorem ipsum dolor.</h1>
<p></p>
</body>
</html>
<!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>jQuery</title> <style> h1 {color: red;} </style> <script src="//code.jquery.com/jquery-3.3.1.js"></script> <script> $(document).ready(function() { var color = $( "h1" ).css( "color" ); $( "p" ).html( "Color is " + color ); }); </script> </head> <body> <h1>Lorem ipsum dolor.</h1> <p></p> </body> </html>
<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>jQuery</title>
    <style>
      h1 {color: red;}
    </style>
    <script src="//code.jquery.com/jquery-3.3.1.js"></script>
    <script>
      $(document).ready(function() {
        var color = $( "h1" ).css( "color" );
        $( "p" ).html( "Color is " + color );
      });
    </script>
  </head>
  <body>
    <h1>Lorem ipsum dolor.</h1>
    <p></p>
  </body>
</html>

예제 2 - 속성 추가하기

  • h1 요소의 글자색을 녹색으로 만듭니다.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!doctype html>
<html lang="ko">
<head>
<meta charset="utf-8">
<title>jQuery</title>
<style>
body {
font-family: Consolas, monospace;
font-size: 20px;
}
</style>
<script src="//code.jquery.com/jquery-3.3.1.js"></script>
<script>
$(document).ready(function() {
$( "h1" ).css( "color", "green" );
});
</script>
</head>
<body>
<h1>Lorem ipsum dolor.</h1>
</body>
</html>
<!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>jQuery</title> <style> body { font-family: Consolas, monospace; font-size: 20px; } </style> <script src="//code.jquery.com/jquery-3.3.1.js"></script> <script> $(document).ready(function() { $( "h1" ).css( "color", "green" ); }); </script> </head> <body> <h1>Lorem ipsum dolor.</h1> </body> </html>
<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>jQuery</title>
    <style>
      body {
        font-family: Consolas, monospace;
        font-size: 20px;
      }
    </style>
    <script src="//code.jquery.com/jquery-3.3.1.js"></script>
    <script>
      $(document).ready(function() {
        $( "h1" ).css( "color", "green" );
      });
    </script>
  </head>
  <body>
    <h1>Lorem ipsum dolor.</h1>
  </body>
</html>

  • h1 요소의 글자색을 녹색으로 만들고, 안쪽 여백과 테두리를 추가합니다.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!doctype html>
<html lang="ko">
<head>
<meta charset="utf-8">
<title>jQuery</title>
<style>
body {
font-family: Consolas, monospace;
font-size: 20px;
}
</style>
<script src="//code.jquery.com/jquery-3.3.1.js"></script>
<script>
$( document ).ready( function() {
$( 'h1' ).css( {
color: 'green',
padding: '20px',
border: '5px solid #dadada'
} );
} );
</script>
</head>
<body>
<h1>Lorem ipsum dolor.</h1>
</body>
</html>
<!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>jQuery</title> <style> body { font-family: Consolas, monospace; font-size: 20px; } </style> <script src="//code.jquery.com/jquery-3.3.1.js"></script> <script> $( document ).ready( function() { $( 'h1' ).css( { color: 'green', padding: '20px', border: '5px solid #dadada' } ); } ); </script> </head> <body> <h1>Lorem ipsum dolor.</h1> </body> </html>
<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>jQuery</title>
    <style>
      body {
        font-family: Consolas, monospace;
        font-size: 20px;
      }
    </style>
    <script src="//code.jquery.com/jquery-3.3.1.js"></script>
    <script>
      $( document ).ready( function() {
        $( 'h1' ).css( {
          color: 'green',
          padding: '20px',
          border: '5px solid #dadada'
        } );
      } );
    </script>
  </head>
  <body>
    <h1>Lorem ipsum dolor.</h1>
  </body>
</html>

참고

  • HTML 요소(element)의 속성(attribute)의 값을 가져오거나 속성을 추가할 때는 .attr()을 사용합니다.
같은 카테고리의 다른 글
jQuery / Reference / .each()

jQuery / Reference / .each()

jQuery의 .each() 함수는 주로 배열, 객체, 또는 jQuery 선택자로 선택된 요소 집합에 대해 반복 작업을 수행하기 위해 사용됩니다. JavaScript의 for 또는 forEach 루프와 유사하지만, jQuery의 특성과 통합된 방식으로 작동합니다.

jQuery / Reference / .removeClass()

jQuery / Reference / .removeClass()

개요 .removeClass()로 선택한 요소에서 클래스 값을 제거할 수 있습니다. 문법 .removeClass( className ) 클래스 값은 큰 따옴표 또는 작은 따옴표로 감쌉니다. $( 'h1' ).removeClass( 'abc' ); 띄어쓰기로 구분하여 여러 개의 값을 제거할 수 있습니다. $( 'h1' ).removeClass( 'ab cd ef' ); 페이지가 로드된 상태에서 클래스 값이 제거되는 것이므로, 제거되기 전의 모양에서 제거된 후의 모양으로 변하는 것을 방문자가 볼 수도 ...

jQuery / Reference / .appendTo()

jQuery / Reference / .appendTo()

개요 .appendTo()는 선택한 요소를 다른 요소의 종료 태그 앞으로 이동시킵니다. 문법 .appendTo( target ) 예제 abc를 클래스 값으로 가지는 span 요소를 h1 요소의 </h1> 앞으로 이동시킵니다. <!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>jQuery</title> <style> body { ...

jQuery / Reference / .has()

jQuery / Reference / .has()

개요 .has()로 특정 요소를 가지고 있는 요소를 선택할 수 있습니다. 문법 .has( selector ) 예를 들어 다음은 span 요소를 가지고 있는 h1 요소를 를 선택합니다. $( 'h1' ).has( 'span' ) 예제 span 요소를 포함하고 있는 h1 요소의 글자색을 빨간색으로 만듭니다. <!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>jQuery</title> <script ...

jQuery / Reference / .toggleClass()

jQuery / Reference / .toggleClass()

개요 .toggleClass()로 선택한 요소에 클래스 값을 넣었다 뺐다 할 수 있습니다. 문법 .toggleClass( className ) 예를 들어 다음은 p 요소에 xyz 클래스가 없으면 추가하고, 있으면 제거합니다. $( 'p' ).toggleClass( 'xyz' ); 예제 버튼을 클릭하면 h1 요소에 ab 클래스 값이 추가되어 배경색이 생기고, 다시 버튼을 클릭하면 ab 클래스 값이 제거되어 배경색이 사라집니다. <!doctype html> <html lang="ko"> <head> ...

jQuery / Reference / .remove()

jQuery / Reference / .remove()

개요 .remove()는 선택한 요소를 HTML 문서에서 제거합니다. 문법 .remove( ) 특정 선택자를 가진 요소를 제거할 때는 괄호 안에 선택자를 넣습니다. 예를 들어 다음은 클래스 값으로 rm을 가진 p 요소를 제거합니다. $( 'p' ).remove( '.rm' ); 다음과 같이 해도 결과는 같습니다. $( 'p.rm' ).remove(); 예제 버튼을 클릭하면 rm을 클래스 값으로 가지는 h1 요소를 제거합니다. <!doctype html> <html lang="ko"> <head> ...

jQuery / Reference / .after()

jQuery / Reference / .after()

개요 .after()는 선택한 요소 뒤에 새 요소를 추가하거나, 다른 곳에 있는 요소를 이동시킵니다. 문법 .after( content ) 예제 1 h1 요소 뒤에 Hello World!를 내용으로 갖는 p 요소를 추가합니다. <!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>jQuery</title> <script src="//code.jquery.com/jquery-3.3.1.min.js"></script> <script> ...

jQuery / Reference / .insertBefore()

jQuery / Reference / .insertBefore()

개요 .insertBefore()는 특정 요소 앞에 요소를 추가하거나 이동시킵니다. 문법 .insertBefore( target ) 예제 1 p 요소 앞에 <h1>Hello</h1>를 추가한다. <!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>jQuery</title> <style> body { font-family: Consolas, monospace; ...

jQuery / Reference / .wrapAll()

jQuery / Reference / .wrapAll()

개요 .wrapAll()은 선택한 요소 모두를 새로운 태그로 감쌉니다. 문법 .wrapAll( wrappingElement ) 예를 들어 다음은 모든 p 요소를 모아서 div 태그로 감쌉니다. $( 'p' ).wrap( '<div></div>' ); 예제 1 .wrap()은 선택한 요소에 각각 적용됩니다. <!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>jQuery</title> <style> div ...

jQuery / Reference / .attr()

jQuery / Reference / .attr()

개요 .attr()은 요소(element)의 속성(attribute)의 값을 가져오거나 속성을 추가합니다. 문법 1 - 속성의 값 가져오기 .attr( attributeName )  예를 들어 아래는 div 요소의 class 속성의 값을 가져옵니다. $( 'div' ).attr( 'class' ); 문법 2 - 속성 추가하기 .attr( attributeName, value )  예를 들어 아래는 h1 요소에 title 속성을 추가하고 속성의 값은 Hello로 합니다. $( 'h1' ).attr( 'title', 'Hello' ); 예제 1 ...