How To Use CSS Effectively
How To Use CSS Effectively
effectively
Content
Box-sizing
Position
Display
Containing block
Relative unit
Reset CSS
Margin Collapse
Some useful tips
2
Box-sizing Property
▸ content-box (default)
The width and height properties includes only
the content. Border and padding are not
included.
▸ border-box
The width and height properties includes
content, padding and border
3
Box-sizing Property
.content-box {
width: 100%;
border: solid #5B6DCD
10px;
padding: 5px;
}
.border-box {
width: 100%;
border: solid #5B6DCD
10px;
padding: 5px;
}
4
Display Property (Document Flow)
The element is completely removed, as if it wasn't in the HTML code in the first
none place
- It starts on a new line,
block - Takes up the whole width of the parent element
- It behaves like simple text.
inline - Height, width, top and bottom margin will be ignored.
- Both an inline and a block element
inline-block - can apply height and width values
- Does not add a line-break after the element
The element is turned into an flexbox container. On its own, it behaves like a
flex block element.
The element is turned into an grid container. On its own, it behaves like a block
grid element.
5
Display Property - Grid
6
Display Property - Flexbox
7
Grid vs Flexbox
8
Block - Inline
» block
» <div>, <h1>-<h6>, <p>, <form>, <header>, <footer> and <section>
» inline
» <span> and <a>
» inline-block
» <img>
9
Viewport
» It does not include your address bar or any other type of persistent
UI that takes away space.
10
Position Property
Default value. Elements render in order, as they appear in the document
static
flow
The element is positioned relative to its normal position, so "left:20px"
relative
adds 20 pixels to the element's LEFT position
The element is positioned relative to its first positioned (not static)
absolute
ancestor element
12
Position - Relative
» The offset does not affect the position of any other elements;
thus, the space given for the element in the page layout is the same
as if position were static.
13
Position - Fixed
» The top, right, bottom, and left properties are used to position the
element.
14
Position - Absolute
» The element is removed from the normal document flow, and no space
is created for the element in the page layout.
» Its final position is determined by the values of top, right, bottom, and
left.
15
Position - Sticky
» Offset relative to its nearest scrolling ancestor based on the values of top,
right, bottom, and left.
» The offset does not affect the position of any other elements.
16
Margin-Left vs Left
Left Margin-Left
- Offset itself from its normal left edge, - Sets the margin area on the left side of an
Relative
Left right will offset each other. element
- Offset from its first positioned (not - Sets the margin area on the left side of an
Absolute static) ancestor element element, does not affect the position of
- Left, right determine the size any other elements
- Sets the margin area on the left side of an
- Offset from left edge of Viewport
Fixed element, does not affect the position of
- Left, right determine the size
any other elements
- Sets the margin area on the left side of an
Sticky - Left is used as offset to become stick
element
- Sets the margin area on the left side of an
Static - Ignored
element
17
Containing block
» The size and position of an element are often impacted by its containing block
» Percentage values that are applied to the width, height, padding, margin, and
offset properties of an element are computed from the element's containing block
18
Identifying the containing block
» If the position property is static, relative, or sticky
» the containing block is formed by the edge of the content box of the nearest
ancestor element that is a block container
19
Percentage unit
» The height, top, and bottom properties compute percentage values from the
height of the containing block.
» The width, left, right, padding, and margin properties compute percentage
values from the width of the containing block.
20
Rem & em
» Allows setting the font-size of an element relative to the font-size of its parent.
» If the parent element doesn’t specify a value for font-size, a value will be looked
for higher up in the DOM tree.
» If no font-size is specified all the way up to the root element <html>, then the
browser default of 16px is used.
» When em units are used on properties other than font-size, the value is relative
to the element’s own font-size.
21
Rem & em
» rem (root em) is based upon the font-size value of the root element which is
<html> element.
» If the <html> element doesn’t have a specified font-size, the browser default of
16px is used.
22
Rem & em – Which is better
» There’s no better unit really, and it all depends on your personal preferences.
» Some people like to design everything in rem units for consistency and
predictability.
» While others like to also use em units in places where the influence of nearby
23
Auto Value
» The initial width of block-level elements like <div> or <p> is auto, which makes
them take the full horizontal space of their containing block.
» The height of an element is equal to its content where the default value is auto.
» Left, right, top, bottom: Lets the browser calculate its position.
24
Reset CSS
» A CSS Reset is a set of CSS rules that resets the styling of all HTML elements
to a consistent baseline.
» Using a CSS Reset makes every browser to have all its styles reset to null, wipe
out the differences that exist between different browsers default styles and
then you build up your styles from there.
25
Reset CSS – Different approach
26
Reset CSS
<head>
<link rel="stylesheet" type="text/css"
href="reset.css">
<link rel="stylesheet" type="text/css"
href="styles.css">
</head>
27
Order of precedence for CSS
Inline css (html style attribute) overrides css rules in style tag and css file.
Rules that appear later in the code override earlier rules if both have the same
specificity.
28
Order of
precedence for
CSS
29
Margin Collapse
» Only vertical margins collapse
CSS rulesets cascade down the CSS hierarchy from parent selectors to their
children selectors.
31
CSS Inheritance - inherit
When you set inherit on a CSS property, the property takes the value from the
element’s parent.
This applies not only to inheritable properties, but to all CSS properties.
Only direct child elements can inherit a CSS property from its parent element
using the inherit value if the CSS property is set by the element’s parent
element.
32
Center a Div with CSS Margin Auto
<div class="container">
<div class="child"></div>
</div>
.container { .child {
font-family: arial; width: 50px;
font-size: 24px; height: 50px;
margin: 25px; /* Center horizontally */
width: 350px; margin: 0 auto;
height: 200px; }
outline: dashed 1px black;
}
33