1 00:00:00,000 --> 00:00:02,000 [CSS] 2 00:00:02,000 --> 00:00:04,810 [Joseph Ong - Harvard University] 3 00:00:04,810 --> 00:00:07,160 [This is CS50. - CS50.TV] 4 00:00:07,160 --> 00:00:11,430 >> Today we'll be talking about CSS or Cascading Style Sheets. 5 00:00:11,430 --> 00:00:14,330 >> So what exactly is CSS? 6 00:00:14,330 --> 00:00:17,120 Well, let's break the term CSS down into 2 parts: 7 00:00:17,120 --> 00:00:19,510 Cascading and Style Sheets. 8 00:00:19,510 --> 00:00:23,900 Cascading is a little bit more complex, and it's something we'll cover in another video. 9 00:00:23,900 --> 00:00:27,930 But for starters, Style Sheets very much hints at what CSS does. 10 00:00:27,930 --> 00:00:30,880 It adds styles to the HTML of a web page, 11 00:00:30,880 --> 00:00:33,720 changing how the web page aesthetically looks. 12 00:00:33,720 --> 00:00:38,310 This means everything from the font of the text to the positioning of the content on the page 13 00:00:38,310 --> 00:00:43,800 to other cool things like making the corners of a box rounded or adding shadows to text. 14 00:00:43,800 --> 00:00:48,230 You can even do crazy things like make things animate on the screen. 15 00:00:48,230 --> 00:00:51,700 >> So how do we use CSS with HTML? 16 00:00:51,700 --> 00:00:53,620 Take this poor-looking site I just wrote. 17 00:00:53,620 --> 00:00:57,290 If Rob were to look at this site right now, he'd probably say something like, 18 00:00:57,290 --> 00:01:01,730 "Wow! My introduction looks terrible. Joseph, you're an awful designer." 19 00:01:01,730 --> 00:01:05,110 "Using the default Times font? Helvetica is so much better." 20 00:01:05,110 --> 00:01:09,600 >> So what might be the simplest way to apply the styling Rob wants? 21 00:01:09,600 --> 00:01:13,530 The most obvious way lots of people initially wrote CSS 22 00:01:13,530 --> 00:01:17,470 was to put what we call style declarations right in the element itself 23 00:01:17,470 --> 00:01:20,560 using the HTML style attribute. 24 00:01:20,560 --> 00:01:26,420 A style declaration simply consists of the HTML element's CSS property we want to change 25 00:01:26,420 --> 00:01:32,140 followed by a colon followed by the new value of the property and a semicolon at the end. 26 00:01:32,140 --> 00:01:36,600 For example, let's say Rob wants a black border around his introduction. 27 00:01:36,600 --> 00:01:40,040 We first put the style attribute on Rob's div here 28 00:01:40,040 --> 00:01:43,830 then inside double quotes put a CSS declaration: 29 00:01:43,830 --> 00:01:47,880 "border: 1px solid black;" 30 00:01:47,880 --> 00:01:52,480 The 1 pixel refers to the width of the border, the solid refers to the style of the border, 31 00:01:52,480 --> 00:01:56,640 and the color at the end determines what the border's color is. 32 00:01:56,640 --> 00:02:01,220 >> If we want multiple styles on an element, write these declarations in sequence. 33 00:02:01,220 --> 00:02:05,650 For example, if Rob wants his header text in Helvetica with a blue background 34 00:02:05,650 --> 00:02:09,270 and more space around the text, we can do this: 35 00:02:09,270 --> 00:02:19,800 style="font-family: Helvetica; background-color: blue; padding: 5px;" 36 00:02:22,150 --> 00:02:28,010 The last semicolon is actually optional, but people usually keep it in for consistency's sake. 37 00:02:28,010 --> 00:02:32,180 >> We'll save explaining some of the cooler and more complex CSS properties 38 00:02:32,180 --> 00:02:34,140 for another video. 39 00:02:34,140 --> 00:02:38,780 If you have something in mind, just Googling the effect you want followed by CSS 40 00:02:38,780 --> 00:02:41,590 will probably give you pretty good results. 41 00:02:41,590 --> 00:02:44,530 The cool thing is that CSS is pretty broad, 42 00:02:44,530 --> 00:02:48,470 so odds are if there is anything you want to do--within reason-- 43 00:02:48,470 --> 00:02:51,350 CSS can probably do it. 44 00:02:51,350 --> 00:02:54,750 We call this kind of styling inline styling. 45 00:02:54,750 --> 00:02:59,030 The element style is, well, in line with the start tag. 46 00:02:59,030 --> 00:03:01,770 >> For folks who really like to be organized, 47 00:03:01,770 --> 00:03:05,800 you might start getting a little peeved at how messy this all looks. 48 00:03:05,800 --> 00:03:09,810 Imagine if you had to do this for every element on the page, 49 00:03:09,810 --> 00:03:13,690 plus now your HTML and CSS are all intermixed and cluttered. 50 00:03:13,690 --> 00:03:15,500 Gross, right? 51 00:03:15,500 --> 00:03:21,110 To fix this, people eventually started catching on to separating their HTML markup 52 00:03:21,110 --> 00:03:26,370 from their CSS styling by using something called CSS selectors. 53 00:03:26,370 --> 00:03:31,780 CSS selectors are used to choose elements to which declarations are applied. 54 00:03:31,780 --> 00:03:38,600 A selector combined with a list of CSS declarations is often referred to as a CSS rule. 55 00:03:38,600 --> 00:03:43,260 These rules would be put between open and close HTML style tags, 56 00:03:43,260 --> 00:03:46,120 often in the head of the document. 57 00:03:46,120 --> 00:03:49,000 >> It's much easier to see with an example, 58 00:03:49,000 --> 00:03:53,000 so let's start by creating a simple CSS rule. 59 00:03:53,000 --> 00:03:58,180 First, let's put style tags in the head section of our HTML. 60 00:03:58,180 --> 00:04:00,020 Next, the selector. 61 00:04:00,020 --> 00:04:03,490 We'll start off by using one of the simplest selectors, the hash symbol, 62 00:04:03,490 --> 00:04:08,080 which selects an HTML element by its ID attribute. 63 00:04:08,080 --> 00:04:11,850 In this case we'll select the div that represents Rob's introduction 64 00:04:11,850 --> 00:04:16,740 by typing the hash symbol followed by the div's ID, rob. 65 00:04:16,740 --> 00:04:18,579 Now the declarations. 66 00:04:18,579 --> 00:04:24,090 We add open and close braces and shift our earlier inline declarations on Rob's div 67 00:04:24,090 --> 00:04:26,880 inside of these braces, refresh, 68 00:04:26,880 --> 00:04:34,160 and, cool, Rob's intro still has a black border around it minus the messy inline ugliness. 69 00:04:34,160 --> 00:04:39,380 >> Now, what if we wanted to select the h1 inside of Rob's intro? 70 00:04:39,380 --> 00:04:44,400 You might think, "Let's put an ID on it and then move our earlier styles." 71 00:04:44,400 --> 00:04:48,760 That works, but CSS has other ways of letting you select elements 72 00:04:48,760 --> 00:04:53,490 by using what we call combinators to mix simple selectors. 73 00:04:53,490 --> 00:04:57,740 For example, a whitespace character can be used as a combinator 74 00:04:57,740 --> 00:05:03,510 to specify all instances of 1 selector that live inside of another selector. 75 00:05:03,510 --> 00:05:06,630 That sounds a lot more complicated than it actually is. 76 00:05:06,630 --> 00:05:08,830 Here's an example. 77 00:05:08,830 --> 00:05:13,900 We will type #rob, add a space, and follow it with an h1, 78 00:05:13,900 --> 00:05:18,840 another simple selector called a tag selector that selects types of elements 79 00:05:18,840 --> 00:05:21,200 like divs or paragraphs. 80 00:05:21,200 --> 00:05:26,140 The space combines our 2 simple selectors, applying all CSS declarations 81 00:05:26,140 --> 00:05:32,560 and the curly braces to h1 tags that live inside of the element with id="rob". 82 00:05:32,560 --> 00:05:38,290 Just to convince you that it works, I'll change the font color to white, refresh, 83 00:05:38,290 --> 00:05:42,090 and, look, Rob's header is now white. 84 00:05:42,090 --> 00:05:47,510 >> All of this work goes to show that by using rules instead of inline styles 85 00:05:47,510 --> 00:05:50,510 we can get much cleaner code. 86 00:05:50,510 --> 00:05:53,900 In fact, if this style block starts getting a little bit large, 87 00:05:53,900 --> 00:05:58,340 I can even pull these styles out into a different file altogether. 88 00:05:58,340 --> 00:06:05,120 >> To include this new file as CSS in this document I'll just use HTML's link tag. 89 00:06:05,120 --> 00:06:10,320 Here I'm telling it that I'm linking in an external style sheet, the rel attribute, 90 00:06:10,320 --> 00:06:14,840 and specifying the path to the file with my href attribute. 91 00:06:14,840 --> 00:06:20,290 Now my HTML markup and CSS are organized nicely in completely separate files, 92 00:06:20,290 --> 00:06:26,090 which is almost always the way designers prefer working with HTML and CSS. 93 00:06:26,090 --> 00:06:30,180 >> In case you're wondering, the simple selectors include ID selectors 94 00:06:30,180 --> 00:06:33,100 and tag selectors like the ones we just saw 95 00:06:33,100 --> 00:06:38,610 as well as class selectors for selecting elements with a certain class, 96 00:06:38,610 --> 00:06:44,000 attribute selectors for selecting elements by other attributes like type="radio" 97 00:06:44,000 --> 00:06:49,180 for radio button inputs, and pseudoselectors like hover and focus 98 00:06:49,180 --> 00:06:55,260 for specifying styling when interactions like mousing over an element occur. 99 00:06:55,260 --> 00:06:58,950 >> The common combinators include whitespace for all children 100 00:06:58,950 --> 00:07:02,080 and commas to distinguish selectors. 101 00:07:02,080 --> 00:07:06,370 Others you may encounter include the arrow for direct children only, 102 00:07:06,370 --> 00:07:10,440 the tilde for all siblings that occur after the element, 103 00:07:10,440 --> 00:07:16,010 and the plus sign for the 1 sibling that comes immediately after the element. 104 00:07:16,010 --> 00:07:18,540 >> By combining these selectors and combinators, 105 00:07:18,540 --> 00:07:22,910 you can broaden the range of styling you can achieve on a given web page 106 00:07:22,910 --> 00:07:25,050 and write CSS more efficiently. 107 00:07:25,050 --> 00:07:28,370 With just a couple of lines of CSS you see me typing here, 108 00:07:28,370 --> 00:07:34,460 I can quickly make something like this look like something like this. 109 00:07:34,460 --> 00:07:37,080 >> I'm Joseph, and this is CS50. 110 00:07:37,080 --> 00:07:39,260 >> [CS50.TV] 111 00:07:39,260 --> 00:07:40,780 >> Uh, where do I start? 112 00:07:40,780 --> 00:07:44,140 Let me do that without-- [laughs] Okay. 113 00:07:44,140 --> 00:07:47,300 Add a-- Let me change that wording. 114 00:07:50,580 --> 00:07:52,470 Ooh. Sorry.