Today we will be making a site from scratch using divs and spans to create a header, footer, sidebar and a content area.
So far, we have mostly seen "block" elements
They appear on the next line, like paragraphs
There are also "inline" elements
They appear on the same line that they are written on.
<div>
<p>Content<p>
<p>Content<p>
</div>
<div id="header">
<h1>Main Heading<h1>
</div>
<div class="sub-content">
<p>Some more content<p>
</div>
.align-right{
text-align:right;
color: purple;
font-weight: bold;
}
<div class="align-right">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit</p>
<p>Sed do eiusmod tempor incididunt ut labore et dolore.</p>
</div>
<p>Magna aliqua. Ut enim ad minim veniam.</p>
<p>Quis nostrud exercitation ullamco.</a>
Lorem ipsum dolor sit amet, consectetur adipisicing elit
Sed do eiusmod tempor incididunt ut labore et dolore.
Magna aliqua. Ut enim ad minim veniam.
Quis nostrud exercitation ullamco.
Let's create a site using divs to separate content into different sections on our page.
Create a header, content area, sidebar, and a footer.
Span is used to apply a specific style inline
.yellow{
color:yellow;
}
<p>Paragraph with <span class ="yellow">yellow</span> text.</p>
Paragraph with yellow text.
Let's add some spans to our content to help highlight some text.
Space between the border and the content
Space between the border and the content
Adds to the total width of the box.
padding: 15px;
10 pixels on top only
padding-top: 10px;
10 on top, 5 on right, 3 on bottom, 5 on left
padding: 10px 5px 3px 5px;
Four values
padding: top right bottom left;
Two values
padding: top/bottom right/left;
One value
padding: all;
padding: 10px 20px 30px 40px;
The edge around the box, specified as "thickness, style, color."
A solid red border
border: 1px solid #ff0000;
A thick dotted black top border
border-top: 4px dotted #000000;
Two different border styles
border-top: 1px solid #ff0000;
border-bottom: 4px dotted #000000;
border-width: 10px;
border-style: dashed;
border-color: #666666;
You can specify each property separately, or all three together.
The transparent area around the box that separates it from other elements.
15 pixels on all sides
margin: 15px;
10 on top, 5 on right, 3 on bottom, 5 on left
margin: 10px 5px 3px 5px;
10 pixels on top
margin-top: 10px;
If a margin is set to auto on a box that has width, it will take up as much space as possible.
CENTERED
margin: auto;
width: 300px;
FLUSH-RIGHT
margin-left: auto;
margin-right: 5px;
width: 300px;
Let's add some padding, borders, and margins to our divs.
Let's center our entire document in the browser.
Sets the width of an element.
Does not include padding or borders, remember these add to the width.
Sets the height of an element.
Does not include padding or borders, remember these add to the width.
Add a width & height to our divs.
Use IDs to target each div with CSS
The browser has a set of pre-defined styles for each element.
To have more control over each element's styles, use a CSS reset.
Without CSS Reset
With CSS Reset
Eric Meyer has created a basic reset that you can copy int your pre-existing CSS file.
meyerweb.com/eric/tools/css/reset/
Basic Reset for today:
html, body, div, span, h1, h2, h3, h4, h5, h6,
p, a, em, strong, ol, ul, li {
margin: 0;
padding: 0;
border: 0;
}
ol, ul {
list-style: none;
}
Add a CSS reset to the top of your CSS file to see what happens!
HTML elements are positioned static by default.
Static elements are positioned in the normal flow of the page
Static elements ignore top, bottom, right or left property specifications.
In normal flow, inline boxes flow from left to right, wrapping to next line when needed.
<img src="images/cookie1.png"/>
<img src="images/cookie2.png"/>
<img src="images/cookie3.png"/>
...
<img src="images/cookie2.png"/>
<img src="images/cookie3.png"/>
In normal flow, block boxes flow from top to bottom, making a new line after every box.
<p>Greetings</p>
<p>Hello</p>
<p>Hi there!</p>
Greetings
Hello
Hi there!
The "relative" value will still put the element in the normal flow, but then offset it according to top/left/right/bottom properties.
.relative{
position: relative;
left: 80px;
top: 20px;
height: 100px;
background-color: yellow;
}
The "absolute" value will take the element out of the normal flow and position it in relation to the window (or the closest non-static element).
.top{
position: absolute;
top: -40px;
right: 10px;
background-color: yellow
}
.bottom{
position: absolute;
bottom: -40px;
left:60px;
background-color: green
}
Here's an example of an image with a caption absolutely positioned over top of it.
The containing div has a position of relative, and the caption has a position of absolute.
When you use positioning to move elements out of the normal flow of content, elements can overlap. You can change the order of overlapping with z-index.
The element with highest z-index goes on top.
.bottom{
position: absolute;
bottom: 10px;
left:60px;
background-color: yellow;
}
.top{
position: absolute;
bottom: 15px;
left:60px;
background-color: green;
z-index: 2;
}
Let's add some positioning.
Let's create a div that contains an image and a caption, and position the caption absolutely overtop the image.
Below a <blockquote> is floated to the left, allowing text to wrap around it on the right
.float{
float:left;
width:200px;
background:yellow;
}
If you want two block level elements to be side by side, you need to float both elements. One left, and one right.
clear: right;
clear: left;
clear: both;
.float{
float:left;
width:50px;
background:yellow;
}
.clear-left{
clear:left
}
Let's float our side bar and content areas.
Changing the format of a link when you hover over it is accomplished by using pseudo-classes.
CSS pseudo-classes are used to add special effects to some selectors.
selector:pseudo-class
{
property:value;
}
Example:
a:link
{
text-decoration: none;
}
a:link {color:#FF0000;} /* unvisited link */
a:visited {color:#00FF00;} /* visited link */
a:hover {color:#FF00FF;} /* mouse over link */
a:active {color:#0000FF;} /* selected link */
Note: a:hover MUST come after a:link and a:visited in the CSS definition in order to be effective!
Note: a:active MUST come after a:hover in the CSS definition in order to be effective!
Add pseudo classes to your links