Newman
Member
- Joined
- May 11, 2021
- Messages
- 106
- Reaction Score
- 0
- Points
- 23
- #1
What is CSS?
CSS is an acronym. It stands for Cascading Style Sheets. I know that doesn’t do that much for you so I figure I’ll give you a more useful definition. Very simply speaking, CSS can be thought of as the style side of a web page. It’s the language designers and developers use to make the page background blue, or the font size 16px, or the line height 175%. It’s a language that cascades down upon itself, meaning, if I write one style for an element near the top of a style sheet and then redefine it down towards the bottom, the bottom style will override the top.Style sheets allow you to set web page margins and position elements. They create the “responsive” in responsive web design. They are the power behind flipping through WordPress themes as fast as you can download them. Style sheets are powerful and fun and CSS is what everyone wants to learn. It’s offers some of the biggest bang for the buck and the wow factor that makes the amateur ask, “Did I do that?”
What’s the Difference Between HTML and CSS?
I’ve read and heard this a million times, but if you’re here to learn about what things are, I think it’s worth repeating. If you consider HTML as your skeleton or the structure of a building, you can think of CSS as your skin and hair or the shell of that building. HTML is a markup language – one that defines what things are and what they mean, while CSS is what gives that structure presentation. CSS formats structure.
For example, if you wanted to put a headline on your webpage, you’d most likely tell the page that it was a headline by using an HTML header tag. If you did that and stopped there, you would have a somewhat plain looking page that was functionally correct. You explained to the browser and humans and search engines that what you put on the page was a header. But what if you were the type of person who wanted that header to look nice? What if you wanted to make it red and really big and have a margin that would space it apart from all other text? If you used CSS to accomplish this, not only would you have a well structured document, you’d also have a well presented one. Now, imagine adding CSS to all your structured elements, such as paragraphs, images and so forth. By using CSS to define your page layout, you could really let your imagination fly.
If you’d like to learn more about the basics of CSS, I suggest you read this post by SitePoint. And if you want to check out a really cool tutorial that will help you get started with CSS, take a look at this set of pages by MDN (Mozilla Developer Network).
What if You Have No Style Sheet?
Did you know that even if you don’t use a style sheet to take advantage of your own look and feel, your web page will still have styles applied to it? That’s right – every browser uses internal styles that they’ve built in. The problem is, not all styles are created equally. Internet Explorer may treat the H1 element differently than Chrome does and Firefox may treat the UL element differently than Opera does. To deal with this, many developers “reset” default browser styles with a separate style sheet that overrides all styles and sets them to zero. Others “normalize” their styles with a normalize file. This file simply created continuity among browsers. The most popular of these types of files can be found here. This file is maintained by Nicolas Gallagher and is widely used across the web.If you’d like more information on default browser styles, I suggest you take a peek at the resources I’ve listed below.
– What Is A CSS Reset? by CSS Reset
And if you’d like to take a look at some of those default browser styles themselves, browse this post:
– User Agent Style Sheets: Basics and Samples by Jens Meiert
Now, what’s the reason a developer might want to take advantage of normalizing or resetting browser default styles? Well, the reason is because as CSS files are created and grow, styles held within them may affect default styles differently. Sometimes small differences across browsers may occur, but sometimes specific areas may not work as intended at all. It’s because of these unknowns and odd behaviors that developers have chosen to create a baseline.
The Syntax of CSS
I think it’s high time to stop talking about CSS and start seeing what CSS looks like. I’m going to offer up some short code below:
Code:
h1 {font-family: Arial, Helvetica, sans-serif;
font-size: 3em;
color: red;
}
In the above example, we have two important areas to look at. The first area is what’s called, the selector. Selectors in CSS target which elements on the page to format and style. In the case above, the selector is the h1 element and in this case, headers that are on a page that uses this specific style sheet would take on the appearance that is specified in the next part of the above code, which is called the declaration. The declaration gives the formatting instructions to the selector.
To learn about the various types of selectors and the rules that apply to them, take a look at these pages:
– CSS Selectors Reference by W3Schools
– The 30 CSS Selectors you Must Memorize by Tuts+ Code Tutorial
– Selectors – Web developer guide by MDN
To continue on with the example I gave above, I’m going to talk about the declaration. In this area of the code, you can see three rules. The first rule instructs which font to display by using the Ariel, Helvetica, sans-serif value for the font-family property. In the second rule, the font-size property was given the value of 3em and in the third rule, the color property was given the red value. So to summarize, each rule in CSS has a property and a value.
Selector Types
There are a few major types of selectors that really should be covered. These selectors will handle the majority of your code, so it’s wise to understand them. I’m going to break them up into sections to make things as clear as possible.Element Selectors
The element selector selects all elements with the specified element name. It’s a broad type selector that can cover a good portion of your entire site. I’ll give you an example of the element selector syntax below and then some examples of element selector in action.
Code:
element {
declarations;
}
If you take a look at the long list of HTML elements, you can easily see why these types of selectors are so powerful. Say you want to style your paragraphs across your entire site to look a certain way. All you would need is some code that looks like this in your CSS file:
Code:
p {
color: red;
line-height: 150%;
}
It’s that simple. If you wanted more style, you would add more declarations. Be careful though because, like I said above, element selectors go a long way. When naming an element selector, all you need to do is use the tag name without the greater than / less than symbols. If you notice above, I use just the p as the selector. The same is true for h1, ul, li, etc…
Now, just to make things super clear, I’ll give you a quick example of a paragraph in an HTML file so you can see the p element.
Code:
<p>This is a sentence.</p>
You can see that the p selector in the CSS code in the first example is selecting the p from the paragraph element in the second example.
Class Selectors
If you’d like to narrow down somewhat and not touch every single element of a certain type across your entire website, you can create class selectors. Here is what class selectors look like:
Code:
.class {
declarations;
}
The way class selectors work is like this – say you know you’re going to have an introduction paragraph and a secondary introduction paragraph on every page of your website and you know you want these particular paragraphs to look a certain way. In this case, you could apply a class to that particular type of paragraph. In HTML, the element and class would look like this:
Code:
<p class="introduction">First introduction paragraph.</p>
<p class="introduction">Second introduction paragraph.</p>
Now, you can select that class in your CSS file, no matter how many times it’s used on a page and no matter how many pages it’s used on your site. In your file, it may look like this:
Code:
.introduction {
color: blue;
}
This would give all classes with the name introduction on your pages the text color of blue.
Here’s something interesting. Say you wanted to use that introduction class on other sections of your pages besides just the intro paragraphs, such as asides or navigation, but wanted to select just the paragraphs for some special styling. If this was the case, you can write code like this to limit those particular styles:
Code:
p.introduction {
color: blue;
}
Notice how I placed the p in front of the .introduction. That limits the styling to the p elements with the introduction class. Lastly, when writing a class selector, you start the class name with a period.
ID Selectors
ID selectors display a hash, or an # in front of their names when used in a CSS file. ID selectors look like this:
Code:
#id {
declarations;
}
While you can have multiple classes on a page, you can only have one ID. IDs are meant for something specific, such as a top content area or something like that. Here is what an ID in HTML may look like:
Code:
<div id="top">
Top content code goes here.
</div>
And this is what the ID selector in your CSS file might look like:
Code:
#top {
background-color: grey;
padding-top: 50px;
}
If we put the classes and IDs together into a working HTML file and then selected them in our CSS file, we might have something that looks like this:
HTML
Code:
<div id="top">
<h1>Our Page Top Content</h1>
<p class="introduction">First introduction paragraph.</p>
<p class="introduction">Second introduction paragraph.</p>
</div>
CSS
Code:
#top {
background-color: grey;
padding-top: 50px
}
.introduction {
color: blue;
}
Descendant Selectors
Descendant selectors target all elements that are descendants of, or within a, specified element. This is fairly simple. Let’s take a look at some HTML code:
Code:
<h1>Our Page Content</h1>
<div>
<p>First introduction paragraph.</p>
<p>Second introduction paragraph.</p>
</div>
<p>Third paragraph.</p>
Say we wanted to select only the p elements that are contained inside the div element for some special styling. We don’t want to alter the third paragraph in any way. In order to do this, we could write some CSS code that looks like this:
Code:
ancestor-element descendant-element {
declarations;
}
Or more specifically:
Code:
div p {
color: blue;
}
The above CSS code would only target the first two paragraphs – the descendant elements – contained within the div element – the ancestor element. It would not affect the third paragraph.
In between the two selectors, we use something called a combinator, or “something that explains the relationship between selectors.” In this case, the combinator is a space, because we’re dealing with a descendant selector.
Grouping Selectors
Let’s say that, for some reason, you wanted to add a left margin of 10 pixels to all elements in our example code. So to refresh your memory of what that code is, I’ll show you again:
Code:
<h1>Our Page Content</h1>
<div>
<p>First introduction paragraph.</p>
<p>Second introduction paragraph.</p>
</div>
<p>Third paragraph.</p>
Instead of writing three duplicate declarations like this:
Code:
h1 {
margin-left: 10px;
}
div {
margin-left: 10px;
}
p {
margin-left: 10px;
}
We could group our selectors together using a comma as a separator, like this:
Code:
h1, div, p {
margin-left: 10px;
}
Now, our code is DRY (Don’t Repeat Yourself) and our code is lean, but still has the same effect.
HTML & CSS Best Practices
The final area I’d like to discuss in this post has to do with the best practices you should follow when coding your HTML and CSS. While many styles of coding may “get the job done,” a well thought out and foresighted approach will be well worth your efforts.While much of what James Williamson discussed in the class I’m taking currently (CSS Fundamentals) had to do with how to best use classes and IDs in HTML and selectors in CSS, I’d like to go a bit further. Instead of only discussing the areas where HTML and CSS intersect, I figure a more broad overview of coding HTML and CSS would be helpful. I did some poking around online and came across a few referral pages that I think would help if looking into this area. They are:
– 30 HTML Best Practices for Beginners by Tuts+ Code Tutorial
– Writing Your Best Code by Learn to Code HTML & CSS
– Front-end Code Standards & Best Practices
– HTML5 (and Some CSS) Best Practice by CodeProject
I hope those references help. There’s a lot to learn when it comes to coding and getting on the best practices bandwagon early on is the way to go.
That’s it for this post. I think I covered some good areas. If you’re interested, keep your eye on the Coding forum here because I’m going to continue my discussions on CSS and beyond. Up next, something about WordPress page templates.