Chapter 4 · Basics
Styles and visibility
In this chapter you'll learn how to change the way elements look: their colours and sizes, their classes, whether they're shown or hidden, and how big they are.
Two ways to change the look
There are two ways to change how an element looks with Nyx:
.css() to change one thing, like the colour. Quick and handy for small changes..addClass() to apply them all at once.Both work. As a rule of thumb, use .css() for one quick change, and classes for anything you'll reuse or that changes several styles together. Classes keep your design in your CSS file, where it belongs.
Changing one style
Give .css() two things: the name of the CSS property and its new value.
.css(Change a style
"color"Which property
, "red"Its new value
)Done
Exam results
Results will be announced on Friday.
Try other colours, like "teal" or "#007aff", or try "background", "lightyellow".
Numbers mean pixels
For sizes, you can give a plain number instead of text. Nyx adds px for you, so these two lines do the same thing:
nyx("#v-info").css("font-size", "18px");
nyx("#v-info").css("font-size", 18);A few properties have no unit, so Nyx leaves their numbers alone: opacity, z-index, font-weight, line-height, flex and order. For any other unit, like % or em, write it as text: "50%".
Two ways to write the property name
You can write the name exactly as in CSS, with a dash, or join the words and use a capital letter instead. Both work:
Like CSS
.css("font-size", 18)Joined up
.css("fontSize", 18)Changing several styles
To change several styles at once, put them inside curly brackets { }, separated by commas. Inside the brackets, use the joined-up names, like fontSize.
{ ... }A group of settings. Each setting is a name, a colon:, then a value.background: "gold",Text values go in quotes. A comma separates one setting from the next.padding: 20,Numbers don't need quotes. This becomes20px.
Reading a style
Give .css() only the property name to find out its current value:
Read my styles
Colours come back in the browser's own format, "rgb(0, 122, 255)", even if you wrote #007aff. They mean the same colour.
Adding and removing classes
First, write the styles in your CSS under a class name. For example, a class that crosses out finished tasks:
.v-done {
text-decoration: line-through;
color: gray;
}Then use Nyx to put the class on an element, or take it off:
.addClass("v-done")Add the class. The element gets all its styles at once..removeClass("v-done")Take the class off again..hasClass("v-done")true if the element has the class, false if not.- Finish maths homework
- Read chapter 4
- Pack school bag
In a selector, a class needs a dot: nyx(".v-done"). But .addClass(), .removeClass() and .hasClass() already know it's a class, so you write the name without the dot: .addClass("v-done").
To add or remove several classes at once, separate them with spaces: .addClass("v-done v-highlight").
On this site, the classes v-done, v-highlight, v-big and v-selected are already in the page's CSS, so you can try them in any example on this page.
Switching a class on and off
.toggleClass() adds a class if it's missing and removes it if it's there. It's perfect for buttons that switch something on and off.
Photosynthesis is how plants make food from sunlight.
Press Run, then click Highlight a few times.
You can also tell .toggleClass() exactly what you want with a second value: true always adds the class, false always removes it.
// Highlight the box only when the score is 90 or more
nyx("#v-score").toggleClass("v-highlight", score >= 90);Showing and hiding
.hide()Hide the element. The page closes the gap where it was..show()Show it again, exactly as it looked before..toggle()Hide it if it's showing, show it if it's hidden..visible()true if it can be seen, false if it's hidden.Hint: the answer is a planet.
The hint starts hidden with the hidden attribute in the HTML. Nyx's .show() and .toggle() know how to undo that too.
Want it to fade or slide?
.hide(), .show() and .toggle() are always instant. In Nyx, every kind of movement has its own name, so you can tell what a line does just by reading it:
.hide().fadeOut("400ms") or .slideUp("400ms").show().fadeIn("400ms") or .slideDown("400ms").toggle().fadeToggle("400ms") or .slideToggle("400ms")To run code once a smooth one has finished, add .done():
nyx("#v-popup").fadeOut("400ms").done(() => {
nyx("#v-msg").text("Popup closed");
});You'll learn all about these in Animation.
In jQuery you might write .hide(400, fn). Nyx doesn't mix instant and smooth in one method. If you give .hide(), .show() or .toggle() a speed or a function, it still works instantly, and Nyx tells you in the console which smooth method to use instead.
Measuring size
.width()How wide the element's content is, in pixels, as a number..height()How tall its content is, in pixels, as a number..width(300)Set the content width to 300 pixels. .height(300) works the same way..offset()Where it is on the page: { top, left }, in pixels from the top-left corner..width() gives you a real number, so you can do maths with it: nyx("#v-bar").width(nyx("#v-bar").width() + 50) makes it 50 pixels wider.
To measure padding, borders, margins and the window too, see Dimensions.
Scrolling to an element
.scrollTo() smoothly scrolls the page until the element is in view. It's great for "Back to top" buttons, or for taking visitors to the first mistake in a form.
nyx("#v-top-btn").on("click", () => {
nyx("#v-header").scrollTo();
});The links in the "In this chapter" box at the top of this page work exactly like this.
Putting it together
Here's a small FAQ. Clicking a question shows or hides its answer, and a class colours the question while it's open. Press Run, then click the questions.
When is the exam?
Monday, 15 February, at 10 am.
What should I bring?
Two pens, a pencil and your admit card.
nyx(".v-q").on("click", ...)Run this code whenever any question is clicked.(event, question) =>Nyx hands you the question that was clicked, already wrapped in Nyx, so it's ready to use. You'll learn more about this in Events.question.toggleClass("v-open");Switch the colour class on that question.question.next().toggle();.next()finds the element right after the question, which is its answer. Then.toggle()shows or hides it. More about.next()in Moving around.
Common mistakes
.addClass(".active")No dot in class methods. Write .addClass("active")..css("color" "red")Missing comma between the two values: .css("color", "red")..css({ font-size: 18 })Inside { }, a name with a dash breaks the code. Write fontSize: 18, or put the name in quotes: "font-size": 18..css("width", "50")Text without a unit means nothing to CSS. Write "50px", or the number 50..addClass("done") with no CSSAdding a class does nothing on its own. Make sure your CSS has a .done { ... } rule.Good to know
.css("color", "")Empty text removes a style you set, so the element goes back to what your CSS file says..removeClass("a b")Removes several classes at once, separated by spaces..toggle(true)Always show. .toggle(false) always hides.Exercise
Fill in the blanks, then press Check answers.
1. Make the text of #title green.
nyx("#title").css("", "green");
2. Add the class active to #tab.
nyx("#tab").("active");
3. Switch the class open on and off on #menu.
nyx("#menu").("open");
4. Hide the element #popup.
nyx("#popup").();
5. Check whether #popup can be seen.
if (nyx("#popup").()) { ... }
6. Make #box 200 pixels wide.
nyx("#box").(200);
Remember
.css("property", "value") changes one style. Use { } for several..css("padding", 12)..addClass(), .removeClass(), .toggleClass(), .hasClass()..hide(), .show() and .toggle() hide and show. .visible() checks..width() and .height() measure in pixels. .scrollTo() brings an element into view.