-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbasic-css.html
453 lines (434 loc) · 26.5 KB
/
basic-css.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
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
<!DOCTYPE html>
<html>
<head>
<meta name="author" content="Jiwon Shin">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Lato:100,100i,300,300i,400,400i,700,700i,900,900i" rel="stylesheet">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css">
<link rel="stylesheet" href="css/style.css">
<script defer src="https://code.getmdl.io/1.3.0/material.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.16/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.16/addons/p5.dom.min.js"></script>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<title>The Basics of CSS</title>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-110518171-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-110518171-1');
</script>
</head>
<style>
.col{
width: 50%;
float: left;
}
.form-row{
width: 33%;
float: left;
margin-bottom: 1rem;
}
#panel{
min-height: 450px;
}
</style>
<body>
<div class="mdl-layout mdl-js-layout mdl-layout--fixed-header">
<main class="mdl-layout__content">
<div class="page-content">
<div class="full-page-content">
<h4><b>The Basics of CSS</b></h4>
<h5>Getting to Know Inspector</h5>
<p>For the purpose of this workshop, I will be using Chrome to demonstrate how we can use inspector to learn and test css. But inspector works just about the same for all other browsers, so you shouldn't have too much difficulty following along even if you are not using Chrome.</p>
<img class="p-img" src="img/basicCss/inspector1.jpg">
<p>When you pull up the inspector window, by either <b>right click > Inspect</b> or on the menu: <b>View > Developer > Developer Tools</b>, you will see a window pop up, like in the image above.</p>
<img class="p-img" src="img/basicCss/inspector2.jpg">
<p>You can change the location of this window by clicking on the "more" icon with three dots. There, you will be able to select your <b>Dock side</b>. The first option will make your inspector a separate window, and the rest three options will change which side of the browser the inspector window is attached to.</p>
<img class="p-img" src="img/basicCss/inspector3.jpg">
<p>For now, we will focus on what is on the <b>Elements</b> tab of the inspector window. We will learn about how to use the <b>Console</b> tab for javascript soon as well. In the Elements tab, you will see that the left side of the inspector displays the html code, while on the right side shows the css of the selected html element. In the image above, I have clicked on the body html tag, so it shows the css associated with the body tag on the right side. As this website contains raw html code, it doesn't seem to have any special styling. However, an important thing to note here is that there is some <b>default css</b> that exists for the body html tag itself, which is:
<pre>
body{
display: block;
margin: 8px;
}
</pre>
Let's take this opportunity to review the syntax of css. First, we see the selector, body. This means that the css declarations inside of the curly brackets are for this particular selector, in this case, a html tag. Remember that we can use 3 different types of selectors: 1) html tags, 2) class name, 3)id name. If we had an html code like this:
<pre>
<img class="grid-img" id="donut-img" src="img/donut.jpg">
</pre>
then we can use the class name "grid-img" as our selector like this:
<pre>
.grid-img{
width: 500px;
height: 400px;
}
</pre>
and we can also use its id name as our selector like this:
<pre>
#donut-img{
width: 1000px;
height: 800px;
}
</pre>
Let me give an example of the different cases for using an html tag, class name and id name as your selector. Let's say that I am creating a web page with a lot of images. If I want all my images to have a uniform size, I will set this using the img html tag as my selector in my css. If I want to create a category of important images, and want to put a red border around them, I will create a class, maybe call it something like important-img, and use .important-img as my selector to set this style. Lastly, if I want to make an exception and use one particular image to be always fixed on the bottom right corner of my website (maybe it's my "move to top" button), I can do that by giving that particular image an id name (in this case, something like #move-top). The html and css code for this example scenario will look something like this:
<br>
<br>
<u>html:</u>
<pre>
<img src="img/normal-img.jpg">
<img class="important-img" src="img/important-img.jpg">
<img src="img/normal-img.jpg">
<img class="important-img" src="img/important-img.jpg">
<img src="img/normal-img.jpg">
<img src="img/normal-img.jpg">
<img class="important-img" src="img/important-img.jpg">
<img id="move-top" src="img/move-top-img.jpg">
</pre>
<u>css:</u>
<pre>
img{
width: 500px;
height: 400px;
}
.important-img{
border-style: solid;
border-color: red;
}
#move-top{
width: 50px;
height: 50px;
position: fixed;
bottom: 0px;
right: 0px;
}
</pre>
Now, let's look at the lines of code inside of the curly brackets. They have a syntax of:
<pre>
css-property: value;
</pre>
This is where we can use our new tool, inspector, to explore.
</p>
<img class="p-img" src="img/basicCss/inspector3.jpg">
<p>This is the same image that we saw before. Note that there is a section on my web page highlighted in blue, with two orange lines above and below. This section of my webpage is highlighted because my mouse is currently hovering over this particular part of html code inside my inspector. It also has a little indicator above that says "p | 1031 x 36". This means that the selected html code is of an html tag <p> and has width of 1031 pixels and height of 36 pixels at this particular browser size.
</p>
<img class="p-img" src="img/basicCss/inspector4.jpg">
<p>If you actually click on this part of your html code on your inspector, you will see the right side of the inspector window change to display the styles for this element. Right now, same as before, there are no particular styles given.</p>
<img class="p-img" src="img/basicCss/inspector5.jpg">
<p>If you click on an empty space below where it says <b>element.style</b>, a blank template for a line of css declaration will appear. Let's try to change the color of this selected text.</p>
<img class="p-img" src="img/basicCss/inspector6.jpg">
<p>Start typing inside the little text box that appears in place for the css property. Knowing that in order to change the color of the text, the given css property is <b>color</b>, as you type in the word, you will notice that it will inspector will try to auto-fill for you and also display options that contain the characters you have already typed before. Thus, you will be able to see that there is are many other css properties that you can set colors of, such as background-color and border-color.</p>
<img class="p-img" src="img/basicCss/inspector7.jpg">
<p>If you hit tab after selecting the css property <b>color</b>, you will see that it also gives you a list of available values for this particular css property. For the color value in particular, it is possible to give it as hexadecimal or RGB, but the options that appear are the colors that can be referred to as a name.</p>
<img class="p-img" src="img/basicCss/inspector8.jpg">
<p>I am going to choose the color red. You will see this particular section of text change its color from black to red immediately after you are done typing the word. <b>A very important thing to note here is that ANY CHANGES DONE ON THE INSPECTOR ARE TEMPORARY</b>. If you refresh the page, the changes you have made, in this case changing the color of text to red, will disappear. The inspector tool should be used for experimenting with css and a way of getting immediate visual feedback of what a particular css declaration does. If you want to actually apply this css style to your web page, you should copy the css code from inspector and save it to your local css file.</p>
<p>For more detailed information about css syntax, please read <a class="p-link" href="https://www.w3schools.com/css/css_syntax.asp"><u>this w3schools' article</u></a>. Generally, <a class="p-link" href="https://www.w3schools.com/css/default.asp"><u>w3schools website</u></a> is also a helpful resource in trying to figure out if there is a css property for what you want to do, and what it exactly does.</p>
<h5>Box Model, Position and Display Property</h5>
<p><u>Box Model</u>:</p>
<img class="p-img" src="img/box_model.png">
<br>
<p>The concept of the box model is crucial for web development and css. The image above is the box model diagram of an element that shows up when you are focused on a particular html element on the inspector. The inner-most blue rectangle shows the box that contains the content of the html element. It shows that the width of the content is 428px and the height is 200px. Next, we see a green rectangle, labeled 'padding', that surrounds the content box. It shows that it has 25px of padding for all four sizes. It can be confusing to choose whether to use padding or margin to give a little spacing between html elements. The difference between padding and margin can become clear if the content has a background-color or background-image. Padding is the space between the content and the edge of the visible area of background-color / background-image. Margin is the space between the edge of the visible area of background-color / background-image and the edge of the browser, or the edge of another html element. Border comes between padding and margin. By default, border-style value is set to "none", meaning that there is no visual border by default, so in order to visually see a border, we need to set the value of border-style to something other than none and override the default "none" value.</p>
<p>It is quite hard to grasp this idea of a box model, so below is a simulator that you can experiment with different values of margin, padding and border. You can also use the simulator to play around with the display and position values.</p>
<p>For more detailed information about the <b>position</b> css property, click on <a class="p-link" href="https://www.w3schools.com/cssref/pr_class_position.asp"><u>this w3school's link</u></a>.</p>
<p>For more detailed information about the <b>display</b> css property, click on <a class="p-link" href="https://www.w3schools.com/cssref/pr_class_display.asp"><u>this w3school's link</u></a>.</p>
<p>For a full page simulator, click <a href="display-position-simulator.html" class="p-link"><u>HERE</u></a>.</p>
<iframe id="code-demo" height="200px" src="css-basic-demo.html"></iframe>
<br>
<form id="panel" oninput="checkNum()" onchange="checkVal()">
<div style="width: 50%" class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="text" name="tVal" id="textVal" value="Comm Lab">
<label class="mdl-textfield__label" for="textVal">Text Content:</label>
</div>
<div class="form-col">
<div class="form-row">
Margin: <span id="currMargin">0</span>px
<br>
<p style="width: 100%; margin-top: 1rem;">
<input class="mdl-slider mdl-js-slider" type="range" name="marginVal" min="0" max="150" value="0" tabindex="0">
</p>
</div>
<div class="form-row">
Padding: <span id="currPadding">0</span>px
<br>
<p style="width: 100%; margin-top: 1rem;">
<input class="mdl-slider mdl-js-slider" type="range" name="paddingVal" min="0" max="150" value="0" tabindex="0">
</p>
</div>
<div class="form-row">
Border: <span id="currBorder">0</span>px
<br>
<p style="width: 100%; margin-top: 1rem;">
<input class="mdl-slider mdl-js-slider" type="range" name="borderVal" min="0" max="50" value="0" tabindex="0">
</p>
</div>
</div>
<div class="col">
Display Value:<br>
<label class="mdl-radio mdl-js-radio mdl-js-ripple-effect">
<input type="radio" class="mdl-radio__button" name="dVal" value="block">
<span class="mdl-radio__label">block</span>
</label>
<br>
<label class="mdl-radio mdl-js-radio mdl-js-ripple-effect">
<input type="radio" class="mdl-radio__button" name="dVal" value="inline" checked>
<span class="mdl-radio__label">inline</span>
</label>
<br>
<label class="mdl-radio mdl-js-radio mdl-js-ripple-effect">
<input type="radio" class="mdl-radio__button" name="dVal" value="inline-block">
<span class="mdl-radio__label">inline-block</span>
</label>
<br>
<label class="mdl-radio mdl-js-radio mdl-js-ripple-effect">
<input type="radio" class="mdl-radio__button" name="dVal" value="none">
<span class="mdl-radio__label">none</span>
</label>
<br><br>
Width: <span id="currWidth">auto</span> (0px = auto)
<br>
<p style="width: 80%; margin-top: 1rem;">
<input class="mdl-slider mdl-js-slider" type="range" name="wVal" min="0" max="800" value="0" tabindex="0">
</p>
<br>
Height: <span id="currHeight">auto</span> (0px = auto)
<br>
<p style="width: 80%; margin-top: 1rem;">
<input class="mdl-slider mdl-js-slider" type="range" name="hVal" min="0" max="800" value="0" tabindex="0">
</p>
</div>
<div class="col">
Position Value:<br>
<label class="mdl-radio mdl-js-radio mdl-js-ripple-effect">
<input type="radio" class="mdl-radio__button" name="pVal" value="static" checked>
<span class="mdl-radio__label">static</span>
</label>
<br>
<label class="mdl-radio mdl-js-radio mdl-js-ripple-effect">
<input type="radio" class="mdl-radio__button" name="pVal" value="relative">
<span class="mdl-radio__label">relative</span>
</label>
<br>
<label class="mdl-radio mdl-js-radio mdl-js-ripple-effect">
<input type="radio" class="mdl-radio__button" name="pVal" value="fixed">
<span class="mdl-radio__label">fixed</span>
</label>
<br>
<label class="mdl-radio mdl-js-radio mdl-js-ripple-effect">
<input type="radio" class="mdl-radio__button" name="pVal" value="absolute">
<span class="mdl-radio__label">absolute</span>
</label>
<br><br>
Top: <span id="currTop">0%</span>
<br>
<p style="width: 80%; margin-top: 1rem;">
<input class="mdl-slider mdl-js-slider" type="range" name="topVal" min="0" max="100" value="0" tabindex="0">
</p>
<br>
Left: <span id="currLeft">0</span>%
<br>
<p style="width: 80%; margin-top: 1rem;">
<input class="mdl-slider mdl-js-slider" type="range" name="leftVal" min="0" max="100" value="0" tabindex="0">
</p>
</div>
</form>
<h5>Demo: Parallax Web Page</h5>
<iframe height="500px" src="parallax-demo.html"></iframe>
<br>
<p><u><a class="p-link" href="parallax-demo.html">Click to view the demo as a full web page.</a></u></p>
<p>Let's start off with creating three divs, one for each background. We will also give class names for each of these sections.</p>
<pre>
<div class="section-1"></div>
<div class="section-2"></div>
<div class="section-3"></div>
</pre>
<p>Now, we will give it background images to each of these sections in css.</p>
<pre>
.section-1{
background-image: url(img/parallax/bg1.jpg);
}
.section-2{
background-image: url(img/parallax/bg2.jpg);
}
.section-3{
background-image: url(img/parallax/bg3.jpg);
}
</pre>
<p>As of now, nothing will appear on your screen, because you haven't given your html, body and div elements any height yet. If a specific height is not set with css, as all of our html content has height of 0 (div has a height of 0px if there is no content inside of the div - remember that a div element is used just to group html elements and doesn't have any visual qualities) and so the web page does not display anything. So we will set the height of our html, body and the 3 sections of div elements to 100%.</p>
<pre>
html, body{
height: 100%;
}
.section-1, .section-2, .section-3{
height: 100%;
}
</pre>
<p>One important thing to note in the css above is that, you can give multiple html / class / id elements the same style by linking them with a comma. For example, as I want all of my three sections to share a css property of height 100%, I can do this in one chunck of css by referring to all three classes at the same time by doing: .section-1, .section-2, .section-3</p>
<p>Now, you will notice, depending on the size of your images, the background images that you gave will be either stretched out or tiled to fit the space of your browser. Let's set our background images to be at the center, to not repeat (to prevent the tiling of the image) and to cover the size of the browser.</p>
<pre>
.section-1, .section-2, .section-3{
height: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
</pre>
<p>With this, we have set the size and position of our background so that it auto-adjusts to the size of the browser. However, for now, it looks like as if the images are just placed in order and it doesn't have the parallax effect yet. Now for the one magic line that will create this parallax effect: <b>background-attachment: fixed;</b></p>
<pre>
.section-1, .section-2, .section-3{
height: 100%;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
</pre>
<p>That additional line of css will fix the position of the background image regardless of the scrolling. As it scrolls, it reveals the background of the next div, from bottom to top.
<br>
Now we have a basic parallax web page. You can of course, put in div elements with text in between the div elements with the background images. You can also put in content inside of the div elements with background images, to place them along with each of the background image of the section div. Here is the complete html and css code of the demo web page:</p>
<p><u>HTML</u>:</p>
<pre>
<div class="section-1">
<div class="caption">
<span class="caption-text">Title of Website</span>
</div>
</div>
<div class="section-text">
<p>This is a place where you can enter in text content.</p>
</div>
<div class="section-2"></div>
<div class="section-text">
<p>This is another place where you can enter in text content.</p>
</div>
<div class="section-3">
<img class="center-img" src="img/cat.png">
</div>
</pre>
<p><u>CSS</u>:</p>
<pre>
html, body{
height: 100%;
margin: 0;
font-family: "Helvetica Neue", sans-serif;
}
.section-1, .section-2, .section-3{
position: relative;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
height: 100%;
}
.section-1{
background-image: url(img/parallax/bg1.jpg);
}
.section-2{
background-image: url(img/parallax/bg2.jpg);
}
.section-3{
background-image: url(img/parallax/bg3.jpg);
}
.caption{
position: absolute;
top: 50%;
left: 0px;
width: 100%;
text-align: center;
}
.caption-text{
background-color: white;
color: #cecece;
font-size: 42px;
font-weight: bold;
padding: 15px
}
.section-text{
padding: 50px 80px;
}
.center-img{
width: 50%;
position: absolute;
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
</pre>
</div>
<footer style="clear: both;" class="mdl-mini-footer">
<div class="mdl-mini-footer__left-section">
<ul class="mdl-mini-footer__link-list">
<li><u><a class="p-link" href="index.html" target="_self">BACK TO MAIN</a></u></li>
</ul>
</div>
<div class="mdl-mini-footer__right-section">
<ul class="mdl-mini-footer__link-list">
<li>Workshops By <u><a class="p-link" href="http://jiwonshin.com" target="_blank">Jiwon Shin</a></u></li>
</ul>
</div>
</footer>
</div>
</main>
</div>
</body>
<script src="js/background.js"></script>
<script>
var newWidth, newHeight;
var newTop, newLeft;
function checkNum(){
var selectSpan = document.getElementById('code-demo').contentDocument.getElementsByClassName('selection')[0];
selectSpan.innerHTML = document.getElementsByName('tVal')[0].value;
newWidth = document.getElementsByName('wVal')[0].value;
newHeight = document.getElementsByName('hVal')[0].value;
if(newWidth == 0){
selectSpan.style.width = "auto";
document.getElementById('currWidth').innerHTML = "auto";
}else{
selectSpan.style.width = newWidth + "px";
document.getElementById('currWidth').innerHTML = newWidth + "px";
}
if(newHeight == 0){
selectSpan.style.height = "auto";
document.getElementById('currHeight').innerHTML = "auto";
}else{
selectSpan.style.height = newHeight + "px";
document.getElementById('currHeight').innerHTML = newHeight + "px";
}
newTop = document.getElementsByName('topVal')[0].value;
newLeft = document.getElementsByName('leftVal')[0].value;
var calcTop = newTop * 8;
if(document.getElementsByName('pVal')[3].checked){
selectSpan.style.top = calcTop + "px";
}else{
selectSpan.style.top = newTop + "%";
}
selectSpan.style.left = newLeft + "%";
document.getElementById('currTop').innerHTML = newTop + "%, " + calcTop + "px";
document.getElementById('currLeft').innerHTML = newLeft;
newMargin = document.getElementsByName('marginVal')[0].value;
newPadding = document.getElementsByName('paddingVal')[0].value;
newBorder = document.getElementsByName('borderVal')[0].value;
selectSpan.style.margin = newMargin + "px";
selectSpan.style.padding = newPadding + "px";
selectSpan.style.borderWidth = newBorder + "px";
document.getElementById('currMargin').innerHTML = newMargin;
document.getElementById('currPadding').innerHTML = newPadding;
document.getElementById('currBorder').innerHTML = newBorder;
}
function checkVal(){
var selectSpan = document.getElementById('code-demo').contentDocument.getElementsByClassName('selection')[0];
for(var i = 0; i < document.getElementsByName('dVal').length; i++){
if(document.getElementsByName('dVal')[i].checked){
selectSpan.style.display = document.getElementsByName('dVal')[i].value;
}
}
for(var i = 0; i < document.getElementsByName('pVal').length; i++){
if(document.getElementsByName('pVal')[i].checked){
selectSpan.style.position = document.getElementsByName('pVal')[i].value;
}
}
}
</script>
</html>