-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbox-model.html
153 lines (127 loc) · 3.22 KB
/
box-model.html
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
<!DOCTYPE html>
<html>
<head>
<title>Box Model</title>
<style>
.box {
/* */
}
test {
padding: 10mm; /* All sides */
padding: 10% 20%; /* top/bottom left/right */
padding: 1em 2em 0.5em 0.25em; /* t r b l */
color: rgb(255, 0, 0);
color: rgba();
color: hsl();
color: hsla();
color: #229;
color: #ff0000;
color: #29292966;
}
body {
tab-size: 2;
font-size: 20px;
}
.container {
display: flex;
}
.code-zone {
padding: 2em;
display: flex;
flex-direction: column;
}
textarea {
flex: 1;
}
#box-zone {
border: 4px solid #999;
width: 600px;
height: 600px;
}
.box {
background-color: red;
/* width: 100px;
height: 100px; */
border-style: solid;
border-width: 0;
}
code {
font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
font-size: 1rem;
}
input, select {
display: inline-block;
width: 4rem;
text-align: right;
}
input, select, textarea {
font-size: 1rem;
padding: 0.25rem;
border: 1px solid #aaa;
border-radius: 0.5rem;
}
select {
width: auto
}
</style>
</head>
<body>
<div class="container">
<div id="box-zone">
<div class="box">
Hello World
</div>
</div>
<div class="code-zone">
<pre>
.box {
font-size: <input id="fontSize" value="16" type="number" data-unit="px">px;
color: <input id="color" value="#f00" type="color">;
background-color: <input id="backgroundColor" value="#f00" type="color">;
width: <input id="width" value="" type="number" data-unit="px">px;
height: <input id="height" value="" type="number" data-unit="px">px;
padding: <input id="padding" value="0" type="number" data-unit="px">px;
margin: <input id="margin" value="0" type="number" data-unit="px">px;
border-width: <input id="borderWidth" value="0" type="number" data-unit="px">px;
border-style: <select id="borderStyle">
<option>none</option>
<option>solid</option>
<option>dotted</option>
<option>dashed</option>
<option>double</option>
<option>groove</option>
<option>ridge</option>
<option>inset</option>
<option>outset</option>
<option>hidden</option>
</select>;
border-color: <input id="borderColor" value="0" type="color">;
}
</pre>
<textarea id="text-area">Hello World</textarea>
</div>
</div>
<script>
const styleOutput = document.querySelector('#style-output')
const textArea = document.querySelector('#text-area')
const box = document.querySelector('.box')
document.querySelector('body').addEventListener('input', updateStyles)
// document.querySelector('body').addEventListener('change', updateStyles)
textArea.addEventListener('input', (e) => {
box.innerHTML = e.target.value
})
function updateStyles(e) {
if (!e.target.matches('input') && !e.target.matches('select')) {
return
}
const value = e.target.value
const id = e.target.id
const unit = e.target.dataset.unit ? e.target.dataset.unit : ''
box.style[id] = `${value}${unit}`
if ((id === 'width' || id === 'height') && value === '') {
box.style[id] = 'unset'
}
}
</script>
</body>
</html>