Professional Documents
Culture Documents
J IEz Hi RAuc 3
J IEz Hi RAuc 3
What is CSS ?
CSS is a language for specifying how
documents are presented to users — how
they are styled, laid out, etc.
For example, you can decide to have the
main heading on your page to be shown as
large red text.
Why CSS
CSS describes how
HTML elements are to Allows you to create
CSS is responsible for
be displayed on great-looking
styling of the website.
screen, paper, or in websites.
other media.
HTML ONLY
HTML and CSS
3 ways of Inserting a stylesheet
External Internal
Inline styles
stylesheet stylesheet
Inline styles
• An inline CSS is used to apply a unique style to a single HTML element.
The following example sets the text colour of the <h1> element to red.
Internal Stylesheet
• An internal stylesheet resides within an HTML document.
When you make a change to an external style sheet, all the pages that reference it are automatically updated as well.
Declaration
The declaration block contains one or more declarations separated by semicolons.
It specifies which of the element's properties you want to style.
Properties
These are ways in which you can style an HTML element. In this example, color is a property of <p>
elements.
Property Value
This chooses one out of many possible appearances for a given property
Basic CSS
rules
In HTML colours are specified with
predefined color names, or with RGB value
using a decimal , HEX value
h1{
color:red
};
• RED, GREEN and BLUE are the three main colours supported in
every browser.
• An RGB colour value is specified with : rgb (red, green, blue).
RGB • Each parameter (red, green, and blue) defines the intensity of
the color as an integer between 0 and 255.
CSS Syntax • This means that there are 256 x 256 x 256 = 16777216 possible
colours
rgb(255,120,3);
• Hexadecimal color values are also supported in all browsers.
Colours
• To display black, set all color parameters to 00, like this: #000000.
• To display white, set all color parameters to ff, like this: #ffffff.
Background Colour
The background-color CSS property sets the background color of an element.
CSS Syntax
background-color : red;
Font size
• The font-size CSS property sets the
size of the font.
• The font-size property is specified in
one of the following ways:
• By using pixels(px)
• By relative values (perc, em, CSS Syntax
rem)
font-size : 15px;
Font Family
The font-family property specifies the font of an element.
The font-family property can hold several font names as a “fallback” system. If the browser does not support the
first font, it tries the next font.
CSS Syntax
h1{
color:red
};
Font Weight
• The font-weight property sets how thick or thin characters in text should be displayed.
• The weights available depend on the font family that is currently set.
• text-decoration-line
• text-decoration-style
• text-decoration-thickness
text-decoration-line
Sets the kind of decoration used, such as underline or line-through.
text-decoration-color
Sets the color of the decoration.
text-decoration-thickness
Sets the thickness of the line used for the decoration.
Text Align
The text-align property specifies the horizontal alignment of text in an element.
h1 { p{
text-align: center; text-align: center;
color: yellow; color: red;
} }
h1, h2, p {
text-align: center;
color: red;
}
Block and
Inline
Elements
A block-level
element always
takes up the full
width available.
It stretches
from left to
right as far as
possible.
Block Level
Elements A block-level
element always
starts on a new
line.
• <span>
• <a>
• <img>
Display Property
• The display CSS property sets whether an element is treated as a block or inline element.
CSS Box
Model
Height & Width
• The CSS height and width properties are used to set the height and width of an element.
CSS Syntax
div{
width: 400px;
height: 200px;
background-color: red;
}
• IMPORTANT – If the height and width is not defined, the element will get it’s height and width by default.
border-style
Border
• The border property sets an element’s border.
• solid
• dotted
• dashed
• groove
• The border-width property specifies the width of four borders. It can be in px, pt, cm, in, etc
Border Radius
• The border-radius property defines the radius of the element’s corners.
first value applies to top-left corner, second value applies to top-right corner, third first value applies to top-left corner, second value applies to top-right and
value applies to bottom-right corner, and fourth value applies to bottom-left corner bottom-left corners, and third value applies to bottom-right corner
first value applies to top-left and bottom-right corners, and the second value applies
to top-right and bottom-left corners the value applies to all four corners, which are rounded equally
Selectors 2
ID Selectors
• The HTML id attribute is used to specify a unique id for an HTML element.
• You cannot have more than one element with the same id in an HTML document.
HTML CSS
Class Selectors
• The HTML class attribute is used to specify a class for an HTML element.
CSS
HTML
Box Model
Content
The content of the box where the text and images appear.
Padding
Border
Margin
Top has a padding of 10px Top and bottom has a padding of 20px
Right has a padding of 20px Right and left have a padding of 100px
Bottom has a padding of 30px
Left has a padding of 40px
If you set the width to be 200px, the finally rendered element will
not exceed 200px width and include the padding, border, and
content within it.
Inline Block
• It’s essentially the same thing as inline, except that you can set height and width values.
Positioning
• The position property specifies the type of positioning method used for an element
• static
• relative
• fixed
• absolute
Static Positioning
• HTML elements are positioned static by default.
• Static positioned elements are not affected by the top, bottom, left, and right properties.
Relative Positioning
• An element with position: relative; 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.
• z-index only works on positioned elements (position: absolute, position: relative, position: fixed, or position:
sticky) and flex items (elements that are direct children of display:flex elements).
Fixed Positioning
• Fixed positioning has a lot in common with absolute positioning.
• Which means it always stays in the same place even if the page is scrolled.
On this instance when right : 20px is applied with position absolute, the
element will be placed 20px away from right relative to the entire
browser.
But it’ll always stay in the same place even if the page is scrolled.
CSS Flexbox
CSS Flexbox
• CSS flexbox is a system of CSS rules that can be used to easily layout html elements.
• We can create a flex layout by adding “display:flex” CSS rule to a div container.
CSS Syntax
.container{
display: flex;
}
CSS Flexbox
• There are two main ways we can manipulate content with flexbox.
• Flex rules added to a flex container can define the layout of the flex items.
Parts of a flex layout
• The height and the width of a flex container by default is defined by the height and the width of the largest flex
item. The are known as Main size for width and Cross size for height.
Flex-direction
• Flex-direction can be used to specify the direction of adding new items.
• Default is row(left to right)
CSS Syntax
.container {
flex-direction: row | row-reverse | column | column-reverse;
}
• column(top to bottom)
Flex-wrap
CSS Syntax
.container {
display: flex;
flex-wrap: nowrap | wrap | wrap-reverse;
}
.container {
display: flex
justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly;
}
Justify-content
• The display CSS property sets whether an element is treated as a block or inline element.
Justify-content
• flex-start (default): items are packed toward the start of the flex-direction.
• space-between: items are evenly distributed in the line; first item is on the start line, last item on the end line
• space-around: items are evenly distributed in the line with equal space around them. Note that visually the
spaces aren’t equal, since all the items have equal space on both sides. The first item will have one unit of
space against the container edge, but two units of space between the next item because that next item has
its own spacing that applies.
• space-evenly: items are distributed so that the spacing between any two items (and the space to the edges) is
equal.
Align-items
CSS Syntax
.container {
align-items: stretch | flex-start | flex-end | center | baseline;
}
Align-items
Align-items
• stretch (default): stretch to fill the container (still respect min-width/max-width).
• flex-start: items are placed at the start of the cross axis.
• flex-end / end / self-end: items are placed at the end of the cross axis.
• If the content is larger than the maximum width, it will automatically change the height of the element.
• If the content is smaller than the maximum width, the max-width property has no effect.
• This prevents the value of the width property from becoming larger than max-width. The value of the max-
width property overrides the width property.
min-width
• The min-width property defines the minimum width of an element.
• If the content is smaller than the minimum width, the minimum width will be applied.
• If the content is larger than the minimum width, the min-width property has no effect.
• This prevents the value of the width property from becoming smaller than min-width.
CSS Grid
CSS Grid
• CSS grid is a modern layout system in CSS that we can use to align UI elements using grid structures.
• Grid rules can be added to a parent container to control how the children of that container is positioned.
CSS Syntax
.container{
display: flex;
}
Parts of a grid layout
CSS Syntax
.container {
display: grid;
}
grid-template-columns
• Defines the columns in a grid container
CSS Syntax
.container {
grid-template-columns: 1fr 1fr;
}
grid-template-rows
• Defines the rows in a grid container
CSS Syntax
.container {
grid-template-rows: 1fr 1fr;
}