Introduction to CSS
Introduction to CSS
* {
color: blue; /*a comment */
margin: 10px;
font: 14px Tahoma;
}
This will change all the tags in my web ( ‘*‘ means all) to look blue with font Tahoma with
14px, and leaving a margin of 10px around.
CSS fields
Here is a list of the most common CSS fields and an example:
● color: #FF0000; red; rgba(255,00,100,1.0); //different ways to specify colors
● background-color: red;
● background-image: url('file.png');
● font: 18px 'Tahoma';
● border: 2px solid black;
● border-top: 2px solid red;
● border-radius: 2px; //to remove corners and make them more round
● margin: 10px; //distance from the border to the outer elements
● padding: 2px; //distance from the border to the inner elements
● width: 100%; 300px; 1.3em; //many different ways to specify distances
● height: 200px;
● text-align: center;
● box-shadow: 3px 3px 5px black;
● cursor: pointer;
● display: inline-block;
● overflow: hidden;
CSS how to add it
There are four ways to add CSS rules to your website:
div {
background-color: red;
}
This CSS rule means that every tag DIV found in our website should have a red background
color. Remember that DIVs are used mostly to represent areas of our website.
We could also change the whole website background by affecting the tag body:
body {
background-color: red;
}
CSS selectors
What if we want to change one specific tag (not all the tags of the same type).
We can specify more precise selectors besides the name of the tag. For instance, by class
or id. To specify a tag with a given class name, we use the dot:
p.intro {
color: red;
}
This will affect only the tags p with class name intro:
<p class="intro">
CSS Selectors
There are several selectors we can use to narrow our rules to very specific tags of our website.
This will affect to the p tags of class intro that are inside the tag div of id main
<div id="main">
<p class="intro">....</p> ← Affects this one
</div>
div#main.intro:hover { ... }
will apply the CSS to the any tag div with id main and class intro if the mouse is over.
And you do not need to specify a tag, you can use the class or id selectors without tag, this
means it will affect to any node of id main
#main { ... }
CSS Selectors
If you want to select only elements that are direct child of one element (not that have an
ancestor with that rule), use the > character:
Finally, if you want to use the same CSS actions to several selectors, you can use the
comma , character:
.grid-item1 {
background: blue;
border: black 5px solid;
grid-column-start: 1;
grid-column-end: 5;
grid-row-start: 1;
grid-row-end: 3;
}
CSS
Fullscreen divs html, body {
width: 100%;
height: 100%;
Sometimes we want to have a div that covers }
the whole screen (to make a webapp),
div {
instead of a scrolling website (more like margin: 0;
regular documents). padding: 0;
}
Understanding the Box Model: a good explanation of how to position the information on your
document.