You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 2-ui/1-document/08-styles-and-classes/article.md
+3-3
Original file line number
Diff line number
Diff line change
@@ -250,12 +250,12 @@ For instance:
250
250
```smart header="Computed and resolved values"
251
251
There are two concepts in [CSS](https://drafts.csswg.org/cssom/#resolved-values):
252
252
253
-
1. A *computed* style value is the value after all CSS rules and CSS inheritance is applied, as the result of the CSS cascade. If can look like `width:auto` or `font-size:125%`.
254
-
2. A *resolved* style value is the one finally applied to the element. Values like `auto` or `125%` are still abstract. The browser takes the computed value and makes all units fixed and absolute, for instance: `width:212px` or `font-size:16px`. For geometry properties resolved values may have a floating point, like `width:50.5px`.
253
+
1. A *computed* style value is the value after all CSS rules and CSS inheritance is applied, as the result of the CSS cascade. If can look like `height:1em` or `font-size:125%`.
254
+
2. A *resolved* style value is the one finally applied to the element. Values like `1em` or `125%` are relative. The browser takes the computed value and makes all units fixed and absolute, for instance: `height:20px` or `font-size:16px`. For geometry properties resolved values may have a floating point, like `width:50.5px`.
255
255
256
256
Long time ago `getComputedStyle` was created to get computed values, but it turned out that resolved values are much more convenient, and the standard changed.
257
257
258
-
So nowadays `getComputedStyle` actually returns the final, resolved value in absolute units.
258
+
So nowadays `getComputedStyle` actually returns the resolved value of the property.
259
259
```
260
260
261
261
````warn header="`getComputedStyle` requires the full property name"
Copy file name to clipboardExpand all lines: 2-ui/1-document/09-size-and-scroll/article.md
+22-21
Original file line number
Diff line number
Diff line change
@@ -154,7 +154,7 @@ They include the content width together with paddings, but without the scrollbar
154
154
155
155

156
156
157
-
On the picture above let's first consider `clientHeight`: it's easier to evaluate. There's no horizontal scrollbar, so its exactly the sum of what's inside the borders: CSS-height `200px` plus top and bottom paddings (`2*20px`) total `240px`.
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`.
158
158
159
159
Now `clientWidth` -- here the content width is not `300px`, but `284px`, because `16px` are occupied by the scrollbbar. So the sum is `284px` plus left and right paddings, total `324px`.
160
160
@@ -167,7 +167,7 @@ So when there's no padding we can use `clientWidth/clientHeight` to get the cont
167
167
## scrollWidth/Height
168
168
169
169
- Properties `clientWidth/clientHeight` only account for the visible part of the element.
170
-
- Properties `scrollWidth/scrollHeight`add the scrolled out (hidden) part:
170
+
- Properties `scrollWidth/scrollHeight`also include the scrolled out (hidden) part:
171
171
172
172

173
173
@@ -176,14 +176,17 @@ On the picture above:
176
176
-`scrollHeight = 723` -- is the full inner height of the content area including the scrolled out part.
177
177
-`scrollWidth = 324` -- is the full inner width, here we have no horizontal scroll, so it equals `clientWidth`.
178
178
179
-
We can use these properties to open the element wide to its full width/height, by the code:
179
+
We can use these properties to expand the element wide to its full width/height.
180
+
181
+
Like this:
180
182
181
183
```js
184
+
// expand the element to the full content height
182
185
element.style.height=element.scrollHeight+'px';
183
186
```
184
187
185
188
```online
186
-
Click the button to open wide the element:
189
+
Click the button to expand the element:
187
190
188
191
<div id="element" style="width:300px;height:200px; padding: 0;overflow: auto; border:1px solid black;">text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text</div>
189
192
@@ -192,43 +195,43 @@ Click the button to open wide the element:
192
195
193
196
## scrollLeft/scrollTop
194
197
195
-
Properties `scrollLeft/scrollTop`show how much is hidden behind the scroll. It's the width/height of the hidden, scrolled out part of the element.
198
+
Properties `scrollLeft/scrollTop`are the width/height of the hidden, scrolled out part of the element.
196
199
197
-
On the picture below we can see `scrollHeight` and `scrollTop` for a block with a vertical scroll:
200
+
On the picture below we can see `scrollHeight` and `scrollTop` for a block with a vertical scroll.
198
201
199
202

