Your Content
+ HTML: Structure
+ CSS: Presentation
= Your Website
A website is a way to present your content to the world, using HTML and CSS to present that content & make it look good.
As a general coding principle, Don't Repeat Yourself (DRY).
To reuse CSS, we use IDs and classes.
ID -- Should only apply to one element on a webpage, i.e., a webpage only has one footer.
The "#" is how you tell CSS "this is an id."
Class -- Lots of elements can have the same class, i.e., There can be many warnings on one webpage.
The "." is how you tell CSS "this is a class name."
#footer {
property: value;
}
Selects all elements with an id of "footer".
<p id="footer">Copyright 2016</p>
The associated HTML.
.warning {
color: red;
}
Selects all elements with a class of "warning".
<p class="warning">It's a trap!</p>
The associated HTML.
p em {
color: yellow;
}
Selects all em elements that are within a paragraph
<p>This is <em>important.</em></p>
The associated HTML.
So this code:
ul li a strong{
color: purple;
}
Means "find a strong tag inside a link inside a list item in an unordered list"
<ul>
<li><a href="programs.html">Our <strong>program</strong></a></li>
</ul>
Styles "cascade" down until changed
p{
color:blue;
font-family: 'Helvetica';
}
.red {
color: red;
}
#special {
font-family: Arial;
}
<p>Paragraph</p>
<p class ="red">Paragraph</p>
<p class = "red" id ="special">Paragraph</p>
Your browser also assigns priority based on the specificity of the selection. More specific selectors have higher priority.
.main .sale .clearance p{ //Most specific
color: red;
}
.header .title p{
color: green;
}
.footer p{ //Least specific
color: blue;
}
The tie-breaker is position. Rules lower in the file overwrite rules higher in the file
a{
background-color: yellow;
}
a{
background-color: teal;
}
a{ //This rule wins
background-color: black;
}
CSS also allows you to specify a set of styles to multiple selectors at once.
#content p, footer .container p {
color: blue;
background: #333;
}
p, .list li, a {
color: red;
}
You can have as many comma separated selectors as you want. Each CSS element is separated by a space and a comma.
Add some comma separated CSS to your site to apply styles to multiple selectors.
Every HTML element has a default 'display' type. For most, they will either be 'inline' or 'block'.
Block: a block element will take up the full width of available space and will stay on its own line.
Inline: an inline element will only take up as much space as the content it has and it will not start on a new line.
Block: div, h1, p
Inline: img, span, a
Inspect elements below to see how they act in HTML:
This is a paragraph
Using CSS, we can change the default value for the 'display' of an HTML element. So if we wanted the default display of inline to change for an img tag, we could use this css:
img {
display: block;
}
This will cause the img tag to sit on it's own line and take up as much space as it can.
Span tags are primarily used within headings or paragraphs to manipulate certain sections of text.
<p>This is normal text <span class="highlight">but this is specially colored </span> but now it's normal text again. </p>
CSS to go with that HTML:
.highlight {
color: red;
}
Add a span tag with a class on it inside one of your headings or paragraphs. Then style it with CSS.
You can apply rounded corners to any HTML element by using the CSS property: border-radius. The higher the border radius number the more rounded the corner is.
A border radius is primarily added if your HTML element has a background color, border, or shadow to it. Otherwise you won't be able to see the rounded corners.
The first example rounds the corners of every side by 10px.
The second example (class circle) creates a circle because the border-radius is half of the width and height.
img {
border-radius: 10px;
}
.circle {
border-radius: 20px;
width: 40px;
height: 40px;
}
.box {
border-radius: 5px 10px 20px 5px;
}
The last example above shows how you can apply different border-radius amounts to different corners of the box.
Pseudo classes are used to target specific HTML elements based on a change in state, location compared to other HTML elements, and attributes.
You can target these 'states' by adding a colon (:) after a CSS selector and then the state of the HTML element you want to target.
Here are some examples
In the examples below, p:hover would be used if you wanted to change the CSS for paragraphs when someone hovers over them with their mouse.
p:hover {
color: red;
}
a:hover {
color: blue;
}
A more common example is the second example above for the link element. It's good practice to give your links a defined hover state.
Using the ":hover" pseudo class, add a hover state to one of your html elements
On the other hand, pseudo elements allow you to insert content into your site through CSS using :before and :after.
The :before pseudo inserts content before the HTML's content and the :after inserts content after the HTML's content.
When writing the CSS for this, we always need to include the 'content' property. The property value can have text, an icon, or an image, but not HTML. After you have specified the content, you can apply any other styles that you want.
p:before {
content: 'this is added text';
color: white;
font-size: 30px;
background: blue;
}
p:after {
content: url(images/testimage.png);
}
When adding text/icon content, you have to wrap the text in quotes.
Using the ":before" or ":after" pseudo element, add additonal text to your site through CSS.