Chapter 7 · Core
Dimensions
Dimensions are sizes: how wide and how tall something is. In this chapter you'll learn to measure and change the size of any element, with or without its padding, border and margin, and to measure the window and the whole page.
The four layers of a box
The browser treats every element as a box with four layers. Think of a photo in a frame, hanging on a wall:
So "how wide is it?" has four answers, depending on which layers you count. Nyx has a method for each:
All the methods
.width()The width of the content..height()The height of the content..innerWidth()The width of the content and padding..innerHeight()The height of the content and padding..outerWidth()The width of the content, padding and border..outerHeight()The height of the content, padding and border..fullWidth()The width of the content, padding, border and margin..fullHeight()The height of the content, padding, border and margin.Empty brackets measure. A value inside the brackets changes the size. A measurement is a plain number of pixels, so you can do maths with it.
width() and height()
These measure only the content. Padding, border and margin aren't counted. Look at the styles in the HTML below, then press Run.
The answers are 160 and 50, exactly as in the style.
innerWidth() and innerHeight()
These count the content and the padding. Padding is on both sides, so 20 pixels of padding adds 40.
Width: 160 + 20 + 20 = 200. Height: 50 + 20 + 20 = 90.
outerWidth() and outerHeight()
These also count the border. This is the whole box you can see, so it's usually the one to use when you ask "how much room does it fill?"
Width: 200 + 5 + 5 = 210. Height: 90 + 5 + 5 = 100.
fullWidth() and fullHeight()
These count everything, including the empty space of the margin. Use them to work out how much space an element takes away from its neighbours.
Width: 210 + 10 + 10 = 230. Height: 100 + 10 + 10 = 120.
jQuery includes the margin with .outerWidth(true) and .outerHeight(true). That works here too, but Nyx reminds you of its own names, .fullWidth() and .fullHeight(), which say what they do.
Changing the size
Put a number inside the brackets to change the size. All eight methods can do it.
nyx("#d-e")On this element
.width(set the width
200to 200 pixels
)Done
.width(200)A number means pixels..width("200px")The same, as text..width("50%")Any other CSS size, like "10em" or "50vw", goes straight to CSS as it is, on whichever method you use..width("auto")Let the browser choose the size..width("")Remove the size you set, and go back to your CSS file..width((old) => old + 20)Work out the new size from the old one.The layer you name is the layer that gets that size. To make the whole visible box 180 pixels wide, use .outerWidth(180). Nyx takes away the padding and border for you:
A grow button
Give a function to change the size you already have. Press Run, then click the button a few times.
Sizes and box-sizing
In CSS, width: 160px can mean two different things. The CSS property box-sizing decides which:
content-boxThe browser's normal setting. The content is 160 pixels wide, and padding and border are added outside it.border-boxVery common on modern sites. The whole box is 160 pixels wide, and padding and border are taken from inside it.You don't need to remember which one a page uses. In Nyx, .width() always means the content and .outerWidth() always means the whole box, on both kinds.
.css() follows box-sizing. On a border-box element, .css("width", 160) makes the whole box 160 pixels wide, while .width(160) makes the content 160 pixels wide, so the box is wider.
Several elements
When a selector finds several elements, measuring gives you the first one's size. Changing the size changes all of them. To measure every one, use .map(), which makes a list of answers.
The window and the page
You can measure two more things. The window is the part of the page the visitor can see right now. The document is the whole page, including the parts they must scroll to. Write both without quotes.
nyx(window).width()The visible width, without the scroll bar. .innerWidth() gives the same answer.nyx(window).height()The visible height, without the scroll bar. .innerHeight() gives the same answer.nyx(window).outerWidth()The visible width, with the scroll bar. .fullWidth() gives the same answer.nyx(window).outerHeight()The visible height, with the scroll bar. .fullHeight() gives the same answer.nyx(document).width()The width of the whole page. All four width methods give the same answer.nyx(document).height()The height of the whole page. All four height methods give the same answer.Make your browser window smaller, then press Run again.
On this website the text scrolls inside its own panel, so the page height is the same as the window height. On an ordinary long page, it's bigger. Code can measure the window, but can't resize it. If you try, Nyx tells you so.
When the window changes size
A measurement is a snapshot: it doesn't update by itself. To measure again whenever the visitor resizes the window or turns their phone, listen for resize. It happens many times a second, so nyx.debounce() waits until the visitor has stopped. You'll learn more about it in Timers and delays.
const update = () => {
nyx("#size").text(nyx(window).width() + " pixels wide");
};
nyx(window).on("resize", nyx.debounce("200ms", update));
update();The last line runs it once at the start, before any resizing. If you only want a different design on small screens, use a CSS media query instead. It needs no JavaScript.
Hidden elements
A hidden element takes up no space, so every measurement of it is 0. Show it first, then measure.
Pictures are similar. Until a picture has loaded, the browser may not know its size. Measure it in a load listener:
nyx(window).on("load", () => {
console.log(nyx("#photo").height());
});Putting it together
Cards with different amounts of text come out at different heights, which looks untidy. This code finds the tallest card and makes the others match.
cards.map((card) => card.outerHeight())Measure each card, with its padding and border, and collect the answers in a list.Math.max(...heights)Find the biggest number. The three dots hand the list over as separate numbers.cards.outerHeight(tallest);Give every card that height. We measured the outer height, so we set the outer height.
Common mistakes
.widthForgot the brackets. Write .width()..innerwidth()Capital letters count. Write .innerWidth().nyx("window")With quotes, Nyx looks for a <window> tag. Write nyx(window)..css("width") + 20.css() reads text like "160px", so this makes "160px20". Use .width() + 20..width() to an .outerWidth() makes the box too big. Use the same method for both.<span> and <a>, ignore sizes. Give them display: inline-block first.Good to know
Math.round() to show them neatly.transform, such as scale(2), changes how big something looks, but not the size these methods give..animate() in Animation.Exercise
Fill in the blanks, then press Check answers.
1. Measure the content width of #box.
nyx("#box").();
2. Measure its height with padding, but no border.
nyx("#box").();
3. Measure its width with padding and border.
nyx("#box").();
4. Measure its height with padding, border and margin.
nyx("#box").();
5. Measure the width of the window.
nyx().width();
6. A box has a content width of 100, padding of 10 on every side and no border. What is its outer width?
Remember
.width() and .height() measure the content.nyx(window) is the part you can see. nyx(document) is the whole page.