200
203
204
+
In other words, `scrollTop` is "how much is scrolled up".
205
+
201
206
````smart header="`scrollLeft/scrollTop` can be modified"
202
-
Unlike most other geometry properties that are read-only, `scrollLeft/scrollTop` can be changed, and the browser will scroll the element.
207
+
Most geometry properties that are read-only, but`scrollLeft/scrollTop` can be changed, and the browser will scroll the element.
203
208
204
209
```online
205
-
If you click the element below, the code `elem.scrollTop += 10` executes. That makes the element content scroll `10px` below.
210
+
If you click the element below, the code `elem.scrollTop+=10` executes. That makes the element content scroll `10px` below.
Setting `scrollTop` to `0` or `Infinity` will make the element scroll to the top/bottom respectively.
215
+
Setting `scrollTop` to `0` or `Infinity` will make the element scroll to the very top/bottom respectively.
211
216
````
212
217
213
218
## Don't take width/height from CSS
214
219
215
-
We've just covered geometry properties of DOM elements. They are normally used to get widths, heights and distances.
220
+
We've just covered geometry properties of DOM elements. They are normally used to get widths, heights and calculate distances.
216
221
217
-
Now let's see what we should not use.
222
+
But as we know from the chapter <info:styles-and-classes>, we can read CSS-height and width using `getComputedStyle`.
218
223
219
-
As we know from the chapter <info:styles-and-classes>, we can read CSS-height and width using `getComputedStyle`.
220
-
221
-
So we can try to read the width of an element like this:
224
+
So why not to read the width of an element like this?
222
225
223
226
```js run
224
227
let elem = document.body;
225
228
226
229
alert( getComputedStyle(elem).width ); // show CSS width for elem
227
230
```
228
231
229
-
Why we should use geometry properties instead?
232
+
Why we should use geometry properties instead? There are two reasons:
230
233
231
-
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 purposes of CSS may break such JavaScript.
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.
232
235
2. Second, CSS `width/height` may be `auto`, for instance for an inline element:
233
236
234
237
```html run
@@ -243,11 +246,9 @@ Why we should use geometry properties instead?
243
246
244
247
From the CSS standpoint, `width:auto` is perfectly normal, but in JavaScript we need an exact size in `px` that we can use in calculations. So here CSS width is useless at all.
245
248
246
-
And there's one more reason. A scrollbar is the reason of many problems. The devil is in the detail. Sometimes the code that works fine without a scrollbar starts to bug with it.
247
-
248
-
As we've seen a scrollbar takes the space from the content in some browsers. So the real width available for the content is *less* than CSS width. And `clientWidth/clientHeight` take that into account.
249
+
And there's one more reason: a scrollbar. Sometimes the code that works fine without a scrollbar starts to bug with it, because a scrollbar takes the space from the content in some browsers. So the real width available for the content is *less* than CSS width. And `clientWidth/clientHeight` take that into account.
249
250
250
-
...But some browsers also take that into account in `getComputedStyle(elem).width`. That is: some of them return real inner widthand some of them -- CSS width. Such cross-browser differences is a reason not to use `getComputedStyle`, but rather rely on geometry propeties.
251
+
...But with `getComputedStyle(elem).width` the situation is different. Some browsers (e.g. Chrome) return the real inner width, minus the scrollbar, and some of them (e.g. Firefox) -- CSS width (ignore the scrollbar). Such cross-browser differences is the reason not to use `getComputedStyle`, but rather rely on geometry propeties.
251
252
252
253
```online
253
254
If your browser reserves the space for a scrollbar (most browsers for Windows do), then you can test it below.
@@ -256,7 +257,7 @@ If your browser reserves the space for a scrollbar (most browsers for Windows do
256
257
257
258
The element with text has CSS `width:300px`.
258
259
259
-
Desktop Windows Firefox, Chrome, Edge all reserve the space for the scrollbar. But Firefox shows `300px`, while Chrome and Edge show less. That's because Firefox returns the CSS width and other browsers return the "real" width.
260
+
On a Desktop Windows OS, Firefox, Chrome, Edge all reserve the space for the scrollbar. But Firefox shows `300px`, while Chrome and Edge show less. That's because Firefox returns the CSS width and other browsers return the "real" width.
260
261
```
261
262
262
263
Please note that the described difference are only about reading `getComputedStyle(...).width` from JavaScript, visually everything is correct.
Copy file name to clipboardExpand all lines: 2-ui/1-document/10-size-and-scroll-window/article.md
+25-26
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Window sizes and scroll
2
2
3
-
How to find out the browser window width? How to get the full height of the document, including the scrolled out part? How to scroll the page from JavaScript?
3
+
How to find out the width of the browser window? How to get the full height of the document, including the scrolled out part? How to scroll the page using JavaScript?
4
4
5
5
From the DOM point of view, the root document element is `document.documentElement`. That element corresponds to `<html>` and has geometry properties described in the [previous chapter](info:size-and-scroll). For some cases we can use it, but there are additional methods and pecularities important enough to consider.
6
6
@@ -13,39 +13,38 @@ Properties `clientWidth/clientHeight` of `document.documentElement` is exactly w
13
13

