File size: 63,229 Bytes
f4752a4 | 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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 | ο»Ώ
Comprehensive Guide to CSS
Welcome to the comprehensive guide on CSS (Cascading Style Sheets)! CSS is a cornerstone technology of the web, enabling you to create visually appealing and responsive websites. This guide is designed to take you from the basics of CSS to more advanced topics, complete with code examples, detailed explanations, exercises, and multiple-choice questions to test your understanding.
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
1
Comprehensive Guide to CSS 1 1. Introduction to CSS 5 What is CSS? 5
Why Use CSS? 5 Basic Example 5
2. CSS Syntax and Selectors 6 Basic Syntax 6 Basic Selectors 7 Element Selector 7
Class Selector 7 ID Selector 7
Grouping Selectors 7 Combinators 8 Descendant Selector 8 Child Selector 8 Adjacent Sibling Selector 8 General Sibling Selector 8 Pseudo-classes and Pseudo-elements 9 Pseudo-classes 9 Pseudo-elements 9 Attribute Selectors 9
3. CSS Box Model 10 Understanding the Box Model 10 Box Sizing 11 Content-box (Default) 11 Border-box 11 Example: Box Model in Action 12
4. CSS Colors and Backgrounds 13 Color Values 13 Background Properties 14 Background Color 14 Background Image 14 Background Repeat 14 Background Position 14 Background Size 15 Shorthand Background Property 15 Example: Styling a Header with Background 15
5. CSS Typography 16
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
2
Font Properties 16 font-family 16 font-size 17 font-weight 17 font-style 17 line-height 17 Text Properties 18 color 18 text-align 18 text-decoration 18 text-transform 18 letter-spacing and word-spacing 18 Example: Styling Text 19
6. CSS Layout 20 Display Property 20 Block 20
Inline 21 Inline-block 21 None 21 Positioning 21 Static 21 Relative 21 Absolute 22 Fixed 22 Sticky 22 Flexbox 23 Basic Flexbox Example 23 Common Flexbox Properties 24 CSS Grid 24 Basic Grid Example 25 Advanced Grid Features 26 Example: Creating a Responsive Layout with Flexbox and Grid 27
7. Responsive Design 29 Media Queries 29 Fluid Layouts and Units 30 Relative Units 30 Flexible Grid Systems 31
Mobile-First Approach 31 Example: Responsive Image Gallery 32
8. CSS Transitions and Animations 33 Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
3
Transitions 33 Transition Shorthand 34 Animations 34 Advanced Animation Example 35 Example: Hover Animation 35
9. CSS Variables (Custom Properties) 36 Defining and Using CSS Variables 36 Fallback Values 37 Dynamic Themes with CSS Variables 37 10. Advanced Selectors and Specificity 39 Specificity 39 Advanced Selectors 40 Universal Selector 40
Child Combinator 40 Adjacent Sibling Selector 41 General Sibling Selector 41 Attribute Selectors 41 Pseudo-classes and Pseudo-elements 41 Descendant Selector 42 Combining Selectors 42
11. CSS Best Practices 42 1. Keep CSS Organized 42 2. Avoid Over-Specificity 43 3. Use Comments 43 4. Leverage CSS Variables 43 5. Minimize Use of !important 43 6. Optimize for Performance 44 7. Responsive Design 44 8. Accessibility 44 Example: Organized and Maintainable CSS 44
12. Projects and Exercises 46 Project 1: Personal Portfolio Website 46 Exercise 1: Creating and Styling a Navigation Bar 48 Exercise 2: Building a Responsive Grid Layout 50 Exercise 3: Implementing a Hover Effect with Transition 51 13. Multiple Choice Questions 52 Question 1 52 Question 2 53 Question 3 53 Question 4 54
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
4
Question 5 54 Question 6 55 Question 7 55 Question 8 56 Question 9 56 Question 10 56 Question 11 57 Question 12 57 Question 13 58 Question 14 58 Question 15 59 14. Conclusion 59
1. Introduction to CSS
What is CSS?
CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of a document written in HTML or XML. CSS defines how elements should be displayed on screen, on paper, in speech, or on other media.
Why Use CSS?
β Separation of Concerns: Separates content (HTML) from presentation (CSS). β Reusability: Apply the same styles to multiple elements.
β Maintainability: Easier to update and manage styles across large websites. β Enhanced Design: Create visually appealing and responsive layouts.
Basic Example
<!DOCTYPE html> <html>
<head>
<title>CSS Example</title> <style>
body {
background-color: #f0f0f0;
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
5
}
h1 {
color: blue;
text-align: center; }
</style> </head> <body>
<h1>Welcome to CSS!</h1> </body>
</html>
Explanation:
β The <style>tag contains CSS rules.
β bodyselector sets the background color. β h1selector styles the heading text.
2. CSS Syntax and Selectors
Understanding CSS syntax and selectors is fundamental to effectively styling web pages.
Basic Syntax
A CSS rule consists of a selector and a declaration block.
selector {
property: value; property: value;
}
Example:
p {
color: green; font-size: 16px;
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
6
}
Explanation:
β pis the selector targeting all <p>elements. β colorand font-sizeare properties.
β greenand 16pxare values assigned to the properties.
Basic Selectors
Element Selector
Targets all instances of a specific HTML element.
/* Targets all <h2> elements */ h2 {
color: purple; }
Class Selector
Targets elements with a specific class attribute. Prefixed with a dot (.).
/* Targets elements with class="highlight" */ .highlight {
background-color: yellow; }
ID Selector
Targets a unique element with a specific ID attribute. Prefixed with a hash (#).
/* Targets the element with id="main-header" */ #main-header {
font-weight: bold; }
Note: IDs should be unique within an HTML document, whereas classes can be reused.
Grouping Selectors
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
7
Apply the same styles to multiple selectors by separating them with commas.
/* Targets both <h1> and <h2> elements */ h1, h2 {
font-family: Arial, sans-serif; }
Combinators
Define relationships between selectors.
Descendant Selector
Targets elements that are descendants of a specified ancestor.
/* Targets all <span> inside <div> */ div span {
color: red; }
Child Selector
Targets direct children of a specified parent.
/* Targets only direct <li> children of <ul> */ ul > li {
list-style-type: square; }
Adjacent Sibling Selector
Targets elements that are immediately preceded by a specified element.
/* Targets <p> that directly follows <h3> */ h3 + p {
margin-top: 0; }
General Sibling Selector
Targets all siblings after a specified element.
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
8
/* Targets all <p> siblings after <h3> */ h3 ~ p {
color: gray; }
Pseudo-classes and Pseudo-elements
Pseudo-classes
Target elements in a specific state.
/* Targets links when hovered */ a:hover {
text-decoration: underline; }
/* Targets the first child of a parent */ li:first-child {
font-weight: bold; }
Pseudo-elements
Target specific parts of an element.
/* Targets the first letter of a paragraph */ p::first-letter {
font-size: 200%;
color: }
/* Inserts
blue;
content before an element */
h2::before { content: "β
"; color: gold;
}
Attribute Selectors
Select elements based on their attributes.
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
9
/* Targets <input> elements with type="text" */ input[type="text"] {
border: 1px solid #ccc; }
/* Targets <a> elements with href containing "example" */ a[href*="example"] {
color: green; }
3. CSS Box Model
The CSS Box Model is a fundamental concept that defines how elements are structured and displayed on a web page. Understanding the box model is crucial for precise layout and design.
Understanding the Box Model
Every element on a web page is considered a rectangular box, consisting of four areas:
1. Content: The actual content of the box, such as text or images. 2. Padding: Clears an area around the content. It's transparent. 3. Border: A border surrounding the padding and content.
4. Margin: Clears an area outside the border. It's also transparent.
Visual Representation:
+---------------------------+ | Margin | | +---------------------+ | | | Border | | | | +---------------+ | | | | | Padding | | | | | | +---------+ | | | | | | | Content | | | | | | | +---------+ | | | | | | | | | | | +---------------+ | |
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
10
| | | | | +---------------------+ | | | +---------------------------+
Box Sizing
The box-sizingproperty alters the default CSS box model used to calculate widths and heights of elements.
Content-box (Default)
The widthand heightproperties include only the content. Padding and border are added outside.
div {
width: 200px; padding: 20px;
border: 5px solid black; box-sizing: content-box;
}
Total Width: 200px (content) + 40px (padding) + 10px (border) = 250px
Border-box
The widthand heightinclude content, padding, and border.
div {
width: 200px; padding: 20px;
border: 5px solid black; box-sizing: border-box;
}
Total Width: 200px (includes content, padding, and border)
Advantages:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
11
β Easier to manage element sizes.
β Prevents elements from unexpectedly increasing in size due to padding or borders.
Example: Box Model in Action
<!DOCTYPE html> <html>
<head>
<title>Box Model Example</title> <style>
.box {
width: 200px; padding: 20px;
border: 5px solid black; margin: 10px;
background-color: #e0e0e0;
box-sizing: border-box; /* Change to content-box to see the difference */
} </style>
</head> <body>
<div class="box"> This is a box.
</div> </body> </html>
Explanation:
β The .boxelement has a set width, padding, border, and margin.
β box-sizing: border-box;ensures the total width remains 200px.
β Changing to content-boxwould make the total width 200px + 40px + 10px = 250px.
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
12
4. CSS Colors and Backgrounds
Colors and backgrounds are essential for creating visually appealing websites. CSS provides various ways to specify colors and manage backgrounds.
Color Values
CSS allows you to specify colors using different formats:
Named Colors: Predefined color names.
p {
color: navy; }
Hexadecimal Notation: #RRGGBBor shorthand #RGB.
h1 {
color: #ff5733; }
RGB: rgb(red, green, blue)with values from 0 to 255.
div {
background-color: rgb(255, 0, 0); /* Red */ }
RGBA: rgba(red, green, blue, alpha)where alpha is opacity (0 to 1).
div {
background-color: rgba(0, 0, 255, 0.5); /* Semi-transparent blue */
}
HSL: hsl(hue, saturation%, lightness%).
span {
color: hsl(120, 100%, 50%); /* Green */ }
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
13
HSLA: hsla(hue, saturation%, lightness%, alpha).
span {
color: hsla(240, 100%, 50%, 0.3); /* Semi-transparent blue */
}
Background Properties
CSS provides a range of properties to control backgrounds.
Background Color
Sets the background color of an element.
body {
background-color: #f0f8ff; /* AliceBlue */ }
Background Image
Sets a background image for an element.
div {
background-image: url('images/background.jpg'); }
Background Repeat
Controls how the background image repeats.
div {
background-repeat: no-repeat; /* Options: repeat, repeat-x, repeat-y, no-repeat */
}
Background Position
Sets the initial position of the background image.
div {
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
14
background-position: center center; /* Options: top, bottom, left, right, center, or specific coordinates */
}
Background Size
Specifies the size of the background image.
div {
background-size: cover; /* Options: auto, cover, contain, or specific dimensions */
}
Shorthand Background Property
Combines multiple background properties into one.
div {
background: url('images/bg.png') no-repeat center center / cover #ffffff;
}
Explanation:
β url('images/bg.png')sets the background image. β no-repeatprevents repetition.
β center centerpositions the image at the center.
β / coversets the background size to cover the element. β #ffffffsets the background color to white.
Example: Styling a Header with Background
<!DOCTYPE html> <html>
<head>
<title>Background Example</title> <style>
.header {
height: 200px;
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
15
background-image: url('header-bg.jpg'); background-size: cover;
background-position: center; background-repeat: no-repeat; color: white;
display: flex;
align-items: center; justify-content: center; font-size: 2em;
} </style>
</head> <body>
<div class="header"> Welcome to My Website
</div> </body> </html>
Explanation:
β The .headerdiv has a background image that covers the entire area. β Flexbox centers the text both vertically and horizontally.
β Text color is set to white for contrast.
5. CSS Typography
Typography plays a crucial role in web design, affecting readability and user experience. CSS provides extensive control over text styling.
Font Properties
font-family
Specifies the typeface to be used for text.
body {
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
16
font-family: "Helvetica Neue", Arial, sans-serif; }
Explanation:
β Lists fonts in order of preference.
β If the first font isn't available, the browser tries the next one.
font-size
Sets the size of the font.
h1 {
font-size: 2em; /* Relative to the parent element's font size */
}
p {
font-size: 16px; /* Absolute size */ }
font-weight
Sets the weight (boldness) of the font.
strong {
font-weight: bold; }
.light-text {
font-weight: 300; }
font-style
Sets the style of the font (e.g., italic).
em {
font-style: italic; }
line-height
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
17
Sets the height between lines of text.
p {
line-height: 1.5; }
Text Properties
color
Sets the color of the text.
h2 {
color: darkgreen; }
text-align
Sets the horizontal alignment of text.
p {
text-align: justify; }
text-decoration
Adds decorations to text, such as underline.
a {
text-decoration: none; }
text-transform
Controls the capitalization of text.
.uppercase {
text-transform: uppercase; }
letter-spacing and word-spacing
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
18
Adjusts spacing between letters and words.
h3 {
letter-spacing: 2px; }
p {
word-spacing: 4px; }
Example: Styling Text
<!DOCTYPE html> <html>
<head>
<title>Typography Example</title> <style>
body {
font-family:
line-height:
"Georgia", serif;
1.6;
color: #333; }
h1 {
font-size: 3em; text-align: center;
text-transform: uppercase; color: #2c3e50;
}
p {
font-size: 1.2em; text-align: justify;
}
a {
color: #2980b9;
text-decoration: none; }
a:hover {
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
19
text-decoration: underline; }
</style> </head> <body>
<h1>Welcome to My Blog</h1> <p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. <a href="#">Read more</a> about our latest updates and
features. </p>
</body> </html>
Explanation:
β The body uses a serif font with a comfortable line height. β The <h1>is large, centered, and uppercase.
β Paragraphs are slightly larger and justified.
β Links are styled with a distinct color and underline on hover.
6. CSS Layout
CSS provides powerful tools to create complex and responsive layouts. This section covers various layout techniques.
Display Property
The displayproperty defines how an element is rendered on the page.
Block
Elements occupy the full width available and start on a new line.
div {
display: block; }
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
20
Inline
Elements occupy only the width necessary and do not start on a new line.
span {
display: inline; }
Inline-block
Elements behave like inline elements but can have set widths and heights.
img {
display: inline-block; width: 100px;
height: 100px; }
None
Elements are not displayed on the page.
.hidden {
display: none; }
Positioning
The positionproperty controls how an element is positioned in the document.
Static
Default positioning. Elements follow the normal flow of the page.
p {
position: static; }
Relative
Positions the element relative to its normal position.
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
21
.box {
position: relative;
top: 10px; /* Moves the element 10px down */
left: 20px; /* Moves the element 20px to the right */ }
Absolute
Positions the element relative to its first positioned ancestor.
.container {
position: relative; }
.box {
position: absolute; top: 50px;
right: 30px; }
Explanation:
β .boxis positioned 50px from the top and 30px from the right of .container. β If .containerhas position: relative;, .boxis positioned within
.container.
Fixed
Positions the element relative to the viewport, remaining in the same place even when scrolling.
.navbar {
position: fixed; top: 0;
width: 100%; }
Sticky
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
22
Positions the element based on the user's scroll position. It toggles between relative and fixed.
.header {
position: sticky; top: 0;
background-color: white; }
Explanation:
β .headersticks to the top of the viewport when scrolled to its position.
Flexbox
Flexbox is a one-dimensional layout method for arranging items in rows or columns.
Basic Flexbox Example <!DOCTYPE html> <html>
<head>
<title>Flexbox <style>
.container
Example</title>
{
display: flex;
justify-content: space-between; /* Align items horizontally */
align-items: center; /* Align items vertically */ height: 100vh;
padding: 20px;
background-color: #f8f8f8; }
.box {
width: 100px; height: 100px;
background-color: #3498db; color: white;
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
23
display: flex;
align-items: center; justify-content: center; border-radius: 5px;
} </style>
</head> <body>
<div class="container">
<div <div <div
</div> </body>
</html>
class="box">1</div> class="box">2</div>
class="box">3</div>
Explanation:
β .containeruses display: flex;to create a flex container.
β justify-content: space-between;distributes space between items. β align-items: center;centers items vertically.
β .boxelements are flex items, centered both horizontally and vertically.
Common Flexbox Properties
β flex-direction: Defines the direction of the flex items (row, column, row-reverse, column-reverse).
β justify-content: Aligns items along the main axis (flex-start, flex-end, center, space-between, space-around).
β align-items: Aligns items along the cross axis (flex-start, flex-end, center, stretch).
β flex-wrap: Controls whether flex items should wrap onto multiple lines (nowrap, wrap, wrap-reverse).
CSS Grid
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
24
CSS Grid is a two-dimensional layout system for creating complex and responsive grid-based layouts.
Basic Grid Example <!DOCTYPE html> <html>
<head>
<title>Grid Example</title> <style>
.grid-container { display: grid;
grid-template-columns: repeat(3, 1fr); grid-gap: 10px;
padding: 10px; }
.grid-item {
background-color: #2ecc71; color: white;
padding: 20px;
text-align: center; border-radius: 5px;
} </style>
</head> <body>
<div class="grid-container">
<div <div <div <div <div <div
</div> </body>
</html>
class="grid-item">1</div> class="grid-item">2</div> class="grid-item">3</div> class="grid-item">4</div> class="grid-item">5</div>
class="grid-item">6</div>
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
25
Explanation:
β .grid-containeruses display: grid;to establish a grid layout.
β grid-template-columns: repeat(3, 1fr);creates three equal-width columns.
β grid-gap: 10px;sets the spacing between grid items. β .grid-itemstyles individual grid cells.
Advanced Grid Features Grid Areas:
.grid-container { display: grid;
grid-template-areas: "header header" "sidebar content"
}
.header
"footer footer";
{
grid-area: header; }
.sidebar {
grid-area: sidebar; }
.content {
grid-area: content; }
.footer {
grid-area: footer; }
Responsive Grid:
@media (max-width: 600px) { .grid-container {
grid-template-columns: 1fr; grid-template-areas:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
26
"header" "content" "sidebar" "footer";
} }
Explanation:
β grid-template-areasdefines named grid areas for easier placement of elements.
β Media queries adjust the grid layout for different screen sizes, ensuring responsiveness.
Example: Creating a Responsive Layout with Flexbox and Grid
<!DOCTYPE html> <html>
<head>
<title>Responsive Layout</title> <style>
body {
margin: 0;
font-family: Arial, sans-serif; }
.navbar {
background-color: #34495e; color: white;
padding: 15px;
text-align: center; }
.container { display: grid;
grid-template-columns: 1fr 3fr; grid-gap: 20px;
padding: 20px;
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
27
}
.sidebar {
background-color: #ecf0f1; padding: 15px;
}
.content {
background-color: #bdc3c7; padding: 15px;
}
.footer {
background-color: #34495e; color: white;
text-align: center; padding: 10px;
grid-column: 1 / -1; }
@media (max-width: 800px) { .container {
grid-template-columns: 1fr; }
} </style>
</head> <body>
<div class="navbar"> My Website
</div>
<div class="container"> <div class="sidebar">
<h3>Sidebar</h3>
<p>Links and information here.</p> </div>
<div class="content"> <h2>Main Content</h2>
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
28
<p>Welcome to the main content area.</p> </div>
</div>
<div class="footer"> Β© 2024 My Website
</div> </body> </html>
Explanation:
β Navbar and Footer: Use full-width grid areas.
β Container: Uses CSS Grid to layout the sidebar and content.
β Responsive Design: At screen widths below 800px, the layout stacks vertically.
7. Responsive Design
Responsive design ensures that web pages look and function well on all devices, from desktops to smartphones. CSS offers several tools to achieve responsiveness.
Media Queries
Media queries apply CSS rules based on device characteristics like screen width, height, orientation, and resolution.
Syntax:
@media (condition) { /* CSS rules */
}
Example:
/* Styles for screens wider than 600px */ @media (min-width: 600px) {
.container { display: flex;
}
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
29
}
/* Styles for screens 600px or narrower */ @media (max-width: 600px) {
.container { display: block;
} }
Common Breakpoints:
β Mobile: max-width: 600px
β Tablet: min-width: 601pxand max-width: 1024px β Desktop: min-width: 1025px
Fluid Layouts and Units
Using relative units makes layouts more adaptable to different screen sizes.
Relative Units
% (Percentage): Relative to the parent element.
.box {
width: 50%; /* 50% of the parentβs width */ }
em: Relative to the font-size of the element.
p {
font-size: 1.2em; /* 1.2 times the parentβs font-size */ }
rem: Relative to the root (html) font-size.
h1 {
font-size: 2rem; /* 2 times the root font-size */ }
vw and vh: Relative to the viewportβs width and height.
.banner {
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
30
width: 100vw; /* 100% of the viewport width */ height: 50vh; /* 50% of the viewport height */
}
Flexible Grid Systems
Using percentages and relative units to create flexible grid layouts that adjust to screen sizes.
Example:
.container { display: flex; flex-wrap: wrap;
}
.column {
flex: 1 1 300px; /* Grow, shrink, basis */ margin: 10px;
}
Explanation:
β .columnelements will flexibly adjust their width, wrapping to new lines as needed based on available space.
Mobile-First Approach
Designing for mobile devices first and then enhancing for larger screens.
Example:
/* Mobile styles */ .container {
display: block; }
/* Enhancements for larger screens */ @media (min-width: 600px) {
.container { display: flex;
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
31
} }
Advantages:
β Prioritizes essential content and performance for mobile users. β Easier to add enhancements for larger screens.
Example: Responsive Image Gallery
<!DOCTYPE html> <html>
<head>
<title>Responsive Gallery</title> <style>
.gallery { display: grid;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
grid-gap: 10px; padding: 10px;
}
.gallery img { width: 100%; height: auto;
border-radius: 5px; }
</style> </head> <body>
<div class="gallery">
<img src="images/photo1.jpg" <img src="images/photo2.jpg"
<img src="images/photo3.jpg"
alt="Photo 1"> alt="Photo 2">
alt="Photo 3">
<!-- Add more images as needed --> </div>
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
32
</body> </html>
Explanation:
β Grid Layout: Uses auto-filland minmaxto create a responsive grid that adjusts the number of columns based on available space.
β Images: Set to width: 100%to fill their grid cells and maintain aspect ratio with height: auto.
8. CSS Transitions and Animations
Enhance user experience by adding dynamic visual effects using CSS transitions and animations.
Transitions
Transitions allow you to change property values smoothly over a specified duration.
Syntax:
selector {
transition: property duration timing-function delay; }
Example:
.button {
background-color: #3498db;
transition: background-color 0.3s ease; }
.button:hover {
background-color: #2980b9; }
Explanation:
β The .buttonchanges its background color smoothly over 0.3 seconds when hovered.
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
33
β easedefines the transition timing function.
Transition Shorthand
transition: background-color 0.3s ease 0s;
Animations
Animations allow more complex and multi-step transitions.
Syntax:
selector {
animation: name duration timing-function delay iteration-count direction fill-mode;
}
@keyframes Rule:
Defines the animation sequence.
Example:
/* Define the animation */ @keyframes fadeIn {
from { opacity: 0; } to { opacity: 1; }
}
.box {
width: 100px; height: 100px;
background-color: #e74c3c; animation: fadeIn 2s ease-in-out;
}
Explanation:
β @keyframes fadeIndefines an animation that changes opacity from 0to 1. β .boxapplies the fadeInanimation over 2 seconds with an ease-in-out
timing function.
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
34
Advanced Animation Example @keyframes moveRight {
0% { transform: translateX(0); }
50% { transform: translateX(100px); } 100% { transform: translateX(0); }
}
.moving-box { width: 50px; height: 50px;
background-color: #2ecc71; animation: moveRight 3s infinite;
}
Explanation:
β The .moving-boxmoves 100px to the right and back to the original position repeatedly every 3 seconds.
Example: Hover Animation
<!DOCTYPE html> <html>
<head>
<title>Hover Animation</title> <style>
.card {
width: 200px; height: 150px;
background-color: #ecf0f1; border-radius: 10px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1); transition: transform 0.3s ease, box-shadow 0.3s
ease;
display: flex;
align-items: center; justify-content: center;
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
35
cursor: pointer; }
.card:hover {
transform: translateY(-10px);
box-shadow: 0 5px 15px rgba(0,0,0,0.3); }
</style> </head> <body>
<div class="card"> Hover Me!
</div> </body> </html>
Explanation:
β The .cardmoves up by 10px and its shadow becomes more prominent when hovered, creating a lifting effect.
9. CSS Variables (Custom Properties)
CSS Variables, also known as Custom Properties, allow you to store reusable values directly in your CSS. They enhance maintainability and make it easier to implement themes.
Defining and Using CSS Variables
Syntax:
:root {
--primary-color: #3498db;
--secondary-color: #2ecc71;
--font-stack: 'Helvetica Neue', Arial, sans-serif; }
.button {
background-color: var(--primary-color);
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
36
color: white;
font-family: var(--font-stack); }
.button-secondary {
background-color: var(--secondary-color); color: white;
font-family: var(--font-stack); }
Explanation:
β :rootis a pseudo-class that matches the document's root element (<html>). Variables defined here are globally accessible.
β --primary-color, --secondary-color, and --font-stackare custom properties.
β var(--property-name)is used to retrieve the value of a custom property.
Fallback Values
Provide fallback values in case the variable is not defined.
p {
color: var(--text-color, #333); }
Explanation:
β If --text-coloris not defined, the color defaults to #333.
Dynamic Themes with CSS Variables
Changing variables at runtime allows for dynamic theming.
Example:
<!DOCTYPE html> <html>
<head>
<title>CSS Variables Theme</title>
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
37
<style> :root {
--bg-color: white;
--text-color: --link-color:
}
black;
#3498db;
body {
background-color: var(--bg-color); color: var(--text-color);
font-family: Arial, sans-serif; }
a {
color: var(--link-color); }
.dark-theme {
--bg-color: #2c3e50;
--text-color: --link-color:
}
#ecf0f1;
#e74c3c;
</style> </head> <body>
<h1>Welcome!</h1>
<p>This is an example of CSS variables.</p> <a href="#">Learn more</a>
<br><br>
<button onclick="toggleTheme()">Toggle Dark Theme</button> <script>
function toggleTheme() { document.body.classList.toggle('dark-theme');
} </script>
</body> </html>
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
38
Explanation:
β CSS variables define default theme colors.
β The .dark-themeclass overrides these variables.
β Clicking the button toggles the .dark-themeclass, switching between light and dark themes.
10. Advanced Selectors and Specificity
Mastering selectors and understanding specificity is key to writing effective and conflict-free CSS.
Specificity
Specificity determines which CSS rule is applied when multiple rules target the same element.
Specificity Hierarchy:
1. Inline styles: Highest specificity (e.g., style="color: red;"). 2. IDs: High specificity (e.g., #header).
3. Classes, attributes, pseudo-classes: Medium specificity (e.g., .button, [type="text"], :hover).
4. Elements and pseudo-elements: Low specificity (e.g., div, p, ::before).
Calculating Specificity:
β Inline styles: 1000 β IDs: 100 per ID
β Classes, attributes, pseudo-classes: 10 per item β Elements and pseudo-elements: 1 per item
Example:
/* Specificity score: 10 */ .button {
color: blue; }
/* Specificity score: 100 */
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
39
#submit-button { color: green;
}
/* Specificity score: 11 */ button[type="submit"] {
color: red; }
/* Specificity score: 1 */ button {
color: black; }
Explanation:
β An element with id="submit-button"will have its color set to green, overriding other rules.
β If an element has both a class and an element selector, the class selector takes precedence.
Advanced Selectors
Universal Selector
Targets all elements.
* {
box-sizing: border-box; }
Explanation:
β Applies box-sizing: border-box;to all elements, simplifying layout calculations.
Child Combinator
Targets direct children of an element.
ul > li {
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
40
list-style-type: disc; }
Adjacent Sibling Selector
Targets an element that is immediately preceded by another.
h2 + p {
margin-top: 0; }
General Sibling Selector
Targets all siblings after a specified element.
h2 ~ p {
color: gray; }
Attribute Selectors
Select elements based on attribute values.
input[type="email"] { border: 2px solid blue;
}
Pseudo-classes and Pseudo-elements
Pseudo-classes: Target elements in a specific state.
a:hover { color: red;
}
Pseudo-elements: Target specific parts of an element.
p::first-line {
font-weight: bold; }
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
41
Descendant Selector
Targets elements nested within other elements.
div p {
margin-bottom: 10px; }
Explanation:
β Applies to all <p>elements inside <div>elements, regardless of depth.
Combining Selectors
Combine multiple selectors to target specific elements.
/* Targets <a> elements inside <nav> with class "active" */ nav a.active {
color: green; }
Explanation:
β Applies styles to <a>elements that have the class activeand are inside a <nav>element.
11. CSS Best Practices
Adhering to best practices ensures your CSS is efficient, maintainable, and scalable.
1. Keep CSS Organized
β Modular Approach: Split CSS into multiple files or sections based on functionality (e.g., layout, typography, components).
Consistent Naming Conventions: Use naming conventions like BEM (Block, Element, Modifier) for clarity.
/* BEM Naming Convention */ .button {
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
42
/* Block */ }
.button__icon { /* Element */
}
.button--primary { /* Modifier */
}
2. Avoid Over-Specificity
β Use selectors with appropriate specificity to prevent conflicts and make overrides easier.
β Prefer classes over IDs for styling.
3. Use Comments
Add comments to explain complex sections or to separate different parts of your CSS.
/* Header Styles */ .header {
background-color: #34495e; }
4. Leverage CSS Variables
Use custom properties to manage colors, fonts, and other reusable values.
:root {
--main-color: #3498db; }
.button {
background-color: var(--main-color); }
5. Minimize Use of !important
β Avoid using !importantas it makes debugging and maintenance harder.
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
43
β Instead, increase selector specificity or restructure your CSS.
6. Optimize for Performance
β Minimize the use of complex selectors.
β Combine and minify CSS files for faster loading. β Use shorthand properties where possible.
7. Responsive Design
β Design with multiple devices in mind.
β Use media queries and relative units to ensure adaptability.
8. Accessibility
β Ensure sufficient color contrast.
β Use relative units for text to support user preferences. β Avoid relying solely on color to convey information.
Example: Organized and Maintainable CSS
/* Variables */ :root {
--primary-color: #3498db;
--secondary-color: #2ecc71;
--font-family: 'Arial, sans-serif'; }
/* Reset Styles */ * {
margin: 0; padding: 0;
box-sizing: border-box; }
/* Typography */ body {
font-family: var(--font-family); color: #333;
}
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
44
h1, h2, h3 {
color: var(--primary-color); }
p {
line-height: 1.6; }
/* Layout */ .container {
width: 90%;
max-width: 1200px; margin: auto; padding: 20px;
}
/* Components */ .button {
background-color: var(--primary-color); color: white;
padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer;
transition: background-color 0.3s ease; }
.button:hover {
background-color: var(--secondary-color); }
Explanation:
β Variables: Centralized color and font definitions.
β Reset Styles: Removes default browser margins and paddings. β Typography: Consistent styling for text elements.
β Layout: Defines a responsive container.
β Components: Reusable .buttonclass with hover effects.
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
45
12. Projects and Exercises
Applying what you've learned through projects and exercises is essential for mastering CSS. Below are several hands-on activities to reinforce your understanding.
Project 1: Personal Portfolio Website
Objective: Create a personal portfolio website to showcase your projects, skills, and contact information.
Features:
β Home Section: Introduction and profile picture. β About Section: Information about yourself.
β Projects Section: Gallery of projects with descriptions. β Skills Section: List of skills with proficiency indicators. β Contact Section: Contact form and social media links.
β Responsive Design: Ensure the site looks good on all devices.
Solution Outline:
1. HTML Structure:
β Create separate sections using semantic HTML elements (<header>, <section>, <footer>).
2. CSS Styling:
β Use Flexbox or Grid for layout.
β Apply consistent typography and color scheme. β Add hover effects to project cards.
3. Responsive Design:
β Implement media queries for different screen sizes. 4. Enhancements:
β Add smooth scrolling between sections. β Use CSS animations for visual appeal.
Sample Code Snippet: Header and Navigation
<!DOCTYPE html> <html>
<head>
<title>My Portfolio</title>
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
46
<link rel="stylesheet" href="styles.css"> </head>
<body>
<header class="navbar"> <div class="container">
<h1>My Portfolio</h1> <nav>
<ul> <li><a <li><a <li><a <li><a <li><a
</ul> </nav>
</div>
</header>
href="#home">Home</a></li> href="#about">About</a></li> href="#projects">Projects</a></li> href="#skills">Skills</a></li>
href="#contact">Contact</a></li>
<!-- Additional sections go here --> </body>
</html>
/* styles.css */ :root {
--primary-color: #3498db;
--secondary-color: #2ecc71;
--font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
font-family: var(--font-family); margin: 0;
padding: 0; color: #333;
}
.navbar {
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
47
background-color: var(--primary-color); padding: 20px 0;
}
.navbar .container { display: flex;
justify-content: space-between; align-items: center;
}
.navbar h1 { color: white; margin: 0;
}
.navbar nav ul {
list-style: none; display: flex;
}
.navbar nav ul li { margin-left: 20px;
}
.navbar nav ul li a { color: white;
text-decoration: none; font-size: 1em;
transition: color 0.3s ease; }
.navbar nav ul li a:hover {
color: var(--secondary-color); }
Exercise 1: Creating and Styling a Navigation Bar
Task: Create a horizontal navigation bar with links to "Home", "About", "Services", and "Contact". Style it with a background color, and change the link color on hover.
Solution:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
48
<!DOCTYPE html> <html>
<head>
<title>Navigation Bar</title> <style>
body {
margin: 0; }
.navbar {
background-color: #34495e; padding: 15px;
}
.navbar ul {
list-style: none; display: flex;
justify-content: center; margin: 0;
padding: 0; }
.navbar li { margin: 0 15px;
}
.navbar a { color: white;
text-decoration: none; font-size: 1.1em;
transition: color 0.3s ease; }
.navbar a:hover { color: #e74c3c;
} </style>
</head> <body>
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
49
<nav class="navbar">
<ul> <li><a <li><a <li><a <li><a
</ul> </nav>
</body>
</html>
href="#home">Home</a></li> href="#about">About</a></li> href="#services">Services</a></li>
href="#contact">Contact</a></li>
Explanation:
β .navbaris styled with a dark background and padding. β .navbar uluses Flexbox to center the navigation links. β Links change color smoothly on hover.
Exercise 2: Building a Responsive Grid Layout
Task: Create a responsive grid layout that displays three columns on desktop screens and one column on mobile screens. Each grid item should have a background color and padding.
Solution:
<!DOCTYPE html> <html>
<head>
<title>Responsive Grid</title> <style>
.grid-container { display: grid;
grid-template-columns: repeat(3, 1fr); grid-gap: 20px;
padding: 20px; }
.grid-item {
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
50
background-color: #ecf0f1; padding: 30px;
text-align: center; border-radius: 5px;
}
@media (max-width: 600px) { .grid-container {
grid-template-columns: 1fr; }
} </style>
</head> <body>
<div class="grid-container">
<div <div <div
</div> </body>
</html>
class="grid-item">Item class="grid-item">Item
class="grid-item">Item
1</div> 2</div>
3</div>
Explanation:
β .grid-containeruses CSS Grid to create three equal columns with gaps. β .grid-itemelements are styled with background color and padding.
β Media query changes the layout to a single column on screens narrower than 600px.
Exercise 3: Implementing a Hover Effect with Transition
Task: Create a square div that changes its background color and scales up slightly when hovered, using CSS transitions.
Solution:
<!DOCTYPE html> <html>
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
51
<head>
<title>Hover Effect</title> <style>
.square {
width: 150px; height: 150px;
background-color: #2ecc71; margin: 50px auto;
transition: background-color 0.3s ease, transform 0.3s ease;
border-radius: 10px; }
.square:hover {
background-color: #27ae60; transform: scale(1.1);
} </style>
</head> <body>
<div class="square"></div> </body>
</html>
Explanation:
β .squareis a green square with smooth transitions.
β On hover, the background color darkens, and the square scales up by 10%.
13. Multiple Choice Questions
Test your understanding of CSS with the following multiple-choice questions. Answers and explanations are provided after each question.
Question 1
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
52
Which of the following is the correct way to apply a class named "active" to a <div>element in CSS?
A) div.active { /* styles */ }
B) div .active { /* styles */ }
C) div #active { /* styles */ }
D) .div.active { /* styles */ }
Answer: A) div.active { /* styles */ }
Explanation:
β div.activetargets <div>elements with the class active.
β Option B (div .active) targets elements with class activeinside a <div>. β Option C uses ID selector instead of class.
β Option D incorrectly uses a dot before div.
Question 2
What does the flex-direction: column;property do in Flexbox?
A) Aligns items horizontally.
B) Aligns items vertically.
C) Reverses the order of items.
D) Wraps items onto multiple lines.
Answer: B) Aligns items vertically.
Explanation:
β flex-direction: column;stacks flex items vertically from top to bottom.
Question 3
Which property is used to change the text color of an element?
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
53
A) background-color
B) font-color
C) color
D) text-color
Answer: C) color
Explanation:
β The colorproperty sets the color of the text.
Question 4
How can you make a text bold in CSS?
A) font-style: bold;
B) text-weight: bold;
C) font-weight: bold;
D) text-style: bold;
Answer: C) font-weight: bold;
Explanation:
β The font-weightproperty controls the boldness of the text.
Question 5
Which CSS property controls the space between lines of text?
A) letter-spacing
B) line-height
C) text-spacing
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
54
D) word-spacing
Answer: B) line-height
Explanation:
β The line-heightproperty sets the height between lines of text.
Question 6
What does the box-sizing: border-box;property do?
A) Includes padding and border in the element's total width and height.
B) Excludes padding and border from the element's total width and height.
C) Sets the box to have a fixed size.
D) Makes the box responsive to content size.
Answer: A) Includes padding and border in the element's total width and height.
Explanation:
β box-sizing: border-box;ensures that widthand heightinclude content, padding, and border.
Question 7
Which of the following is NOT a valid CSS color format?
A) rgb(255, 0, 0)
B) #FF0000
C) hsl(0, 100%, 50%)
D) rgba(255, 0, 0, 256)
Answer: D) rgba(255, 0, 0, 256)
Explanation:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
55
β The alpha value in rgbashould be between 0and 1. 256is invalid.
Question 8
Which CSS property is used to create space between the border and the content of an element?
A) margin
B) padding
C) border-spacing
D) gap
Answer: B) padding
Explanation:
β paddingcreates space inside the element between the border and content.
Question 9
How do you select all <p>elements that are direct children of a <div>?
A) div p { /* styles */ }
B) div > p { /* styles */ }
C) div + p { /* styles */ }
D) div ~ p { /* styles */ }
Answer: B) div > p { /* styles */ }
Explanation:
β The >combinator targets direct children only.
Question 10
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
56
Which property is used to make an element stay fixed in place even when the page is scrolled?
A) position:
B) position:
relative;
absolute;
C) position:
D) position:
fixed;
sticky;
Answer: C) position: fixed;
Explanation:
β position: fixed;fixes the element relative to the viewport, maintaining its position during scroll.
Question 11
Which CSS property allows you to control the opacity of an element?
A) visibility
B) opacity
C) display
D) z-index
Answer: B) opacity
Explanation:
β The opacityproperty sets the transparency level of an element.
Question 12
What is the default value of the displayproperty for <span>elements?
A) block
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
57
B) inline
C) inline-block
D) flex
Answer: B) inline
Explanation:
β <span>elements are inline by default.
Question 13
Which CSS property is used to change the font of an element?
A) font-style
B) font-weight
C) font-family
D) font-size
Answer: C) font-family
Explanation:
β font-familyspecifies the typeface for text.
Question 14
How can you center a block-level element horizontally within its container?
A) text-align: center;
B) margin: auto;
C) display: flex; justify-content: center;
D) All of the above
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
58
Answer: D) All of the above
Explanation:
β Option A: text-align: center;centers inline and inline-block elements. β Option B: margin: auto;centers block-level elements with a defined width. β Option C: Using Flexbox to center elements.
Question 15
Which pseudo-class would you use to style an element when it is being hovered over by the mouse?
A) :active
B) :focus
C) :hover
D) :visited
Answer: C) :hover
Explanation:
β :hoverapplies styles when the user hovers over an element.
14. Conclusion
Congratulations! You've completed the comprehensive guide to CSS. This guide has covered everything from the basics of CSS syntax and selectors to advanced topics like Flexbox, Grid, responsive design, transitions, animations, and best practices. By working through the code examples, exercises, and multiple-choice questions, you've built a solid foundation in CSS that will empower you to create stunning and responsive web designs.
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
59
|