-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelative-units.html
More file actions
474 lines (419 loc) · 18.7 KB
/
relative-units.html
File metadata and controls
474 lines (419 loc) · 18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
<!DOCTYPE html>
<html lang="hu">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Relatív Mértékegységek</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
:root {
--base-font-size: 16px;
font-size: var(--base-font-size);
}
body {
font-family: Arial, sans-serif;
line-height: 1.6;
padding: 20px;
background-color: #f0f0f0;
}
.container {
max-width: 1200px;
margin: 0 auto;
background-color: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.section {
margin: 40px 0;
padding: 20px;
border: 2px solid #ddd;
border-radius: 8px;
background-color: #fff;
}
.demo-area {
margin: 20px 0;
padding: 20px;
background-color: #f8f9fa;
border-radius: 8px;
border: 1px solid #dee2e6;
}
.control-panel {
margin: 20px 0;
padding: 15px;
background-color: #e9ecef;
border-radius: 8px;
}
.comparison-box {
display: flex;
gap: 20px;
flex-wrap: wrap;
margin: 20px 0;
}
.unit-box {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
background-color: white;
}
.pixel-value {
font-size: 12px;
color: #666;
margin-top: 5px;
}
.viewport-demo {
resize: both;
overflow: auto;
min-width: 200px;
min-height: 200px;
max-width: 100%;
max-height: 500px;
border: 2px dashed #666;
margin: 20px 0;
padding: 10px;
}
.explanation {
background-color: #e3f2fd;
padding: 15px;
border-left: 4px solid #2196f3;
margin: 15px 0;
}
.interactive-test {
background-color: #fff3e0;
padding: 20px;
border-radius: 8px;
margin: 20px 0;
}
input[type="range"] {
width: 100%;
max-width: 300px;
}
.demo-button {
display: inline-block;
padding: 10px 20px;
border: none;
border-radius: 4px;
background-color: #2196f3;
color: white;
cursor: pointer;
margin: 10px;
}
.demo-button-em {
font-size: 1em;
padding: 1em 2em;
}
.demo-button-rem {
font-size: 1rem;
padding: 1rem 2rem;
}
/* Viewport egységek demonstrációja */
.vw-element { width: 50vw; }
.vh-element { height: 50vh; }
.vmin-element { width: 50vmin; }
.vmax-element { width: 50vmax; }
/* Mértékegység összehasonlító elemek */
.size-2em { font-size: 2em; }
.size-2rem { font-size: 2rem; }
.size-2vh { font-size: 2vh; }
.size-16px { font-size: 16px; }
</style>
</head>
<body>
<div class="container">
<h1>CSS Relatív Mértékegységek</h1>
<div class="explanation">
<p>Ez az oldal bemutatja a különböző CSS relatív mértékegységek működését.
A relatív mértékegységek rugalmasabbá teszik a weboldal méretezését,
és segítenek a reszponzív design megvalósításában.</p>
</div>
<!-- Alapméret állító -->
<div class="section">
<h2>Alapméret Beállítása</h2>
<div class="explanation">
<p>Húzd a csúszkát az oldal alapbetűméretének módosításához.
Figyeld meg, hogyan változnak a különböző mértékegységekkel megadott elemek!</p>
</div>
<div class="control-panel">
<label for="base-size">Alapbetűméret (jelenleg: <span id="base-size-value">16</span>px):</label><br>
<input type="range" id="base-size" min="12" max="24" value="16">
</div>
</div>
<!-- em vs rem összehasonlítás -->
<div class="section">
<h2>EM vs REM Összehasonlítás</h2>
<div class="explanation">
<p><strong>em:</strong> Az aktuális elem betűméretéhez viszonyított méret. Öröklődik a szülőelemektől!<br>
<strong>rem:</strong> Mindig a html (gyökér) elem betűméretéhez viszonyít, függetlenül a szülőelemektől.</p>
</div>
<!-- Egyszerű példa -->
<div class="comparison-box">
<div class="unit-box">
<button class="demo-button demo-button-em">1em Gomb</button>
<div class="pixel-value">Kiszámított méret: <span class="em-value"></span>px</div>
</div>
<div class="unit-box">
<button class="demo-button demo-button-rem">1rem Gomb</button>
<div class="pixel-value">Kiszámított méret: <span class="rem-value"></span>px</div>
</div>
</div>
<!-- Beágyazott példa -->
<h3 style="margin-top: 30px;">Beágyazott elemek viselkedése</h3>
<div class="nested-comparison" style="display: flex; gap: 20px; margin-top: 20px;">
<div class="unit-box" style="flex: 1;">
<h4>EM - Öröklődő méretezés</h4>
<div style="font-size: 1.5em; border: 1px dashed #ccc; padding: 10px;">
1.5em méretű szöveg
<div style="font-size: 1.5em; border: 1px dashed #ccc; padding: 10px;">
Újabb 1.5em (eredeti × 1.5 × 1.5)
<div style="font-size: 1.5em; border: 1px dashed #ccc; padding: 10px;">
Még egy 1.5em szint
<div class="pixel-value">Eredeti méret × 1.5 × 1.5 × 1.5</div>
</div>
</div>
</div>
</div>
<div class="unit-box" style="flex: 1;">
<h4>REM - Konstans méretezés</h4>
<div style="font-size: 1.5rem; border: 1px dashed #ccc; padding: 10px;">
1.5rem méretű szöveg
<div style="font-size: 1.5rem; border: 1px dashed #ccc; padding: 10px;">
Újabb 1.5rem (mindig ugyanakkora)
<div style="font-size: 1.5rem; border: 1px dashed #ccc; padding: 10px;">
Még egy 1.5rem szint
<div class="pixel-value">Mindig az eredeti × 1.5</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Viewport egységek -->
<div class="section">
<h2>Viewport Mértékegységek (vh, vw, vmin, vmax)</h2>
<div class="explanation">
<p><strong>vh (viewport height):</strong> A böngészőablak magasságához viszonyít.
Például: ha a böngésződ ablaka 1000 pixel magas, akkor 1vh = 10 pixel.
Tökéletes választás teljes képernyős elemek (pl. hero szekciók, landing page-ek) méretezéséhez.</p>
<p><strong>vw (viewport width):</strong> A böngészőablak szélességéhez viszonyít.
Például: ha a böngésződ 1200 pixel széles, akkor 1vw = 12 pixel.
Ideális választás széles elemek, bannerek, háttérképek méretezéséhez.</p>
<p><strong>vmin:</strong> A viewport kisebb méretét veszi alapul.
Például: ha az ablakod 1200×800 pixeles, akkor 1vmin = 8 pixel (a 800-ból).
Akkor hasznos, ha azt szeretnéd, hogy az elem a kisebb képernyőméretet vegye figyelembe.</p>
<p><strong>vmax:</strong> A viewport nagyobb méretét veszi alapul.
Például: ha az ablakod 1200×800 pixeles, akkor 1vmax = 12 pixel (az 1200-ból).
Akkor jó választás, ha azt szeretnéd, hogy az elem a nagyobb képernyőméretet vegye figyelembe.</p>
</div>
<div class="viewport-demo" id="viewport-demo" style="position: relative;">
<div class="vw-element" style="background-color: #bbdefb; margin-bottom: 10px;">
50vw széles doboz
<div class="pixel-value vw-value"></div>
</div>
<div class="vh-element" style="background-color: #c8e6c9; margin-bottom: 10px;">
50vh magas doboz
<div class="pixel-value vh-value"></div>
</div>
<div class="vmin-element" style="background-color: #ffcdd2; margin-bottom: 10px;">
50vmin széles doboz
<div class="pixel-value vmin-value"></div>
</div>
<div class="vmax-element" style="background-color: #fff9c4; margin-bottom: 10px;">
50vmax széles doboz
<div class="pixel-value vmax-value"></div>
</div>
<p style="position: absolute; bottom: 10px; right: 10px;">
👉 Méretezd át a dobozt a jobb alsó saroknál! 👆
</p>
</div>
</div>
<!-- Azonos számértékű összehasonlítás -->
<div class="section">
<h2>Azonos Számértékű Összehasonlítás (2-es szorzó)</h2>
<div class="explanation">
<p>Itt láthatod, hogyan néz ki ugyanaz a számérték különböző mértékegységekkel.</p>
</div>
<div class="comparison-box">
<div class="unit-box">
<div class="size-2em">2em szöveg</div>
<div class="pixel-value">Kiszámított méret: <span class="size-2em-value"></span>px</div>
</div>
<div class="unit-box">
<div class="size-2rem">2rem szöveg</div>
<div class="pixel-value">Kiszámított méret: <span class="size-2rem-value"></span>px</div>
</div>
<div class="unit-box">
<div class="size-2vh">2vh szöveg</div>
<div class="pixel-value">Kiszámított méret: <span class="size-2vh-value"></span>px</div>
</div>
<div class="unit-box">
<div class="size-16px">16px szöveg (referencia)</div>
<div class="pixel-value">Kiszámított méret: 16px</div>
</div>
</div>
</div>
<!-- Interaktív teszt terület -->
<div class="section">
<h2>Interaktív Teszt Terület</h2>
<div class="explanation">
<p>Itt kipróbálhatod a különböző mértékegységeket. Válassz egy mértékegységet és egy értéket,
majd figyeld meg, hogyan változik a tesztdoboz mérete!</p>
</div>
<div class="interactive-test">
<div class="control-panel">
<label for="unit-select">Válassz mértékegységet:</label>
<select id="unit-select">
<option value="em">em</option>
<option value="rem">rem</option>
<option value="vh">vh</option>
<option value="vw">vw</option>
<option value="vmin">vmin</option>
<option value="vmax">vmax</option>
</select>
<br><br>
<label for="unit-value">Érték:</label>
<input type="number" id="unit-value" value="1" min="0" max="10" step="0.1">
<br><br>
<label for="property-select">Tulajdonság:</label>
<select id="property-select">
<option value="font-size">Betűméret</option>
<option value="width">Szélesség</option>
<option value="height">Magasság</option>
<option value="padding">Belső margó</option>
<option value="margin">Külső margó</option>
</select>
</div>
<div id="test-box" style="margin: 20px; padding: 20px; border: 1px solid #ccc; background-color: #fff;">
Tesztdoboz
<div class="pixel-value">Kiszámított érték: <span id="test-box-value"></span>px</div>
</div>
</div>
</div>
</div>
<script>
// Alapméret állító
const baseSizeSlider = document.getElementById('base-size');
const baseSizeValue = document.getElementById('base-size-value');
baseSizeSlider.addEventListener('input', function() {
const size = this.value;
document.documentElement.style.setProperty('--base-font-size', size + 'px');
baseSizeValue.textContent = size;
updateAllPixelValues();
});
// Viewport értékek kezelése
function updateViewportValues() {
const demo = document.getElementById('viewport-demo');
if (!demo) return;
const demoRect = demo.getBoundingClientRect();
const demoWidth = demoRect.width;
const demoHeight = demoRect.height;
// vw elem frissítése
const vwElement = demo.querySelector('.vw-element');
if (vwElement) {
const vwValue = Math.round(demoWidth * 0.5); // 50vw
vwElement.querySelector('.vw-value').textContent = `${vwValue}px (a konténer 50%-a)`;
}
// vh elem frissítése
const vhElement = demo.querySelector('.vh-element');
if (vhElement) {
const vhValue = Math.round(demoHeight * 0.5); // 50vh
vhElement.querySelector('.vh-value').textContent = `${vhValue}px (a konténer 50%-a)`;
}
// vmin elem frissítése
const vminElement = demo.querySelector('.vmin-element');
if (vminElement) {
const vminValue = Math.round(Math.min(demoWidth, demoHeight) * 0.5); // 50vmin
vminElement.querySelector('.vmin-value').textContent =
`${vminValue}px (a kisebb méret 50%-a)`;
}
// vmax elem frissítése
const vmaxElement = demo.querySelector('.vmax-element');
if (vmaxElement) {
const vmaxValue = Math.round(Math.max(demoWidth, demoHeight) * 0.5); // 50vmax
vmaxElement.querySelector('.vmax-value').textContent =
`${vmaxValue}px (a nagyobb méret 50%-a)`;
}
}
// Összes pixel érték frissítése
function updateAllPixelValues() {
// Em és rem gombok méretének frissítése
const emButton = document.querySelector('.demo-button-em');
const remButton = document.querySelector('.demo-button-rem');
if (emButton && remButton) {
const emSize = window.getComputedStyle(emButton).fontSize;
const remSize = window.getComputedStyle(remButton).fontSize;
document.querySelector('.em-value').textContent = emSize;
document.querySelector('.rem-value').textContent = remSize;
}
// Beágyazott példák méretének frissítése
const nestedEms = document.querySelectorAll('.nested-comparison .unit-box:first-child .pixel-value');
const nestedRems = document.querySelectorAll('.nested-comparison .unit-box:last-child .pixel-value');
nestedEms.forEach(elem => {
const fontSize = window.getComputedStyle(elem.parentElement).fontSize;
elem.textContent = `Aktuális méret: ${fontSize}`;
});
nestedRems.forEach(elem => {
const fontSize = window.getComputedStyle(elem.parentElement).fontSize;
elem.textContent = `Aktuális méret: ${fontSize}`;
});
// 2em, 2rem, 2vh összehasonlítás
const size2em = document.querySelector('.size-2em');
const size2rem = document.querySelector('.size-2rem');
const size2vh = document.querySelector('.size-2vh');
if (size2em && size2rem && size2vh) {
document.querySelector('.size-2em-value').textContent =
window.getComputedStyle(size2em).fontSize;
document.querySelector('.size-2rem-value').textContent =
window.getComputedStyle(size2rem).fontSize;
document.querySelector('.size-2vh-value').textContent =
window.getComputedStyle(size2vh).fontSize;
}
// Viewport értékek frissítése
updateViewportValues();
}
// Interaktív teszt doboz kezelése
const unitSelect = document.getElementById('unit-select');
const unitValue = document.getElementById('unit-value');
const propertySelect = document.getElementById('property-select');
const testBox = document.getElementById('test-box');
const testBoxValue = document.getElementById('test-box-value');
function updateTestBox() {
if (!testBox || !testBoxValue) return;
const value = unitValue.value;
const unit = unitSelect.value;
const property = propertySelect.value;
const newValue = `${value}${unit}`;
testBox.style[property] = newValue;
// Kis késleltetés a kiszámított érték pontosabb leolvasásához
setTimeout(() => {
const computedStyle = window.getComputedStyle(testBox);
let pixelValue = computedStyle[property];
testBoxValue.textContent = pixelValue;
}, 50);
}
// Eseményfigyelők
if (unitSelect && unitValue && propertySelect) {
unitSelect.addEventListener('change', updateTestBox);
unitValue.addEventListener('input', updateTestBox);
propertySelect.addEventListener('change', updateTestBox);
}
// ResizeObserver a viewport demo átméretezésének figyeléséhez
const viewportDemo = document.getElementById('viewport-demo');
if (viewportDemo) {
const resizeObserver = new ResizeObserver(() => {
updateViewportValues();
});
resizeObserver.observe(viewportDemo);
}
// Oldal betöltésekor és ablak átméretezésekor frissítés
window.addEventListener('load', () => {
updateAllPixelValues();
updateTestBox();
});
window.addEventListener('resize', updateAllPixelValues);
</script>
</body>
</html>