Skip to content

Commit 2d261b1

Browse files
committed
Update "Element size and scrolling" files
1 parent 6a3167f commit 2d261b1

File tree

12 files changed

+39
-40
lines changed

12 files changed

+39
-40
lines changed

2-ui/1-document/09-size-and-scroll/2-scrollbar-width/task.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ importance: 3
66

77
Write the code that returns the width of a standard scrollbar.
88

9-
For Windows it usually varies between `12px` and `20px`. If the browser doesn't reserves any space for it, then it may be `0px`.
9+
For Windows it usually varies between `12px` and `20px`. If the browser doesn't reserve any space for it (the scrollbar is half-translucent over the text, also happens), then it may be `0px`.
1010

1111
P.S. The code should work for any HTML document, do not depend on its content.

2-ui/1-document/09-size-and-scroll/4-put-ball-in-center/field.svg

Lines changed: 1 addition & 1 deletion
Loading

2-ui/1-document/09-size-and-scroll/article.md

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
There are many JavaScript properties that allow us to read information about element width, height and other geometry features.
44

5-
We often need them when moving or positioning elements in JavaScript, to correctly calculate coordinates.
6-
5+
We often need them when moving or positioning elements in JavaScript.
76

87
## Sample element
98

@@ -18,8 +17,8 @@ As a sample element to demonstrate properties we'll use the one given below:
1817
width: 300px;
1918
height: 200px;
2019
border: 25px solid #E8C48F;
21-
padding: 20px;
22-
overflow: auto;
20+
padding: 20px;
21+
overflow: auto;
2322
}
2423
</style>
2524
```
@@ -33,26 +32,24 @@ The element looks like this:
3332
You can [open the document in the sandbox](sandbox:metric).
3433

3534
```smart header="Mind the scrollbar"
36-
The picture above demonstrates the most complex case when the element has a scrollbar. Some browsers (not all) reserve the space for it by taking it from the content.
35+
The picture above demonstrates the most complex case when the element has a scrollbar. Some browsers (not all) reserve the space for it by taking it from the content (labeled as "content width" above).
3736
38-
So, without scrollbar the content width would be `300px`, but if the scrollbar is `16px` wide (the width may vary between devices and browsers) then only `300 - 16 = 284px` remains, and we should take it into account. That's why examples from this chapter assume that there's a scrollbar. If there's no scrollbar, then things are just a bit simpler.
37+
So, without scrollbar the content width would be `300px`, but if the scrollbar is `16px` wide (the width may vary between devices and browsers) then only `300 - 16 = 284px` remains, and we should take it into account. That's why examples from this chapter assume that there's a scrollbar. Without it, some calculations are simpler.
3938
```
4039

41-
```smart header="The `padding-bottom` may be filled with text"
42-
Usually paddings are shown empty on illustrations, but if there's a lot of text in the element and it overflows, then browsers show the "overflowing" text at `padding-bottom`, so you can see that in examples. But the padding is still there, unless specified otherwise.
40+
```smart header="The `padding-bottom` area may be filled with text"
41+
Usually paddings are shown empty on our illustrations, but if there's a lot of text in the element and it overflows, then browsers show the "overflowing" text at `padding-bottom`, that's normal.
4342
```
4443
4544
## Geometry
4645
47-
Element properties that provide width, height and other geometry are always numbers. They are assumed to be in pixels.
48-
49-
Here's the overall picture:
46+
Here's the overall picture with geometry properties:
5047
5148
![](metric-all.svg)
5249
53-
They are many properties, it's difficult to fit them all in the single picture, but their values are simple and easy to understand.
50+
Values of these properties are technically numbers, but these numbers are "of pixels", so these are pixel measurements.
5451
55-
Let's start exploring them from the outside of the element.
52+
Let's start exploring the properties starting from the outside of the element.
5653
5754
## offsetParent, offsetLeft/Top
5855
@@ -66,7 +63,7 @@ That's the nearest ancestor that is one of the following:
6663
2. `<td>`, `<th>`, or `<table>`, or
6764
3. `<body>`.
6865
69-
In most practical cases we can use `offsetParent` to get the nearest CSS-positioned ancestor. And `offsetLeft/offsetTop` provide x/y coordinates relative to its upper-left corner.
66+
Properties `offsetLeft/offsetTop` provide x/y coordinates relative to `offsetParent` upper-left corner.
7067
7168
In the example below the inner `<div>` has `<main>` as `offsetParent` and `offsetLeft/offsetTop` shifts from its upper-left corner (`180`):
7269
@@ -85,7 +82,6 @@ In the example below the inner `<div>` has `<main>` as `offsetParent` and `offse
8582

8683
![](metric-offset-parent.svg)
8784

88-
8985
There are several occasions when `offsetParent` is `null`:
9086

9187
1. For not shown elements (`display:none` or not in the document).
@@ -105,12 +101,12 @@ For our sample element:
105101
- `offsetWidth = 390` -- the outer width, can be calculated as inner CSS-width (`300px`) plus paddings (`2 * 20px`) and borders (`2 * 25px`).
106102
- `offsetHeight = 290` -- the outer height.
107103

108-
````smart header="Geometry properties for not shown elements are zero/null"
109-
Geometry properties are calculated only for shown elements.
104+
````smart header="Geometry properties are zero/null for elements that are not displayed"
105+
Geometry properties are calculated only for displayed elements.
110106
111107
If an element (or any of its ancestors) has `display:none` or is not in the document, then all geometry properties are zero (or `null` for `offsetParent`).
112108
113-
For example, `offsetParent` is `null`, and `offsetWidth`, `offsetHeight` are `0`.
109+
For example, `offsetParent` is `null`, and `offsetWidth`, `offsetHeight` are `0` when we created an element, but haven't inserted it into the document yet, or it (or its ancestor) has `display:none`.
114110
115111
We can use this to check if an element is hidden, like this:
116112
@@ -136,13 +132,15 @@ In our example:
136132

137133
![](metric-client-left-top.svg)
138134

139-
...But to be precise -- they are not borders, but relative coordinates of the inner side from the outer side.
135+
...But to be precise -- these properties are not border width/height, but rather relative coordinates of the inner side from the outer side.
140136

141137
What's the difference?
142138

143139
It becomes visible when the document is right-to-left (the operating system is in Arabic or Hebrew languages). The scrollbar is then not on the right, but on the left, and then `clientLeft` also includes the scrollbar width.
144140

145-
In that case, `clientLeft` would be not `25`, but with the scrollbar width `25 + 16 = 41`:
141+
In that case, `clientLeft` would be not `25`, but with the scrollbar width `25 + 16 = 41`.
142+
143+
Here's the example in hebrew:
146144

147145
![](metric-client-left-top-rtl.svg)
148146

@@ -154,7 +152,9 @@ They include the content width together with paddings, but without the scrollbar
154152

155153
![](metric-client-width-height.svg)
156154

157-
On the picture above let's first consider `clientHeight`: it's easier to evaluate. There's no horizontal scrollbar, so it's exactly the sum of what's inside the borders: CSS-height `200px` plus top and bottom paddings (`2 * 20px`) total `240px`.
155+
On the picture above let's first consider `clientHeight`.
156+
157+
There's no horizontal scrollbar, so it's exactly the sum of what's inside the borders: CSS-height `200px` plus top and bottom paddings (`2 * 20px`) total `240px`.
158158

159159
Now `clientWidth` -- here the content width is not `300px`, but `284px`, because `16px` are occupied by the scrollbar. So the sum is `284px` plus left and right paddings, total `324px`.
160160

@@ -166,8 +166,7 @@ So when there's no padding we can use `clientWidth/clientHeight` to get the cont
166166

167167
## scrollWidth/Height
168168

169-
- Properties `clientWidth/clientHeight` only account for the visible part of the element.
170-
- Properties `scrollWidth/scrollHeight` also include the scrolled out (hidden) parts:
169+
These properties are like `clientWidth/clientHeight`, but they also include the scrolled out (hidden) parts:
171170

172171
![](metric-scroll-width-height.svg)
173172

@@ -217,11 +216,11 @@ Setting `scrollTop` to `0` or a big value, such as `1e9` will make the element s
217216
218217
## Don't take width/height from CSS
219218
220-
We've just covered geometry properties of DOM elements. They are normally used to get widths, heights and calculate distances.
219+
We've just covered geometry properties of DOM elements, that can be used to get widths, heights and calculate distances.
221220
222221
But as we know from the chapter <info:styles-and-classes>, we can read CSS-height and width using `getComputedStyle`.
223222
224-
So why not to read the width of an element like this?
223+
So why not to read the width of an element with `getComputedStyle`, like this?
225224
226225
```js run
227226
let elem = document.body;
@@ -231,7 +230,7 @@ alert( getComputedStyle(elem).width ); // show CSS width for elem
231230
232231
Why should we use geometry properties instead? There are two reasons:
233232
234-
1. First, CSS width/height depend on another property: `box-sizing` that defines "what is" CSS width and height. A change in `box-sizing` for CSS purposes may break such JavaScript.
233+
1. First, CSS `width/height` depend on another property: `box-sizing` that defines "what is" CSS width and height. A change in `box-sizing` for CSS purposes may break such JavaScript.
235234
2. Second, CSS `width/height` may be `auto`, for instance for an inline element:
236235
237236
```html run
@@ -271,7 +270,7 @@ Elements have the following geometry properties:
271270
- `offsetWidth/offsetHeight` -- "outer" width/height of an element including borders.
272271
- `clientLeft/clientTop` -- the distances from the upper-left outer corner to the upper-left inner (content + padding) corner. For left-to-right OS they are always the widths of left/top borders. For right-to-left OS the vertical scrollbar is on the left so `clientLeft` includes its width too.
273272
- `clientWidth/clientHeight` -- the width/height of the content including paddings, but without the scrollbar.
274-
- `scrollWidth/scrollHeight` -- the width/height of the content including the scrolled out parts. Also includes paddings, but not the scrollbar.
275-
- `scrollLeft/scrollTop` -- width/height of the scrolled out part of the element, starting from its upper-left corner.
273+
- `scrollWidth/scrollHeight` -- the width/height of the content, just like `clientWidth/clientHeight`, but also include scrolled-out, invisible part of the element.
274+
- `scrollLeft/scrollTop` -- width/height of the scrolled out upper part of the element, starting from its upper-left corner.
276275
277-
All properties are read-only except `scrollLeft/scrollTop`. They make the browser scroll the element if changed.
276+
All properties are read-only except `scrollLeft/scrollTop` that make the browser scroll the element if changed.

0 commit comments

Comments
 (0)