Scripting Languages_3
Scripting Languages_3
•The content area is the space where the actual content of the element resides. Its width depends on the element type (block-level or inline - we will consider the
difference between them and their specificity to work with further in the course). Its height depends on the content itself.
•The padding is the area between the content and the border, which can be used to add space between the content and the border.
•The border surrounds the element. By default, the border width is 0. Also, if we do not specify the border color, the border takes the color of the content.
•The margin is the space between the border and other elements on the page.
<!DOCTYPE html> .text {
<html lang="en"> border: 3px solid royalblue;
<head> padding: 20px;
<link rel="stylesheet" href="index.css" /> margin: 30px;
</head> }
<body>
<p class="text">
A wonderful serenity has taken possession of my entire
soul, like these sweet mornings of
spring which I enjoy with my whole heart. I am alone, and
feel the charm of existence in this
spot, which was created for the bliss of souls like mine. I am
so happy, my dear friend, so
absorbed in the exquisite sense of mere tranquil existence.
</p>
</body>
</html>
width and height properties
width and height properties are used to define the size of an element. The width and height of an element can vary depending on the type of bo x model.
.box {
<!DOCTYPE html> box-sizing: content-box;
<html lang="en"> margin: 30px;
<head> border: 5px solid royalblue;
<link rel="stylesheet" href="index.css" /> padding: 20px;
</head> width: 200px;
<body> height: 40px;
<div class="box"></div> text-align: center;
</body> }
</html>
We observe that when we specify box-sizing: content-box;, the width property is 200px, and the height property is 40px, indicating that the content area will be exactly 200px
wide and 40px tall.
border-box
•When an element is set to border-box, its size calculation includes the padding and border in total size.
•This means that if we set an element's width and height properties with box-sizing: border-box, the specified width and height values include any padding and border.
•In this case, the content area will adjust to accommodate the specified width and height, including the padding and border.
•This is often used to ensure that an element's overall size remains consistent, regardless of the padding and border.
in this example, the element's width and height will be 200px and 60px, respectively, and these dimensions include the padding and border. So, the content area will be
adjusted to fit the remaining space.
inherit
•The inherit value allows an element's box-sizing property to inherit its value from its parent element.
•This means that the box-sizing behavior of the current element is determined by the box-sizing property of its parent element.
•It can help ensure a consistent box-sizing behavior throughout a document by setting it on a parent container.
Add Space to Elements
The margin, padding, and border properties help us add more space to elements, improving the web resource's appearance, readability, and usability. Let's take a closer look
at each of them.
padding property
padding refers to the space between an element's content and its border.
margin property
margin refers to the space between an element and the adjacent elements. It is the area outside the element's border.
Shorthand
border: 4px solid brown;
It means that the border of all sides will look the same. Their width is 4px, style is solid, and color is brown.
.box-one {
<!DOCTYPE html> border: 2px solid brown;
<html lang="en"> }
<head> .box-two {
<link rel="stylesheet" href="index.css" /> border: 2px dotted brown;
</head> }
<body> .box-three {
<div class="box-one">solid</div> border: 2px dashed brown;
<div class="box-two">dotted</div> }
<div class="box-three">dashed</div> .box-four {
<div class="box-four">double</div> border: 2px double brown;
</body> }
</html>
/* This part is not the focus of the current chapter. */
body {
display: flex;
justify-content: center;
}
div {
width: 120px;
height: 120px;
margin: 5px;
text-align: center;
}
Each Border Separately.
<!DOCTYPE html> /* Set the the font size for all <li> elements */
<html lang="en"> .item {
<head> font-size: 12px;
<link rel="stylesheet" href="index.css" /> }
</head>
<body> /* Set the font size for the last element */
<ul> .item:last-child {
<li class="item">Lorem ipsum dolor sit amet.</li> font-size: 22px;
<li class="item">Lorem ipsum dolor sit.</li> }
<li class="item">Lorem, ipsum dolor.</li>
</ul>
<ul>
<li class="item">Lorem ipsum dolor sit amet.</li>
<li class="item">Lorem ipsum dolor sit.</li>
<li class="item">Lorem, ipsum dolor.</li>
</ul>
</body>
</html>
nth-child pseudo-class
The :nth-child pseudo-class targets an element based on its position in the hierarchy. It takes an argument in the form of an expression, usually in the form of an+b,
which helps determine the specific child elements to select. Let's break down the components of this expression:
•a represents the loop period.
•n is the loop counter, starting at 0 and increasing by 1 with each iteration.
•b is the offset, influencing the starting point of the selection.
/* Set the font weight for all <li> elements */
<!DOCTYPE html> .item {
<html lang="en"> font-weight: 400;
<head> }
<link rel="stylesheet" href="index.css" />
</head> /* Set the font weight for the third element */
<body> .item:nth-child(3) {
<ul> font-weight: 900;
<li class="item">Lorem ipsum dolor sit amet.</li> }
<li class="item">Lorem, ipsum.</li>
<li class="item">Lorem ipsum dolor sit.</li>
<li class="item">Lorem, ipsum dolor.</li>
</ul>
</body>
</html>
not() pseudo-class
The :not() pseudo-class targets elements that do not match a specified selector. For instance, :not(p) would select all elements except for <p> elements. Let's explore
some examples:
/* Set color for all elements except the last element */
<!DOCTYPE html>
li:not(:last-child) {
<html lang="en">
color: pink;
<head>
}
<link rel="stylesheet" href="index.css" />
</head>
/* Set background color for all elements except the first
<body>
element */
<ul>
li:not(:first-child) {
<li>Lorem, ipsum.</li>
background-color: purple;
<li>Lorem ipsum dolor sit.</li>
}
<li>Lorem, ipsum dolor.</li>
<li>Lorem ipsum dolor sit amet.</li>
<li>Lorem.</li>
</ul>
</body>
</html>
Element Types
•inline elements typically do not start on a new line and only take up as much width as necessary. They allow other
elements to sit beside them on the same line.
•Block elements, on the other hand, typically start on a new line and extend the full width of their container. They
create a new "block" or "box" for content.
Block elements
Block elements occupy the entire width of their parent container and typically begin on a new line. Examples of block elements encompass <div>, <h1> through
<h6>, <p>, <ul>, <ol>, <li>, and various others. These elements can have their width, height, margin, padding, and border properties customized using CSS.
<!DOCTYPE html> h4 {
<html lang="en">
<head>
padding: 12px;
<link rel="stylesheet" href="index.css" /> }
</head>
<body> p{
<ol>
<li><p>Lorem ipsum dolor sit amet.</p></li> border: 1px solid tan;
<li><p>Lorem ipsum</p></li> }
<li><p>Lorem ipsum dolor sit amet.</p></li>
</ol>
<ul>
<li><h4>Lorem, ipsum.</h4></li>
<li><h4>Lorem, ipsum dolor.</h4></li>
<li><h4>Lorem, ipsum dolor.</h4></li>
</ul>
</body>
</html>
Inline elements
Inline elements do not initiate a new line and only occupy the width essential for their content. Examples of inline elements encompass <a>, <span>, <img>, <input>, and
several others. Unlike block elements, inline elements cannot be assigned specific values for height, width, margin, or padding using CSS. However, it's worth noting that border
can be applied to inline elements.
The flexbox layout model operates on a parent-child relationship. The parent, referred to as the flex container, is
responsible for dictating the layout of its child elements, known as flex items. By leveraging a range of flexbox
properties, we can precisely control the size, position, and order of flex items within the flex container.
a{
text-decoration: none;
}
column <!DOCTYPE html> .navigation-list {
<html lang="en"> background-color: honeydew;
<head> display: flex;
<link rel="stylesheet" href="index.css" /> flex-direction: column;
</head> padding: 10px;
<body> }
<ul class="navigation-list">
<li class="navigation-item"> .navigation-item {
<a href="#" class="navigation-link">Home</a> padding: 10px;
</li> border-radius: 6px;
<li class="navigation-item"> background-color: violet;
<a href="#" class="navigation-link">About</a> }
</li>
<li class="navigation-item"> .navigation-item:not(:last-child) {
<a href="#" class="navigation-link">Price</a> margin-bottom: 20px;
</li> }
<li class="navigation-item">
<a href="#" class="navigation-link">Team</a> .navigation-link {
</li> color: wheat;
<li class="navigation-item"> }
<a href="#" class="navigation-link">Support</a>
</li> /* Remove default decoration */
</ul> ul {
</body> list-style: none;
</html> }
a{
text-decoration: none;
}
column-reverse .navigation-list {
background-color: honeydew;
<!DOCTYPE html>
display: flex;
<html lang="en">
<head> flex-direction: column-reverse;
<link rel="stylesheet" href="index.css" /> padding: 10px;
</head> }
<body>
<ul class="navigation-list"> .navigation-item {
<li class="navigation-item">
padding: 10px;
<a href="#" class="navigation-link">Home</a>
</li> border-radius: 6px;
<li class="navigation-item"> background-color: violet;
<a href="#" class="navigation-link">About</a> }
</li>
<li class="navigation-item"> .navigation-item:not(:last-child) {
<a href="#" class="navigation-link">Price</a>
margin-top: 20px;
</li>
<li class="navigation-item"> }
<a href="#" class="navigation-link">Team</a>
</li> .navigation-link {
<li class="navigation-item"> color: wheat;
<a href="#" class="navigation-link">Support</a> }
</li>
</ul>
</body> /* Remove default decoration */
</html> ul {
list-style: none;
}
a{
text-decoration: none;
}
Justify Content Horizontally
The justify-content property is instrumental in determining the position of flex items along the main axis. Its default value is flex-start.
flex-basis
The flex-basis property defines the initial size of a flex item before any remaining space is distributed. It specifies the ideal size of a flex item, which may be
div {
background-color property
background-color: limegreen;
color: #1e1e1e;
background-color: value;
}
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<div>
The sun was setting over the horizon, casting a warm glow
over the trees.
Birds chirped happily in the distance, while a gentle breeze
rustled the
leaves. It was a peaceful evening, and for a moment,
everything felt right
in the world.
</div>
</body>
</html>
static vs gradient color
Static color refers to a single color value that is applied uniformly to an element, such as red or #0000FF . On the other hand, gradient color involves blending two or
more colors together to create a smooth transition between them.
.container {
<!DOCTYPE html> display: flex;
<html lang="en"> justify-content: space-around;
<head> }
<link rel="stylesheet" href="index.css" />
</head> .static-color {
<body> width: 150px;
<div class="container"> height: 150px;
<div class="static-color"></div> background-color: red;
<div class="gradient-color"></div> }
</div>
</body> .gradient-color {
</html> width: 150px;
height: 150px;
background-image: linear-gradient(to right, #3a6186,
#89253e);
}
linear gradient :root {
--starting-color: rgb(204, 43, 94);
--ending-color: rgb(117, 58, 136);
}
background-image: linear-gradient("direction", "color1", "color2", ...);
.container {
display: flex;
<!DOCTYPE html> flex-wrap: wrap;
<html lang="en"> }
justify-content: space-around;
<head>
.box {
<link rel="stylesheet" href="index.css" /> margin: 10px;
width: 240px;
</head> height: 240px;
<body> /* put the box text in the center of the box */
display: flex;
<div class="container"> align-items: center;
justify-content: center;
<div class="box">to bottom</div>
<div class="box">to right</div> color: white;
font-size: 24px;
<div class="box">to top</div> font-weight: 500;
}
<div class="box">to left</div>
.box:nth-child(1) {
</div> background-image: linear-gradient(
</body> to bottom,
var(--starting-color),
</html> var(--ending-color)
);
}
.box:nth-child(2) {
background-image: linear-gradient(
to right,
var(--starting-color),
var(--ending-color)
);
}
.box:nth-child(3) {
Background Image .box {
background-image: url("image-url.jpg"); width: 300px;
height: 140px;
<!DOCTYPE html> background-image: url("https://codefinity-content-
<html lang="en"> media.s3.eu-west-1.amazonaws.com/code-1/background-first-
<head> example.jpg");
<link rel="stylesheet" href="index.css" /> }
</head>
<body>
<div class="box">Lorem ipsum dolor sit amet consectetur
adipisicing elit. Modi, aliquam.</div>
</body>
</html>
background-repeat
background-repeat: repeat | repeat-x | repeat-y | no-repeat; body {
display: flex;
<!DOCTYPE html> flex-wrap: wrap;
justify-content: space-around;
<html lang="en"> }
<head> .box {
margin: 10px;
<link rel="stylesheet" href="index.css" /> width: 140px;
height: 140px;
</head> border: 1px solid darkorchid;
background-image: url("https://codefinity-content-media.s3.eu-west-1.amazonaws.com/code-1/background-repeeat-
<body> example.webp");
.box:nth-child(2) {
background-repeat: repeat;
}
.box:nth-child(3) {
background-repeat: repeat-x;
}
.box:nth-child(4) {
background-repeat: repeat-y;
}
background-position
body {
display: flex;
background-position: x y flex-wrap: wrap;
justify-content: space-around;
}
<!DOCTYPE html>
<html lang="en"> .box {
margin: 10px;
<head> width: 140px;
<link rel="stylesheet" href="index.css" /> height: 140px;
border: 1px solid darkorchid;
</head> background-image: url("https://codefinity-content-media.s3.eu-west-
<body> 1.amazonaws.com/code-1/background-position-example.webp");
background-repeat: no-repeat;
<div class="box"><span>in the center</span></div> display: flex;
<div class="box"><span>on the right top</span></div> justify-content: center;
align-items: center;
<div class="box"><span>on the left bottom</span></div> }
<div class="box"><span>using px</span></div>
span {
</body> padding: 5px;
</html> font-size: 18px;
background-color: rgba(145, 165, 182, 0.45);
}
.box:nth-child(1) {
background-position: center;
}
.box:nth-child(2) {
background-position: right top;
}
.box:nth-child(3) {
background-position: left bottom;
background-size
background-size: auto | cover | contain | value in px/em/rem;
•auto - default value, which sets the background image to its original size.
•cover - scales the background image to completely cover the element while maintaining its aspect ratio. This may cause some parts of the image to be cropped if
the aspect ratio of the element and the image are different.
•contain - scales the background image to fit the entire element while maintaining its aspect ratio. This may cause empty spaces to be visible around the image if
the aspect ratio of the element and the image are different.
•value - sets the width and height of the background image. body {
display: flex;
flex-wrap: wrap;
<!DOCTYPE html> justify-content: space-around;
<html lang="en"> }
<head> .box {
width: 160px;
<link rel="stylesheet" href="index.css" /> height: 140px;
</head> border: 1px solid darkorchid;
background-image: url("https://codefinity-content-media.s3.eu-west-1.amazonaws.com/code-
<body> 1/background+size+image+example.png");
background-repeat: no-repeat;
<div class="box"><span>auto</span></div> display: flex;
<div class="box"><span>cover</span></div> justify-content: center;
align-items: center;
<div class="box"><span>contain</span></div> }
<div class="box"><span>value in px</span></div> span {
</body> padding: 5px;
font-size: 28px;
</html> background-color: rgba(145, 165, 182, 0.45);
}
.box:nth-child(1) {
background-size: auto;
}
.box:nth-child(2) {
background-size: cover;
Work with Images
object-fit
object-fit: fill | contain | cover | none | scale-down;
•fill stretches the image to fill the container, regardless of its aspect ratio. This may cause the image to be cropped or distorte d.
•contain scales the image to fit the container while preserving its aspect ratio. This may result in empty space around the image if the container and the image have
different aspect ratios.
•cover scales the image to completely cover the container while preserving its aspect ratio.
•none displays the image at its original size, regardless of the size of the container. This may cause the image to overflow the co ntainer.
•scale-down scales the image down to fit the container if it is larger than the image's natural size, or displays the image at its natura l size if it fits within the container.
Decorative Pseudo-Elements
Pseudo-elements ::before and ::after are special selectors that allow us to insert content before or after an element's content. We use them to add decorative ele ments,
such as icons or borders, to an element's content without modifying the actual HTML content.
.list {
•::before add the content before the element content. margin-top: 0;
•::after adds the content after the element content. margin-bottom: 0;
padding-left: 0;
list-style: none;
<!DOCTYPE html>
display: flex;
<html lang="en"> justify-content: space-between;
<head> background-color: darkcyan;
<link rel="stylesheet" href="index.css" /> padding: 5px;
border-radius: 4px;
</head> }
<body>
.item {
<ul class="list"> padding: 5px 10px;
background-color: antiquewhite;
<li class="item"><a href="#" class="link">contacts</a></li> border-radius: 4px;
<li class="item"><a href="#" class="link">details</a></li> margin: 5px;
}
<li class="item"><a href="#" class="link">price</a></li>
.link {
<li class="item"><a href="#" class="link">faq</a></li> display: flex;
</ul> align-items: center;
text-decoration: none;
</body> color: black;
</html> }
font-size: 36px;
div {
width: calc(100% / 2 - 20px);
padding: 10px;
border-radius: 4px;
}
div:first-child {
margin-right: 20px;
}
div:nth-child(2) {
box-shadow: 14px -8px 20px 1px rgba(0, 0, 0, 0.75);
}
p{
text-align: center;
}