So you want to create a CSS stylesheet, huh? Well, let's start with the basics - a CSS stylesheet is a file that contains all the styles for your website, written in a language called CSS (Cascading Style Sheets).
A CSS stylesheet can be linked to an HTML document in three main ways: internal, external, and inline. Internal styles are written directly in the HTML document, external styles are linked to an external CSS file, and inline styles are written directly in the HTML element.
As we'll see in the next section, CSS selectors are used to target specific HTML elements and apply styles to them. There are many types of CSS selectors, including class selectors, ID selectors, and tag selectors.
What Are Stylesheets?
Stylesheets are a game-changer for web developers, as they allow you to separate structure and presentation, making maintenance and updates a breeze.
In the late '90s, HTML coders noticed that they were retyping the same old tags again and again, leading to bigger HTML files and frustration.
Having one file that defines all the values that those piles of tags would have done is a genius idea, and that's exactly what stylesheets do.
This file, known as a stylesheet, acts as a partner to your HTML code, taking care of all the layout, fonts, colors, and overall look of your site.
If you ever decide to change the look of your site, you modify that one CSS file, and all the HTML pages reading from that file will display differently.
The fewer repetitions, the leaner the source code, which is reflected online in shorter download times and a faster site loading time.
CSS is also notated in text form, which can be directly in the HTML document or in separate CSS documents.
Benefits of Stylesheets
Using stylesheets can make a huge difference in the efficiency and maintainability of your website. Pages download faster, sometimes by as much as 50%.
You'll have to type less code, and your pages will be shorter and neater. This is because you define things once in the stylesheet, rather than repeating them in every HTML page.
The look of your site is kept consistent throughout all the pages that use the same stylesheet. This makes updating your design and maintenance much easier.
Updating your design and general site maintenance are made much easier, and errors caused by editing multiple HTML pages occur far less often.
Well-authored CSS also improves the accessibility of web content, allowing access through various devices and ensuring that users with disabilities can still receive it.
Here are some best practices for using stylesheets:
- Use one CSS file for all your pages.
- Integrate CSS commands into the head of each of your documents.
- Use the style attribute to put CSS code directly into a HTML element.
Types of Stylesheets
In CSS, a stylesheet is a collection of rules that define the visual style of a web page.
There are three main types of stylesheets: internal, external, and inline.
Internal stylesheets are defined within the HTML document itself.
External stylesheets are separate files that contain the styles and are linked to the HTML document using the link element.
Inline styles are defined directly within the HTML elements using the style attribute.
Basic Syntax and Selectors
The basic syntax of CSS is quite straightforward. A design instruction consists of a selector, a declaration, and a semicolon. The selector deals with the representation of the HTML elements being defined by the design instruction.
The declaration subsists of a property-value combination, which is then listed in the braces. For example, you can assign a font color to a headline using the selectorh2 and the declaration: color: red.
A rule set may also specify several selectors, separated by commas before the declaration. This allows you to apply the same design instruction to multiple elements.
Here's a breakdown of the different types of selectors you can use in CSS:
The universal selector asterisk (*) is particularly useful for applying a design instruction to all elements in a document. For example, you can use it to change the font of all text elements on a website.
Basic Syntax
CSS is a stylesheet language that defines the design of a website, and its basic structure is essential to understand.
The basic structure of a design instruction in CSS consists of a selector, a declaration, and a semicolon.
A selector represents the HTML elements being defined by the design instruction.
The declaration contains a property-value combination, which is listed inside braces.
Every declaration must end with a semicolon.
For example, assigning a font color to a headline can be done by using the selector h2, the declaration color red, and ending with a semicolon.
Alternatively, detailed color settings can be defined using hexadecimal color codes.
Web designers can choose to assign a single property to a selector or define extensive rule sets that contain detailed design instructions.
A rule set can list all properties together for clarity, and property-value combinations are defined inside braces.
For instance, a property bundle could define the font color, family, size, style, and alignment of text.
A rule set can also specify multiple selectors separated by commas before the declaration.
Selectors
Selectors are a crucial part of CSS, allowing you to target specific elements on a webpage and apply styles to them.
A type selector corresponds to the name of the HTML element to which it refers, and formatting is applied to all HTML elements of the same type. For example, the selector h2 will format all second headline classes.
You can also use a class selector to refer to all elements of a certain class. Class selectors are formed with a period (.) and a chosen class name, such as .example. Classes are assigned to HTML elements using the class attribute (class="example").
ID selectors are used to target an element with a unique ID, which is integrated in the HTML source code with the id attribute (id="example"). ID selectors take priority over class and type selectors, and can be used to see which sections in the source text are unique.
The universal selector asterisk (*) refers to all HTML elements in a document, and can be used to apply styles to all elements. For example, the selector * will apply the styles to all text elements of the website.
Here's a summary of the types of selectors:
By using these selectors, you can create complex and specific styles for your webpage.
Text Formatting
Text formatting in CSS is a crucial aspect of web design, allowing you to control the visual appearance of text on your website.
You can use CSS to align text to the left, right, center, or justify it, using the text-align property. The standard values are left, right, center, justify, or inherit, which means it will inherit the parent element's alignment.
The text-align property also has some relative values, such as start and end, which depend on the text direction. For left-to-right text, start means left and end means right, while for right-to-left text, start means right and end means left.
Here are some common values for the text-align property:
You can also use the text-transform property to transform text into uppercase, lowercase, or capitalize the first letter of each word. The property has the following values: uppercase, lowercase, capitalize, none, and inherit.
Text Formatting
Text formatting is a crucial aspect of web design, and CSS provides a range of properties to control the look and feel of text. You can use the text-align property to align text and inline elements, with standard values including left, right, center, justify, and inherit.
The text-align property can also be used to define the text alignment in relation to the direction of the text. For example, the value "start" aligns the text to the side on which it begins, which means left for left-to-right text direction and right for right-to-left text direction.
You can use the text-transform property to carry out text transformations, such as displaying text in majuscules (uppercase letters) or minuscules (lowercase letters). The property enables transformations like capitalize, uppercase, lowercase, none, and inherit.
The hyphens property allows you to set up an automatic syllable separation, with available values including manual, none, auto, and inherit. The CSS property hyphens is not currently supported by all web browsers.
Here are the possible values for the text-align property:
You can use the word-spacing property to define the internal word spacing of a text element, with available values including normal, inherit, and specified sizes. The following code example defines a word spacing of 2 em.
The letter-spacing property is used to define the spacing of the letters, with available values including normal, inherit, and specified sizes. The following code example shows a text selection with a letter spacing of 1 em.
The text-indent property allows you to define indentations that are only applied to the first line of a paragraph, with possible values including positive and negative values, as well as percentages in relation to the width of the respective text blocks. The following code example defines an indentation for the first line of 5 percent.
The text-decoration property provides text elements with decorations, such as underlines, with possible values including none, underline, overline, line-through, and inherit. The following code example defines underlines for the selected word groups within the text selection.
Family
Georgia is defined as a prioritized font, with Garamond as the alternative font.
A font stack is a prioritized list of suitable fonts, built by naming the desired font first, followed by matching display alternatives.
This ensures that if the desired font isn't installed on the system, the web browser will display the text in the alternative font, in this case, Garamond.
Other fonts can be used as generic alternatives, such as Times New Roman, which is a serif font.
Sans-serif fonts, like Arial, Trebuchet, or Verdana, can also be used as generic alternatives.
Fonts whose names consist of several words should be placed in single or double quotes in the CSS declaration.
Size
Text formatting allows you to control the size of your text, making it easier to read and understand.
The size of your text can be adjusted using font sizes, which are typically measured in points (pt). A standard font size is 12pt, but you can choose from a range of sizes, including 10pt, 11pt, and 14pt.
A larger font size can be helpful for readers who have trouble seeing small text, such as older adults or people with visual impairments. In the "Typography" section, we saw that the font size of 14pt was used for headings to make them stand out.
Sources
- HTML - Style Sheet (tutorialspoint.com)
- » World Wide Web Consortium (w3.org)
- CSS 3 (w3.org)
- » CSS 2 (w3.org)
- » CSS 1 (w3.org)
- » Chrome (google.com)
- » W3C's Style page (w3.org)
- » AlertBox: Effective Use of StyleSheets (useit.com)
- » Style Master (westciv.com)
- » Cascading Style Sheets: The Definitive Guide (amazon.com)
- » CSS Pocket Reference (amazon.com)
- CSS Tutorial. (w3schools.com)
- Adobe Edge Web Fonts. (adobe.com)
- Google Fonts (google.com)
- w3schools.com (w3schools.com)
- HTML Color Picker (webfx.com)
- Subtle Patterns (toptal.com)
- patterncooler.com (patterncooler.com)
- w3schools.com (w3schools.com)
- selectors (w3.org)
- W3C (w3.org)
- Mozilla Developer Network (mozilla.org)
- wiki W3 (w3schools.com)
- What is CSS: Cascading Style Sheet Explained for Beginners (hostinger.com)
Featured Images: pexels.com