Placeholder Image

Subtitles section Play video

  • (electronic intro chime)

  • - [Instructor] Custom properties introduce

  • a new level of configuration and customization to CSS

  • and being able to hook into

  • and manipulate these custom properties using JavaScript,

  • opens up a whole new dimension of possibilities.

  • In this example, I've used custom properties

  • to set the color and background color

  • of each of the link items.

  • You'll notice here, I'm setting up the color

  • to use the custom property color,

  • and then there's a fallback value, black,

  • if we don't have color defined,

  • and the same thing for background color.

  • There's a variable or a custom property called BG color

  • and then a fallback call for transparent

  • if there is no custom property defined.

  • I'm using these fallbacks because in the main style sheet,

  • there is no custom properties to find.

  • These two colors here,

  • the color and background color are never defined.

  • There are some other custom properties

  • defined at the very top.

  • If we go look, we have max width and white space,

  • but there's no color and no BG color.

  • This is one example of how we can use custom properties

  • to do really advanced styling for individual items

  • because although color and BG color are not defined

  • in the style sheet,

  • they are defined in line on one of the elements.

  • You can see it here.

  • We have the element, then a style attribute,

  • and then we have color and BG color defined

  • and as a result,

  • these custom properties kick in for this one element here.

  • So that means we can define

  • how the styling is going to happen in our style sheet

  • and then use custom properties only where necessary

  • in line in the elements themselves,

  • which is really cool

  • and we can see how this could easily be extended

  • using JavaScript by injecting custom property values

  • into elements when needed.

  • So this begs the obvious question,

  • how do we access custom properties using JavaScript?

  • The answer is the same way we've been doing it so far.

  • So here's an example of how to access

  • the in line custom properties for that element.

  • We first go grab the element itself,

  • and then we say console.log(listItem), so the element,

  • style, and then use getPropertyValue

  • and call for the property value.

  • Run this in the browser and you see in the console,

  • we have the value for that property.

  • And this is the value of the custom property

  • defined in line.

  • We can go back and look at it.

  • It's this value right here.

  • You'll also notice I'm using get property value here

  • because the custom property name starts with two dashes

  • and as we've now learned,

  • two dashes as a JavaScript property name does not work.

  • If we try to say listItemstyle.--color,

  • it simply wouldn't work.

  • It would be a syntax error,

  • so here we have to use get property value.

  • It also means we can't do some of this traversing

  • that we've been doing previously

  • because the custom properties are just different.

  • So that's in line styles and it makes sense.

  • What about style sheets?

  • You'll remember in my style sheet,

  • I've defined some custom properties here at the very top.

  • So this is the first rule.

  • That means if I go and say document.styleSheets[0],

  • I get the first style sheet.

  • Then I can go in and grab the CSS rules

  • on the first element from that.

  • So I'll say console.log(styleSheet).

  • That gives me the current style sheet

  • and then I'll say cssRules.

  • And here I want the first rule

  • so that has index number zero.

  • And then I want to know about the custom properties.

  • So here I have max width and white space.

  • So let's say style.getPropertyValue and --max=width.

  • Save, back in the browser, 70 VW.

  • And that's really all there is to it.

  • Custom properties behave exactly the same way

  • as any other CSS property.

  • The only thing you need to keep in mind is that

  • there are no JavaScript defined property names for these.

  • So these camel case property names,

  • because the custom properties always start with dash dash.

  • So here you have to use getPropertyValue

  • to get at the property value.

  • (upbeat electronic music)

(electronic intro chime)

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it

A2 custom property defined javascript style sheet

Javascript Tutorial - Explore custom properties in CSS

  • 40 7
    Summer posted on 2022/10/09
Video vocabulary

Keywords

individual

US /ˌɪndəˈvɪdʒuəl/

UK /ˌɪndɪˈvɪdʒuəl/

  • adjective
  • Made for use by one single person
  • Relating to, or characteristic of, a single person or thing.
  • Single; separate.
  • Having a striking or unusual character; original.
  • Made for or relating to a single person or thing.
  • Having a distinct manner different from others
  • Relating to, or characteristic of, a single person or thing.
  • Single; separate.
  • Having a striking personal quality or style.
  • noun
  • Single person, looked at separately from others
  • A particular person or thing distinguished from others of the same kind.
  • A person, especially one of specified character.
  • A person, especially one of a specified kind.
  • A single thing or item, especially when part of a set or group.
  • A single human being as distinct from a group.
  • A competition for single people.
obvious

US /ˈɑbviəs/

UK /ˈɒbviəs/

  • adjective
  • Easily understood and clear; plain to see
  • Easily perceived or understood; clear, self-evident, or apparent.
  • Very easy to notice; blatant.
  • Easily perceived or understood; clear, self-evident, or apparent.
  • Easy to see or notice.
  • Predictable or lacking in subtlety.
  • Unmistakable; easily recognized.
  • Easily noticed; not subtle.
access

US /ˈæksɛs/

UK /'ækses/

  • noun
  • The ability or right to enter, use, or look at something.
  • Way to enter a place, e.g. a station or stadium
  • The means or opportunity to approach or enter a place.
  • A means of approaching or entering a place.
  • The right or opportunity to use or look at something.
  • verb
  • To obtain or retrieve (computer data or a file).
  • (Of a computer) to find and use (data).
  • To be able to use or have permission to use
  • To obtain or retrieve (data or information) from a computer or other device.
  • other
  • The action or way of approaching, entering, or using.
  • The means or opportunity to approach or enter a place.
  • The opportunity or right to use something or to see someone.
  • other
  • To obtain or retrieve (data or information, typically held in a computer).
grab

US /ɡræb/

UK /ɡræb/

  • verb
  • To take and hold something quickly
sense

US /sɛns/

UK /sens/

  • noun
  • Certain mental feeling or emotion
  • Normal or clear state of mind
  • Meaning of a particular word, phrase or text
  • verb
  • To perceive using sight, sound, taste touch etc.
  • To recognize the presence of something
manipulate

US /məˈnɪpjəˌlet/

UK /mə'nɪpjʊleɪt/

  • verb
  • To try to control others to gain an advantage
  • To handle or work in a skillful manner
  • other
  • To control or influence (a person or situation) cleverly, unfairly, or unscrupulously.
  • To falsify or change (data or statistics).
  • To handle or control (a tool or mechanism) in a skillful manner.
current

US /ˈkɚrənt, ˈkʌr-/

UK /'kʌrənt/

  • adjective
  • In general use or accepted by most people.
  • Of or relating to the present time; up-to-date.
  • Happening or being in the present time
  • Happening or existing now; belonging to the present time.
  • Valid or up-to-date.
  • noun
  • Electricity flowing through wires
  • Currency in circulation.
  • Movement of water in a river, or air in the sky
  • A widespread feeling or opinion.
  • A general tendency or course of events.
  • other
  • A flow of electrical charge through a conductor.
property

US /ˈprɑpəti/

UK /'prɒpətɪ/

  • noun
  • Particular quality that someone or something has
  • A quality or characteristic of something.
  • A building or area of land.
  • A rule or fact in mathematics.
  • Buildings or piece of land owned by someone
  • A quality or characteristic of something.
  • An object used on stage or in a film.
  • An object used on the stage in a play or film; a prop.
  • other
  • Land and buildings.
  • The legal right to own something.
  • Something that is owned by a person or organization.
simply

US /ˈsɪmpli/

UK /ˈsɪmpli/

  • adverb
  • In an easy or clear manner
transparent

US /trænsˈpærənt/

UK /trænsˈpærənt/

  • adjective
  • Being open to see and to public criticism
  • Able to be seen through
  • Being simple and easily understood