14
14
15
15
```online
16
-
For instance, the button below shows the height of your window:
16
+
For instance, this button shows the height of your window:
Browsers also support properties `window.innerWidth/innerHeight`. They look like what we want. So what's the difference?
22
23
23
-
Properties `clientWidth/clientHeight`, if there's a scrollbar occupying some space, return the width/height inside it. In other words, they return width/height of the visible part of the document, available for the content.
24
+
If there's a scrollbar occupying some space, `clientWidth/clientHeight` provide the width/height inside it. In other words, they return width/height of the visible part of the document, available for the content.
24
25
25
26
And `window.innerWidth/innerHeight` ignore the scrollbar.
26
27
27
-
If there's a scrollbar and it occupies some space, then these two lines show different values:
28
+
If there's a scrollbar, and it occupies some space, then these two lines show different values:
28
29
```js run
29
30
alert( window.innerWidth ); // full window width
30
31
alert( document.documentElement.clientWidth ); // window width minus the scrollbar
31
32
```
32
33
33
-
In most cases we need the *available* window width: to draw or position something. That is: inside scrollbars if there are any. So we should use `documentElement.clientWidth`.
34
+
In most cases we need the *available* window width: to draw or position something. That is: inside scrollbars if there are any. So we should use `documentElement.clientHeight/Width`.
34
35
````
35
36
36
37
```warn header="`DOCTYPE` is important"
37
38
Please note: top-level geometry properties may work a little bit differently when there's no `<!DOCTYPE HTML>` in HTML. Odd things are possible.
38
39
39
-
In modern HTML we should always write it. Generally that's not a JavaScript question, but here it affects JavaScript as well.
40
+
In modern HTML we should always write `DOCTYPE`. Generally that's not a JavaScript question, but here it affects JavaScript as well.
40
41
```
41
42
42
43
## Width/height of the document
43
44
44
-
Theoretically, as the visibble part of the document is `documentElement.clientWidth/Height`, the full size should be `documentElement.scrollWidth/scrollHeight`.
45
-
46
-
That's correct for regular elements.
45
+
Theoretically, as the root document element is `documentElement.clientWidth/Height`, and it encloses all the content, we could measure its full size as `documentElement.scrollWidth/scrollHeight`.
47
46
48
-
But for the whole page these properties do not work as intended. In Chrome/Safari/Opera if there's no scroll, then `documentElement.scrollHeight` may be even less than `documentElement.clientHeight`! For regular elements that's a nonsense.
47
+
These properties work well for regular elements. But for the whole page these properties do not work as intended. In Chrome/Safari/Opera if there's no scroll, then `documentElement.scrollHeight` may be even less than `documentElement.clientHeight`! For regular elements that's a nonsense.
49
48
50
49
To have a reliable full window size, we should take the maximum of these properties:
51
50
@@ -63,11 +62,11 @@ Why so? Better don't ask. These inconsistencies come from ancient times, not a "
63
62
64
63
## Get the current scroll [#page-scroll]
65
64
66
-
Regular elements have their current scroll state in `scrollLeft/scrollTop`.
65
+
Regular elements have their current scroll state in `elem.scrollLeft/scrollTop`.
67
66
68
-
What's with the page? Most browsers provide that for the whole page in `documentElement.scrollLeft/Top`, but Chrome/Safari/Opera have bugs (like [157855](https://code.google.com/p/chromium/issues/detail?id=157855), [106133](https://bugs.webkit.org/show_bug.cgi?id=106133)) and we should use `document.body` instead of `document.documentElement` there.
67
+
What's with the page? Most browsers provide `documentElement.scrollLeft/Top` for the document scroll, but Chrome/Safari/Opera have bugs (like [157855](https://code.google.com/p/chromium/issues/detail?id=157855), [106133](https://bugs.webkit.org/show_bug.cgi?id=106133)) and we should use `document.body` instead of `document.documentElement` there.
69
68
70
-
Luckily, we don't have to remember that at all, because of the special properties `window.pageXOffset/pageYOffset`:
69
+
Luckily, we don't have to remember these pecularities at all, because of the special properties `window.pageXOffset/pageYOffset`:
71
70
72
71
```js run
73
72
alert('Current scroll from the top: ' + window.pageYOffset);
To scroll the page from JavaScript, its DOM must be fully loaded.
81
+
To scroll the page from JavaScript, its DOM must be fully built.
82
+
83
+
For instance, if we try to scroll the page from the script in `<head>`, it won't work.
83
84
```
84
85
85
86
Regular elements can be scrolled by changing `scrollTop/scrollLeft`.
@@ -88,9 +89,7 @@ We can do the same for the page:
88
89
- For all browsers except Chrome/Safari/Opera: modify `document.documentElement.scrollTop/Left`.
89
90
- In Chrome/Safari/Opera: use `document.body.scrollTop/Left` instead.
90
91
91
-
It should work.
92
-
93
-
But there's a simpler, more universal solution: special methods [window.scrollBy(x,y)](mdn:api/Window/scrollBy) and [window.scrollTo(pageX,pageY)](mdn:api/Window/scrollTo).
92
+
It should work, but smells like cross-browser incompatibilities. Not good. Fortunately, there's a simpler, more universal solution: special methods [window.scrollBy(x,y)](mdn:api/Window/scrollBy) and [window.scrollTo(pageX,pageY)](mdn:api/Window/scrollTo).
94
93
95
94
- The method `scrollBy(x,y)` scrolls the page relative to its current position. For instance, `scrollBy(0,10)` scrolls the page `10px` down.
96
95
@@ -107,16 +106,16 @@ But there's a simpler, more universal solution: special methods [window.scrollB
For completeness, let's cover one more method: [elem.scrollIntoView(top)](mdn:api/Element/scrollIntoView).
115
114
116
-
The call to `elem.scrollIntoView(top)` scrolls the page to make `elem` visible. It has one argument `top`:
115
+
The call to `elem.scrollIntoView(top)` scrolls the page to make `elem` visible. It has one argument:
117
116
118
-
- if `top=true` (that's the default), then the page will be scrolled to make `elem` appear on the top of the window, upper side of the element aligned with the window top.
119
-
- if `top=false`, then the element bottom is aligned with the window bottom.
117
+
- if `top=true` (that's the default), then the page will be scrolled to make `elem` appear on the top of the window. The upper edge of the element is aligned with the window top.
118
+
- if `top=false`, then the page scrolls to make `elem` appear at the bottom. The bottom edge of the element is aligned with the window bottom.
120
119
121
120
```online
122
121
The button below scrolls the page to make itself show at the window top:
@@ -130,7 +129,7 @@ And this button scrolls the page to show it at the bottom:
130
129
131
130
## Forbid the scrolling
132
131
133
-
Sometimes we need to make the document "unscrollable". For instance, when we need to show a large message over it, and we want the visitor to interact with that message, not with the document.
132
+
Sometimes we need to make the document "unscrollable". For instance, when we need to cover it with a large message requiring immediate attention, and we want the visitor to interact with that message, not with the document.
134
133
135
134
To make the document unscrollable, its enough to set `document.body.style.overflow = "hidden"`. The page will freeze on its current scroll.
136
135
@@ -148,7 +147,7 @@ We can use the same technique to "freeze" the scroll for other elements, not jus
148
147
149
148
The drawback of the method is that the scrollbar disappears. If it occupied some space, then that space is now free, and the content "jumps" to fill it.
150
149
151
-
That looks a bit odd, but can be worked around if we compare `clientWidth` before and after the freeze, and if it increased (the content area became wider) then add `padding` to `document.body`.
150
+
That looks a bit odd, but can be worked around if we compare `clientWidth` before and after the freeze, and if it increased (the scrollbar disappeared) then add `padding` to `document.body` in place of the scrollbar, to keep the content width same.
152
151
153
152
## Summary
154
153
@@ -168,8 +167,8 @@ Geometry:
168
167
Scrolling:
169
168
170
169
- Read the current scroll: `window.pageYOffset/pageXOffset`.
0 commit comments