.fear {display: none;} — A Beginners Guide to Tackling CSS Fearlessly

Computer graphic resembling a Mac with “CSS” written on it

What is CSS?

Before we dive into the wonderful (albeit occasionally fickle) world of CSS, we need to define what it is and what it does. CSS stands for Cascading Style Sheet and is used to format and style the layout, text, table sizes, plus many other features of web pages. It allows a developer to create uniform styling from page to page by defining classes, IDs, or by inline-styling directly in the HTML file.

What are classes and IDs?

Classes are used when you want to consistently style multiple elements throughout a website or application. Classes are really useful for when you have, or might possibly have in the future, multiple elements that need to be styled in the same way. Classes can be used on multiple elements and the same element can have more than one class assigned to it! Furthermore, classes are generally used to define behavioral styles, not just visual ones.

Cool, right? The CSS would look like this on the styling sheet:

IDs are used for elements that will only get a style once since ID’s must be unique and are for single-use only. They are unique because they have a higher specificity value than a class does. ID’s can only be used on one element and the same element can only have one ID attached to it. It can, however, be attached to an element that has class styling in addition to the ID.

The CSS would look like this on the styling sheet:

Where does the CSS go?

The most common place to implement CSS is inside a separate CSS file using external styling, but you can also use internal styling inside some <style> tags in your HTML’s <head> section, or even by adding inline styling directly to your HTML attributes.

External styling is probably the most convenient way to add CSS to your website or application. Any changes made in your external file will be reflected immediately on your website or application globally. The page will load the fastest of the three options because the styling is not on the page. It also makes your HTML pages smaller and have a cleaner structure.

You just have add a simple reference to the .css file inside the <head> section of your .html file as shown below:

Open your .css file and input the following code:

Once you have those files typed up and linked together, open the page in your browser and you should see a page that looks like this:

A grey background with navy blue “This is a heading” on the first line and white “This is a paragraph.” on the second line
A grey background with navy blue “This is a heading” on the first line and white “This is a paragraph.” on the second line

Internal styling is done by putting your CSS styling in <style></style> tags within the <head></head> of your HTML document. That means the classes and IDs are defined but they are only active on that one particular page. Internal CSS styles are downloaded every time the page loads so it could increase loading speeds, however, there’s a time and place to use internal stylesheets. A great example would be sending someone a page template — as everything is in one page, it is a lot easier to see a preview.

The styling below is added between the <style> tags:

And once the page is opened in your browser… surprise! It looks exactly the same as before!

A grey background with navy blue “This is a heading” on the first line and white “This is a paragraph.” on the second line
A grey background with navy blue “This is a heading” on the first line and white “This is a paragraph.” on the second line

Inline styling uses a <style> attribute on each specific HTML tag. Using CSS this way is not recommended since each HTML tag has to be styled individually. However, it can be useful in some circumstances, like when you need to test and/or preview changes, need a quick fix, don’t have access to .css files, or need to apply styling on a single element. It can also lower your HTTP requests!

An example of HTML page with inline CSS would look like this:

Once saved and opened in browser, it once again turns into the same page that we’ve now made three different ways!

A grey background with navy blue “This is a heading” on the first line and white “This is a paragraph.” on the second line
A grey background with navy blue “This is a heading” on the first line and white “This is a paragraph.” on the second line

Tips and Tricks

Hover effects are used on buttons, navigation bars, text links, icons, box selections, and so much more! You can do anything you want on a hover and nothing will automatically change unless you tell it to. In the example below, we’re changing the text color from black to red.

When you’re designing a website, make sure your links fit in with your color palette. Link styling is missed by a lot of designers and it has the potential to confuse your visitors to a point that might cause a usability issue. The :link portion of the CSS below controls all links that haven’t been clicked on yet and the :visited handles the styling of all of the links you’ve already visited.

If you’ve ever run into padding or layout issues then box-sizing will solve all of your problems and it’s really simple! The code below is an example that would construct something similar to the boxes in the photo. The box-sizing styling will keep the box 300px by 300px even with the added padding!

Drop caps are used in traditional printed books and are easily applied to <p> tags with the css below. The “p” portion is referring to the <p> tag and the “:first-letter” portion is referring to — you guessed it! — the first letter! To change the size of your letter, just change the font-size.

Absolute positioning is used when you want to control where a certain element is placed on a webpage, no matter what. The code below sets whatever object gets this class to sit 20px from the top and 20px from the right, so it’ll be in the top right-hand corner no matter what!

Using a “*” before an element selects every single instance of that element in your HTML and sets styling for it. For example, the code below changes the font color to white in every single <p> tag on the page, no matter when it was added.

When you have transitions between hover effects, whether on a button, menu, or image, you don’t want things to happen too quickly. Ideally you’d like to ease from one style to the next, which is where the transition property can really help you make your website/application look professional! The code below will make the change happen over .3 seconds instead of instantly and is much more pleasing to use.

Full-Stack Developer, Software Engineer, and UX/UI Aficionado

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store