CSS!

CSS stands for Cascading Style Sheets! Using CSS allows for us to style our sites beyond basic HTML elements! We've already run into styling from the last lesson! Now we're going to take what we've learned from that lesson and expand on it!

We can write our css stylings either inline (which we've already done in the past lesson), in the <style> section of our page's <head>, or in a completely separate file (we'll get to that at the end, for now we'll start moving our CSS to the style section in the head).

 

Syntax:

CSS Syntax looks completely different from HTML! This is because it's a different type of language!

When we write CSS in the style section or a CSS file, we don't wrap it in quotations, like we did with inline styling:

In CSS, the property:value pair is a declaration.

CSS Doesn't care how many spaces/new lines your code has, but just make sure you have both opening and closing curly braces!

 

Using CSS:

Like mentioned above, we've already seen inline styling. This works completely fine and well, but that takes too long to change every single inline style you use. Inline styling is great for one or two changes, but when you have to change every line, that becomes painful. What happens if our website has multiple pages, each with hundreds of lines? Let's quickly convert some inline styled code to use CSS!

 

We have styling for our body, h1, and our img. Instead of our stylings and put them in a <style> section our <head>! See how the code renders like the above code, but with styling in the header?

 

Notice how we put our styling in the <style> section of our site, following the below format:

By using the above format, we've told our site that every time we use a h1, it will always have a border-left, and a padding of 10 pixels, and all of our <img> elements will have a width of 500 pixels! As far as our body is concerned, we've only got one, but it's nice to have our styling all in one place instead of spread out throughout the HTML's code!

 

Classes and Identities:

What if we wanted our divs to all have different styles? We can't do that with the CSS above! By using Classes and Identities, we can use as many different styles as we want!

The differences between classes and IDs is that IDs are unique, and classes aren't! Multiple elements can have the same class, whereas only single elements can have an id.

What does this mean?

Classes

To share CSS among multiple elements, we should use Classes!

To create a Class in a stylesheet, you need to start your selector with a . This way we get to share everything!

 

So, now we can use the same class over and over and over again! When we try to use the class, we add the attribute 'class' in our element, and assign it to the name of our class in the css 'IamAClass'.

 

You can mix and match as many classes as you want ( remember, some classes might override others). What color do you think our div will be?

 

IDs

Creating an ID in a stylesheet, you need to start your selector with the #. See it in action.

 

The ID that we made in our <style></style> element is used by the <div></div> element in our body. Remember that the ID absolutely has to match (so it's case sensitive).

By using the ID, we then get to turn our div's color to red!

A downside to using ID is that only one element should use it in your whole page! We shouldn't do this:

This is because IDs are just that, a specific ID! It's like if somebody was trying to use your driver's license (or school ID, or anything). They're not you! (Note: because our browsers are really good at trying to fix our mistakes, it will render the colors properly, but having more than 1 ID can lead to more problems later on!)

 

You can also combine a CSS class and a CSS ID.

 

Specificity Rules

When applying the same CSS style via Tags, Classes, and ID's, the browser will need to decide which style to apply. If this sounds confusing, that's because it is! Check out this example.

 

The answer is GREEN! In CSS, the most specific rule will be applied. IDs are meant to represent only 1 thing, so it is the most specific. The order of specificity is as follows:

  1. IDs
  2. Classes
  3. Tags

 

Things to Remember

 

Commenting Your CSS!

When we wrote comments before in HTML, we used <!-- -->. Because CSS is a different language, we comment differently. We use /* */. Just like html, you put your comments between the text: /* I am a comment! */. See (or really don't see) the comments in action!

 

Stylings:

Now let's explore the differing kinds of stylings we can use!

Background colors:

We've already seen background-color, but it's good to revisit it! To change background colors is background-color:

b { background-color: green }

See it again in action:

 

Background images!

You can add a background image if you want (though not always the greatest idea)! Can you spot the cat in the background??

Try changing the image height and width to be larger. Notice that the image repeats? You can change that using the attribute: 'background-repeat:no-repeat' in your css!

With background-repeat, you can use the values:

You can use both direct and relative links too! If you have an image file named myImage.jpg in the same folder as your index.html file, you could just write url('./myImage.jpg')

 

Background-Attachment:

This allows your image to scroll with the page!

 

You can also have your image fixed, so that the text scrolls, while your image does not!

 

Background position:

You don't always want to have your background chilling in the top left of your page. That's boring. Pur yours wherever you want with background-position :

You can position it to have the values:

We'll come back to this, but you can also set your postiion by using pixels and percentages too!

 

Text Formatting!

We did some text formatting earlier, but let's take a deeper dive!

Text Color

To change your text's color in an element, you can use the color property. You can name your colors with either the color's name OR use the hex code value.

 

Italics

Sometimes you want to italicise things. When you do that, you can use font-style property. This takes in either italic, oblique, or none (italic and oblique are more or less the same thing):

 

Bolds

When you want to make something bold, you want to use the font-weight property! Font-weight takes the values of: bold and normal

 

Underlines Overlines and Strikethroughs

When you want to underline, overline, or strikethrough things - we need to use another property called text-decoration:

 

Font Properties!
Font-Size:

Font-Size - you guessed it - it changes the size of our font. Font-size takes in px values (pixel values, like 12px), but also has relatively normal values like xx-small, x-small, smaller, small, medium, large, larger, x-large, and xx-large. Using the px allows for a greater level of customization, but if you just want some big font and don't want to play with what specific pixel amount you want, go for the worded values!

 

Font Family:

This is how you change the actual font (like Times New Roman, Helvetica, ect). Most browsers will be able to render most fonts, but sometimes a browser might not have the proper font installed. For you to ensure that your site looks good, you can include a whole list of fonts to make sure the browser will properly render your site.

Because font-family takes in a list of fonts, the values need to be comma separated. If your font has a space in the name, then we need to have the names wrapped in quotations:

div { font-family: "Fira Code", "Comic Sans", Helvetica, sans-serif }

Check out the code:

Notice that the div didn't find Fira code or comic sans and went to Helvitica? That's because Fira Code is a very special font that most browsers don't support, and comic sans is actually called "Comic Sans MS", give that a go and see how that works!

Text Properties:
Text align:

To align your text is where the text is aligned on your page (like in MS Word). The values we need to use are left, center, right, and justify.

 

Text indent:

This will indent your text a new section of words according to its value! The values for the property are: length and percentage:

 

Text transform

Suppose you're feeling lazy, and don't want to type out a certain format in a sentence. Text transform takes the values capitalize, uppercase, lowercase, and none.

 

Styling Lists

You've seen this before, but let's style our lists not inline! Lists are boring! So, we should style them in some way to spice them up a little bit! We can do this with list-style-type. This property can take a value of: disc, circle, square, decimal, upper-roman, lower-roman,and upper-alpha.

Bullets, numbers, etc:

 

Custom List Images:

Suppose you wanted to make a really bonkers list, and you wanted your own special list item image. What we're really doing is basically setting a background image for all of our list items and then we remove the actual list-style-tyle, so that there aren't any bullets:

 

Adding Width and Height to Elements:

You can add width and height to any element (we did this with images earlier!). We can set special elements to take up smaller amounts of space, or significantly larger!

Width:

Takes a value of either some length or a perentage. If you don't specifically say what you want the width to be, the browser will automatically adjust it to whatever it thinks the right size is.

 

Height:

This works the exact same as width, but It's mostly recommended to use pixels or some other length measurement. The downside is that height doesn't always work with auto and percentages (you'll see below). Try taking this code and opening it on a browser on your own machine (that is, save it to a file called height.html and double click on it):

 

 

Borders:

We've seen a lot of borders already. Borders take values of either, thin, medium, thick, and some value of length. Up to now, we've only been using solid borders, but borders can be: none, solid, dotted, ridge, inset, double, groove, outset, and dashed. You absolutely need to choose a type of border for it to work! For the 3d borders, we need to add border-color too.

 

You can change specific parts of the border width by adding a direction: top, right, bottom, left:

 

Spacing Margins and Padding!

You've probably noticed that we've been adding padding on some of our examples to be a bit more clear! Let's go over that now!

Margins are the spacing AROUND the element (like OUTSIDE of our border)

Padding is the spacing INSIDE the element ( like INSIDE of our border)

Just like 'border', we can access specific sides for our margins and padding:

 

 

 

Changing Mousetype!

If you want to have a mouse cursor that's different than just the normal mouse, you can create your own! This may be for when your site's loading, it may just be because you want to have something different!

There are a some cursors that you can use that are pre-supplied values: crosshair, default, help, move, text, wait, and resize elements: (n-resize, s-resize, e-resize, w-resize, ne-resize, nw-resize, se-resize, and sw-resize)

 

 

Display!

Display is a very powerful property that we'll use more in the future, once we start working with javascript, but for now, we'll just look at a few things we can do with display.

Turn any non-block element into a block element:

Remember how we used <strong> to make words bold? Strong only surrounded a given word, and that was it. It didn't take up any more space than it needed. We can change that, though, with the display property. This works for literally any non-block element!

Vice Versa, sometimes we don't want block elements to take up an entire section! We can then use the value 'inline' for the display property to take care of that!

 

Suppose we wanted our inline block to have a certain amount of width? It won't work because display's inline value takes away all width, height, margin-top, margin-bottom, and float properties.

To get around this, we can use the inline-block value!

 

 

Float!

Let's say you want your divs to all be right next to each other, but don't want to constantly be using display's inline-block value. We can get around this by using float!

Remember that divs are block elements, so they take up as much space as possible (width wise):

When we use float, though, we're able to get around the whole "divs taking up the entire width of the page, thing" in a more dynamic way than using display: inline-block. Let's check it out:

Making divs right next to each other is neat, but what's the point? Well, let's say we wanted to have a cool photo blog? Instagram is great and all, but I want to have my own special site. Let's try to make one of those:

We haven't done any extra work to make sure our images are all similar sizes. Let's try again, by adding a width to our images in css

This looks a lot better. Let's add some padding to get some borders around our images! Also, let's add a title:

 

FlexBox!

Flex box does a lot of this for us! Let's take a look!