CSS Properties

Now that we’ve gone over adding HTML tags to the page, let’s cover adding styles to those tags. We can do quite a lot with styles! We can change:

  • Typography
  • Colors
  • Appearance (Corners, Borders, Decorations)
  • Layout
  • Position
  • Inline vs Block
  • Animations
  • and many more

CSS styles are always written in property: value pairs (like background: blue;) and terminated with a semicolon.

Applying CSS to an HTML file

CSS can be applied to HTML tags in three different ways.

  1. Inline using an HTML tag’sstyleattribute
    1. <div style="background: blue; color: white;">Hello </div>
  2. Via a <style> tag in the HTML page
  3. Through an external CSS file
    1. <link rel="stylesheet" href="./css-demo-finished.css" />

Targeting specific elements

Inline styles are always applied directly to the element you place them on, but <style> tags and external CSS files need a way to match elements with their respective style sets. This is done with CSS selectors. When selectors are combined with CSS styles, we call this a ruleset.

CSS rulesets take on the following form:

  1. selector1,
  2. selector2 {
  3. property1: value1;
  4. property2: value2;
  5. }

Here’s a more detailed view from Chris Eppstein:
image.png

Selectors can be a single tag, class, ID, or attribute. It can also be a combination of those elements.

Below is a series of selectors and property/value combinations that we’ll apply to our CSS demo page.

  1. /* Targeting the entire page */
  2. body {
  3. font: 1.2em sans-serif;
  4. }
  5. /* Targeting an HTML tag */
  6. h1 {
  7. /* Color name */
  8. color: black;
  9. /* 6-digit hex */
  10. background: #ababab;
  11. /* Margin: specified separately for each side */
  12. margin-bottom: 15px;
  13. margin-top: 15px;
  14. /* Shorthand: Padding applies to all sides */
  15. padding: 10px;
  16. /* Border shorthand and 3-digit hex */
  17. border: 1px solid #ddd;
  18. }
  19. /* Overriding inherited styles */
  20. span {
  21. color: #004578;
  22. }
  23. /* Sibling selector */
  24. a ~ a {
  25. /* Changing elements from inline to block */
  26. display: block;
  27. }
  28. /* Targeting a class name */
  29. .tiles {
  30. display: flex;
  31. }
  32. /* Descendant selector */
  33. .tiles img {
  34. width: 100%;
  35. }
  36. /* Direct descendant selector */
  37. .tiles > div {
  38. /* rgb color */
  39. background: rgb(10, 10, 10);
  40. color: white;
  41. flex-basis: 100%;
  42. /* Padding/margin shorthand. Goes clockwise from top.
  43. 10px - all
  44. 10px 20px - top/bottom left/right
  45. 10px 20px 15px - top left/right bottom
  46. */
  47. padding: 10px 20px 15px;
  48. margin: 10px 20px 10px 0;
  49. border: 1px solid white;
  50. }
  51. /* Qualified selector */
  52. div.important-links {
  53. background: #004578;
  54. }
  55. /* Style inheritance only works for unstyled elements */
  56. a {
  57. color: white;
  58. }
  59. /* Hover pseudo-selector */
  60. a:hover {
  61. color: #ccc;
  62. }
  63. /* Positional pseudo-selector */
  64. .tiles > div:last-child {
  65. /* overrides margin-right but leaves other margins alone */
  66. margin-right: 0;
  67. }
  68. /* ID selector */
  69. #contact-form {
  70. display: flex;
  71. flex-direction: column;
  72. }
  73. /* Attribute selector */
  74. input[type='submit'] {
  75. margin-top: 10px;
  76. }

注:本节 demo 中的 style.css 代码都已屏蔽,可以一步步取消屏蔽观察 CSS 效果。