Cours3 CSS
Cours3 CSS
Cours3 CSS
As we said, this chapter we are going to be interested to styling, and give some good look to our
web page. The language responsible of styling is CSS.
Well in this chapter we are going to learn:
What’s CSS?
How to add css into html?
How to select an html element?
How to change the position an element?
CSS Selector:
After attaching the css style, the next step is how to attach the wanted style to the proper
element?
If we want to change the style of a particular element in our HTML page, We have to select that
element.
To do so, CSS provides a set of rules, each of which consists of a selector (To indicate which
elements, you are trying to modify) , followed by a declaration block that contains a set of
properties and those properties values.
Type Selector:
In the previous slides, we learned how to pick an HTML element to apply style on it and the types
of CSS selectors.
Now, we will see, the first type of selector and use it in a real example.
Type:
Using a type selector is as simple as typing the name of the element, but when we should this
type. This kind of selectors is used when we want to apply the same rule on every element in the
page.
For example, we want every h1 title to be in red, the font will be roboto and the text-
align will be in the center
HTML
<!DOCTYPE html>
<html lang="">
<head>
<title>Type selector</title>
</head>
<body>
<h1>Welcome to my blog</h1>
<p>
repellat vitae.
</p>
</body>
</html>
CSS
h1 {
color: red;
font-family: roboto;
text-align: center;
Output:
Descendant Combinator:
The descendant combinator typically represented by a single space character, combines two
selectors such that elements matched by the second selector are selected if they have an
ancestor (parent, parent's parent, parent's parent's parent, etc.) element matching the first
selector. Selectors that utilize a descendant combinator are called descendant selectors.
html
CSS
.box p {
color: red;
Output
CSS pseudo classes:
Did you ever try to put your cursor on a button and its color changes? Or did you ever click on a
link and its color changes (marked as clicked)?
Well, it’s all done with CSS pseudo-classes.
As you can see, the structure is clear. You name your SELECTOR, add : then your action.
Here are some awesome actions you can do. Try them on your editor.
/* unvisited link */
a:link {
color: tomato;
/* visited link */
a:visited {
color: tomato;
div:hover {
color: tomato;
/* selected link */
a:active{
color: #0000FF
}
Content: The content of the box, where text and images appear
Padding: The padding is transparent, Clears an area around the content.
Border: A border that goes around the padding and content. We already have seen it
earlier.
Margin: Clear an area outside the border. The margin is transparent.
Also, we can determine the dimension of any html element through the width and the height
Font styling:
To style the font, css gives us a large set of properties, we are going to see some of them:
Font-family
It helps us change the font family of html element, we set one or more font for this propertie.
Color
It changes the text color, it acccept named color, hexadecimal value and rgb value.
/* same as
color: blue;
color: #0000ff;
color: #00f;
Font-style
It's used to turn italic text on and off. Possible values are as follows (you'll rarely use this, unless
you want to turn some italic styling off for some reason).
font-style: bold;
/*
font-style:normal;
font-style:italic;
font-style:oblique;
*/
Font-weight
It sets how bold the text is. This has many values available in case you have many font variants
available (such as -light, -normal, -bold, -extrabold, -black, etc.), but realistically you'll rarely use
any of them except for normal and bold.
font-weight: normal;
/*
font-weight:bold;
font-weight:bolder;
font-weight:lighter;
font-weight:100-900
*/
Text-decoration
It sets or unsets text decorations on fonts (you'll mainly use this to unset the default underline on
links when styling them.) Available values are:
text-decoration: none;
/*
text-decoration:underline;
text-decoration:overline;
text-decoration:line-through;
*/
Css display:
Every element on a web page is a rectangular box. The display property in CSS determines just
how that rectangular box behaves.
Display:none
We can hide elements by declaring a display: none value. Another way is to declare visibility:
hidden instead of display: none, but there is a difference between them.
#box-2 {
display: none;
width: 100px;
height: 100px;
background: blue;
#box-2 {
width: 100px;
height: 100px;
background: blue;
visibility: hidden;
Display:inline
Like the name indicate, this feature enables you to make the specified HTML elements in the
same line. Let’s understand it with an example.
<p>One</p>
<p>Two</p>
<p>Three</p>
p {
display: inline;
Display:block
HTML elements are divided into default block elements ( automatically get back to line
without the use of <br> ) and inline elements (need <br> to get back to line).
Block-level elements:
div{
display: block;
p {
height: 100px;
width: 100px;
background: red;
color: white;
Display:inline-block
In some cases, both of the display values may not be enough for better web design. At that point,
a third display behavior comes to the rescue and also makes alignment much easier: display:
inline-block.
As we can understand from its name, display: inline-block declaration shows both the
characteristics of inline and block-level elements.
In other words, we can think of an inline element, that width & height properties can be set, or we
can think of a block-level element, that doesn’t have to start with a new line.
<div class="test"></div>
<div class="test"></div>
.test {
width: 50px;
height: 50px;
display: inline-block;
CSS Conclusion:
We have been learning many useful st about how to style a web page. Now, we can change the
style of our text content, change it position and manipulate the elements layout.
CSS is the language for describing the presentation of Web pages, including colors, layout, and
fonts.
It allows one to adapt the presentation to different types of devices, such as large screens, small
screens, or printers.
It's independent of HTML and can be used with any XML-based markup language.
The separation of HTML from CSS makes it easier to maintain sites, share style sheets across
pages, and tailor pages to different environments. This is referred to as the separation of
structure (or: content) from presentation.
OBJECTIVES
Remember that portfolio we have create in the last checkpoint, now it’s time to give it
some style.
INSTRUCTIONS
Hint: You could use the Internet to search for some portfolio model.
https://drive.google.com/file/d/1W8n_KuZsacyvRUmh89tOfetDL2N1-TVw/view