COM271, Week 9

DHTML: Accessing CSS properties through the DOM

Syllabus | Table of Pages | Assignments | References and Useful Links

With the brief overview of the DOM, let's look a bit closer at accessing CSS properties. Any CSS property has an equivalent name as a javascript DOM style object. The properties are similar: JavaScript drops any hyphens, substitution a camelback style. For example, border-bottom becomes borderBottom.

For the complete list, see "HTML DOM Style Object" (http://www.w3schools.com/jsref/dom_obj_style.asp), which is the source of what follows:

Style object

The Style object represents an individual style statement.

The Style object can be accessed from the document or from the elements to which that style is applied.

Syntax for using the Style object properties:

document.getElementById("id").style.property="value"

Note: Click on property, below, to go to w3schools pages for acceptable values for each property.

The Style object property categories:

Background

PropertyDescription
backgroundSets all background properties in one
backgroundAttachment Sets whether a background-image is fixed or scrolls with the page
backgroundColor Sets the background-color of an element
backgroundImage Sets the background-image of an element
backgroundPosition Sets the starting position of a background-image
backgroundPositionX Sets the x-coordinates of the backgroundPosition property
backgroundPositionY Sets the y-coordinates of the backgroundPosition property
backgroundRepeat Sets if/how a background-image will be repeated

Border and Margin

PropertyDescription
borderSets all properties for the four borders in one
borderBottomSets all properties for the bottom border in one
borderBottomColorSets the color of the bottom border
borderBottomStyleSets the style of the bottom border
borderBottomWidthSets the width of the bottom border
borderColorSets the color of all four borders (can have up to four colors)
borderLeftSets all properties for the left border in one
borderLeftColorSets the color of the left border
borderLeftStyleSets the style of the left border
borderLeftWidthSets the width of the left border
borderRightSets all properties for the right border in one
borderRightColor>Sets the color of the right border
borderRightStyleSets the style of the right border
borderRightWidthSets the width of the right border
borderStyleSets the style of all four borders (can have up to four styles)
borderTopSets all properties for the top border in one
borderTopColorSets the color of the top border
borderTopStyleSets the style of the top border
borderTopWidthSets the width of the top border
borderWidthSets the width of all four borders (can have up to four widths)
marginSets the margins of an element (can have up to four values)
marginBottomSets the bottom margin of an element
marginLeftSets the left margin of an element
marginRightSets the right margin of an element
marginTopSets the top margin of an element
outlineSets all outline properties in one
outlineColorSets the color of the outline around a element
outlineStyleSets the style of the outline around an element
outlineWidthSets the width of the outline around an element
paddingSets the padding of an element (can have up to four values)
paddingBottomSets the bottom padding of an element
paddingLeftSets the left padding of an element
paddingRightSets the right padding of an element
paddingTopSets the top padding of an element

Layout (partial list)

PropertyDescription
clearSets on which sides of an element other floating elements are not allowed
clipSets the shape of an element
cssFloatSets where an image or a text will appear (float) in another element
cursorSets the type of cursor to be displayed
displaySets how an element will be displayed
heightSets the height of an element
maxHeightSets the maximum height of an element
maxWidthSets the maximum width of an element
minHeightSets the minimum height of an element
minWidthSets the minimum width of an element
overflowSpecifies what to do with content that does not fit in an element box
verticalAlignSets the vertical alignment of content in an element
visibilitySets whether or not an element should be visible
widthSets the width of an element

List properties

PropertyDescription
listStyleSets all the properties for a list in one
listStyleImage Sets an image as the list-item marker
listStylePositionPositions the list-item marker
listStyleTypeSets the list-item marker type

Positioning

PropertyDescription
bottomSets how far the bottom edge of an element is above/below the bottom edge of the parent element
leftSets how far the left edge of an element is to the right/left of the left edge of the parent element
positionPlaces an element in a static, relative, absolute or fixed position
rightSets how far the right edge of an element is to the left/right of the right edge of the parent element
topSets how far the top edge of an element is above/below the top edge of the parent element
zIndexSets the stack order of an element

Printing

PropertyDescription
pageBreakBeforeSets the page-breaking behavior before an element
pageBreakInsideSets the page-breaking behavior inside an element

Table

PropertyDescription
borderCollapseSets whether the table border are collapsed into a single border or detached as in standard HTML
borderSpacingSets the distance that separates cell borders
captionSideSets the position of the table caption
emptyCellsSets whether or not to show empty cells in a table

Text

PropertyDescription
colorSets the color of the text
fontSets all font properties in one
fontFamilySets the font of an element
fontSizeSets the font-size of an element
fontSizeAdjustSets/adjusts the size of a text
fontStretchSets how to condense or stretch a font
fontStyleSets the font-style of an element
fontVariantDisplays text in a small-caps font
fontWeightSets the boldness of the font
letterSpacingSets the space between characters
lineHeightSets the distance between lines
textAlignAligns the text
textDecorationSets the decoration of a text
textIndentIndents the first line of text
textTransformSets capitalization effect on a text
whiteSpaceSets how to handle line-breaks and white-space in a text
wordSpacingSets the space between words in a text

—Source: W3Cschools.org

JS & DOM Reference

Overview

JavaScript Objects

JS Array JS Boolean JS Date JS Math JS Number JS String JS RegExp JS Global

Browser Objects

Window Navigator Screen History Location

HTML DOM Objects

Document Events Elements Anchor Area Base Body Button Form Frame/IFrame Frameset Image Input Button Input Checkbox Input File Input Hidden Input Password Input Radio Input Reset Input Submit Input Text Link Meta Object Option Select Style Table TableCell TableRow Textarea