|
| 1 | +goals do |
| 2 | + goal "Code some common HTML attributes" |
| 3 | + goal "Use class and id attributes as hooks for CSS styles" |
| 4 | +end |
| 5 | + |
| 6 | +overview do |
| 7 | + |
| 8 | + message <<-MARKDOWN |
| 9 | + |
| 10 | +## Why do we need attributes? |
| 11 | + |
| 12 | +HTML tags first give your browser information about the content they wrap. They can also be extended to include other information about the content, like what type of form input the tag is, or like the `media` attribute, which we saw on the last page can be used to indicate whether the content will be displayed on a screen or printed out. |
| 13 | + |
| 14 | +When creating a form, you'll use attributes to tell the browser what type of input they are, |
| 15 | +and the results will be easy to tell apart. This input: |
| 16 | + |
| 17 | + MARKDOWN |
| 18 | + source_code :html, "<input type='radio' name='rando-radio'>" |
| 19 | + message <<-MARKDOWN |
| 20 | + |
| 21 | +looks like a radio button: <input type='radio' name='rando-radio'>, but |
| 22 | + |
| 23 | + MARKDOWN |
| 24 | + source_code :html, "<input type='password' name='fake-password' value='ick'>" |
| 25 | + message <<-MARKDOWN |
| 26 | + |
| 27 | +looks like a password text input: <input type='password' name='rando-radio' value='ick'> even though they use the same **tag**. |
| 28 | + |
| 29 | +## How do CSS and HTML connect? |
| 30 | + |
| 31 | +Tags and attributes, mostly! |
| 32 | + |
| 33 | +So far we've only applied styles to HTML tags like `<p>` and `<h1>`. But what if we want to apply a style to only a few of the instances of a given tag? We don't want _every_ paragraph to look special, so we can't add our style directly to the `<p>` tag. |
| 34 | + |
| 35 | +This is where `class=` and `id=` attributes come in. Adding a class or ID to an HTML attribute allows you to add CSS styles to just that attribute. |
| 36 | + |
| 37 | + |
| 38 | + MARKDOWN |
| 39 | + |
| 40 | +end |
| 41 | + |
| 42 | +steps do |
| 43 | + |
| 44 | + step do |
| 45 | + message "Let's add some classes and ids to our HTML. Start by adding one or two more paragraphs of text to the bottom of your HTML document. The last lines might look like this:" |
| 46 | + source_code :html, <<HTML |
| 47 | +<h1>Hello <em>World</em>!</h1> |
| 48 | +<p>My name is Rachel.</p> |
| 49 | +<p>I like:</p> |
| 50 | +<ul> |
| 51 | + <li>Polka Dots</li> |
| 52 | + <li>Soccer</li> |
| 53 | + <li>Programming</li> |
| 54 | +</ul> |
| 55 | +<p>I hear RailsBridge needs volunteers, should I volunteer!?!</p> |
| 56 | +HTML |
| 57 | + end |
| 58 | + |
| 59 | + step do |
| 60 | + message "Add the class 'special' to your first paragraph. It'll look something like this:" |
| 61 | + source_code :html, <<HTML |
| 62 | +<h1>Hello <em>World</em>!</h1> |
| 63 | +<p class="special">My name is Rachel.</p> |
| 64 | +<p>I like:</p> |
| 65 | +<ul> |
| 66 | + <li>Polka Dots</li> |
| 67 | + <li>Soccer</li> |
| 68 | + <li>Programming</li> |
| 69 | +</ul> |
| 70 | +<p>I hear RailsBridge needs volunteers, should I volunteer!?!</p> |
| 71 | +HTML |
| 72 | + message "Refresh the page in the browser. You should see any new paragraphs you added, but no styling changes." |
| 73 | + message "Many HTML attributes, like classes and ids, don't directly convey visual information. Your site will look the exact same until we use the class to add CSS styling." |
| 74 | + end |
| 75 | + |
| 76 | + step do |
| 77 | + message "To add a style rule that will apply to a class, use the syntax `.class-name` for your selector. It will be almost the same as the styles that you added to `<h1>` and `<p>`, but with a period at the beginning of your class name." |
| 78 | + message "Try giving the 'special' class a green border. Add this rule inside of your `style` tag:" |
| 79 | + source_code :css, <<CSS |
| 80 | +.special { |
| 81 | + border: 1px solid green; |
| 82 | +} |
| 83 | +CSS |
| 84 | + message "Refresh the page in the browser. You'll see something like this:" |
| 85 | + img src: 'img/css_class.png' |
| 86 | + end |
| 87 | + |
| 88 | + step do |
| 89 | + message "Let's wrap your name in a `span` tag and give that an ID of 'user-name'. It'll look something like this:" |
| 90 | + source_code :html, <<HTML |
| 91 | +<h1>Hello <em>World</em>!</h1> |
| 92 | +<p class="special">My name is |
| 93 | + <span id="user-name">Rachel</span> |
| 94 | +</p> |
| 95 | +<p>I like:</p> |
| 96 | +<ul> |
| 97 | + <li>Polka Dots</li> |
| 98 | + <li>Soccer</li> |
| 99 | + <li>Programming</li> |
| 100 | +</ul> |
| 101 | +<p>I hear RailsBridge needs volunteers, should I volunteer!?!</p> |
| 102 | +HTML |
| 103 | + message "Save and refresh the page in the browser. Again, you shouldn't see any difference." |
| 104 | + end |
| 105 | + |
| 106 | + step do |
| 107 | + message "Now, add the corresponding style rule for your ID, using the syntax `#id-name` for your selector. Try making the 'user-name' id look bold. Add this rule inside of your `style` tag:" |
| 108 | + source_code :css, <<CSS |
| 109 | +#user-name { |
| 110 | + font-weight: bold; |
| 111 | +} |
| 112 | +CSS |
| 113 | + message <<-MARKDOWN |
| 114 | +(Note: The span is just an element that lets you apply a class, id, or other attribute to a string of text without adding any line breaks. Browsers won't give it any styling by default.) |
| 115 | + |
| 116 | +Once you add your style rule and refresh the page in the browser, you'll see something rather ugly like this: |
| 117 | + MARKDOWN |
| 118 | + img src: 'img/css_id.png' |
| 119 | + end |
| 120 | + |
| 121 | +end |
| 122 | + |
| 123 | + |
| 124 | +explanation do |
| 125 | + message <<-MARKDOWN |
| 126 | + |
| 127 | +## When should I use a class, and when should I use an ID? |
| 128 | + |
| 129 | +### Classes |
| 130 | +A **class** attribute should be used to group elements that _could_ exist |
| 131 | +more than once on a given page. For example, you might give certain |
| 132 | +paragraphs a class of `"special"` and use that class to highlight them. |
| 133 | +Even if you only have one paragraph that's special now, you might |
| 134 | +want to add more later. |
| 135 | + |
| 136 | +A class attribute will look like `class="special"` in the HTML and `.special` in the CSS. Don't mix up where the dot goes! |
| 137 | + |
| 138 | +### IDs |
| 139 | +An **id** is a unique label. Use it to identify things you'd only have one |
| 140 | +of. For example, if you ran a news website, you would only have one element |
| 141 | +on a page that would serve as a masthead, and you could give it an id |
| 142 | +like `id="handsome-header"` |
| 143 | + |
| 144 | +An id attribute will look like `id="handsome-header"` in the HTML, and `#handsome-header` in the CSS. |
| 145 | + |
| 146 | +Within these guidelines, when you use classes and IDs is a matter of style / taste. |
| 147 | +MARKDOWN |
| 148 | + |
| 149 | +end |
| 150 | + |
| 151 | +next_step "dev_tools" |
| 152 | + |
0 commit comments