Scripting Languages_2
Scripting Languages_2
•The p is the selector, indicating that this rule applies to all <p> elements.
•color: purple; specifies that the text color should be purple.
•background-color: green; specifies that the background color should be green.
CSS allows us to select elements and apply styles inside the curly braces {}.
Connecting HTML and CSS
Inline styles, Embedded style sheets, and External style sheets.
Inline Styles
Inline styles introduce CSS directly to an HTML element using the style attribute. These styles are convenient when dynamic changes are needed – a realm where
JavaScript often wields its magic. However, the drawback is their limited scope; they can't be easily extended or reused acro ss different elements.
<tag style="...">Some content</tag>
Embedded Style Sheet
The embedded style sheet resides within an HTML document's <head>, encapsulated within <style> tags. This method allows us to tailor styles specifically to a single page.
However, it lacks the versatility needed to be shared across multiple pages.
<!DOCTYPE html>
<html lang="en">
<head>
<style>
p{
color: blueviolet;
font-size: 36px;
}
</style>
</head>
<body>
<p>We work with an embedded style sheet in the head of
HTML.</p>
</body>
</html>
External Style Sheet
The external style sheet emerges as the grand maestro of CSS management. By employing the <link> tag within the <head> of an HTML document, this technique offers
scalability, maintainability, and reusability – qualities coveted in web projects.
But how does it work? We create a file with the css extension and reference it in our HTML document. Below is a glimpse of both files, index.html and index.css.
<!DOCTYPE html>
<html lang="en">
<head> p{
<!-- We can add multiple links to CSS style sheets if needed. - color: purple;
-> }
<link rel="stylesheet" href="index.css" />
</head>
<body>
<p>We work with an external style sheet connected in the
head.</p>
</body>
</html>
The rel attribute stands for "relationship". rel="stylesheet" denotes how the index.css is related to the index.html.
Selectors for Styling HTML Elements
Tag selector
One way to apply styles is by using the element tag itself. Styles specified using a tag selector will affect all elements with that tag. This is useful for applying consistent
styling to elements across the website.
Syntax: In the HTML, we have a p element:
<p>It was all in my mind</p>
To apply styles in the CSS file, use the tag name ( p) as the selector:
p{
p{ color: green;
property: value; font-size: 12px;
} background-color: antiquewhite;
Let's run the following example and check how it works. }
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<p>It was all in my mind</p>
</body>
</html>
Class selector
A more precise way to style elements is by using class selectors. These selectors target elements with specific class names, allowing us to apply styles selectively.
Syntax: In the HTML, add a class attribute with a meaningful class name:
selector:pseudo-class {
property: value;
}
•selector - can be any selector we considered in the previous chapters;
•pseudo-class - needs : before its declaration, and we do not add any space;
Pseudo-class :hover
The :hover pseudo-class is used to respond to user actions when they interact with an item using a pointing device, such as hovering the mouse pointer over the item.
We can test this out in the following example by hovering over the button and a elements to see the effect.
<!DOCTYPE html> button {
<html lang="en"> }
background-color: lightgrey;
<head>
button:hover {
<link rel="stylesheet" href="index.css" /> background-color: royalblue;
</head> }
<body> a{
color: darkgreen;
<button>Hover me</button> }
<a href="#">Any link to</a>
a:hover {
</body> color: greenyellow;
}
</html>
Pseudo-class :focus
The :focus pseudo-class functions in a similar way to the :hover pseudo-class. It responds to element focus, which is typically achieved through keyboard navigation using
the "Tab" key. To ensure consistency in the behavior of interactive elements for all users, it's recommended to use both pseudo-classes together for styling. This way, users
who navigate with their mouse and those who navigate with the keyboard can receive the same experience.
The difference between :hover and :focus
•:hover reacts to mouse cursor activity;
•:focus reacts to the keyboard activity ("Tab" key);
In the following example, we have the same element with different pseudo-classes. Please pay attention to the index.css file. We can notice that we can add the same styles
for the hover and focus effect by separating the selector and pseudo-class with ,.
/* Styles for the first button */
.button-one {
background-color: green;
<!DOCTYPE html> }
</html>
.button-three:hover,
.button-three:focus {
background-color: yellow;
}
Pseudo-class :active
:active is triggered when an item is activated, for example, clicking on a link. Although any element can be activated, this pseudo -class is generally used for links and buttons.
<!DOCTYPE html>
<html lang="en">
<head>
.nav-link {
<link rel="stylesheet" href="index.css" />
color: darkblue;
</head>
}
<body>
<ul>
.nav-link:hover,
<li>
.nav-link:focus {
<a class="nav-link" href="https://www.google.com"
color: orange;
target="_blank">Google</a>
}
</li>
<li>
.nav-link:active {
<a class="nav-link" href="https://www.tesla.com/"
color: orangered;
target="_blank">Tesla</a>
}
</li>
<li>
<a class="nav-link" href="https://www.apple.com"
target="_blank">Apple</a>
</li>
</ul>
</body>
</html>
Pseudo-class :visited
:visited pseudo-class is applied to links that have been visited. By default, links are displayed in blue and changed to purple when visited.
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="index.css" /> .social-links .link {
</head> color: green;
<body> }
<ul class="social-links">
<li> .social-links .link:hover,
<a href="https://twitter.com" class="link" .social-links .link:focus {
target="_blank">Twitter</a> color: yellow;
</li> }
<li>
<a href="https://www.instagram.com" class="link" .social-links .link:visited {
target="_blank">Instagram</a> color: blue;
</li> }
<li>
<a href="https://www.facebook.com" class="link"
target="_blank">Facebook</a>
</li>
</ul>
</body>
</html>
Text Colors
<!DOCTYPE html>
<html lang="en">
.text-color-name {
<head>
color: red;
<link rel="stylesheet" href="index.css" />
}
</head>
<body>
.text-color-hex {
<ul class="social-links">
color: #ff0000;
<li>
}
<p class="text-color-name">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Doloremque, numquam.
.text-color-rgb {
</p>
color: rgb(255, 0, 0);
</li>
}
<li>
<p class="text-color-hex">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Minima, debitis.
</p>
</li>
<li>
<p class="text-color-rgb">
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nulla, porro!
</p>
</li>
</ul>
</body>
</html>
Background color
We can set the color of an element's background with the help of the background-color property.
<!DOCTYPE html>
<html lang="en">
<head> .text {
<link rel="stylesheet" href="index.css" /> background-color: darkolivegreen;
</head> color: antiquewhite;
<body> }
<p class="text">
Lorem ipsum dolor sit amet consectetur adipisicing elit.
Eveniet, inventore? Et, nemo dolores
explicabo quae eaque numquam perspiciatis non facere
repudiandae cum repellendus aut atque est
cumque cupiditate sapiente quidem.
</p>
</body>
</html>
Ways to specify color
There are three main ways: color name, hex, rgb.
Color name
We can use reserved color words. The list of reserved colors can be found at htmlcolorcodes.com
Hex format
Hex represents colors as a six-digit code consisting of three pairs of two-digit values. Each pair represents the intensity
of red, green, and blue (in that order).
Hex format values range from 00 (no intensity) to FF (maximum intensity).
RGB Format
RGB represents colors as a series of three numbers that correspond to the intensity of red, green, and blue (in that
order) RGB format values range from 0 (no intensity) to 255 (maximum intensity).
Effective Work with CSS
Variables
CSS variables, also known as CSS custom properties, provide a way to define reusable values throughout our CSS code. By utilizing variables, we can create dynamic and
flexible designs that are easier to maintain and update.
To declare a variable, we use the :root pseudo-class at the top of our CSS file and prefix the variable name with --. For example, let's define a --blue-color variable with the
value #3f42f3 in our :root block:
:root {
--blue-color: #3f42f3;
}
Once we have declared a variable, we can apply it to any CSS property using the var() function. For instance, to assign the color property of an <h1> element the value of
our --blue-color variable, we can write:
h1 {
color: var(--blue-color);
}
Let's combine the knowledge of variable declaration and usage in the following example:
<!DOCTYPE html> :root {
<html lang="en"> --background-color: rgba(23, 23, 23, 0.8); /* variable can be any value */
<head> --text-color: #d1d0d0;
}
<link rel="stylesheet" href="index.css" />
</head> .container {
<body> background-color: var(--background-color);
}
<div class="container">
<h3 class="title">About the company</h3> .title {
</div> color: var(--text-color);
</body> }
</html>
Overriding Properties
Sometimes, we need to override specific properties in CSS to achieve the desired styling. This can be accomplished by
declaring a property with the same name within a nested selector and assigning a new value.
Consider the following example of a traffic light with different color signals:
<!DOCTYPE html>
<html lang="en"> .signal {
<head> font-size: 18px;
<link rel="stylesheet" href="index.css" /> font-weight: 600;
</head> color: blue;
<body> }
<h4>Traffic Light</h4>
<ul> .stop {
<li> color: red;
<p class="signal stop">stop</p> }
</li>
<li> .slow-down {
<p class="signal slow-down">slow-down</p> color: yellow;
</li> }
<li>
<p class="signal proceed">proceed</p> .proceed {
</li> color: green;
</ul> }
</body>
</html>
Text Styling
text-decoration
The text-decoration property can be applied to any text content, including links, headings, paragraphs, and lists. It adds line effects such as underlining. Common values:
text-decoration: none | line-through | underline | overline
Let's run the following example and see the difference between all the property values.
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="index.css" />
.link {
</head> text-decoration: none;
<body> }
<ul>
<li> .text-lined-through {
<div class="container"> text-decoration: line-through;
<h3>none</h3> }
<a class="link">Links are underlined by default. We fix it</a>
</div>
.text-underlined {
</li>
<li>
text-decoration: underline;
<div class="container"> }
<h3>line-through</h3>
<p class="text-lined-through"> .text-over-lined {
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ut, sequi. text-decoration: overline;
</p> }
</div>
</li> /* This part is not the focus of the current chapter. We will consider it further. */
<li>
ul {
<div class="container">
<h3>underline</h3>
display: flex;
<p class="text-underlined"> justify-content: space-around;
Lorem ipsum dolor sit amet consectetur adipisicing elit. Reiciendis, list-style: none;
optio! padding-left: 0;
</p> }
</div>
</li> .container {
<li> width: 160px;
<div class="container">
border: 1px dashed darksalmon;
<h3>overline</h3>
<p class="text-over-lined">
}
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Commodi,
similique! h3 {
</p> text-align: center;
</div> }
</li>
</ul>
</body>
</html>
text-transform
The text-transform property allows us to change the capitalization of text content in HTML elements.
text-transform: capitalize | lowercase | uppercase | none
<!DOCTYPE html>
Let's consider the difference between all the property values:
<html lang="en">
<head> .capitalized-text {
<link rel="stylesheet" href="index.css" /> text-transform: capitalize;
</head> }
<body>
<ul>
<li> .uppercased-text {
<div class="container"> text-transform: uppercase;
<h3>capitalized</h3>
<p class="capitalized-text"> }
Lorem ipsum dolor sit amet consectetur adipisicing elit. Maxime,
ullam? .lowercased-text {
</p>
</div>
text-transform: lowercase;
</li> }
<li>
<div class="container">
<h3>uppercase</h3>
.not-modified-text {
<p class="uppercased-text"> text-transform: none;
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ut, sequi. }
</p>
</div>
</li> /* This part is not the focus of the current chapter. We will consider it further. */
<li> ul {
<div class="container"> display: flex;
<h3>lowercase</h3>
<p class="lowercased-text"> justify-content: space-around;
Lorem ipsum dolor sit amet consectetur adipisicing elit. Reiciendis, list-style: none;
optio! padding-left: 0;
</p>
</div> }
</li>
<li> .container {
<div class="container">
<h3>none</h3> width: 160px;
<p class="not-modified-text"> border: 1px dashed darksalmon;
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Commodi, }
similique!
</p>
</div> h3 {
</li> text-align: center;
</ul>
}
</body>
</html>
text-align
The text-align property allows you to horizontally align text content.
text-align: start | end | center | justify
<!DOCTYPE html> h3 {
<html lang="en"> text-align: center;
<head> }
<link rel="stylesheet" href="index.css" />
</head>
<body>
.text-start {
<ul> text-align: start;
<li> }
<div class="container">
<h3>start</h3> .text-end {
<p class="text-start">Lorem, ipsum.</p>
</div>
text-align: end;
</li> }
<li>
<div class="container"> .text-center {
<h3>end</h3> text-align: center;
<p class="text-end">Lorem, ipsum.</p>
</div>
}
</li>
<li> .text-justify {
<div class="container"> text-align: justify;
<h3>center</h3> }
<p class="text-center">Lorem, ipsum.</p>
</div>
</li> /* This part is not the focus of the current chapter. We will consider it further. */
<li> ul {
<div class="container"> display: flex;
<h3>justify</h3> justify-content: space-around;
<p class="text-justify">Lorem, ipsum.</p>
<p class="text-justify">
list-style: none;
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Sequi, padding-left: 0;
voluptates. }
</p>
</div> .container {
</li>
</ul>
width: 160px;
</body> border: 1px dashed darksalmon;
</html> }
Text Formatting
line-height
The line-height property defines the height of a line and is often used to adjust the space between lines of text. By default, line-height depends on the text font and is
determined by the browser.
<!DOCTYPE html> p{
<html lang="en"> font-family: 'DM Sans';
<head> font-size: 23px;
<link rel="stylesheet" href="index.css" /> font-weight: 400;
</head> }
<body>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Modi
rerum deserunt, tenetur deleniti
minus praesentium laudantium accusantium eum similique eos
laboriosam ipsam, sint unde
obcaecati totam. Consequuntur, esse. Quam, asperiores.
</p>
</body>
</html>
Font Properties
Let's consider the most common font-related properties.
selector {
font-family: 'Montserrat' | 'Oswald' | 'Lato' |...; /* any font family */
font-size: 12px | 0.5em | 0.8rem |...; /* it can ne any value in px/em/rem units */
font-weight: 400 | 500 | 700 | 900 |...; /* value 100 - 900 */
font-style: normal | italic | oblique; /* only these values available */
}
font-family
The font-family property defines the font family or families that will be used for displaying text content. It is possible to specify a single font family name or a list of multiple
font family names separated by commas.
font-size
The font-size property sets the font size for displaying text content. It can be a fixed size measured in px or a relative size measured in em or rem units.
font-weight
The font-weight property determines the weight or thickness of the font used to display text content. It can be specified as a numerical value (e.g., 300, 500, 700, ...) or as a
keyword (e.g., lighter, normal, or bold).
font-style
The font-style property specifies the style of the font to be used for text content. This can be normal (the default value), italic, or oblique.
Pseudo-Class first-letter
The first-letter pseudo-class selects and styles the first letter of a paragraph or heading. To use this pseudo-class, we need to put double :: after a selector and add the
keyword first-letter.
selector::first-letter {
/* some styles */
}
Let's run the next example to see how it works.
.description {
<!DOCTYPE html> font-size: 14px;
<html lang="en"> font-weight: 400;
<head> }
<link rel="stylesheet" href="index.css" />
</head> .description::first-letter {
<body> font-size: 22px;
<p class="description"> font-weight: 700;
The sun rose slowly over the mountains, casting a warm font-style: italic;
glow over the sleepy town below. }
</p>
</body>
</html>