CSS Part 2
CSS Part 2
CSS layout
The 3 core concepts to understand about CSS layout are:
1. The CSS box model
2. Floating
3. Positioning
These 3 concepts control the way elements are arranged and
displayed on a page.
2
CSS Box Model
CSS Box Model
When a browser draws an object on a page, it
places it into an invisible rectangular space called
a “bounding box.”
You can specify the size, look, and feel of the
margins, the padding, the border, and the
contents of that bounding box.
Internet Explorer interprets CSS box styles
differently than most other web browsers.
CSS: Box Model
The margin is the area outside of the element. It never has color; it is
always transparent.
The border extends around the element, on the outer edge of any
padding. The border can be of several types, widths, and colors.
The padding exists around the content and inherits the background
color of the content area.
The content is surrounded by padding.
Padding
element
Border
Margin
The CSS Box Model
The Box Model control the way elements are arranged and displayed
on a page.
Every block element in CSS is divided into regions consisting:
1. Content
2. Padding
3. Border
4. Margins
6
Understanding the Box Model
The CSS box model describes the boxes that are formed
around elements in a Web page
The content area is the area that contains the box content
The box properties are used to add white space and a border around
the content
Padding is the white space that surrounds the box content
A border can be placed around the padding
The margin is white space outside the border
7
The CSS Box Model
Every element in HTML is considered a “box,” whether it is a paragraph, a
<div>, an image, or so on.
Boxes have consistent properties.
The box model describes the way in which every HTML block-level element
has the potential for a border, padding, and margin and how the border,
padding, and margin are applied.
<ul>
<ol> gkkkg </ol>
<ol> gkkkg </ol>
</ul>
<p> The box model <strong>describes</strong> the way in which every HTML
block-level element has the potential for a border, padding, and margin and
how the border, padding, and margin are applied.
</p>
Understanding the Box Model
The CSS box model describes the boxes that are formed
around elements in a Web page
The content area is the area that contains the box content
The box properties are used to add white space and a border around
the content
Padding is the white space that surrounds the box content
A border can be placed around the padding
The margin is white space outside the border
9
Settings
The core border properties are:
Width: absolute (px, in, cm, or ‘thin’, ‘medium’, ‘thick’), or
relative (ems)
Style: dotted, dashed, solid, double, groove, ridge, inset, outset,
hidden, etc
Color: ‘blue’, ‘red’, #FF9900, etc
You can also create the effect of a border by using a graphic
image in a CSS background property, instead of the border
property
10
Margins & Padding
Margins
Margins define the space around elements outside the border
Margin properties can have negative values in order to deliberately
overlap content
Margin properties will affect the position of background elements
(graphics and/or colours) in relation to the edges of the containing
block element
Margin properties can be defined independently on top, right,
bottom and left, or all-at-once using CSS shorthand
11
Margins & Padding
Padding
Padding defines the space around elements inside the border; i.e
between the border and the content itself
Padding properties cannot have negative values
Padding properties do not affect the position of background
elements (graphics and/or colors) in the containing block element;
only the position of content.
Padding properties can be defined independently on top, right,
bottom and left, or all-at-once using CSS shorthand
12
Margin
the margin property works with space outside of the
element.
Following are the style properties for setting margins:
margin-top—Sets the top margin
margin-right—Sets the right margin
margin-bottom—Sets the bottom margin
margin-left—Sets the left margin
margin—Sets the top, right, bottom, and left margins as a
single property
How to set Margin
To set a margin of 15 pixels on all sides.
First method:
margin-top:15px;
margin-right:15px;
margin-bottom:15px;
margin-left:15px;
<html>
<head>
<title>Color Blocks</title>
<style type=”text/css”>
body {
margin:0px;
}
div {
width:250px;
height:100px;
border:5px solid #000000;
color:black;
font-weight:bold;
text-align:center;
}
Example1: Simple Code to Produce Four Color <div>s with Borders and Margins (Cont.)
Div#d1 {
background-color:red;
margin:15px;
}
div#d2 {
background-color:green;
margin:15px;
}
div#d3 {
background-color:blue;
}
div#d4 {
background-color:yellow;
margin:15px;
}
</style>
</head>
Example1: Simple Code to Produce Four Color <div>s with Borders and Margins (Cont.)
<body>
<div id=“d1”>DIV #1</div>
<div id=“d2”>DIV #2</div>
<div id=“d3”>DIV #3</div>
<div id=“d4”>DIV #4</div>
</body>
</html>
Class Exercise 1: Simple Code to Produce Four Colored <div>s with Bordersand Margins
padding-bottom: 30px;
padding-left: 12px; 30
}
6
div#d4 {
background-color:yellow;
text-align:center;
padding:50px;
}
</style>
</head>
<body>
<div id=”d1”>DIV #1</div>
<div id=”d2”>DIV #2</div>
<div id=”d3”>DIV #3</div>
<div id=”d4”>DIV #4</div>
</body>
</html>
CSS Float
Understanding the float property
The float property allows elements to be moved around in
the design such that other elements can wrap around them.
used in conjunction with images, etc.
Elements float horizontally, not vertically, thus, two possible
values: right and left.
example,
if you have three <div>s float values of left, they will all line up
to the left of the containing body element.
If you have your <div>s within another <div>, they will line
up to the left of that element, even if that element itself is
floated to the right.
Floats
Syntax: inline
text text text text text text text text text text text
float: position; text text text text text text text text text text text
text text text text text text text text text text text
text text text
inline
text text text text text text text text
where position is left, right, text text text text text text text text text text text
text text text text text text text text text text text
or none. text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
block text text text text text text text textinline
text text text
text text text text text text text text text text text
Setting the float text text text text text text text text text text text
text text text text text text text text text text text
property to left or right text text text text text text text text text text text
text text text text text text text text text text text
causes a box to be text text text text text
30
Restoring the Normal Flow: “Clear”
To restore the “normal flow”,
inline
text text text text text text text text text text text
we can use the clear property. text text text text text text text text text text text
text text text text text text text text text text text
The clear property has text text text
inline
text text text text text text text text
text text text text text text text text text text text
three value options: text text text text text text text text text text text
text text text text text text text text text text text
clear: left; text text text text text text text text text text text
text text text text text text text text text text text
clear: right; block text text text text text text text textinline
text text text
text text text text text text text text text text text
clear: both; text text text text text text text text text text text
text text text text text text text text text text text
These specify which text text text text text text text text text text text
text text text text text text text text text text text
side of the element’s box text text text text text
may not have a float
next to it.
block
31
Example1: Using float to Place <div>s
<html> div#d2 {
<head> background-color:green;
<title>Color Blocks</title> float:left;
<style type=”text/css”> }
body { div#d3 {
margin:0px; background-color:blue;
} float:left;
div { }
width:250px; div#d4 {
height:100px; background-color:yellow;
border:5px solid #000000; float:right;
color:black; }
font-weight:bold; </style>
margin:25px; </head>
} <body>
div#d1 { <div id=”d1”>DIV #1</div>
background-color:red; <div id=”d2”>DIV #2</div>
float:left; <div id=”d3”>DIV #3</div>
} <div id=”d4”>DIV #4</div>
</body> </html>
CSS Positioning (CSS-P)
• CSS-P was released by W3C in 1997
34
CSS Positioning
There are different types of positioning that can be applied to CSS
boxes:
• Relative Positioning
• Absolute Positioning
• Static Positioning
• Fixed Positioning
Understanding the differences between the two is difficult at first, but
important!
property value description
position static default position
relative offset from its normal static position
absolute a fixed position within its containing element
fixed a fixed position within the browser window
top, bottom, positions of box's corners
left, right
35
CSS Positioning: Relative Positioning
A relatively positioned element will stay exactly where it is, in
relation to the normal flow.
You can then offset its position “relative” to its starting point in the
normal flow:
Containing box
36
CSS Positioning: Relative Positioning
In this example, box 2 is offset 20px, top and left. The result is the box
is offset 20px from its original position in the normal flow.
Box 2 may overlap other boxes in the flow, but other boxes still
recognise its original position in the flow.
top: 20px
37
CSS Positioning: Absolute Positioning
top: 20px
Left: 20px
Box 1 Box 3
Position: absolute
Box 2
top: 20px
#myBox {
Left: 20px
position: absolute;
Box 1 Box 3
left: 20px; Position: absolute
top: 20px;
Box 2
}
Containing box (relatively positioned ancestor)
39
CSS Positioning: Fixed Positioning
Fixed Positioning is a sub-category of Absolute Positioning
Allows the creation of floating elements that are always fixed in the
same position in the browser window, while the rest of the content
scrolls as normal
(rather like the effect of fixed background attachments)
PROBLEM: fixed positioning is not supported in IE5 and IE6(!), but
can be made to work with JavaScript for those browsers
Div #ad {
position: fixed;
right: 10%;
top: 45%;
}
40 CSS
Fixed positioning
“Fixed positioning” has a lot in common with
absolute positioning:
the element is removed from the normal flow of the
page, and the coordinate system is relative to the
entire browser window. The key difference is that
fixed elements don’t scroll with the rest of the page.
removed from normal flow (like floating ones)
positioned relative to the browser window
even when the user scrolls the window,
element will remain in the same place
Floats & Positioning
Summary:
Floats (also a form of positioning) takes boxes out of the normal flow and
“floats” them left or right edges of the containing block
Relative Positioning is “relative” to the element’s initial position in the
normal flow.
Absolute Positioning is “relative” to the nearest positioned ancestor, or (if
one doesn’t exist) the initial container block (usually the browser
window)
Fixed Positioning is fixed in one position “relative” to the browser window
only — does not scroll with content (Not supported in IE5, IE6)
42
Internal Style sheet
43
External Style Sheets
Save the CSS file with a .css filename extension.
In the <head> section of the XML file, enter the following code
to link to the external CSS file:
44
End
The CSS Box Model
div {
width: 250px;
height: 100px;
background-color: #ff0000;
border: 1px solid #000000;
}
div#d2 {
width: 250px;
height: 100px;
background-color: #ff0000;
border: 5px solid #000000;
margin: 10px;
padding: 10px;
}
How to justify the content with CSS
The HTML attribute is used to justify-content Example
is align attribute. The align attribute of <p> <!DOCTYPE html>
tag is used to justify the text on a web page. <html>
This can be done by assigning the value to the
aligned attribute as justified. <head>
<title>Justified content</title>
</head>
<body>
<h3>The content is justified</h3>
Syntax
text-align: start; <p align="justify">
text-align: end; Welcome to Geeks for Geeks. It is a
computer science portal for geeks.
text-align: left;
It contains well written, well thought articles.
text-align: right; We are learning how to justify content on a
text-align: center; web page.
text-align: justify; </p>
</body>
</html>
3.11 Background Images <style>
body {
- The background-image property background-image: url('img_girl.jpg');
background-repeat: no-repeat;
→ SHOW back_image.html and background-attachment: fixed;
display background-size: cover;
}
- Repetition can be controlled </style>
- background-repeat property
- Solution: a new tag to define an element in the content of a larger element - <span>
<p>
Now is the <span> best time </span> ever!
</p>
...
<p>
Now is the
<span class = "bigred">
best time </span> ever!
</p>
3.12 The <span> and <div> tags (continued)
- The <span> tag is similar to other HTML tags, they can be nested and they
have id and class attributes
- Used to create document sections (or divisions) for which style can be
specified
- e.g., A section of five paragraphs for which you want some particular
style
CSS Float
If you want to wrap content around other content (such as
text around a picture), you can use the float property.
Slide 51 (of 54) CS 22: Enhanced Web Site Design - Cascading Style
Sheets
3.10 CSS Float
- Margin – the space between the border of an element and its
neighbor element
Absolute positioning defines the position of a given bounding box from the top and left
side margins of the web page. This not only allows objects to be placed in an exact
location, it also allows objects to be placed one on top of another.
Relative positioning defines the positioning in such a way that elements are offset from
the previous element in the HTML code. This allows objects to be placed in relation to
one another.
Fixed positioning defines the position of a given box relative to the window and remains
in its specified location even as the content scrolls underneath it. This value does not
work in Internet Explorer 6 or earlier. In IE 7, the browser must be in “standards-
compliance mode”.
Inherit positioning explicitly sets the value to that of the parent (if the parent is
position:absolute, the child will be position:absolute; if the parent is position:fixed, the child
will be position:fixed).
Static positioning is the default. It defines the position of a given box essentially as an
unpositioned element – it flows in the normal rendering sequence of the web page.
Example: position:absolute
Example: position:relative
Example: position:fixed (Code View)
<!DOCTYPE html> <div id="main">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing
<html> elit. Quisque ultrices, nibh ac rhoncus fermentum,
orci sem dapibus nisi, sed tincidunt lectus lectus at
<head> augue. In consectetuer vehicula enim. In hac habitasse
platea dictumst. Donec a nisl vitae tortor tristique
<title>Untitled Document</title> viverra. Sed at lorem a ante lobortis molestie. Nulla
ullamcorper urna accumsan diam. Aliquam non eros.
<style type="text/css"> Pellentesque egestas ultricies enim. Aenean lobortis.
Nulla interdum commodo turpis. Sed ut mi id elit
<!-- vehicula sollicitudin. Sed lobortis, ligula sit amet
euismod egestas, mi ante iaculis nunc, ut rhoncus
#links { magna lectus ac arcu. In hac habitasse platea
dictumst. Proin quis ligula vitae quam pharetra
position:fixed; adipiscing. Pellentesque tincidunt suscipit nibh. Ut
fermentum suscipit justo. </p>
border:dotted;
<p>Fusce purus lectus, ultricies nec, aliquam at,
border-color:#000000; facilisis id, arcu. Vestibulum quis mi vel massa
porta hendrerit. Nulla ullamcorper ligula nec lectus.
width:20%; Quisque tempor, augue in molestie gravida, eros arcu
luctus tortor, eu dignissim diam urna sed urna. Ut
height:100%; dictum ultrices lacus. In hac habitasse platea
dictumst. Suspendisse sed purus blandit metus
z-index:1; ultricies suscipit. Proin diam justo, consequat id,
rhoncus eget, facilisis ut, lacus. Vivamus dignissim
left: 0px; dui in justo. Suspendisse elit. Nam nulla tortor,
fringilla sed, faucibus quis, ullamcorper a, leo.
top: 0px; Fusce blandit condimentum turpis. Pellentesque vel odio
et odio suscipit egestas. Nullam ullamcorper sagittis
background-color: #FFFFCC; ipsum. Maecenas fringilla malesuada pede. Duis ut
quam. </p>
} </div>
#main { <div id="links">
position:absolute; <p>This area is fixed and will never move. It's good for
things like navigation bars.</p>
left:25%; <ul>
top:0px; <li><a href="page1.html">Page 1</a></li>
width:70%; <li><a href="page2.html">Page 2</a></li>
<li><a href="page3.html">Page 3</a></li>
}
<li><a href="page4.html">Page 4</a></li>
-->
<li><a href="page5.html">Page 5</a></li>
</style> </ul>
</head> </div></body></html>
Example: position:fixed (in Firefox)
Relative Or Absolute Positioning
<html>
<head>
<title>Positioning the Color Blocks</title>
<style type=”text/css”>
div {
position:absolute;
width:250px;
height:100px;
border:5px solid #000;
color:black;
font-weight:bold;
text-align:center;
}
div#d1 {
background-color:red;
left:0px;
top:0px;
z-index:0;
}
Example1: z-index to Alter the Display of Elements in the Color Blocks Sample (Continued)
div#d2 {
background-color:green;
left:75px;
top:25px;
z-index:3;
}
div#d3 {
background-color:blue;
left:150px;
top:50px;
z-index:2;
}
Example1: z-index to Alter the Display of Elements in the Color Blocks Sample (Continued)
div#d4 {
background-color:yellow;
left:225px;
top:75px;
z-index:1;
}
</style>
</head>
<body>
<div id=”d1”>DIV #1</div>
<div id=”d2”>DIV #2</div>
<div id=”d3”>DIV #3</div>
<div id=”d4”>DIV #4</div>
</body>
</html>
Practice
Read the following case study and give reasons for asking certain question in relation to style
sheets.
A school wishes to design a website. The function of this website is to let its
students know when assignments are due, the dates of exams, timetables,
subjects taught, syllabuses and the teachers who teach them.
The school wishes to have quality controls over the Web design, since the
teachers themselves will be responsible for updating the information on the
Web.
1. The school has approached you to advise them on how this may be possible.
2. The school wants you to explain the process you wish to use, and why it is
better than letting every teacher write their own style.
3. Write a list of ten or more HTML tags and classes that may be used for the
website. Consider the attributes that may have to be used, such as background
colour, departmental style, tables (for timetable, exam, curriculum and annual),
headers and so on.
4. After choosing the HTML tags, consider the CSS code required to style them.
Additional Resources
1. https://webdesign.tutsplus.com/articles/what-you-may-
not-know-about-the-z-index-property--webdesign-16892
2. https://www.w3schools.com/cssref/pr_pos_z-index.asp