-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcss
2739 lines (1793 loc) · 122 KB
/
css
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
CSS
Intro to CSS
The basic structure of every web page, HTML, is very plain on its own. The beautiful websites that you see across the internet are styled with a variety of tools, including CSS.
CSS, or Cascading Style Sheets, is a language that web developers use to style the HTML content on a web page. If you’re interested in modifying colors, font types, font sizes, shadows, images, element positioning, and more, CSS is the tool for the job!
In this lesson, you’ll learn how to select which HTML elements you wish to style and set up your CSS file structure.
<link href="style.css" type="text/css" rel="stylesheet">
https://discuss.codecademy.com/t/what-does-the-cascading-in-css-mean/340936
Inline Styles
Although CSS is a different language than HTML, it’s possible to write CSS code directly within HTML code using inline styles.
To style an HTML element, you can add the style attribute directly to the opening tag. After you add the attribute, you can set it equal to the CSS style(s) you’d like applied to that element.
<p style="color: red;">I'm learning to code!</p>
The code in the example above demonstrates how to use inline styling. The paragraph element has a style attribute within its opening tag. Next, the style attribute is set equal to color: red;, which will set the color of the paragraph text to red within the browser.
You might be wondering about the syntax of the following snippet of code: color: red;. At the moment, the details of the syntax are not important; you’ll learn more about CSS syntax in other exercises. For now, it’s important to know that inline styles are a quick way of directly styling an HTML element.
If you’d like to add more than one style with inline styles, simply keep adding to the style attribute. Make sure to end the styles with a semicolon (;).
<p style="color: red; font-size: 20px;">I'm learning to code!</p>
https://discuss.codecademy.com/t/this-exercise-talks-about-the-style-attribute-what-is-an-attribute/340937
https://www.youtube.com/watch?v=r5kfkpYtOiw
The <style> Tag
Inline styles are a fast way of styling HTML, but they also have limitations. If you wanted to style, for example, multiple <h1> elements, you would have to add inline styling to each element manually. In addition, you would also have to maintain the HTML code when additional <h1> elements are added.
Fortunately, HTML allows you to write CSS code in its own dedicated section with the <style> element. CSS can be written between opening and closing <style> tags. To use the <style> element, it must be placed inside of the <head> element.
<head>
<style>
</style>
</head>
After adding a <style> tag in the head section, you can begin writing CSS code.
<head>
<style>
p {
color: red;
font-size: 20px;
}
</style>
</head>
The CSS code in the example above changes the color of all paragraph text to red and also changes the size of the text to 20 pixels. Note how the syntax of the CSS code matches (for the most part) the syntax you used for inline styling. The main difference is that you can specify which elements to apply the styling to.
Again, the details of the CSS syntax in the example above aren’t important at the moment. You will learn more about the details of CSS syntax in later lessons.
https://discuss.codecademy.com/t/it-seems-strange-to-be-mashing-two-languages-together-in-a-single-document-is-this-common-practice/340938
The .css file
Developers avoid mixing code by storing HTML and CSS code in separate files (HTML files contain only HTML code, and CSS files contain only CSS code).
You can create a CSS file by using the .css file name extension, like so: style.css
With a CSS file, you can write all the CSS code needed to style a page without sacrificing the readability and maintainability of your HTML file.
https://discuss.codecademy.com/t/what-is-the-preferred-way-of-including-css-within-a-project-how-do-i-know-when-to-use-inline-styles-the-style-tags-or-a-css-file/340939
https://discuss.codecademy.com/t/how-do-i-link-my-html-and-css/423288
Linking the CSS File
Perfect! We successfully separated structure (HTML) from styling (CSS), but the web page still looks bland. Why?
When HTML and CSS code are in separate files, the files must be linked. Otherwise, the HTML file won’t be able to locate the CSS code, and the styling will not be applied.
You can use the <link> element to link HTML and CSS files together. The <link> element must be placed within the head of the HTML file. It is a self-closing tag and requires the following three attributes:
href — like the anchor element, the value of this attribute must be the address, or path, to the CSS file.
type — this attribute describes the type of document that you are linking to (in this case, a CSS file). The value of this attribute should be set to text/css.
rel — this attribute describes the relationship between the HTML file and the CSS file. Because you are linking to a stylesheet, the value should be set to stylesheet.
When linking an HTML file and a CSS file together, the <link> element will look like the following:
<link href="https://www.codecademy.com/stylesheets/style.css" type="text/css" rel="stylesheet">
Note that in the example above the path to the stylesheet is a URL:
https://www.codecademy.com/stylesheets/style.css
Specifying the path to the stylesheet using a URL is one way of linking a stylesheet.
If the CSS file is stored in the same directory as your HTML file, then you can specify a relative path instead of a URL, like so:
<link href="./style.css" type="text/css" rel="stylesheet">
Using a relative path is very common way of linking a stylesheet.
https://discuss.codecademy.com/t/how-do-i-link-my-css-stylesheet-to-html-file/307799/1
https://discuss.codecademy.com/t/can-the-link-tag-be-used-to-link-files-aside-from-css-to-our-html-document/340940
https://discuss.codecademy.com/t/can-the-link-tag-be-used-to-include-javascript-in-our-projects/340942
Tag Name
CSS can select HTML elements by using an element’s tag name. A tag name is the word (or character) between HTML angle brackets.
For example, in HTML, the tag for a paragraph element is <p>. The CSS syntax for selecting <p> elements is:
p {
}
In the example above, all paragraph elements will be selected using a CSS selector. The selector in the example above is p. Note that the CSS selector matches the HTML tag for that element, but without the angle brackets.
In addition, two curly braces follow immediately after the selector (an opening and closing brace, respectively). Any CSS properties will go inside of the curly braces to style the selected elements.
https://discuss.codecademy.com/t/in-css-can-we-select-any-html-element-by-using-its-tag-name-as-demonstrated-in-this-exercise/340943
Class Name
CSS is not limited to selecting elements by tag name. HTML elements can have more than just a tag name; they can also have attributes. One common attribute is the class attribute. It’s also possible to select an element by its class attribute.
For example, consider the following HTML:
<p class="brand">Sole Shoe Company</p>
The paragraph element in the example above has a class attribute within the <p> tag. The class attribute is set to "brand". To select this element using CSS, we could use the following CSS selector:
.brand {
}
To select an HTML element by its class using CSS, a period (.) must be prepended to the class’s name. In the example above case, the class is brand, so the CSS selector for it is .brand.
https://discuss.codecademy.com/t/in-the-example-given-in-this-exercise-why-would-we-want-to-use-the-brand-selector-rather-than-the-p-selector-to-target-our-paragraph/340946
Multiple Classes
We can use CSS to select an HTML element’s class attribute by name.
So far, we’ve selected elements using only one class name per element. If every HTML element had a single class, all the style information for each element would require a new class.
Luckily, it’s possible to add more than one class name to an HTML element’s class attribute.
For instance, perhaps there’s a heading element that needs to be green and bold. You could write two CSS rules like so:
.green {
color: green;
}
.bold {
font-weight: bold;
}
Then, you could include both of these classes on one HTML element like this:
<h1 class="green bold"> ... </h1>
We can add multiple classes to an HTML element’s class attribute by separating them with a space. This enables us to mix and match CSS classes to create many unique styles without writing a custom class for every style combination needed.
https://discuss.codecademy.com/t/in-the-example-shown-in-this-exercise-wouldn-t-it-be-easier-to-just-apply-the-color-and-font-weight-properties-to-the-h2-selector-rather-than-applying-two-separate-classes-to-the-opening-h1-tag/340947
ID Name
If an HTML element needs to be styled uniquely (no matter what classes are applied to the element), we can add an ID to the element. To add an ID to an element, the element needs an id attribute:
<h1 id="large-title"> ... </h1>
Then, CSS can select HTML elements by their id attribute. To select an id element, CSS prepends the id name with a hashtag (#). For instance, if we wanted to select the HTML element in the example above, it would look like this:
#large-title {
}
The id name is large-title, therefore the CSS selector for it is #large-title.
font-family: cursive;
text-transform: capitalize;
https://discuss.codecademy.com/t/what-happens-if-we-try-to-apply-the-same-id-to-multiple-elements-will-our-styles-not-get-applied-to-the-targeted-ided-elements/340948
Classes and IDs
CSS can select HTML elements by their tag, class, and ID. CSS classes and IDs have different purposes, which can affect which one you use to style HTML elements.
CSS classes are meant to be reused over many elements. By writing CSS classes, you can style elements in a variety of ways by mixing classes on HTML elements.
For instance, imagine a page with two headlines. One headline needs to be bold and blue, and the other needs to be bold and green. Instead of writing separate CSS rules for each headline that repeat each other’s code, it’s better to write a .bold CSS rule, a .green CSS rule, and a .blue CSS rule. Then you can give one headline the bold green classes, and the other the bold blue classes.
While classes are meant to be used many times, an ID is meant to style only one element. As we’ll learn in the next exercise, IDs override the styles of tags and classes. Since IDs override class and tag styles, they should be used sparingly and only on elements that need to always appear the same.
https://discuss.codecademy.com/t/can-classes-be-used-as-a-page-anchors-in-a-similar-way-to-ids/340950
Specificity
Specificity is the order by which the browser decides which CSS styles will be displayed. A best practice in CSS is to style elements while using the lowest degree of specificity, so that if an element needs a new style, it is easy to override.
IDs are the most specific selector in CSS, followed by classes, and finally, tags. For example, consider the following HTML and CSS:
<h1 class="headline">Breaking News</h1>
h1 {
color: red;
}
.headline {
color: firebrick;
}
In the example code above, the color of the heading would be set to firebrick, as the class selector is more specific than the tag selector. If an ID attribute (and selector) were added to the code above, the styles within the ID selector’s body would override all other styles for the heading. The only way to override an ID is to add another ID with additional styling.
Over time, as files grow with code, many elements may have IDs, which can make CSS difficult to edit, since a new, more specific style must be created to change the style of an element.
To make styles easy to edit, it’s best to style with a tag selector, if possible. If not, add a class selector. If that is not specific enough, then consider using an ID selector.
https://discuss.codecademy.com/t/is-it-possible-for-two-selectors-targeting-the-same-element-to-have-the-same-specificity-if-so-which-styles-win-out/340952
Chaining Selectors
When writing CSS rules, it’s possible to require an HTML element to have two or more CSS selectors at the same time.
This is done by combining multiple selectors, which we will refer to as chaining. For instance, if there was a .special class for h1 elements, the CSS would look like:
h1.special {
}
The code above would select only the h1 elements that have a class of special. If a p element also had a class of special, the rule in the example would not style the paragraph.
https://discuss.codecademy.com/t/oes-a-chained-selector-have-a-higher-specificity-than-a-class-or-id-selector-for-example-which-selector-would-win-special-or-h1-special-or-special/340954
Nested Elements
In addition to chaining selectors to select elements, CSS also supports selecting elements that are nested within other HTML elements. For instance, consider the following HTML:
<ul class='main-list'>
<li> ... </li>
<li> ... </li>
<li> ... </li>
</ul>
The nested <li> elements are selected with the following CSS:
.main-list li {
}
In the example above, .main-list selects the .main-list element (the unordered list element). The nested <li> are selected by adding li to the selector, separated by a space, resulting in .main-list li as the final selector (note the space in the selector).
Selecting elements in this way can make our selectors even more specific by making sure they appear in the context we expect.
https://discuss.codecademy.com/t/this-exercise-uses-selectors-that-target-elements-which-are-direct-children-of-parent-elements-can-we-similarly-target-more-deeply-nested-elements/340956
Chaining and Specificity
In the last exercise, instead of selecting all h5 elements, you selected only the h5 elements nested inside the .description elements. This CSS selector was more specific than writing only h5. Adding more than one tag, class, or ID to a CSS selector increases the specificity of the CSS selector.
For instance, consider the following CSS:
p {
color: blue;
}
.main p {
color: red;
}
Both of these CSS rules define what a p element should look like. Since .main p has a class and a p tag as its selector, only the p elements inside the .main element will appear red. This occurs despite there being another more general rule that states p elements should be blue.
https://discuss.codecademy.com/t/according-to-this-exercise-main-p-is-more-specific-than-the-p-selector-is-main-p-also-more-specific-than-the-p-main-or-main-selectors/340961
Important
There is one thing that is even more specific than IDs: !important. !important can be applied to specific attributes instead of full rules. It will override any style no matter how specific it is. As a result, it should almost never be used. Once !important is used, it is very hard to override.
The syntax of !important in CSS looks like this:
p {
color: blue !important;
}
.main p {
color: red;
}
Since !important is used on the p selector’s color attribute, all p elements will appear blue, even though there is a more specific .main p selector that sets the color attribute to red.
The !important flag is only useful when an element appears the same way 100% of the time. Since it’s almost impossible to guarantee that this will be true throughout a project and over time, it’s best to avoid !important altogether. If you ever see !important used (or are ever tempted to use it yourself) we strongly recommend reorganizing your CSS. Making your CSS more flexible will typically fix the immediate problem and make your code more maintainable in the long run.
https://discuss.codecademy.com/t/this-exercise-talks-about-making-our-css-more-flexible-what-does-this-mean-and-how-can-we-accomplish-this/340964
Multiple Selectors
In order to make CSS more concise, it’s possible to add CSS styles to multiple CSS selectors all at once. This prevents writing repetitive code.
For instance, the following code has repetitive style attributes:
h1 {
font-family: Georgia;
}
.menu {
font-family: Georgia;
}
Instead of writing font-family: Georgia twice for two selectors, we can separate the selectors by a comma to apply the same style to both, like this:
h1,
.menu {
font-family: Georgia;
}
By separating the CSS selectors with a comma, both the h1 and the .menu elements will receive the font-family: Georgia styling.
https://discuss.codecademy.com/t/this-exercise-illustrates-how-we-can-use-multiple-selectors-to-create-more-concise-code-are-there-additional-tactics-for-creating-less-repetitive-stylesheets/340966
Review CSS Selectors
Throughout this lesson, you learned how to select HTML elements with CSS and apply styles to them. Let’s review what you learned:
CSS can change the look of HTML elements. In order to do this, CSS must select HTML elements, then apply styles to them.
CSS can select HTML elements by tag, class, or ID.
Multiple CSS classes can be applied to one HTML element.
Classes can be reusable, while IDs can only be used once.
IDs are more specific than classes, and classes are more specific than tags. That means IDs will override any styles from a class, and classes will override any styles from a tag selector.
Multiple selectors can be chained together to select an element. This raises the specificity, but can be necessary.
Nested elements can be selected by separating selectors with a space.
The !important flag will override any style, however it should almost never be used, as it is extremely difficult to override.
Multiple unrelated selectors can receive the same styles by separating the selector names with commas.
Great work this lesson. With this knowledge, you’ll be able to use CSS to change the look and feel of websites to make them look great.
CSS Structure
To style an HTML element using CSS, you need to write a CSS declaration inside the body of a CSS selector.
h1 {
color: blue;
}
The example above selects the <h1> element. Inside of the selector’s body, we typed color: blue. This line is referred to as a CSS declaration. CSS declarations consist of a property and a value.
Property — the property you’d like to style of that element (i.e., size, color, etc.).
Value — the value of the property (i.e., 18px for size, blue for color, etc.).
In the example above, the property is color and the value is blue. The property and value are separated by a colon (:). A semicolon (;) should always be used at the end of a declaration.
Finally, the entire snippet of code in the example above is known as a CSS rule or rule set. A CSS rule consists of the selector (here, h1) and all declarations inside of the selector.
https://discuss.codecademy.com/t/what-is-a-css-selector/340980
body {
/* Old browsers */
background: #141E30;
/* Chrome 10-25, Safari 5.1-6 */
background: -webkit-linear-gradient(-45deg, #35577D, #141E30);
/* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
background: linear-gradient(-45deg, #35577D, #141E30);
margin: 0;
padding: 0;
}
h1 {
color: #FFF;
font-size: 2em;
padding-top: 100px;
width: 100%;
}
h2 {
border-bottom: 1px solid rgba(255, 255, 255, 0.5);
color: rgba(255, 255, 255, 0.5);
font-weight: 100;
font-size: 22px;
line-height: 24px;
padding-bottom: 30px;
text-align: left;
width: 70%;
}
p {
color: AliceBlue;
line-height: 1.3em;
text-align: left;
width: 100%;
}
.byline {
font-family: Helvetica;
color: rgba(255, 255, 255, 0.5);
float: left;
font-size: 14px;
padding-left: 10px;
text-transform: uppercase;
}
.caption {
display: block;
font-family: 'Playfair Display', serif;
font-size: 14px;
font-style: italic;
line-height: 14px;
margin-left: 20px;
padding: 10px;
position: relative;
top: 80%;
width: 60%;
}
.content {
padding: 40px;
}
.image {
background-image: url("https://s3.amazonaws.com/codecademy-content/courses/web-101/unit-4/htmlcss1-img_soccer.jpeg");
background-size: cover;
background-position: center;
height: 300px;
}
.writer-img {
-webkit-box-shadow: 5px 0px 5px 0px rgba(0, 0, 50, 0.97);
-moz-box-shadow: 5px 0px 5px 0px rgba(0, 0, 50, 0.97);
box-shadow: 5px 0px 5px 0px rgba(0, 0, 50, 0.97);
float: left;
width: 50px;
}
Font Family
If you’ve ever used a formatted word processor, chances are that you probably also used a feature that allowed you change the font you were typing in. Font refers to the technical term typeface, or font family.
To change the typeface of text on your web page, you can use the font-family property.
h1 {
font-family: Garamond;
}
In the example above, the font family for all main heading elements has been set to Garamond.
When setting typefaces on a web page, keep the following points in mind:
The font specified in a stylesheet must be installed on a user’s computer in order for that font to display when a user visits the web page.
The default typeface for all HTML elements is Times New Roman. You may be familiar with this typeface if you have ever used a formatted word processor. If no font-family attribute is defined, the page will appear in Times New Roman.
It’s a good practice to limit the number of typefaces used on a web page to 2 or 3. This helps the page load faster in some cases and is usually a good design decision.
When the name of a typeface consists of more than one word, it’s a best practice to enclose the typeface’s name in quotes, like so:
h1 {
font-family: "Courier New";
}
You can find a reference of web safe fonts here.
https://www.cssfontstack.com/
https://en.wikipedia.org/wiki/Typeface
https://discuss.codecademy.com/t/with-the-advent-of-web-fonts-can-t-a-user-see-a-site-s-font-even-if-those-fonts-aren-t-installed-on-her-computer/340981
Font Size
Changing the typeface isn’t the only way to customize text. Often times, different sections of a web page are highlighted by modifying the font size.
To change the size of text on your web page, you can use the font-size property.
p {
font-size: 18px;
}
In the example above, the font-size of all paragraphs was set to 18px. px means pixels and is a way to measure font size.
https://discuss.codecademy.com/t/what-is-a-pixel-and-is-this-the-only-measure-of-font-size/340983
Font Weight
In CSS, the font-weight property controls how bold or thin text appears.
p {
font-weight: bold;
}
In the example above, all paragraphs on the web page would appear bolded.
The font-weight property has a another value: normal. Why does it exist?
If we wanted all text on a web page to appear bolded, we could select all text elements and change their font weight to bold. If a certain section of text was required to appear normal, however, we could set the font weight of that particular element to normal, essentially shutting off bold for that element.
https://discuss.codecademy.com/t/can-all-fonts-be-bolded-with-the-font-weight-property/340987
Text Align
No matter how much styling is applied to text (typeface, size, weight, etc.), text always appears on the left side of the browser.
To align text we can use the text-align property. The text-align property will align text to the element that holds it, otherwise known as its parent.
h1 {
text-align: right;
}
The text-align property can be set to one of the following three values:
left — aligns text to the left hand side of its parent element, which in this case is the browser.
center — centers text inside of its parent element.
right — aligns text to the right hand side of its parent element.
https://discuss.codecademy.com/t/can-the-text-align-property-only-be-used-to-align-text-or-can-we-use-it-to-align-other-content-as-well/340988
Color
Before discussing the specifics of color, it’s important to make two distinctions about color. Color can affect the following design aspects:
Foreground color
Background color
Foreground color is the color that an element appears in. For example, when a heading is styled to appear green, the foreground color of the heading has been styled.
Conversely, when a heading is styled so that its background appears yellow, the background color of the heading has been styled.
In CSS, these two design aspects can be styled with the following two properties:
color: this property styles an element’s foreground color
background-color: this property styles an element’s background color
h1 {
color: red;
background-color: blue;
}
In the example above, the text of the heading will appear in red, and the background of the heading will appear blue.
https://discuss.codecademy.com/t/how-do-we-know-which-background-and-foreground-colors-work-well-together/340991
Opacity
Opacity is the measure of how transparent an element is. It’s measured from 0 to 1, with 1 representing 100%, or fully visible and opaque, and 0 representing 0%, or fully invisible.
Opacity can be used to make elements fade into others for a nice overlay effect. To adjust the opacity of an element, the syntax looks like this:
.overlay {
opacity: 0.5;
}
In the example above, the .overlay element would be 50% visible, letting whatever is positioned behind it show through.
https://discuss.codecademy.com/t/what-is-an-overlay-effect/340993
Background Image
CSS has the ability to change the background of an element. One option is to make the background of an element an image. This is done through the CSS property background-image. Its syntax looks like this:
.main-banner {
background-image: url("https://www.example.com/image.jpg");
}
The background-image property will set the element’s background to display an image.
The value provided to background-image is a url. The url should be a url to an image. The url can be a file within your project, or it can be a link to an external site. To link to an image inside an existing project, you must provide a relative file path. If there was an image folder in the project, with an image named mountains.jpg, the relative file path would look like:
.main-banner {
background-image: url("images/mountains.jpg");
}
https://discuss.codecademy.com/t/what-is-a-relative-path/340994
Review Visual Rules
Incredible work! You used CSS to alter text and images throughout a website. Throughout this lesson, you learned concepts including:
CSS declarations are structured into property and value pairs.
The font-family property defines the typeface of an element.
font-size controls the size of text displayed.
font-weight defines how thin or thick text is displayed.
The text-align property places text in the left, right, or center of its parent container.
Text can have two different color attributes: color and background-color. color defines the color of the text, while background-color defines the color behind the text.
CSS can make an element transparent with the opacity property.
CSS can also set the background of an element to an image with the background-image property.
https://youtu.be/InA5Ff7mxrc
https://discuss.codecademy.com/t/what-is-linear-gradient-and-why-do-we-need-three-different-background-declarations/340996
The Box Model
The box model comprises the set of properties which define parts of an element that take up space on a web page. The model includes the content area’s size (width and height) and the element’s padding, border, and margin. The properties include:
Width and height — specifies the width and height of the content area.
Padding — specifies the amount of space between the content area and the border.
Border — specifies the thickness and style of the border surrounding the content area and padding.
Margin — specifies the amount of space between the border and the outside edge of the element.
The image to the right is a visual representation of the box model.
Open the box model image in a new tab so you can reference the box model as you move through the lesson.
Height and Width
An element’s content has two dimensions: a height and a width. By default, the dimensions of an HTML box are set to hold the raw contents of the box.
The CSS height and width properties can be used to modify these default dimensions.
p {
height: 80px;
width: 240px;
}
In this example, the height and width of paragraph elements are set to 80 pixels and 240 pixels, respectively — the px in the code above stands for pixels.
Pixels allow you to set the exact size of an element’s box (width and height). When the width and height of an element are set in pixels, it will be the same size on all devices — an element that fills a laptop screen will overflow a mobile screen.
https://discuss.codecademy.com/t/why-some-elements-dont-have-height-and-width/364946
Borders
A border is a line that surrounds an element, like a frame around a painting. Borders can be set with a specific width, style, and color.
width — The thickness of the border. A border’s thickness can be set in pixels or with one of the following keywords: thin, medium, or thick.
style — The design of the border. Web browsers can render any of 10 different styles. Some of these styles include: none, dotted, and solid.
color — The color of the border. Web browsers can render colors using a few different formats, including 140 built-in color keywords.
p {
border: 3px solid coral;
}
In the example above, the border has a width of 3 pixels, a style of solid and a color of coral. All three properties are set in one line of code.
The default border is medium none color, where color is the current color of the element. If width, style, or color are not set in the CSS file, the web browser assigns the default value for that property.
p.content-header {
height: 80px;
width: 240px;
border: solid coral;
}
In this example, the border style is set to solid and the color is set to coral. The width is not set, so it defaults to medium.
https://developer.mozilla.org/en-US/docs/Web/CSS/border-style#Values
https://developer.mozilla.org/en-US/docs/Web/CSS/color_value
Border Radius
Ever since we revealed the borders of boxes, you may have noticed that the borders highlight the true shape of an element’s box: square. Thanks to CSS, a border doesn’t have to be square.
You can modify the corners of an element’s border box with the border-radius property.
div.container {
border: 3px solid rgb(22, 77, 100);
border-radius: 5px;
}
The code in the example above will set all four corners of the border to a radius of 5 pixels (i.e. the same curvature that a circle with radius 5 pixels would have).
You can create a border that is a perfect circle by setting the radius equal to the height of the box, or to 100%.
div.container {
height: 60px;
width: 60px;
border: 3px solid rgb(22, 77, 100);
border-radius: 100%;
}
The code in the example above creates a <div> that is a perfect circle.
https://discuss.codecademy.com/t/oval-as-a-result-of-border-radius-100/365307
Padding I
The space between the contents of a box and the borders of a box is known as padding. Padding is like the space between a picture and the frame surrounding it. In CSS, you can modify this space with the padding property.
p.content-header {
border: 3px solid coral;
padding: 10px;
}
The code in this example puts 10 pixels of space between the content of the paragraph (the text) and the borders, on all four sides.
The padding property is often used to expand the background color and make content look less cramped.
If you want to be more specific about the amount of padding on each side of a box’s content, you can use the following properties:
padding-top
padding-right
padding-bottom
padding-left
Each property affects the padding on only one side of the box’s content, giving you more flexibility in customization.
p.content-header {
border: 3px solid fuschia;
padding-bottom: 10px;
}
In the example above, only the bottom side of the paragraph’s content will have a padding of 10 pixels.
https://discuss.codecademy.com/t/are-padding-and-margin-interchangeable/365314
Padding II
Another implementation of the padding property lets you specify exactly how much padding there should be on each side of the content in a single declaration.
p.content-header {
border: 3px solid grey;
padding: 6px 11px 4px 9px;
}
In the example above, the four values 6px 11px 4px 9px correspond to the amount of padding in a clockwise rotation. In order, it specifies the amount of padding on the top (6 pixels), right (11 pixels), bottom (4 pixels), and left (9 pixels) sides of the content.
When using this implementation of the padding property, we must specify a padding value for all four sides of the element.
However, if the top and bottom values for padding will equal each other, and the left and right values for padding will also equal each other, you can use the following shortcut:
p.content-header {
padding: 5px 10px;
}
The first value, 5px, sets the padding value for the top and bottom sides of the content. The second value, 10px, sets the padding value for the left and right sides of the content.
https://discuss.codecademy.com/t/single-declaration/365316
Margins I
So far you’ve learned about the following components of the box model: content, borders, and padding. The fourth and final component of the box model is margin.
Margin refers to the space directly outside of the box. The margin property is used to specify the size of this space.
p {
border: 1px solid aquamarine;
margin: 20px;
}
The code in the example above will place 20 pixels of space on the outside of the paragraph’s box on all four sides. This means that other HTML elements on the page cannot come within 20 pixels of the paragraph’s border.
If you want to be even more specific about the amount of margin on each side of a box, you can use the following properties:
margin-top
margin-right
margin-bottom
margin-left
Each property affects the margin on only one side of the box, providing more flexibility in customization.
p {
border: 3px solid DarkSlateGrey;
margin-right: 15px;
}
In the example above, only the right side of the paragraph’s box will have a margin of 15 pixels. It’s common to see margin values used for a specific side of an element.
https://discuss.codecademy.com/t/margin-in-the-box-model/365319
Margins II
What if you don’t want equal margins on all four sides of the box?
A similar implementation of the margin property is used to specify exactly how much margin there should be on each side of the box in a single declaration.
p {
margin: 6px 10px 5px 12px;
}
In the example above, the four values 6px 10px 5px 12px refer to the amount of margin around the box in a clockwise rotation. In order, it specifies the amount of margin on the top (6 pixels), right (10 pixels), bottom (5 pixels), and left (12 pixels) sides of the box.
When using this implementation of the margin property, the margin value must be specified for all four sides of the box.
Just like the padding shortcut, when you’re certain that the top and bottom values for margin will equal each other, and that the left and right values for margin will also equal each other, you can use the following shortcut:
p {
margin: 6px 12px;
}
The first value, 6px, sets a margin value for the top and bottom of the box. The second value, 12px, sets a margin value for the left and right sides of the box.
CSS
Margins II
What if you don’t want equal margins on all four sides of the box?
A similar implementation of the margin property is used to specify exactly how much margin there should be on each side of the box in a single declaration.
p {
margin: 6px 10px 5px 12px;
}
In the example above, the four values 6px 10px 5px 12px refer to the amount of margin around the box in a clockwise rotation. In order, it specifies the amount of margin on the top (6 pixels), right (10 pixels), bottom (5 pixels), and left (12 pixels) sides of the box.
When using this implementation of the margin property, the margin value must be specified for all four sides of the box.
Just like the padding shortcut, when you’re certain that the top and bottom values for margin will equal each other, and that the left and right values for margin will also equal each other, you can use the following shortcut:
p {
margin: 6px 12px;
}
The first value, 6px, sets a margin value for the top and bottom of the box. The second value, 12px, sets a margin value for the left and right sides of the box.
https://discuss.codecademy.com/t/margin-property-with-three-values/365322
Auto
The margin property also lets you center content. However, you must follow a few syntax requirements. Take a look at the following example:
div {
margin: 0 auto;
}
In the example above, margin: 0 auto; will center the divs in their containing elements. The 0 sets the top and bottom margins to 0 pixels. The auto value instructs the browser to adjust the left and right margins until the element is centered within its containing element.
The div elements in the example above should center within an element that fills the page, but this doesn’t occur. Why?
In order to center an element, a width must be set for that element. Otherwise, the width of the div will be automatically set to the full width of its containing element, like the <body>, for example. It’s not possible to center an element that takes up the full width of the page.
div.headline {
width: 400px;
margin: 0 auto;
}
In the example above, the width of the div is set to 400 pixels, which is less than the width of most screens. This will cause the div to center within a containing element that is greater than 400 pixels wide.
https://discuss.codecademy.com/t/0-auto-value-in-margin/365324
Margin Collapse
As you have seen, padding is space added inside an element’s border, while margin is space added outside an element’s border. One additional difference is that top and bottom margins, also called vertical margins, collapse, while top and bottom padding does not.
Horizontal margins (left and right), like padding, are always displayed and added together. For example, if two divs with ids #div-one and #div-two, are next to each other, they will be as far apart as the sum of their adjacent margins.
#img-one {
margin-right: 20px;
}
#img-two {
margin-left: 20px;
}
In this example, the space between the #img-one and #img-two borders is 40 pixels. The right margin of #img-one (20px) and the left margin of #img-two (20px) add to make a total margin of 40 pixels.
Unlike horizontal margins, vertical margins do not add. Instead, the larger of the two vertical margins sets the distance between adjacent elements.
#img-one {
margin-bottom: 30px;
}
#img-two {
margin-top: 20px;
}
In this example, the vertical margin between the #img-one and #img-two elements is 30 pixels. Although the sum of the margins is 50 pixels, the margin collapses so the spacing is only dependent on the #img-one bottom margin.
It may be helpful to think of collapsing vertical margins as a short person trying to push a taller person. The tall person has longer arms and can easily push the short person, while the person with short arms cannot reach the person with long arms.
https://discuss.codecademy.com/t/collapsing-margins/365328
Minimum and Maximum Height and Width
Because a web page can be viewed through displays of differing screen size, the content on the web page can suffer from those changes in size. To avoid this problem, CSS offers two properties that can limit how narrow or how wide an element’s box can be sized to.
min-width — this property ensures a minimum width of an element’s box.
max-width — this property ensures a maximum width of an element’s box.
p {
min-width: 300px;
max-width: 600px;
}
In the example above, the width of all paragraphs will not shrink below 300 pixels, nor will the width exceed 600 pixels.
Content, like text, can become difficult to read when a browser window is narrowed or expanded. These two properties ensure that content is legible by limiting the minimum and maximum widths of an element.
You can also limit the minimum and maximum height of an element.
min-height — this property ensures a minimum height for an element’s box.
max-height — this property ensures a maximum height of an element’s box.
p {
min-height: 150px;
max-height: 300px;
}
In the example above, the height of all paragraphs will not shrink below 150 pixels and the height will not exceed 300 pixels.
What will happen to the contents of an element’s box if the max-height property is set too low? It’s possible for the content to spill outside of the box, resulting in content that is not legible. You’ll learn how to work around this issue in the next exercise.
https://discuss.codecademy.com/t/min-max-width-height-on-text-elements-vs-just-font-size/365538
Overflow
All of the components of the box model comprise an element’s size. For example, an image that has the following dimensions is 364 pixels wide and 244 pixels tall.
300 pixels wide
200 pixels tall
10 pixels padding on the left and right
10 pixels padding on the top and bottom
2 pixels border on the left and right
2 pixels border on the top and bottom
20 pixels margin on the left and right
10 pixels margin on the top and bottom
The total dimensions (364px by 244px) are calculated by adding all of the vertical dimensions together and all of the horizontal dimensions together. Sometimes, these components result in an element that is larger than the parent’s containing area.
How can we ensure that we can view all of an element that is larger than its parent’s containing area?
The overflow property controls what happens to content that spills, or overflows, outside its box. It can be set to one of the following values:
hidden - when set to this value, any content that overflows will be hidden from view.
scroll - when set to this value, a scrollbar will be added to the element’s box so that the rest of the content can be viewed by scrolling.
visible - when set to this value, the overflow content will be displayed outside of the containing element. Note, this is the default value.
p {
overflow: scroll;
}
In the example above, if any of the paragraph content overflows (perhaps a user resizes their browser window), a scrollbar will appear so that users can view the rest of the content.
The overflow property is set on a parent element to instruct a web browser how to render child elements. For example, if a div’s overflow property is set to scroll, all children of this div will display overflowing content with a scroll bar.
https://discuss.codecademy.com/t/overflow-to-scroll-not-working-without-height/365548
Resetting Defaults
All major web browsers have a default stylesheet they use in the absence of an external stylesheet. These default stylesheets are known as user agent stylesheets. In this case, the term “user agent“ is a technical term for the browser.
User agent stylesheets often have default CSS rules that set default values for padding and margin. This affects how the browser displays HTML elements, which can make it difficult for a developer to design or style a web page.
Many developers choose to reset these default values so that they can truly work with a clean slate.
* {
margin: 0;
padding: 0;
}
The code in the example above resets the default margin and padding values of all HTML elements. It is often the first CSS rule in an external stylesheet.
Note that both properties are both set to 0. When these properties are set to 0, they do not require a unit of measurement.
https://discuss.codecademy.com/t/are-margin-and-padding-values-the-only-ones-that-need-reset/365556
Visibility
Elements can be hidden from view with the visibility property.
The visibility property can be set to one of the following values:
hidden — hides an element.
visible — displays an element.
<ul>
<li>Explore</li>
<li>Connect</li>
<li class="future">Donate</li>
<ul>
.future {
visibility: hidden;
}
In the example above, the list item with a class of future will be hidden from view in the browser.
Keep in mind, however, that users can still view the contents of the list item (e.g., Donate) by viewing the source code in their browser. Furthermore, the web page will only hide the contents of the element. It will still leave an empty space where the element is intended to display.
Note: What’s the difference between display: none and visibility: hidden? An element with display: none will be completely removed from the web page. An element with visibility: hidden, however, will not be visible on the web page, but the space reserved for it will.
https://discuss.codecademy.com/t/when-is-it-better-to-use-visibility-hidden/365560
Review
In this lesson, we covered the four properties of the box model: height and width, padding, borders, and margins. Understanding the box model is an important step towards learning more advanced HTML and CSS topics. Let’s take a minute to review what you learned.
The box model comprises a set of properties used to create space around and between HTML elements.
The height and width of a content area can be set in pixels or percentage.
Borders surround the content area and padding of an element. The color, style, and thickness of a border can be set with CSS properties.
Padding is the space between the content area and the border. It can be set in pixels or percent.
Margin is the amount of spacing outside of an element’s border.
Horizontal margins add, so the total space between the borders of adjacent elements is equal to the sum of the right margin of one element and the left margin of the adjacent element.
Vertical margins collapse, so the space between vertically adjacent elements is equal to the larger margin.
margin: 0 auto horizontally centers an element inside of its parent content area, if it has a width.
The overflow property can be set to display, hide, or scroll, and dictates how HTML will render content that overflows its parent’s content area.
The visibility property can hide or show elements.
Box Model: Content-Box
Many properties in CSS have a default value and don’t have to be explicitly set in the stylesheet.
For example, the default font-weight of text is normal, but this property-value pair is not typically specified in a stylesheet.
The same can be said about the box model that browsers assume. In CSS, the box-sizing property controls the type of box model the browser should use when interpreting a web page.
The default value of this property is content-box. This is the same box model that is affected by border thickness and padding.
Box Model: Border-Box
Fortunately, we can reset the entire box model and specify a new one: border-box.
* {