3.3_CSS-Box_Model
3.3_CSS-Box_Model
DEVELOPMENT
IS-224
RAPID
APPLICATION
DEVELOPMENT
LECTURE 3.3
CSS
Box Model
R A P I D A P P L I C AT I O N
DEVELOPMENT
<head> Output
<style>
Sample Para 1
*{
border: 1 px solid red; Sample Para 2
Block-Level Elements } Link 1 Link 2
</style>
</head>
<body>
Inline Elements <p> Sample Para 1 </p>
<p> Sample Para 2 </p>
<a> Link 1</a>
<a> Link 2 </a>
</body>
R A P I D A P P L I C AT I O N
DEVELOPMENT
Model
Margin HELLO Border
Padding
Content
R A P I D A P P L I C AT I O N
DEVELOPMENT
Width & Height This is done using the CSS properties: height and width
<body> Output
<div> </div>
HTML <div> </div>
</body>
div:nth-child(1) {
background-color: red;
width: 100px;
height: 100px;
}
CSS
div:nth-child(2) {
background-color: green;
width: 500px;
height: 100px;
}
R A P I D A P P L I C AT I O N
DEVELOPMENT
CSS Padding
The padding sits inside the border and surrounds the content of the HTML element
padding-top
HELLO
HELLO padding-right
padding-left
padding-bottom
Padding
R A P I D A P P L I C AT I O N
DEVELOPMENT
CSS padding values can also be defined using a shortcut property called padding
CSS Border
The border sits inside the margin, and surrounds the padding and content of the HTML element
border-top
border-right
HELLO
border-left
HELLO
Border
border-bottom
R A P I D A P P L I C AT I O N
DEVELOPMENT
Each border can have three properties: width, style and color
style
solid
dotted
dashed
double
R A P I D A P P L I C AT I O N
DEVELOPMENT
CSS border values can also be defined for all borders using the border property
<body> Output
<div> </div>
HTML <div> </div>
</body>
div:nth-child(1) {
background-color: red;
width: 100px;
height: 100px;
border-radius: 50%; }
CSS
div:nth-child(2) {
background-color: green;
width: 500px;
height: 100px;
border-radius: 20px; }
R A P I D A P P L I C AT I O N
DEVELOPMENT
CSS Margin
The outermost part is the margin between this HTML element to other HTML elements
margin-top
HELLO
margin-
HELLO margin-
left right
Margin margin-bottom
R A P I D A P P L I C AT I O N
DEVELOPMENT
CSS margin values can also be defined using a shortcut property called margin
CSS Units
R A P I D A P P L I C AT I O N
DEVELOPMENT
CSS Units
When specifying dimensions of elements, font sizes, border widths etc. in CSS you
will usually use some kind of unit to specify how the numeric dimension is to be
interpreted
px
%
em
rem
R A P I D A P P L I C AT I O N
DEVELOPMENT
Example
div {
height: 32px;
}
R A P I D A P P L I C AT I O N
DEVELOPMENT
Important Note: The percentage is based on the parent element if the element is nested
Example Output
div {
<body> width: 50%;
<div> }
<div id=“inner”>
#inner {
</div> width: 50%
</div> }
</body>
R A P I D A P P L I C AT I O N
DEVELOPMENT
CSS Units - em
This is relative to the font-size value of the element (parent)
The size is relative to the text on the page so that it is easier to read on a smaller
device
With the <div> being 3 times that, you get the 48px
R A P I D A P P L I C AT I O N
DEVELOPMENT
The rem unit of measure is based off of the font-size of the root element
Example
div {
font-size: 3rem;
}
R A P I D A P P L I C AT I O N
DEVELOPMENT
CSS Colors
R A P I D A P P L I C AT I O N
DEVELOPMENT
CSS Colors
There are several CSS properties which specify colors for parts of HTML elements.
For instance, color, background-color, border etc.
When you specify a color, you can do so using the following formats:
Some common preset colors are black, green, blue, red, yellow, white, gray, purple and so on
Example
div {
background-color: green;
}
R A P I D A P P L I C AT I O N
DEVELOPMENT
Example
div {
background-color: #ff00ff;
}
https://coolors.co – Use this cool website to generate color themes for your website
R A P I D A P P L I C AT I O N
DEVELOPMENT
You use decimal numbers between 0 and 255 for each component – Red, Green and Blue
Example
div {
background-color: rgb(255, 0, 255)
}
R A P I D A P P L I C AT I O N
DEVELOPMENT
The RGB part is the same as in RGB colors. The A (alpha channel) specifies the opacity of the color
The alpha channel value can be set between 0 (fully transparent) and 1.0 (fully opaque)
Example
div {
background-color: rgba(255, 0, 255, 0.7)
}
R A P I D A P P L I C AT I O N
DEVELOPMENT
CSS Display
R A P I D A P P L I C AT I O N
DEVELOPMENT
CSS Display
As mentioned earlier, each HTML element is either block or inline by default
You can change this property of an element using the display property
<body> Output
<p>Sample Para 1</p>
<p>Sample Para 2</p> Sample Para 1 Sample Para 2
HTML <a> Link 1</a> Link 1
<a> Link 2 </a>
Link 2
</body>
p{
display: inline;
}
CSS
a{
display: block
}
R A P I D A P P L I C AT I O N
DEVELOPMENT
CSS Display
Using the value “none” with display will remove the element from the browser display
<body> Output
<p>Para 1</p>
<p>Para 2</p> Link 1
HTML <a> Link 1</a> Link 2
<a> Link 2 </a>
</body>
NOTE:
p{
display: none; The space for the element
} is collapsed
CSS
a{
display: block
}
R A P I D A P P L I C AT I O N
DEVELOPMENT
CSS Visibility
If you want to preserve the space of the element (as opposite to display: none)
<body> Output
<p>Para 1</p>
<p>Para 2</p>
HTML <a> Link 1</a>
<a> Link 2 </a>
</body> Link 1
Link 2
p{
visibility: hidden; NOTE:
}
CSS The space for the element
a{
is preserved
display: block
}
R A P I D A P P L I C AT I O N
DEVELOPMENT
CSS Display
Important Note: If an element is inline, the properties width and height are ignored
<body> Output
<p>Para 1</p>
<a> Link 1</a> Para 1
HTML <a> Link 2 </a>
</body>
a{
height: 500px
} Link 1 Link 2
CSS
p{
height: 500px
}
R A P I D A P P L I C AT I O N
DEVELOPMENT
Let’s learn
CSS Layouts
R A P I D A P P L I C AT I O N
DEVELOPMENT
CSS Layouts
Now we know that each HTML element is shown as a box on the web page
If elements are block-level, the boxes are placed on top of each other
If elements are inline, the boxes are placed beside each other
When making a complex layout, you should be able to move the boxes as your like
Building from scratch using Built in CSS Layouts such as Using external CSS Frameworks such
floats and clear Grid and Flexbox as Bootstrap and so on
R A P I D A P P L I C AT I O N
DEVELOPMENT
CSS Layouts
Let's learn this with an example
Current Expected
<body> Output
<div id=“container”>
<div class=“inner”></div>
<div class=“inner”></div>
<div class=“inner”></div>
</div>
</body>
#container
{
width: 900 px;
}
.inner
{
background-color: red;
height: 100px;
}
R A P I D A P P L I C AT I O N
DEVELOPMENT
STEP 1
Set the size of each <div> box – Calculate the Size
3x = 900 - 30
3x = 870
x = 290
R A P I D A P P L I C AT I O N
DEVELOPMENT Try Here https://codepen.io/drmisbha/pen/RwoprQB
<body> Output
<div id=“container”>
<div class=“inner”></div>
<div class=“inner”></div>
<div class=“inner”></div>
</div>
</body>
#container
{
width: 900 px;
}
.inner
{
background-color: red;
height: 100px;
width: 290px;
}
R A P I D A P P L I C AT I O N
DEVELOPMENT
STEP 2
We need to move the boxes one beside the other
NO
STEP 2
We need to move the boxes one beside the other
1 1 2 3 3 2 1
<body> Output
<div id=“container”>
<div class=“inner”></div>
<div class=“inner”></div>
<div class=“inner”></div>
</div>
</body>
#container
{
width: 900 px;
}
.inner
{
background-color: red;
height: 100px;
width: 290px;
float: left;
}
R A P I D A P P L I C AT I O N
DEVELOPMENT
STEP 3
Set the margins in between the boxes
<body> Output
<div id=“container”>
<div class=“inner”></div>
<div class=“inner”></div>
<div class=“inner”></div>
</div>
</body>
#container
{
width: 900 px;
}
.inner
{
background-color: red;
height: 100px;
WHY?
width: 290px;
float: left;
margin-right: 15px;
}
R A P I D A P P L I C AT I O N
DEVELOPMENT
STEP 3
Set the margins in between the boxes
Reason: Adding margin-right to all will increase the area
3 (290) + 15 + 15 + 15 = 915
<body> Output
<div id=“container”>
<div class=“inner”></div>
<div class=“inner”></div>
<div class=“inner”></div>
</div>
</body>
#container .inner:last-child
{ {
width: 900 px; margin-right: 0px;
} }
.inner
{
background-color: red;
height: 100px;
width: 290px;
float: left;
margin-right: 15px;
}
R A P I D A P P L I C AT I O N
DEVELOPMENT
This is when text below the floating elements comes in between when there is space
<body> Output
<div id=“container”>
<div class=“inner first”></div> Hello World
<div class=“inner last”></div>
<p> Hello World </p>
</div>
</body>
#container .first
{ {
width: 900 px; float: left;
} }
.inner .last
{ {
background-color: red; float: right;
height: 100px; }
width: 290px
}
R A P I D A P P L I C AT I O N
DEVELOPMENT
CSS Clear
The issue with bleeding can be fixed using CSS property called clear
Clear property can have three values: left, right and both
Left is used when items are floating on left, right is used when items are floating on right and
both is used when items are floating in both directions
R A P I D A P P L I C AT I O N
DEVELOPMENT Try Here https://codepen.io/drmisbha/pen/bGBqELy
<body>
Output
<div id=“container”>
<div class=“inner first”></div>
<div class=“inner last”></div>
<div class=“clear”></div>
<p> Hello World </p>
</div>
</body> Hello World
#container .first
{ {
width: 900 px; float: left;
} }
.inner .last
{ {
background-color: red; float: right;
height: 100px; }
width: 290px
} .clear {
clear: both;
}
R A P I D A P P L I C AT I O N
DEVELOPMENT
Next Lecture
Building from scratch using Built in CSS Layouts such as Using external CSS Frameworks such
floats and clear Grid and Flexbox as Bootstrap and so on
R A P I D A P P L I C AT I O N
DEVELOPMENT
THANK YOU