Home Web Front-end CSS Tutorial Styling Our Content

Styling Our Content

Jul 18, 2024 pm 03:53 PM

Styling Our Content

Intro to Styling

For the past few weeks, we've discussed how to get all the information you need onto a web page. However, if you've been following along and coding as you go, you've probably noticed that your pages don't look very appealing. They might even look quite bad, actually…

So far, we haven't added any styles to our web pages. Styles are rules we give to our browser to tell it how we want our HTML elements to look on the page. We can change the size, font, color, position, alignment, and many other things! But how?

Adding Our First Styles

<p style="color: blue;">This text is blue now!</p>
Copy after login

That's it! If you're curious, copy and paste that into a document and open it in your browser. What did you see? The text is blue now! Nice!

So, what's going on here? We surrounded our text content with a paragraph element and gave that paragraph element a style attribute. Notice the style= in the opening

tag. In the attribute's value, we assigned the color style to a value of blue: "color: blue;".

Now, this is cool, but what if we wanted to center the text on the page?

<p style="color: blue; text-align: center;">This text is blue and centered!</p>
Copy after login

All we had to change was the value in the style attribute! Powerful! But what's happening in that value attribute? We haven't seen that syntax before! That's CSS! But wait, why is it here if we're writing HTML?

We are! But HTML isn't designed for styling; it's designed for structure and semantics (which we'll cover in a later post). If we want to change the appearance of the page, we have to modify the stylesheet. By default, our browser decides how to interpret the information in our HTML using what's called the "user agent stylesheet." The font sizes and colors you've seen up to this point have been based on that! When we add CSS to our style attribute, the styles we've defined will override the user agent stylesheet, and any styles we haven't defined will fall back to that sheet. CSS stands for Cascading Style Sheets, and that's why!

This is great. Now that we can change the look of our page, the possibilities are endless. But imagine if we wanted to get more custom:

<h1 style="font-size: 36px; 
    font-weight: 500; 
    text-align: center;
    text-decoration: underline;
    padding-bottom: 16px;
    color: #b0b1b2;
    opacity: 0.7;">Welcome to My Website!</h1>
<p style="font-size: 16px;
    font-weight: 300;
    text-align: left;
    padding-left: 100px;
    background-color: yellow;">I hope you're having a great day!</p>
Copy after login

Wow… that's a lot of code for just two lines of text… and it's so hard to quickly see what's going on! This is a problem because if you were to try and come back to edit this information later, it would take you much longer than if it looked like this:

<h1>Welcome to My Website</h1>
<p>I hope you're having a great day!</p>
Copy after login

How can we solve this problem?

Introducing CSS

To keep our HTML from getting cluttered, we move all of our styling into a .css file! For small projects, we typically call this style.css. Beyond just tidying up our code, moving styles into a new file also fulfills a programming concept called separation of concerns. This means that we want our code to be segmented into its functional components. Our code shouldn't try to do everything but rather be broken down into smaller pieces that do one thing well!

In this example, instead of having one file that structures our content and styles it, we have two files: one that structures, and one that styles. Concerns separated! So, what might this look like?

h1 {
  font-size: 36px; 
  font-weight: 500; 
  text-align: center;
  text-decoration: underline;
  padding-bottom: 16px;
  color: #b0b1b2;
  opacity: 0.7;
}

p {
  font-size: 16px;
  font-weight: 300;
  text-align: left;
  padding-left: 100px;
  background-color: yellow;
}
Copy after login

Ah, much better! Now, our HTML file isn’t cluttered with all of this styling! What you see above are two CSS rulesets. A ruleset consists of a selector and two curly brackets that contain all the information about how to style the selector. In this example, the two selectors we see are h1 and p. This means that all the styles listed here will be applied to any HTML

or

element, respectively!

We can do this for any type of element! In fact, there are many ways that we can select objects with CSS Selectors, but we'll save that for another week. For now, just know that if you type in the type of element, you can add styles within the curly braces!

We’ve defined some styles, but how do we make sure our browser knows which file to apply them to?

The Link Element

The first element we will learn is the element. There are a few use cases for it, but for our purposes, we will use it to link our style.css to our index.html. In other words, this element tells our browser which stylesheet to use for the page. In practice, it will look like this:

<!DOCTYPE html>
<html>
  <head>
    <title>My Webpage</title>
    <link href="./style.css" rel="stylesheet"/>
  </head>
  <body>
    ...
  </body>
</html>
Copy after login

And that’s it! Our style.css is linked to our HTML document, and the styles will be applied.

Let’s break down this link element. First, it is a self-closing element, so we don’t need a closing tag, and it doesn’t take any content inside. There are two required attributes for this element: href and rel. These are required because the element links external resources to the HTML document. So, we need to tell our browser 1) where that resource is and 2) what that resource is to be used for. The href, which stands for "hypertext reference," determines the where, while rel, or relationship, determines the use!

