Ephrem Tesfaye IP I Chapter Three Note & Questions
Ephrem Tesfaye IP I Chapter Three Note & Questions
Topic Contents
3. CSS Basics
3.1 Cascading Style Sheet and Its Advantages
3.2 Types of Cascading Style Sheet and CSS – Inclusion
3.3 Types of Selectors
3.4 CSS Units
3.5 CSS Properties and values
3.6 CSS Layout
Objectives
After studying this chapter, you should be able to:
Discuss about the basic concepts of CSS and types of CSS
List and describe selector types
Describe properties and values of CSS
Design an attractive layout using CSS
Style every HTML element in a way that most websites are styled
If you want to know more about the differences between versions, refer to this link
https://hackr.io/blog/difference-between-css-css2-and-css3
without showing them pictures or videos, just written instruction. Let's say the person rearranging
your house only speaks English. You don't have to know or use every single English word to
effectively communicate with this person. You just use the words you know effectively. In this
case, you are directly communicating with the browser. Same concept when using CSS to instruct
the browser to style our HTML. You just use the CSS rules you know to instruct the browser.
A CSS rule consists of a selector and a declaration block.
CSS Syntax
Out
put
Out
put
Mystyle.css
Cascading Order
What style will be used when there is more than one style specified for an HTML element?
All the styles in a page will "cascade" into a new "virtual" style sheet by the following rules, where
number one has the highest priority:
1. Inline style (inside an HTML element)
2. External and internal style sheets (in the head section)
3. Browser default
So, an inline style has the highest priority, and will override external and internal styles and
browser defaults.
.className {
/* Instruction goes in here */
}
ID selector
This is a way of selecting an HTML element using their id. We use the hashtag (#) identifier for
that
#id {
/* Instruction goes in here */
}
Type selector (element type selector)
A type selector (sometimes referred to as an element type selector) matches elements with the
corresponding element name, such as <p>, <span>, and <div> tags. Type selectors are generally
used to make “broad stroke” changes to the style of a site.
Example, if we want all the links on our website to be of color red, we use the following selection
and styling
a{
color: red;
}
The star/wild/universal selector
The * selector selects all elements. This is when we want to apply a certain instruction on every
single HTML element. Example, if we don't want any of the HTML elements to have a margin
(margin is just the space outside of an element's boarder), we use the * selector to make the margin
bordering ALL elements 0 like below:
*{
margin: 0;
}
Descendant selector
The next most common selector is the descendant selector. Just use space between the first and
second element. When you need to be more specific with your selectors, you use these. For
example, what if, rather than targeting all anchor tags, you only need to target the anchors which
are within an unordered list? This is specifically when you'd use a descendant selector.
li a {
text-decoration: none;
}
Adjacent selector
This is referred to as an adjacent selector. It will select only the element that is immediately
preceded by the former element. In this case, only the first paragraph after each ul will have red
text.
ul + p {
color: red;
}
Child Selector
The difference between the standard descendant selector (X Y) and child selector (X > Y) is that
the latter will only select direct children.
#container > ul {
border: 1px solid black;
}
Attribute Selectors
X[title]
This will only select the anchor tags that have a title attribute. Anchor tags which do not will not
receive this particular styling.
a[title] {
color: green;
}
X[href="foo"]
The snippet below will style all anchor tags which link to https://google.com; they'll receive our
branded green color. All other anchor tags will remain unaffected.
a[href="https://google.com"] {
color: #83b348; /* Envato green */}
X[href*="foo"]
The star designates that the proceeding value must appear somewhere in the attribute's value. That
way, style on the next example covers unitypark.et, unity.com, and even uniquesite.com
a[href*="uni"] {
color: #83b348; /* Envato green */
}
X[href^="http"]
When you need to be more specific, use ^ and $, to reference the beginning and end of a string,
respectively.
a[href^="http"] {
background: url(path/to/external/icon.png) no-repeat;
padding-left: 10px;
}
Ever wonder how some websites are able to display a little icon next to the links which are
external? I'm sure you've seen these before; they're nice reminders that the link will direct you to
an entirely different website.
This is a cinch with the carat symbol. It's most commonly used in regular expressions to designate
the beginning of a string. If we want to target all anchor tags that have an href which begins with
http, we could use a selector similar to the snippet shown above.
a[href$=".jpg"] {
color: red;
}
Again, we use a regular expressions symbol, $, to refer to the end of a string. In this case, we're
searching for all anchors which link to an image—or at least a URL that ends with .jpg. Keep in
mind that this won't capture GIF and PNG images.
Pseudo Selectors – Pseudo Classes
X:visited and X:link
We use the :link pseudo-class to target all anchor tags which have yet to be clicked on.
Alternatively, we also have the :visited pseudo class, which, as you'd expect, allows us to apply
specific styling to only the anchor tags on the page which have been clicked on, or "visited".
p::first-line {
font-weight: bold;
font-size: 1.2em;
}
p::first-letter {
font-size: 2em;
font-weight: bold;
font-family: cursive;
padding-right: 2px;
}
p::first-line {
font-weight: bold;
font-size: 1.2em;
}
Background
The background property is used to set the background of an HTML element. Please note that, the
background of an element can be an image or color. The background property can be a shorthand
property for the following sub-properties:
background-color (specifies the background color to be used)
background-image (specifies ONE or MORE background images to be used)
background-position (specifies the position of the background images)
background-size (specifies the size of the background images)
Example on how to set different background properties in one declaration
body {
background: green url("img_tree.gif") center cover;
}
Display property
The display CSS property determines how an element is going to be displayed on a browser. The
property sets whether an element is treated as a block or inline element. By default, the block mode
is assumed. Below are the most common values for display property:
block (this causes an inline element to act like block-level element. It makes an inline
element start on a new line and take up take up as much horizontal space as it can
Example: span {display: block} makes all spans to start on a new line and take up take up
as much horizontal space as they can
inline (this causes a block-level element to act like an inline element)
Example: p{display: inline} causes all your paragraphs to display on the same line
inline-block (this causes the block-level element to act like an inline element, but inline-
block allows for additional width and height properties to be applied to the element)
none (this causes the element to be completely removed)
Flex (a flex container expands items under it to fill available free space or shrinks them to
prevent overflow. The width of flex items automatically adjusts to fit inside the container.)
Position property
The position CSS property sets how an element is positioned in a document. Elements are then
positioned using the top, bottom, left, and right properties. Note: top, bottom, left and right
properties will not work unless the position property is first set. The most common types of
positioning in CSS are:
static: this is the default value, meaning, is not positioned in any special way; it is always
positioned according to the normal flow of the page.
fixed: the element is positioned related to the to the viewport or browser window. This
means that, it always stays in the same place even if the page is scrolled. The top, right,
bottom, and left properties are used to position the element.
relative: the element is positioned relative to its normal position. Setting the top, right,
bottom, and left properties of a relatively positioned element will cause it to be adjusted
away from its normal position
absolute: positioned relative to the nearest positioned ancestor (instead of positioned
relative to the viewport, like fixed position)
Width and height properties are used closely with display:block and display:inline to set the width
and height of HTML elements while creating a website. Common units units for Width and Height
are:
px - Pixels.
rem - Root em. Same measurement as em, but makes life much easier without the inheritance
problem
% - Percentages.
max-width vs width
max-width: it sets the maximum width an element can have. When the width of the browser
window/viewport is smaller than the specified width of an element, the browser adds a horizontal
scroll bar to allow us to view the element by scrolling to the side. In such cases, it is better to use
max-width to set the width of the element not to be above the width of the browser/viewport.
width: if an element has a width less than the browser’s/viewport’s width, the width property will
be used as opposed to the max-width property.
Font
The CSS font property is used to change the face of the font of a text. The font property can be a
shorthand property for the following sub-properties:
font-style: the font-style CSS property sets whether a font should be styled with a normal,
italic, or oblique face from its font-family
font-weight: the font-weight CSS property sets the weight (or boldness) of the font. The
weights available depend on the font-family that is currently set.
font-size: the font-size CSS property sets the size of the font. Changing the font size also
updates the sizes of the font size-relative <length> units, such as em, ex, and so forth.
font-family: the font-family CSS descriptor allows authors to specify the font family for
the font specified in an @font-face rule.
The @font-face: the CSS at-rule specifies a custom font with which to display text; the
font can be loaded from either a remote server or a locally installed font on the user's own
computer. See the example below:
@font-face {
url("/fonts/OpenSans-Regular-webfont.woff") format("woff");
Text
The text-transform property is used to change the case of text giving it one of the followingvalues:
The text-align property allows you to control the alignment of text. The property can take one of
four values:
Border
The border-width property is used to control the width of a border. The value of this property can
either be given in pixels or using one of the following values:
thin
medium
thick
pixels
You can control the style of a border using the border-style property. This property can take the
following values:
The padding property allows you to specify how much space should appear between the content
of an element and its border.
You can specify different values for each side of a box using:
padding-top
padding-right
padding-bottom
padding-left
Or you can use a shorthand (where the values are in clockwise order: top, right, bottom, left):
padding: 10px 5px 3px 1px;
The margin property controls the gap between boxes. Its value is commonly given in pixels. You
can specify values for each side of a box using:
margin-top
margin-right
margin-bottom
margin-left
You can also use the shorthand (where the values are in clockwise order: top, right, bottom, left):
Sometimes you might see the following, which means that the left and right margins should be 10
pixels and the top and bottom margins should be 20 pixels:
Floats are the primary tool for creating columns on web pages.
The strategy
Set widths on both column elements and float them to the left. Clear the footer to keep it at the
bottom of the page. The underlying structure and resulting layout is shown in the next Figure.
The markup
The styles
#main {
float: left;
width: 60%/650px;
margin: 0 5%;
}
#extras {
float: left;
width: 25%/250px;
margin: 0 5% 0 0;
}
#footer {
clear: left;
}
Note
The source document has been divided into four divs, one each for the header, main
content, extras, and footer. The markup shows the order in which they appear in the source.
Both #main and #extras have been floated to the left. Because they are floats, widths were
specified for each. You can make your columns as wide as you like.
The #main element has a 5% margin applied on the left and right sides.
The #extras element only needs a margin on the right. The margins on the top have been
set to zero so they vertically align.
The #footer is cleared so it starts below the floated content.
Grid Layout
CSS Grid Layout is a two-dimensional layout system for the web. It lets you lay content out in
rows and columns. It has many features that make building complex layouts straightforward. A
grid is a collection of horizontal and vertical lines creating a pattern against which we can line up
our design elements. A grid will typically have columns, rows, and then gaps between each row
and column. The gaps are commonly referred to as gutters.
.container {
display: grid;
Flexible grids with the fr (fraction) unit. In addition to creating grids using lengths and
percentages, we can use fr. The fr unit represents one fraction of the available space in the
grid container to flexibly size grid rows and columns. Change your track listing to the
following definition, creating three 1fr tracks:
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}
You now have flexible tracks. The fr unit distributes space proportionally. You can specify
different positive values for your tracks like so:
.container {
display: grid;
grid-template-columns: 2fr 1fr 1fr;
}
The first track gets 2fr of the available space and the other two tracks get 1fr, making the
first track larger.
You can mix fr units with fixed length units. In this case, the space needed for the fixed
tracks is used up first before the remaining space is distributed to the other tracks.
To create gaps between tracks, we use the properties:
column-gap for gaps between columns
row-gap for gaps between rows
gap as a shorthand for both
.container {
display: grid;
gap: 20px;
You can repeat all or merely a section of your track listing using the CSS repeat() function.
Change your track listing to the following:
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
You'll now get three 1fr tracks just as before. The first value passed to the repeat() function
specifies the number of times you want the listing to repeat, while the second value is a track
listing, which may be one or more tracks that you want to repeat.
We now move on from creating a grid to placing things on the grid. Our grid always has
lines — these are numbered beginning with 1. For example, column line 1 (written left-to-
right) would be on the left-hand side of the grid and row line 1 at the top.
We can arrange things in accordance with these lines by specifying the start and end line.
We do this using the following properties:
grid-column-start
grid-column-end
grid-row-start
grid-row-end
These properties can all have a line number as their value. You can also use the shorthand
properties:
grid-column
grid-row
These let you specify the start and end lines at once, separated by a forward slash /.
header {
grid-column: 1 / 3;
grid-row: 1;
}
article {
grid-column: 2;
grid-row: 2;
}
aside {
grid-column: 1;
grid-row: 2;
}
footer {
grid-column: 1 / 3;
grid-row: 3;
}
The grid-area property can be used as a shorthand property for the grid-row-start, grid-
column-start, grid-row-end and the grid-column-end properties.
Make "item8" start on row-line 1 and column-line 2, and end on row-line 5 and column
line 6:
.item8 {
grid-area: 1 / 2 / 5 / 6;
}
Flex Layout
The Flexible Box Layout Module, makes it easier to design flexible responsive layout structure
without using float or positioning.
To start using the Flexbox model, you need to first define a flex container. The element
below represents a flex container with three flex items
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
The flex container becomes flexible by setting the display property to flex:
.flex-container {
display: flex;
}
The flex container properties are:
flex-direction : Values (column, column-reverse, row, row-reverse)
flex-wrap : Values (wrap, no-wrap, wrap-reverse)
flex-flow : The flex-flow property is a shorthand property for setting both the flex-
direction and flex-wrap properties.
.flex-container {
display: flex;
flex-flow: row wrap;
}
justify-content : Values(center, flex-start, flex-end, space-around, space-between)
align-items : Values(center, flex-start, flex-end, stretch)
align-content : Values(center, flex-start, flex-end, space-around, space-between)
NB: Set both the justify-content and align-items properties to center, and the flex item will be
perfectly centered:
Reference Materials
CSS Versions
https://hackr.io/blog/difference-between-css-css2-and-css3
CSS Selectors
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors
https://code.tutsplus.com/tutorials/the-30-css-selectors-you-must-memorize--net-16048
CSS Units
https://www.freecodecamp.org/news/css-unit-guide/
https://www.w3schools.com/cssref/css_units.php
https://www.w3schools.com/cssref/index.php
https://zellwk.com/blog/9-important-css-properties-you-must-know/
https://www.pagecloud.com/blog/how-to-add-custom-fonts-to-any-website
https://developer.mozilla.org/en-US/docs/Web/CSS/Reference
https://www.w3schools.com/css/css_positioning.asp
CSS Colors
https://www.w3.org/wiki/CSS/Properties/color/keywords
_______________The End!_______________