Hello there! Today, we are venturing into an exploration of CSS (Cascading Style Sheets), a language that contributes endlessly to bringing color, life, and structure to webpages. Think about this: once you have constructed a building with bare walls with HTML, CSS becomes the tool that paints the walls, decorates the room, and makes it cozy. Amazing, isn't it? Let's dive in!
The syntax of CSS is similar to writing commands for your dog — you choose the dog's name (selector), and then tell it what to do (declarations). For instance, let's create a CSS rule-set:
CSS1h1 { 2 color: blue; 3 font-size: 12px; 4}
In this example, h1
is the selector (our pet), color
and font-size
are the properties (commands), and blue
and 12px
are the corresponding values that indicate how to perform the command. This code makes all <h1>
elements have a blue text that is 12 pixels
big.
There are three ways we can connect CSS to HTML:
style
attribute:HTML, XML1<h1 style="color:blue;">A Blue Heading</h1>
<style>
tag inside the head
section of the HTML document.HTML, XML1<head> 2 <style> 3 h1 {color: red;} 4 p {color: blue;} 5 </style> 6</head>
<link>
tag:HTML, XML1<head> 2 <link rel="stylesheet" href="styles.css"> 3</head>
Accompanied by the corresponding styles.css
:
CSS1h1 {color: red;} 2p {color: blue;}
CSS selectors are used to "find" HTML elements based on properties of the elements. There are several different types of selectors in CSS:
CSS1p { 2 color: red; 3 font-weight: bold; 4}
In this example, all <p>
(paragraph) elements on the page would receive the styles specified (in this case, they will have red text in bold).
/*
and */
notations used in the code snippet below mark the comments in CSS):CSS1.important-text { 2 color: red; 3 font-style: italic; /* makes the text italic */ 4}
In your HTML, you'd then add the class attribute with the value important-text
to the elements you want affected:
HTML, XML1<p class="important-text">This text is considered important!</p>
This class can be reused across multiple elements on your page, allowing them all to share the same styles.
CSS1#special-text { 2 color: red; 3}
And, in HTML you'd assign this id to one specific element:
HTML, XML1<p id="special-text">This text is unique and hence, red!</p>
Remember, id is meant to be unique, and should only be applied to one element on a page.
By mastering these selectors, you'll be able to control the presentation of every element on your page for a beautiful and consistent design.
div
and span
tags group HTML elements, allowing a style to be applied swiftly to all elements within the group.
HTML, XML1<body> 2 <div style="background-color:yellow;"> 3 <h2>This is a heading inside a div element</h2> 4 <p>This is a paragraph inside a div element.</p> 5 </div> 6</body>
In this case both h2
and p
elements will have a yellow background occupying not only the text but the full row.
HTML, XML1<p>My mother has <span style="color:blue;">blue</span> eyes.</p>
Notice that the word “blue” is colored in blue. That’s the span
tag at work!
To help you understand this better, let's define some additional terms:
In HTML, elements are often enclosed within other elements. The enclosing element is called the parent element, and the enclosed elements are known as child elements. In the above example, the div
element is a parent element to the h2
and p
child elements.
Block-level Elements: By default, a block-level element starts on a new line, causes a line break after itself, and expands to fill the full width of its parent element. <div>
, <p>
, and <h1>
through <h6>
are some examples. Though these elements behave this way by default, their behavior can be modified using advanced CSS techniques.
Inline Elements: On the other hand, inline elements do not start on a new line and do not cause a line break after them. They take up only as much width as necessary for their content. Multiple inline elements can be placed next to each other on the same line. Examples of inline elements include <span>
, <a>
, <img>
, etc.
Understanding the concepts of parent and child elements, as well as block-level and inline elements, and how to use div
and span
tags are essential for effective webpage layout and styling.
Congratulations! You have successfully mastered the basics of CSS. The knowledge you acquired today is the first step towards creating appealing and dynamic webpages. Up next, we have hands-on exercises to reinforce your understanding. Remember, practice is the ladder to mastery. Let's dive in and code happily!