Jump to: navigation, search

JQuery CSS

From w3cyberlearnings

Contents

What is the css method in jQuery?

  • Apply the CSS property using jQuery method.

The jQuery css method syntax

SELECTOR.css(CSS_PROPERTY_NAME, CSS_PROPERTY_VALUE);

Example 1

  • Set the div content property to have the blue background color.
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>CSS Example 1</title>
		<script 
			type="text/javascript" 
			src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
					
		</script>
		<script type="text/javascript" language="javascript">
			$(document).ready(function() {
				$('div#mycontent').css('background-color','blue'); 
			});
		</script>
	</head>
	<body>
		<div id="mycontent">Hello CSS jQuery</div>
	</body>
</html>

Multiple CSS properties

  • You can set multiple CSS properties.

Multiple CSS properties Syntax

SELECTOR.css(
          {
             Name_1:Value_1,
             Name_2:Value_2,
             Name_3:Value_3
          });

Example -Multiple CSS Properties 1

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>CSS Example 1</title>
		<script 
			type="text/javascript" 
			src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
					
		</script>
		<script type="text/javascript" language="javascript">
			$(document).ready(function() {
			       $('div#mycontent').css({
					'background-color':'blue',
					'font-size':'25',
					'color':'white'}); 
			});
		</script>
	</head>
	<body>
		<div id="mycontent">Hello CSS jQuery</div>
	</body>
</html>

Assign CSS property using jQuery addClass method

  • Require to define the CSS stylesheet first before assign in the addClass method.

addClass method syntax

SELECTOR.addClass(CSS);

Example -addClass method

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Using addClass</title>
		<script 
			type="text/javascript" 
			src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
					
		</script>
		<style type="text/css">
			.myCss {
				font-size: 18px;
				border: 2px solid #06b;
			}
		</style>
		<script type="text/javascript" language="javascript">
			$(document).ready(function() {
				$('div#mycontent').addClass('myCss');			
			});
		</script>
	</head>
	<body>
		<div id="mycontent">Learning jQuery @ w3cyberlearnings dot com</div>
	</body>
</html>

Remove CSS stylesheet property with removeClass method

  • removeClass method uses to remove the CSS stylesheet class.

The removeClass method syntax

  SELECTOR.removeClass(CSS);

Example -removeClass method

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Using addClass</title>
		<script 
			type="text/javascript" 
			src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
					
		</script>
		<style type="text/css">
			.myCss {
				font-size: 18px;
				border: 2px solid #06b;
			}
		</style>
		<script type="text/javascript" language="javascript">
			$(document).ready(function() {
				$('div#mycontent').addClass('myCss').removeClass('myCss');			
			});
		</script>
	</head>
	<body>
		<div id="mycontent">Learning jQuery @ w3cyberlearnings dot com</div>
	</body>
</html>

Example -addClass and removeClass

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>CSS Example</title>
		<script 
			type="text/javascript" 
			src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
					
		</script>
		<style type="text/css">
			.css1 {
				font-size: 18px;
				border: 2px solid #06b;
			}
			.css2 {
				color:red;
				background-color:green;
			}
		</style>
		<script type="text/javascript" language="javascript">
			$(document).ready(function() {
				$('#mybtn_add').click(function(){
					$('div#mycontent').addClass('css1');
					$('div#mycontent').removeClass('css2');
				});
				$('#mybtn_remove').click(function(){
					$('div#mycontent').addClass('css2');
					$('div#mycontent').removeClass('css1');
				});		
			});
		</script>
	</head>
	<body>
		<button id="mybtn_add">Add</button>
		<button id="mybtn_remove">Remove</button>
		<div id="mycontent">Learning jQuery @ w3cyberlearnings dot com</div>
	</body>
</html>


Related Links


Jquery and HTML Elements

  1. jQuery SELECT Element
  2. jQuery Radio Button
  3. jQuery Checkbox Button
  4. jQuery Order and Un-order List
  5. jQuery Form Serialize
  6. jQuery Manipulate HTML Table
  7. jQuery Form Elements Selector
  8. jQuery Validate Form
  9. jQuery and CSS Style Sheet
Navigation
Web
SQL
MISC
References