Jan. 26, 2017 | Lesson 1 (CSS Fundamentals)

Today, I started a new course called CSS Fundamentals on lynda.com. This course was less hands on than my previous one, but it was a good intro course to understanding the importance and use of CSS.

CSS stands for Cascading Style Sheets, which is a style sheet language developed to control the presentation of markup language documents like HTML. HTML controls the structure of the web, while CSS controls the presentation of it.

A good visual example of this is you can think of HTML as the structure of a new building. You can see the structure as it’s being built, but you don’t really know what the building is going to look like.

screen-shot-2017-01-26-at-1-09-41-pm

CSS, on the other hand, serves as the king of the building and determines what the outside of the building will look like.

You can change how things look by simply changing the CSS files, all without changing the underlying structure – or HTML. In terms of syntax, CSS is not a markup language like HTML, or even a scripting language like JavaScript. Below are a few things CSS is.

screen-shot-2017-01-26-at-1-10-48-pm

Websites cane be controlled and constructed by five main elements.

screen-shot-2017-01-26-at-1-11-28-pm

Now, we’ve talked about HTML and CSS, the other three (JavaScript, PHP and mySQL) are other languages used to control websites. JavaScript controls the behavior within the browser. Server side language, such as PHP, controls processing and business language. And databases, such as mySQL, store content.

A website might use all of these elements, while other it might only use HTML and CSS. This modular approach means that you can change each of these elements independently of each other. In terms of CSS, you can change the entire layout and design of a page without ever chaining its content or structure. This can also speed development and site maintenance by controlling the formatting for an entire site through a small number of CSS files.

screen-shot-2017-01-26-at-1-15-53-pm

All browsers have a default style sheet that is applied to HTML. By some this is considered “unstyled.”

screen-shot-2017-01-26-at-1-16-33-pm

While it certainly looks unstyled, that’s not entirely accurate. Before authors were allowed to control HTML pages through CSS, browsers had to have some way of displaying HTML content visually. Although the approaches among early browser manufacture differed, essentially they all developed default style sheets that told the browsers how to display specific elements.

When you write CSS, one of the things you’re actually doing is overwriting those default browser styles, and replacing them with how you want the page to look.

screen-shot-2017-01-26-at-1-17-22-pm

Now, let’s get into some of the syntax of CSS and how to read the language. CSS style sheets are made up of two parts: the selector and the declaration. The selector, here “p” for paragraph, tells the browser which elements or elements in the document style.

screen-shot-2017-01-26-at-1-22-32-pm

The above selector, targets all paragraphs within a style document to be formatted. While this element selector is pretty simple, selectors can be very complex depending upon which elements on the page you’re trying to target. Selectors can be grouped together or even combined to allow for more precise element targeting.

The declaration, which is enclosed in these curly braces, uses the formatting instructions for the style. Here, there are two rules: one telling the browser which font to use and another defining the size of the font.

screen-shot-2017-01-26-at-1-22-44-pm

The rules themselves are made of two parts: the property and the value. These are separated by colons and use a semicolon to tell the browser to stop evaluating and move on to the next rule.

screen-shot-2017-01-26-at-1-23-04-pm

While this syntax is simple, you will need to learn the myriad of selectors and selector combinations which make it possible to exercise a greater amount of control over page elements. You’ll also need to learn the various properties you can set for each of the elements and the values that are allowed for that property.

For the most part, the use of whitespace doesn’t matter. For example, the image below shows that both these CSS rules will give you the exact same results.

screen-shot-2017-01-26-at-1-25-42-pm

However, sometimes, whitespace does matter, so it’s important to be familiar with when whitespace is important and when it’s not.

screen-shot-2017-01-26-at-1-26-01-pm

Now that I’ve went over a little bit of CSS syntax, I’m going to focus on selectors in a bit more detail. Selectors allow us to tell the browser which elements on the page we want to style. In some cases you want to apply styles broadly all across the site. In  other situations, you’r going to want to target a smaller number of elements or even a single element. Understand how selectors work will allow you to do just that.

We’ll start with the Element Selector which are global in nature, meaning they’re going to affect every element of that type in a styled document. You simply need to know what the tag is for a given element in order to style it.

Unlike HTML, we don’t need angled brackets around the tag name, just the tag itself. For example, to style a paragraph you’ll use “p”, for h1 headings you’d use “h1”, for unordered lists you’d use an “ul” and so on. While these selectors are efficient, they’re also very broad, which is why they’re mostly used to set global, site-wide styles.

screen-shot-2017-01-27-at-1-43-09-pm

Another basic selector type is the Class Selector. Classes are HTML attributes that can be seton any HTML element. You can name a class anything you want and you can use it on as many elements and as many times on the page as you need. In the image below, for example, the browser would look through all the elements on the  page and apply styling to any elements with the class attribute of “subheading.”

screen-shot-2017-01-27-at-1-44-50-pm

Note that classes are identified in CSS by the period in front of their name.

Next, ID Selectors are similar to Class Selectors in that they represent an HTML attribute. They differ from classes in one very important aspect: IDs must be unique to the page. Meaning that if you assign an ID to a page element, no other element on that page may have that specific ID. In this example, the browser would find the element on the page that has the ID attribute of “sidebar” and then apply styling.

screen-shot-2017-01-27-at-1-45-46-pm

IDs are identified by the octothorps, or as is more commonly known, the pound symbol, in front of the ID name. For those more Twitter oriented, you could also call it the hashtag symbol.

You can also make a Class and ID Selectors element-specific by adding an element to the front of the selector. This limits the styling to only elements with the specific class or specific ID applied to it. For example, below, styling will only be applied to h2 headings with a class of subheading, or divs with an ID of sidebar.

screen-shot-2017-01-27-at-1-52-19-pm

This allows you write a single general class or ID style and then follow that with a more focused element-specific style, if necessary. Also, here are three important things to remember:

screen-shot-2017-01-27-at-2-00-03-pm

Another type of selector is the Descendent Selector. These selectors allow you to target an element based on where it’s found within another element. You simply string the selectors together, separating them with white space. The parent selectors are added first, followed by each successive nested selectors.

screen-shot-2017-01-31-at-2-38-18-pm

For example, in the above image, the browser would find any span elements inside of paragraphs elements which were also inside of div elements.

Below, are a couple of examples of how this would work. In the first example, the browser would locate any paragraph found within a div tag, and then apply the styling. In the second one, it would find any span element inside of the paragraph, which is also inside of a div and then apply the styling.

 

You can also group selectors together by using commas between the selectors themselves. This can make writing styles more efficient; by grouping selectors together, they don’t need the same styling. Instead of writing three separate selectors like on the left, you can simply write one Group Selector on the right.

Previously, we talked about being consistent with the CSS rules that you create when applying styles. The same could also be said when structuring your website with HTML. New authors often structure the same content in different ways on different pages. This makes it considerably harder to write global styles for the entire site. Below is an example.

screen-shot-2017-01-27-at-2-08-18-pm

On page one it’s structured with a div tag, on page two with a block quote and on page three with simple paragraph tag. To style this consistently across the site, you’d need several different rules that are essentially doing exactly the same thing. By establishing a guideline for how content should be structured , and then making sure that you’re consistent in following those guidelines, you can mean the process of writing styles much easier for yourself.

screen-shot-2017-01-27-at-2-08-28-pm

Advertisements

One thought on “Jan. 26, 2017 | Lesson 1 (CSS Fundamentals)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s