Professional Documents
Culture Documents
In the first part we covered the fundamental of CSS, in this part we are going to talk
about Advanced HTML and CSS Topics. That are used in building almost every website.
ALL these topics are so important,
You’ll Learn
1. Layouts
2. Typography
3. Images
4. Forms
5. Transformations
6. Animations
7. Clean CSS
Layout
We have learned basic of CSS, let’ talking about creating Layouts. So in this section. We
will learn everything we need to know Sizing and positioning element on the serene.
1- Box model
…
2- Sizing Elements
Now let’s talking about sizing elements, let’s create two pages the one for HTML
(Index.html) the anther for CSS (styles.css).
- Now let’s define a rules (styling) for box, and gave (width, height, background-
color).
.box {
width: 100px;
height: 100px;
background-color: gold;
}
DOM element
This where a lot of people new to CSS get confuse, if we apply padding
or border our box is will be bigger. Let me show you.
Let’s set padding 20px and border 10 solid and color orange.
.box {
width: 100px;
height: 100px;
background-color: gold;
padding: 20px;
border: 10px solid orange;
}
Left Right
- If we remove the padding and border property, our box is still (100 x
100) the pixels is.
.box {
width: 100px;
height: 100px;
background-color: gold;
box-sizing: border-box;
}
- By setting the box-sizing property calculating the size of elements
become easy and more predictable. But there is problem, we don’t want
to apply this property to every single HTML element, what if we have
another element with class of product we don’t want to repeat this. This
where we use the universal selector, so on the top we can use universal
selector to select all HTML selectors.
* {
box-sizing: border-box;
}
.box {
width: 100px;
height: 100px;
background-color: gold;
}
.box {
width: 100px;
height: 100px;
background-color: gold;
}
Take look here is our box, we have pseudo element ::before , this
element (::before) is using default box sizing that is content box, let
me show you, look up the computed tab in DevTools, we have just
four properties (content, display, height, width) there is no box-sizing
Computed Tab
Pseudo Element
But if we select another element like our box, here we have the box-
sizing property.
Computed Tab
Box-sizing
Box Element
- So to solve this problem we need to extend this rules
like this all element befor(*::before) and all element
after (*::after)
*,
*::before,
*::after {
box-sizing: border-box;
}
.box::before {
content: "Hello";
}
.box {
width: 100px;
height: 100px;
background-color: gold;
}
Now this box-sizing property is applying to all element (*) as will all
the elements are inserted before and after the content of this element.
3- Overflowing
An important concept you need to know, when dealing with fixed size element
is that sometimes Overflow may happen, so you may have container with fixed
size, and you may have too much content inside this container, so the container
can’t fit the entire content, that is when Overflow is happen.
For example: just like when we have a glass of water, and we put too much
water in.
- So let’s create a <div> with class of (box) and add a long paragraph
<div class="box">
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Aper
iam amet
iste similique consequuntur a quod laudantium modi consectetur
voluptatibus, provident commodi sed error eveniet eligendi nat
us fuga
voluptas corrupti sunt earum quis, ad quae maiores excepturi?
Nostrum,
</div>
- And let’s define a rules for our box, give border with
3px and width 150px or height 150px.
.box {
border: 5px solid gold;
width: 150px;
height: 150px;
overflow: hidden;
}
.box {
border: 5px solid gold;
width: 150px;
height: 150px;
overflow: hidden;
}
- Another passable is value is (scroll), we use this, we see two scroll bar, a
horizontal and vertical.
.box {
border: 5px solid gold;
width: 150px;
height: 150px;
overflow: scroll;
}
- Let’s give some style to this box, I going to give width 100px and height
100px and background color.
.box {
width: 100px;
height: 100px;
background-color: gold;
}
- The size of this box is always the same, no matter how big is the screen is,
so if we view this page on small devise like (Mobil,
tablet), our box is gone stay at the same size. We
can verify this by resizing browser windows.
- For this reason, some people are agree that we should never use pixel values,
because they are not scalable we cannot use them to create webpages, that
are responsive to screen sizes, I am disagree, pixel values also have
applications.
- Now let’s talk about relative values, I am going to change the width of this
to 50% that means of the width of the parent element what is the parent of
this element the <body>. The body element currently doesn’t have width, so
let’s give a width of 300px, that’s mean our box will be 150px.
body {
width: 300px;
}
.box {
width: 50%;
height: 100px;
background-color: gold;
}
- So let’s verify this, here is our box let’s inspect (DevTool) , go to the
computed tab, down look up the width property is 150px .
Computed Tab
So all these relative values, eventually get converted to pixel value by the
browser.
- What if we remove this property from body class, what is the default
width of this element, the body element is block level element and by-
default the width of block level element is one hundred percent (100%), so
our body element takes 100% width of the page and our box takes half of
the page.
body {
}
.box {
width: 50%;
height: 100px;
background-color: gold;
}
- Here is
the
interesting part if we resize the browser window our box also get resize,
this the benefit of using relative units, with relative units we can create
layouts that are adjust to screen sizes, with relative units we can create
more scalable layouts.
What if we want this box to take up the available vertical space, you may say
we can set the height to 100%. Let see what happen.
body {
}
.box {
width: 50%;
height: 100%;
background-color: gold;
border-top: 3px solid orange;
}
Where is the box, are saying the height of this box should be 100%
percent of the height of its parent element, the parent element which is
the body element, currently doesn’t have height property so by-default
its height gone be zero (0). The only absolute unit we are using the
border-top property, so the height of body element will increase 3px,
that the reason why we see the border, but not the box,
So how can we make the box take of entire available vertical space
(height) this is where we use (viewport) units, with this unit we can
size our elements according to width or height of viewport.
We also have (vw) viewport width, so the width: 50vw, means 50%
percent of the width viewport.
- We have two more relative units called (em and rem) which are relative
to the Font Size, we use these units in situation where we want to adjust the
layout according to the Font Size for example if the font come bigger we
want the width increase as will.
So if we set the (width to 10em), 10em means 10 time the font size of
the current element. (10 * font size of the current element), now the .box
element doesn’t have the font size which is going to inherit font size
property from its parent element which is the body element, but the body
element also have not the font size property, so it’ going to inherit the
font size property from html element .
body {
}
.box {
width: 10em;
height: 100vh;
background-color: gold;
border-top: 3px solid orange;
}
All the browser gives the html element the base font size 16px. So the
10em means, 10 times 16px which mean 160px. ( 10*16px = 160px ).
body {
}
.box {
/* 10 x 16px = 160px */
width: 10em;
height: 100vh;
background-color: gold;
border-top: 3px solid orange;
}
So in this case our box inherit the font size of the root element which
is the html element.
If we set the font size direct here in box 20px (font-size:20px), so the
width will be 20 times 10 which is 200px (10*20 = 200px).
.box {
/* 10 x 20px = 200px */
font-size: 20px;
width: 10em;
height: 100vh;
background-color: gold;
border-top: 3px solid orange;
}
/* 10 x 10px = 160px */
width: 15em;
height: 100vh;
background-color: gold;
border-top: 3px solid orange;
}
5- Positioning
In this section we will learn about, how we have position element on the screen.
Position property have some value which are (static, relative, absolute, fixed).
Relative Position: with relative positioning we can position element relative to its
normal position. The other’s element is not affected.
Absolute Positon: with
absolute positon we can
position an element
relative to its position
container, that’ why we
have to apply relative
position on the container.
<div class="box box-one">box one</div>
<div class="box box-two">box two</div>
<div class="box box-three">box three</div>
</div>
.boxes{
border: 4px solid orange;
}
- And for each .box we define a width 5rem and height 5rem.
.boxes{
border: 4px solid orange;
}
.box{
width: 5rem;
height: 5rem;
}
.boxes{
border: 4px solid orange;
}
.box{
width: 5rem;
height: 5rem;
}
.box-one{
background-color: gold;
}
.box-two{
background-color: tomato;
}
.box-three{
background-color: dodgerblue
}
- Let’s we want to position this second box (.box-two) and move to the
right, this where we use positon property, now to position this element
we need to change the positon to (relative) so we can position this
element to relative to its normal position.
to use these property (top, left, bottom, right) we can move this box
around, so if we set left to 5rem (left:5rem), we are adding 5rem of space
on the left side of this box, and this will push to the right side.
.box-two {
background-color: tomato;
position: relative;
left: 5rem;
}
Here we can also use negative value like -4rem (left:4rem) and this will
move the box to the left side.
- we also have right property if we set to 4rem, this exactly like setting
left property to negative 4 rem (left: -4rem)
.box-two {
background-color: tomato;
position: relative;
right: 4rem;
}
-
we also have top and button, we can add some space at top the box, so we
can say (top:2rem).
.box-two {
background-color: tomato;
position: relative;
left: 4rem;
top: 2rem;
}
Or we can add space from bottom, let’s say bottom: 4rem
.box-two {
background-color: tomato;
position: relative;
left: 4rem;
bottom: 4rem;
}
One thing I want pay attention and the yellow and blue box are
exactly where they supposed to be they are not effected by position of
this element.
also note here we have overlapping this (tomato
box) is appear on top the (yellow box) element,
because it’s come last in the DOM, but we can
easily change it.
So back to the second box (.box-two) we can set the (z-index) property to
positive or negative value, to move this box to z-index. Now let set
z-index value to (-1)
.box-two {
background-color: tomato;
position: relative;
left: 4rem;
bottom: 4rem;
z-index: -1;
}
So with relative positioning we can move an element relative to its
normal position and this doesn’t affect the others element on the page.
background-color: tomato;
position: absolute;
left: 0;
bottom: 0;
}
Save the
changes, and
see what we
get.
.box-two {
background-color: tomato;
position: fixed;
top: 0;
}
If you pay attention the tomato box is now outside of container because
it’s position relative to the viewport, so this element always stays on the
top even if we scroll down,
- One last thing there is two ways to set the size of element, one we use to
the width and height property’s, anther way is use the combination of
left and right or top and button.
For example, for our second box lets set left to 2rem (left:2rem) and right
to 2rem (right:2rem) and also set the width to auto (width:auto),
because we use width auto we have already use the width and height in
the box property (.box).
.box-two {
background-color: tomato;
position: fixed;
top: 0;
left: 2rem;
right: 2rem;
width: auto;
}
6- Floating Elements
Now let’s talk about floating elements this is another technique for positioning
elements and creating layouts.
Let’s say we want to display a tweet; what HTML element can be used to
represent tweet.
- We can use <article> element, because with article element we can represent
any independent sub content piece of content.
So we have an <article> with class of (tweet) for styling and inside of this
article we have another <div> with class of (avatar) for image. After that we
.avatar {
width: 5rem;
height: 5rem;
background-color: gold;
}
- To put this paragraph next to the avatar (to the right side) we use float
property (float: left) and this will float the avatar (box) element to left
side of its container and all the sub sequins element will flow rounded
.avatar {
width: 5rem;
height: 5rem;
background-
color: gold;
float: left;
}
- let’s add paragraph with some more text. Let’s say 50 words.
<body>
<article class="tweet">
<div class="avatar"></div>
<p>Lorem ipsum dolor sit amet.</p>
<p>
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Quo beatae
deserunt saepe soluta enim quam impedit. Alias quis facere veritatis
magni aliquam, rem eligendi, nihil nesciunt rerum libero in illum eum
exercitationem ipsam cumque optio cum, aperiam fuga? Laborum officia non
at incidunt autem delectus harum eaque amet inventore omnis.
</p>
</article>
</body>
See our text is flowing around the box (avatar/image) this how float
working.
See currently our text is so close to our box (avatar/image) what CSS
property should we use to space them the (Margin) property right.
.avatar {
width: 5rem;
height: 5rem;
background-color: gold;
float: left;
margin-right: 10px;
}
- we can also float element to the right side of the container, so if we set
float the right (float:right).
.avatar {
width: 5rem;
height: 5rem;
background-color: gold;
float: right;
margin-right: 10px;
}
- sometimes we don’t want the second element here to flow around the box
(avatar/image) floated element. Maybe we have here link or icon for like
this tweet or retweeting it. We want all these icons appear below the
tweet box (avatar/image), so how can we push the second paragraph
below the box (avatar/image.
This where we use the (clear) property (clear:both) if we have
element that floated to the left or right, this will clear all of those floats.
So let’s create another class call it (.clear) in this class set the clear
property to (clear: both).
.avatar {
width: 5rem;
height: 5rem;
background-color: gold;
float: left;
margin-right: 10px;
}
.clear {
clear: both;
}
<article class="tweet">
<div class="avatar"></div>
<p>Lorem ipsum dolor sit amet.</p>
<p class="clear">
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Quo beatae
deserunt saepe soluta enim quam impedit. Alias quis facere veritatis
at incidunt autem delectus harum eaque amet inventore omnis.
</p>
</article>
</body>
Save the changes, come back to the browser, see our second paragraph
is below the box
- but there is problem with float called (Parent Collapsing) which means
parent element don’t see floated elements and they collapse. Let me show
what I mean.
Back to our stylesheet I am going to define a rules for tweet (.tweet) and
give border 3px (border: 3px solid lightgrey).
.tweet {
border: 3px solid lightgrey;
}
.avatar {
width: 5rem;
height: 5rem;
background-color: gold;
float: left;
margin-right: 10px;
}
.clear {
clear: both;
}
Everything looks fine now, but let’s see what happen if we remove
the second paragraph,
<body>
<article class="tweet">
<div class="avatar"></div>
<p>Lorem ipsum dolor sit amet.</p>
</article>
</body>
Article element
avatar
See what happen our parent element which is the article with class of
(tweet) has collapse, now it only content this single paragraph
element, it’s not contenting the box (avatar) because avatar is
floated element, and by-default parent element don’t see the floated
element it’s invisible to them.
<body>
<article class="tweet">
<div class="avatar"></div>
<p>Lorem ipsum dolor sit amet.</p>
<div class="clear"></div>
</article>
</body>
border: 3px solid lightgrey;
}
.clerfix {
content: "";
display: block;
clear: both;
}
.avatar {
width: 5rem;
height: 5rem;
background-color: gold;
float: left;
margin-right: 10px;
}
.clear {
clear: both;
}
<div class="container">
<div class="box">A</div>
<div class="box">B</div>
<div class="box">C</div>
</div>
Now let define a bunch of rules, for our .container we set the border
and for our box we set (width, height, background-color, margin).
.container {
border: 3px solid lightgray;
}
.box {
width: 5rem;
height: 5rem;
background: gold;
margin: 1rem;
}
.box {
width: 5rem;
height: 5rem;
background: gold;
margin: 1rem;
}
Look our boxes are laid out in horizontals style, with single line of code,
we didn’t have to use float we didn’t have to clear everything this is
beauty of Flex.
.container {
border: 3px solid lightgray;
display: flex;
flex-direction: column;
}
.box {
width: 5rem;
height: 5rem;
background: gold;
margin: 1rem;
}
- (fl ex-
using these axes, we can easily align item, inside the container.
- Now to align items there is two properties we need to remember (justify-
content and align-items).
.container {
border: 3p x solid lightgr
ay;
display: f lex;
flex-direction: row;
justify-content: flex-end;
}
- We can also distribute these items along the horizontal axis, here we have
three 3 property’s (space-evenly, space-between, space-around)
1.The first one is space-evenly which distribute our item with equal spacing
between them, (justify-content:space-evenly).
.container {
border: 3px solid lightgray;
display: flex;
flex-direction: row;
justify-content: space-evenly;
}
2.The second one is space-between , the first and last items push to the end of
horizontal axis, and other item are distribute equal space between them .
.container {
border: 3px solid lightgray;
display: flex;
flex-direction: row;
justify-content: space-between;
}
3.The third one is space-around , with space around we have some space before the
first box and after the last item, but the amount of space we have here, is half of space
we have in between other items.
.container {
border: 3px solid lightgray;
display: flex;
flex-direction: row;
justify-content: space-around;
}
- Align-items: using the align-items property we can align item along the
(cross / secondary) axis which is in this case vertical axis. The default value
is (align-items: flex-start), but we can set flex-end to push these items to the
end of vertical axis (align-items: flex-start).
.container {
border: 3px solid lightgray;
display: flex;
flex-direction: row;
justify-content: center;
align-items: flex-end;
}
now in this case nothing happens, because there is not enough space in
our container .
Container
that brings us important concept in flex event though we set the
(display:flex) property to flex here, this container is still block level
element, so by-default it’s get tall enough to fit it’s children, so on
the outset this element is block level element but on the inset it
behave according to the flex box rules.
.container {
border: 3px solid lightgray;
display: flex;
flex-direction: row;
justify-content: center;
align-items: flex-end;
height: 90vh;
}
s
Container
Vertical axis
See now we have a lot of space here and our boxes are push end of
the vertical axis.
Container
Vertical axis
.container {
border: 3px solid lightgray;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
flex-wrap: wrap;
height: 90vh;
}
See, now items keep its original size and any extra items can not fit
in one-line wrap to second line.
So early when we have second line we put our elements in the center
of vertical axis, but now that we have multiple lines look our
element not to the center, the first line is closer to the beginning of
vertical axis, and the second line is closer to the end. This is where
beginning
end
align-content: This is where the align-content property comes to the
play, with this property we can align multiple lines or entire content as
whole, so we can put the entire content in the center (align-content:
center) on the vertical axis.
.container {
border: 3px solid lightgray;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
flex-wrap: wrap;
align-content: center;
height: 90vh;
}
- align- self:
We have three 3
boxes in the center of our container, now what if we want to change the
alignment one of these boxes, like if we want to move the first box to the
Top this where we use the align-self property.
Frist we give to this box an extra class like (box-one)
<div class="container">
<div class="box box-one">A</div>
<div class="box">B</div>
<div class="box">C</div>
</div>
Now back to our stylesheet let’s define a new rule. For box-one we set
the align-self to flex-start, (align-self: flex-start).
.container {
border: 3px solid lightgray;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
height: 90vh;
}
.box-one {
align-self: flex-start;
}
.box {
width: 5rem;
height: 5rem;
background: gold;
margin: 1rem;
}
We can also move down by setting (flex-end)
.box-one {
align-self: flex-end;
}
Flex-basis: which is use for setting the initial size of a flex item.
Flex-grow: for setting the growth factor.
Flex-shrink: for seeing the shrink factor.
Flex : which is the short hand property which is combine all top
three property’s
All these properties should be applying on flex items not flex container.
- so let’s see them in action, back to our stylesheet, here in (.box) class, we
can use the flex-basis property to set the initial size of boxes, by-default
it’s set to (auto) (flex-basis:auto) which mean’s the browser is going to
look up the width property.
. box
{
al
i gn-
self
: aut
o ;
wi
dth: 5rem;
height: 5rem;
background: gold;
margin: 1rem;
}
.container {
border: 3px solid lightgray;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
height: 90vh;
}
.box {
Flex-basis: 10rem;
width: 5rem;
height: 5rem;
background: gold;
margin: 1rem;
}
flex-basis: 5rem;
}
So
currently our boxes are each one is 5rem and we have extra space
available, on left and right side. With flex-grow we can allow these
items to grow and take the available space.
So by-default flex-grow is set to zero 0 flex-grow:0; that is why our
box are not growing, but if we set to 1 one. Flex-grow:1; now all
these boxes are growing at same factor.
.box {
flex-basis: 5rem;
flex-grow: 1;
width: 5rem;
height: 5rem;
background: gold;
margin: 1rem;
}
We can change grow factor for one or more boxes, for example for
our first box we can set flex-grow to 0 zero, flex-grow:0;
.box-one {
flex-grow: 0;
}
So now all
the
available
space is
allocated to second (B) box and to third box (C) boxes, now we can
change this to two 2 flex-grow:2;
.box-one {
flex-grow: 2;
}
Now you can see the first box has growing more than others boxes, you
may be thinking why the first box is not twice as biggest others boxes,
well this common miss concept about flex grow property.
See this example.
We have
space here on left or right side, when we set flex grow property the
browser is going to look at the grow factor of each these boxes, for the
first box the grow factor is 2 for two others boxes the grow factor is
one 1, so (2+1+1= 4) so the browser is going to take up entire available
space divided for four 4 slices then it’s gone allocate 2 two slices to box
A, one slice to box B and one slice to box C. this how flex grow
- Flex-shrink: we have another property called flex-shrink which is the
opposite of flex-grow, it tells the browser how these element should
shrink if there is not enough space to fit in the container.
It is like responsive
- Flex: now we have this flex property which short hand for these
peripteries (flex-basis,flex-grow,flex-shrink).
so if we apply single value (flex:1) that is going to be use for the (flex-
grow) property and if we apply the second value (flex:1 2) that is going
to be use for the flex-shrink property and the third property is for (flex-
basis).
One thing I want to know here is that (flex-grow and flex-shrink) just take a
number they don’t take unite.
We have cover a lot of in this section, but trust me flex easy then we think as we
practice more it be come easy.