Skip to content

Commit f3347c6

Browse files
committed
Add css-styling-dimensions to jquery-basics
1 parent b54dadd commit f3347c6

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
chapter : "jqfundamentals"
3+
section : "3"
4+
title : "CSS, Styling, & Dimensions"
5+
---
6+
## CSS, Styling, & Dimensions
7+
8+
jQuery includes a handy way to get and set CSS properties of elements.
9+
10+
<div class="note" markdown="1">
11+
### Note
12+
13+
CSS properties that normally include a hyphen need to be camel cased in JavaScript.
14+
For example, the CSS property font-size is expressed as fontSize when used as a property name in JavaScript.
15+
This does not apply, however, when passing the name of a CSS property to the $.fn.css method as a string — in that case, either the camel cased or hyphenated form will work.
16+
</div>
17+
18+
<div class="example" markdown="1">
19+
Getting CSS properties
20+
21+
$('h1').css('fontSize'); // returns a string such as "19px"
22+
$('h1').css('font-size'); // also works
23+
</div>
24+
25+
<div class="example" markdown="1">
26+
Setting CSS properties
27+
28+
$('h1').css('fontSize', '100px'); // setting an individual property
29+
$('h1').css({ 'fontSize' : '100px', 'color' : 'red' }); // setting multiple properties
30+
</div>
31+
32+
Note the style of the argument we use on the second line — it is an object that contains multiple properties. This is a common way to pass multiple arguments to a function, and many jQuery setter methods accept objects to set mulitple values at once.
33+
34+
### Using CSS Classes for Styling
35+
36+
As a getter, the $.fn.css method is valuable; however, it should generally be avoided as a setter in production-ready code, because you don't want presentational information in your JavaScript. Instead, write CSS rules for classes that describe the various visual states, and then simply change the class on the element you want to affect.
37+
38+
<div class="example" markdown="1">
39+
Working with classes
40+
41+
var $h1 = $('h1');
42+
43+
$h1.addClass('big');
44+
$h1.removeClass('big');
45+
$h1.toggleClass('big');
46+
47+
if ($h1.hasClass('big')) { ... }
48+
</div>
49+
50+
Classes can also be useful for storing state information about an element, such as indicating that an element is selected.
51+
52+
### Dimensions
53+
54+
jQuery offers a variety of methods for obtaining and modifying dimension and position information about an element.
55+
56+
The code in “Basic dimensions methods”, is just a very brief overview of the dimensions functionality in jQuery;
57+
for complete details about jQuery dimension methods, visit [http://api.jquery.com/category/dimensions/](http://api.jquery.com/category/dimensions/ "Dimensions documentation on api.jquery.com").
58+
59+
<div class="example" markdown="1">
60+
Basic dimensions methods
61+
62+
$('h1').width('50px'); // sets the width of all H1 elements
63+
$('h1').width(); // gets the width of the first H1
64+
65+
$('h1').height('50px'); // sets the height of all H1 elements
66+
$('h1').height(); // gets the height of the first H1
67+
68+
$('h1').position(); // returns an object containing position
69+
// information for the first H1 relative to
70+
// its "offset (positioned) parent"
71+
</div>

0 commit comments

Comments
 (0)