{"id":387,"date":"2019-02-04T23:45:03","date_gmt":"2019-02-04T23:45:03","guid":{"rendered":"http:\/\/miriamposner.com\/classes\/dh201w19\/?page_id=387"},"modified":"2019-02-06T20:32:09","modified_gmt":"2019-02-06T20:32:09","slug":"css-part-2-divs-classes-and-ids","status":"publish","type":"page","link":"http:\/\/miriamposner.com\/classes\/dh201w19\/tutorials-guides\/web-development\/css-part-2-divs-classes-and-ids\/","title":{"rendered":"CSS Part 2: Divs, classes, and IDs"},"content":{"rendered":"\n<p>We\u2019ve learned how to use CSS styles to format content on our webpage, and the code we\u2019ve used is great for when you want to format&nbsp;<em>all<\/em>&nbsp;paragraphs, headers, or images. But what happens if you want to, say, distinguish among paragraphs on your page? Or break your page up into sections? That\u2019s where divs, classes, and IDs come in handy.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Use IDs to distinguish among elements<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/miriamposner.com\/dh101f15\/wp-content\/uploads\/2015\/10\/media_1445883439712.png\" alt=\"media_1445883439712.png\"\/><\/figure>\n\n\n\n<p>I like the way my paragraphs look, but I want to create a special style for paragraphs I want to highlight. I can do this by adding an&nbsp;<strong>ID tag<\/strong>&nbsp;to those paragraphs. First, after the tag for the paragraph I want to highlight, I type&nbsp;<code>id=\"highlight\"<\/code>. Then I create a style for that ID tag on my stylesheet by typing&nbsp;<code>#highlight<\/code>&nbsp;and entering styling information, just as I would for any element. The hashtag in front of \u201chighlight\u201d tells the browser that it refers to an ID tag.<\/p>\n\n\n\n<p>Notice that when I refresh the page, the first paragraph is treated as before, since I haven\u2019t labeled it \u201chighlight.\u201d The second paragraph, because of its ID tag, looks different.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Divide your page into sections with divs<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/miriamposner.com\/dh101f15\/wp-content\/uploads\/2015\/10\/media_1445883990276.png\" alt=\"media_1445883990276.png\"\/><\/figure>\n\n\n\n<p>The last step works well when you want to style individual elements. But what if you want to create sections on your page?&nbsp;<strong>Div<\/strong>&nbsp;tags come in handy for that. A&nbsp;<strong>div&nbsp;<\/strong>creates a new, invisible box around whatever elements it encloses, allowing you to style these elements as one block.<\/p>\n\n\n\n<p>To label divs, use a&nbsp;<strong>class<\/strong>, which is just a way of naming an element. It\u2019s very similar to an ID tag, except classes refer to elements that appear multiple times per page, while ID tags can be used only once. (In practice, this distinction is pretty blurry, but that\u2019s the idea.)<\/p>\n\n\n\n<p>Create a new&nbsp;<strong>div<\/strong>&nbsp;around part of your page and give it a&nbsp;<strong>class<\/strong>. Then style it on your stylesheet just as you did with the ID tag. The only difference is that instead of placing the hashtag in front of the div\u2019s class, you\u2019ll place a period in front of it, to indicate that it\u2019s a class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Get even crazier with floats<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/miriamposner.com\/dh101f15\/wp-content\/uploads\/2015\/10\/media_1445884244795.png\" alt=\"media_1445884244795.png\"\/><\/figure>\n\n\n\n<p><strong>Floats<\/strong>&nbsp;can be kind of confusing, but the idea is that if you apply a&nbsp;<strong>float<\/strong>&nbsp;to an element, it\u2019ll attempt to move in that direction, pushing all of its neighbors to the side. Try it by floating one of your divs left or right.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Taking precedence<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/miriamposner.com\/dh101f15\/wp-content\/uploads\/2015\/10\/media_1445884580146.png\" alt=\"media_1445884580146.png\"\/><\/figure>\n\n\n\n<p>We\u2019re almost done, but there\u2019s one last thing it\u2019s important for you to know. What happens if you have contradictory styling information on your stylesheet? For example, what if you style your&nbsp;<strong>h1<\/strong>s one way at the top and another way toward the bottom? Try it.<\/p>\n\n\n\n<p>The later style takes precedence.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Your browser can help you out<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/miriamposner.com\/dh101f15\/wp-content\/uploads\/2015\/10\/media_1445884827025.png\" alt=\"media_1445884827025.png\"\/><\/figure>\n\n\n\n<p>Right-click (or hold down&nbsp;<strong>control<\/strong>&nbsp;and click)&nbsp;somewhere on your webpage, and select&nbsp;<strong>Inspect Element<\/strong>. You\u2019ll see something like the image above, which gives you information about how that element is styled. (In this case, I\u2019ve clicked on my dog image.) You can actually style elements directly on the page by clicking on the style information. It won\u2019t stick, though; the page will \u201creset\u201d as soon as you refresh it.<\/p>\n\n\n\n<p>Hey, you\u2019re done! Nice work. If you have extra time, take a look at\u00a0<a href=\"http:\/\/www.htmldog.com\/guides\/css\/intermediate\/\">some of the more advanced ways<\/a>\u00a0you can use CSS (start with \u201cGrouping and Nesting\u201d).<\/p>\n\n\n\n<p>Now that you&#8217;re a CSS pro, <a href=\"http:\/\/miriamposner.com\/classes\/dh201w19\/tutorials-guides\/web-development\/publish-your-website-with-github-pages\/\">let&#8217;s publish your site to the web<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We\u2019ve learned how to use CSS styles to format content on our webpage, and the code we\u2019ve used is great for when you want to format&nbsp;all&nbsp;paragraphs, headers, or images. But &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/miriamposner.com\/classes\/dh201w19\/tutorials-guides\/web-development\/css-part-2-divs-classes-and-ids\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;CSS Part 2: Divs, classes, and IDs&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":371,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_eb_attr":"","footnotes":""},"class_list":["post-387","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"http:\/\/miriamposner.com\/classes\/dh201w19\/wp-json\/wp\/v2\/pages\/387","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/miriamposner.com\/classes\/dh201w19\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/miriamposner.com\/classes\/dh201w19\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/miriamposner.com\/classes\/dh201w19\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/miriamposner.com\/classes\/dh201w19\/wp-json\/wp\/v2\/comments?post=387"}],"version-history":[{"count":0,"href":"http:\/\/miriamposner.com\/classes\/dh201w19\/wp-json\/wp\/v2\/pages\/387\/revisions"}],"up":[{"embeddable":true,"href":"http:\/\/miriamposner.com\/classes\/dh201w19\/wp-json\/wp\/v2\/pages\/371"}],"wp:attachment":[{"href":"http:\/\/miriamposner.com\/classes\/dh201w19\/wp-json\/wp\/v2\/media?parent=387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}