It's important to note that with the href attribute, the path you provide can be either absolute or relative. This means you can give the location of a file path from the root directory, e.g., /Users/username/Documents/project/style.css (absolute). Or you can do it relative to the location of the file you are working on, as done above. The ./ before the file name indicates that the browser should look in the same folder (or directory) as the HTML file for style.css. You could also add a URL here; many content delivery networks allow you to use pre-made stylesheets, which you will connect using the element too!

Challenge

Alright, we've covered a lot today. Now it's time to put it into practice. Take the About Me page you made in last week's challenge and it to a new file called style.css. (Make sure you put it in the same folder as your HTML file!)

Then, create styles for each of your elements. Play around with the different styles you can assign until your About Me page looks more presentable! (Note: You can also target the and elements!)

For a complete list of the styles you can apply, check out the Mozilla Developer Network. They host complete documentation for web development languages: HTML, CSS, and JS! Here’s a link to their site. Use the list of properties under "Reference" in the sidebar to see what's possible!

See you next week!

The above is the detailed content of Styling Our Content. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1673
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
A Comparison of Static Form Providers A Comparison of Static Form Providers Apr 16, 2025 am 11:20 AM

Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

Weekly Platform News: HTML Loading Attribute, the Main ARIA Specifications, and Moving from iFrame to Shadow DOM Weekly Platform News: HTML Loading Attribute, the Main ARIA Specifications, and Moving from iFrame to Shadow DOM Apr 17, 2025 am 10:55 AM

In this week&#039;s roundup of platform news, Chrome introduces a new attribute for loading, accessibility specifications for web developers, and the BBC moves

Some Hands-On with the HTML Dialog Element Some Hands-On with the HTML Dialog Element Apr 16, 2025 am 11:33 AM

This is me looking at the HTML element for the first time. I&#039;ve been aware of it for a while, but haven&#039;t taken it for a spin yet. It has some pretty cool and

Paperform Paperform Apr 16, 2025 am 11:24 AM

Buy or build is a classic debate in technology. Building things yourself might feel less expensive because there is no line item on your credit card bill, but

Weekly Platform News: Text Spacing Bookmarklet, Top-Level Await, New AMP Loading Indicator Weekly Platform News: Text Spacing Bookmarklet, Top-Level Await, New AMP Loading Indicator Apr 17, 2025 am 11:26 AM

In this week&#039;s roundup, a handy bookmarklet for inspecting typography, using await to tinker with how JavaScript modules import one another, plus Facebook&#039;s

Where should 'Subscribe to Podcast' link to? Where should 'Subscribe to Podcast' link to? Apr 16, 2025 pm 12:04 PM

For a while, iTunes was the big dog in podcasting, so if you linked "Subscribe to Podcast" to like:

Options for Hosting Your Own Non-JavaScript-Based Analytics Options for Hosting Your Own Non-JavaScript-Based Analytics Apr 15, 2025 am 11:09 AM

There are loads of analytics platforms to help you track visitor and usage data on your sites. Perhaps most notably Google Analytics, which is widely used

See all articles