CSS Programing WEB
CSS Programing WEB
ii
Contents
CSS Inheritance
1.1
Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2
1.2.1
Automatic Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2.2
Forced/Applied Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.3.1
1.3.2
Inheritance Manipulations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.3.3
1.3.4
Class Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.4
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.5
Download . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.3
2.2
2.3
10
Alignment Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.1.2
Color Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.1.3
Size Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.1.4
Text Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.1.5
Elements Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2.2
Some Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.3.1
Do not Overwrite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.3.2
2.3.3
2.4
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.5
Download . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.2
3.3
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.4
Download . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.2
4.3
4.4
26
4.1.2
Spacing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.2.2
Border Styling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
4.2.3
Cell Styling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
4.3.2
4.3.3
4.3.4
4.3.5
Table Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
4.3.6
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Download . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
42
5.1
Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
5.2
Basic Styling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5.3
21
3.1
4.1
iii
5.2.1
5.2.2
5.2.3
Button States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Start Small . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
5.3.2
Icons on Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
5.3.3
Gradients on Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
5.3.4
Patterns on Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
5.4
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
5.5
Download . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
52
6.1
Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
6.2
6.3
6.4
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
6.5
Download . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
iv
58
7.1
7.2
7.2.2
7.3
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
7.4
Download . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
63
8.1
Basic Set Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
8.2
Variations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
8.2.1
Pressed Effect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
8.2.2
8.2.3
8.2.4
8.2.5
8.2.6
8.2.7
8.2.8
8.3
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
8.4
Download . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
9.2
9.3
71
Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
9.1.1
Basic Set Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
9.1.2
9.1.3
9.2.2
9.3.2
9.3.3
9.3.4
9.3.5
9.4
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
9.5
Download . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
82
89
95
vi
vii
Preface
Cascading Style Sheets (CSS) is a style sheet language used for describing the look and formatting of a document written in a
markup language. Although most often used to change the style of web pages and user interfaces written in HTML and XHTML,
the language can be applied to any kind of XML document, including plain XML, SVG and XUL. Along with HTML and
JavaScript, CSS is a cornerstone technology used by most websites to create visually engaging webpages, user interfaces for web
applications, and user interfaces for many mobile applications.
CSS is designed primarily to enable the separation of document content from document presentation, including elements such
as the layout, colors, and fonts. This separation can improve content accessibility, provide more flexibility and control in the
specification of presentation characteristics, enable multiple HTML pages to share formatting by specifying the relevant CSS in
a separate .css file, and reduce complexity and repetition in the structural content, such as semantically insignificant tables that
were widely used to format pages before consistent CSS rendering was available in all major browsers. CSS makes it possible
to separate presentation instructions from the HTML content in a separate file or style section of the HTML file. (Source:
https://en.wikipedia.org/wiki/Cascading_Style_Sheets)
In this ebook, we provide a compilation of CSS based examples that will help you kick-start your own web projects. We cover a
wide range of topics, from text styling and table design, to class inheritance and hover effects. With our straightforward tutorials,
you will be able to get your own projects up and running in minimum time.
viii
1 / 100
Chapter 1
CSS Inheritance
In this example, well focus on a css property value that I guess many of you ignore (do not use) while styling elements on
websites.
It is inherit. Before we go straight into that, know the following:
Each element in HTML is part of a tree, and every element (except the initial html element) has a parent element that encloses
it.
Whatever styles applied to the parent element can be applied to the elements inside it if the properties are inherited.
Below, were going to have a look at several cases of inheritance property.
1.1
Prerequisites
Create a html document with the basic syntax inside like this:
<!DOCTYPE html>
<html>
<head>
<title>CSS Inheritance Property Value</title>
</head>
<body>
<!-- STYLE SECTION -->
<style type="text/css">
</style>
<!-- HTML SECTION -->
</body>
</html>
In the html section, well add some elements to show their relation to other elements and understand when they automatically
inherit property values from their parents and when not.
Lets first see a basic application of the inherit property value.
1.2
The inherit property value is at some cases automatically applied to child elements, and at other cases needs to be applied
manually.
1.2.1
Automatic Inheritance
I always tend to give the body element a custom font-family so that all elements have the same font applied.
In other words, all elements inherit the font-family property from the body parent.
Look at the code below:
<!-- HTML SECTION -->
This is a <span>parent</span> element.
<div class="child">This is a <span>child</span> element.
This is a <a href="#">link</a> inside a paragraph
<!-- end child element -->
</div><!-- end parent element -->
I have added a parent division and inside that two children, another div and a paragraph.
The classes given are so that we can give attributes to parent and see the results to children.
Now below look at the some initial properties Ive given to these elements:
<!-- STYLE SECTION -->
<style type="text/css">
body {
font-family: "Aller","sans-serif";
}
.parent {
width: 15em;
height: 5em;
color: gray;
font-weight: bold;
font-size: 2em;
border: 0.1em solid red;
}
.parent span {
color: green;
}
</style>
Now to see what what properties have been inherited look at the browser view:
2 / 100
3 / 100
1.2.2
Forced/Applied Inheritance
Simple as that, refer to the .child class and inherit the border for the child and color property from parent for the link:
.parent a {
color: inherit;
}
.child {
border: inherit;
inside */
}
4 / 100
1.3
Here we look at some essential cases and examples where we can use the inherit property value.
1.3.1
Change the html code to accommodate the following image and text elements:
<h4>Everything you can imagine is real.<br>-The Parent Image</h4>
<div class="child-image">
<h4>Glad to have looked after you.<br>-The Child Image</h4>
<!-- end child image -->
</div><!-- end parent image -->
5 / 100
}
.child-image {
width: inherit;
height: inherit;
background-image: inherit;
background-repeat: inherit;
}
/* inherited width */
/* inherited height */
/* inherited background */
Now notice the child element inherits the background and size attributes from the parent:
1.3.2
Inheritance Manipulations
You can also use the inherit property value to avoid inheritance from one level higher parent.
Look at the codes below:
HTML
<!-- HTML SECTION -->
<div class="child1">
<h3>A title here</h3>
A paragraph text here
<button>A button here</button>
<!-- end child1 element -->
CSS:
.parent h3 {
font-variant: small-caps;
color: red;
}
6 / 100
.parent p {
text-indent: 1em;
color: blue;
}
.parent button {
padding: 1em 2em;
background-color: gray;
color: white;
border-radius: 0.5em;
border: 0.1em solid gray;
}
.child2 p {
from parent */
text-indent: inherit;
color: inherit;
}
.child2 h3 {
parent */
font-variant: inherit;
color: inherit;
}
.child2 button {
parent */
background-color: inherit;
color: inherit;
}
1.3.3
7 / 100
Tag Inheritance
To explain this, take for example the button tag of html:
<button>Button</button>
And apply this class to all elements wrapping a title like so:
2. Why us?
Just a paragraph to show that this is not a title.
<ul>
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
3. Help Center
Notice that elements given the title class now have a title look and feel.
8 / 100
1.3.4
Class Inheritance
CSS
.text-style-1 {
color: #50838e;
font-weight: italic;
font-size: 1.2em;
}
.link {
color: blue;
text-decoration: underline;
}
.bold {
font-weight: bolder;
}
9 / 100
1.4
Conclusion
On a more professional approach, we can state that using inheritance is not of much usage as most elements will need specific
styling and independence from parent elements they are in.
However, when using inheritance property value to give elements styling attributes, keep in mind that inherit can be applied
just as a single value (i.e you cant have something like: border: inherit 1em #ccc; ) so you dont get individual values inherited.
If you want to see the browser default styles (from which your element attributes are inherited), you can inspect element and
check "Show Inherited Properties" in Chrome.
1.5
Download
Download You can download the full source code of this example here: CSS Inheritance Example
10 / 100
Chapter 2
2.1
In this step, lets create different kind of classes and give attributes to them to set up a group of classes that we can later use in
html. So, first create a basic html file with a style tag in it where we can add the css, like below:
<!DOCTYPE html>
<html>
<head>
<title>Multiple Classes on Elements</title>
</head>
<body>
<!-- STYLE SECTION -->
<style type="text/css">
</style>
<!-- HTML SECTION -->
</body>
</html>
2.1.1
Alignment Classes
Creating alignment classes will enable us to easy align elements on the web page. Lets name two alignment classes:
.pull-left - this will align an element to the left .pull-right - this will align an element to the right
The respective CSS code for these classes would be:
<style type="text/css">
body{
11 / 100
/* ALIGNMENT CLASSES */
.pull-left {
float: left !important;
}
.pull-right {
float: right !important;
}
</style>
2.1.2
Color Classes
Creating color classes will let us color elements/backgrounds on the web page easier.
Lets name four color classes and three background color classes:
.red - this will give a text element or the border of a shape the red color. .green - this will give a text element or the border
of a shape the green color. .blue - this will give a text element or the border of a shape the blue color. .white - this will give
a text element or the border of a shape the white color.
.bg-red - this will give an element a red background color. .bg-green - this will give an element a green background color.
.bg-blue - this will give an element a blue background color.
The respective CSS code for these classes would be:
/* COLOR CLASSES */
.red, .red li, .red a {
color: #e85139;
}
.bg-red
{
/* bg-red class referred and given backgrounf color attr
/
*
background-color: #e85139;
}
.bg-green {
/* bg-green class referred and given background color attr
*/
background-color: #4ca640;
}
.bg-blue
*/
12 / 100
background-color: #319cd6;
}
Note that we also added the color attributes for li or a tags on the html.
That is so we can access lis or as directly with these classes.
2.1.3
Size Classes
Creating size classes will let you control the elements sizes on the web page. Lets name three size classes:
.small - this will make an element look small on the screen. .normal - this will make an element look medium on the screen.
.large - this will make an element look large on the screen.
The respective CSS code for these classes would be:
/* SIZE CLASSES */
.small {
font-size: 1em;
padding: 0.2em;
width: 50%;
height: 50%;
}
.normal {
font-size: 1.5em;
padding: 0.4em;
width: 100%;
height: 100%;
}
.large {
font-size: 2em;
padding: 0.6em;
width: 150%;
height: 150%;
}
2.1.4
Text Classes
Creating text classes will make it easier to control text appearance. Lets name four text classes:
.decor1 - this will make the text underline and uppercase. .decor2 - this will make the text overline and lowe
rcase. .decor3 - this will make the text line-through and capitallize. .center - this will make the text align
center.
The respective CSS code for these classes would be:
/* TEXT CLASSES */
.decor1 {
text-decoration: underline;
text-transform: uppercase;
font-weight: bold;
}
.decor2 {
text-decoration: overline;
text-transform: lowercase;
font-weight: bold;
13 / 100
}
.decor3 {
text-decoration: line-through;
text-transform: capitalize;
font-weight: bold;
}
.center {
position: relative;
bottom: 0px;
text-align: center;
}
2.1.5
Elements Classes
It is time to create some specific elements classes, which will represent the initial attributes of the elements we are going to give
several classes. Here is what we will create:
.button - a very popular element which will be used to demonstrate. .menu - a four links menu with very little prestyled
attributes. .rectangular - a prestyled fixed width and height element as example.
The respective CSS code for these classes would be:
/* ELEMENTS CLASSES */
.button {
border: 0.1em solid;
border-radius: 0.3em;
width: 5em;
height: 2em;
line-height: 2em;
margin-top: 5em;
margin-bottom: 2em;
}
.menu li {
display: inline;
padding-right: 1em;
text-decoration: none;
}
.menu {
margin-bottom: 2em;
}
.rectangular {
*/
border: 0.1em solid;
width: 10em;
height: 5em;
line-height: 5em;
margin-bottom: 2em;
}
2.2
Here is where the magic happens, we will first create the basic elements structure in html
and then continue adding appropriate classes to each elements to see the results of what we;ve done so far!
2.2.1
14 / 100
Under your tag start the html code. Add four elements: a button, a menu, a rectangular and an image like so:
(Do not give these elements classes, we will do that next)
<!-- HTML SECTION -->
<!-- button element -->
<a href="#">Button</a>
<!-- menu element -->
<ul>
<li
<li
<li
<li
</ul>
class=""><a
class=""><a
class=""><a
class=""><a
href="#">Home</a></li>
href="#">About</a></li>
href="#">Help</a></li>
href="#">Contact</a></li>
Because none of these elements is styled (or better say, not given a class) the view we would get is this:
15 / 100
class=""><a
class=""><a
class=""><a
class=""><a
href="#">Home</a></li>
href="#">About</a></li>
href="#">Help</a></li>
href="#">Contact</a></li>
Note that the img element doesnt still have a class, because we did not create attributes especially for it.
Now lets see what weve got in the browser.
2.2.2
Now lets go ahead and add classes that weve already given attributes to in css to make elements look better.
The way you apply multiple classes to an element is just by seperating them with a space inside the quotes like this:
class="example class1 class2 class3 class4 class5"
1. Adding Classes to the Button
16 / 100
17 / 100
It means do not underline any links by default, and that was needed for our button.
3. Adding Classes to the Rectangular
For the rectangular element, lets add 5 more classes after the rectangular basic class:
center - this will center the text inside the rectangular. bg-green - this will give it a green background color. white - this
will make the text color white decor3 - this decor will make the text line-through, capitallize and bold. normal
- this will adjust the size of the rectangular to what we defined normal.
By now you should have learned how to add these classes, it will look like this in the code:
<!-- rectangular element -->
Im a rectangular
18 / 100
19 / 100
2.3
Some Considerations
The mupltiple classes application seems a great method for saving time and creating some standards for yourself, and it is, but
there are also certain aspects and considerations you should keep in mind.
2.3.1
Do not Overwrite
Given the element specific classes for various types of look, alignment, color ect you want, do not write other/extra classes of the
same property you have previously used. Depending on the case, it will sometimes apply the first/second class, or none of them
at some cases. The code like below would be logically wrong:
<!-- rectangular element -->
And guess what, the rectangular is still the same in the browser view, nothing has changed.
So there is no reason to use multiple classes to apply the same property.
2.3.2
You might have noticed that we did not use all of the classes we referred to in html.
For example, we didnt use pull-left because by default all our elements were aligned on the left, we didnt also use the
small class because it made elements we needed too small, and so on and so forth with come color classes. Knowing that, you
are recommended to follow one of the practices below:
1. Do not use classes that you think will never be needed on the elements you are going to use.
20 / 100
2. Use any class you like or think will be needed, and clean up the unused classes when you finish your project.
Done that, you will have a more organised code suited to your needs.
2.3.3
You might get deep into this and create a set of classes (like, a lot of them) and this would be the approach of a css framework.
If you have reached a level of professionalism that you feel you can do that, go ahead.
Otherwise, feel free to use popular and productive frameworks like Bootstrap (http://getbootstrap.com/) which have thousands of
predefined classes you can use to get your project going.
2.4
Conclusion
To conclude, there is a lot you can do using multiple classes on elements, like easier and faster create forms, menus, buttons,
tables, align, color elements ect, and there are advantages to using it in your projects, but always remember to give them names
according to the main function they do, so that you wont get stuck in long code projects and have it easier to understand what
has been used each class for, without having to search in the css code.
Play around with it to see how this works by yourself.
2.5
Download
Download You can download the full source code of this example here: Multiple Classes Example
21 / 100
Chapter 3
3.1
First, go ahead and create a blank html document and then add the basic syntax inside like so:
<!DOCTYPE html>
<html>
<head>
<title>CSS3 Image Rotate Example</title>
</head>
<body>
<!-- STYLE SECTION -->
<style type="text/css">
</style>
<!-- HTML SECTION
-->
</body>
</html>
Now to apply the last-child selector, first add some elements like below:
<!-- HTML SECTION
This
This
This
This
This
is
is
is
is
is
the
the
the
the
the
-->
first paragraph
second paragraph
third paragraph
fourth paragraph
fifth paragraph
Now the most basic application would be: tag:last-child or class:last-child like below:
<!-- STYLE
22 / 100
SECTION -->
<style type="text/css">
p:last-child{
background-color: yellow;
}
</style>
In the browser, you can see the last line out of five having a yellow background color:
3.2
Another case would be when you change a text styling or boxes with text inside. Look at the html below:
<!-- HTML SECTION
-->
<div class="parent">
<div>This will be a default size font
Also this is going to be default text
Random Text 3
</div>
SECTION -->
<style type="text/css">
.parent div:last-child{
font-family: "Montserrat";
font-size: 2em;
width: 10em;
height: 5em;
color: red;
}
.box {
width: 8em;
height: 5em;
border: 0.2em solid #ccc;
margin-bottom: 1em;
}
.boxes .box:last-child {
background-color: yellow;
text-align: center;
font-size: 2em;
font-family: "Lato";
padding-top: 3em;
}
</style>
23 / 100
24 / 100
-->
Applying the fourth child element means selecting the second, because the counting starts from the last element:
<!-- STYLE
SECTION -->
<style type="text/css">
p:nth-last-child(4){
font-family: "Calibri";
font-weight: bold;
25 / 100
font-size: 2em;
}
</style>
3.3
Conclusion
To conclude, the last-child css selector is good for targeting only the last element of a parent element.
However, if you want to select more than one element and counting from the bottom up, consider using nth-last-child.
It is up to you to decide if you need the first-child or last-child selector according to your needs.
3.4
Download
Download You can download the full source code of this example here: CSS Last Child Selector
26 / 100
Chapter 4
4.1
First, go on and create a html file. This file is going to be the one we are putting both HTML and CSS code so as to keep it as
simple as possible. However, when considering more complicated webpages with a lot of HTML, I recommend you separate the
styling code in a css file.
4.1.1
The initial layout consists of creating the basic structure of html we need.
To do this, lets first explain what kind of tags do we have for tables in html and what do they mean:
1. table - this tag defines a table in html.
2. tr - this tag divides the table into table rows.
3. td - this tags creates the cell, it means table data.
4. th - this tag creates a table heading.
With this said, lets create the basic table structure in html with randomly 3 rows and 3 columns, like below:
<!DOCTYPE html>
<html>
<head>
<title>CSS Tables Example</title>
</head>
<body>
</body>
</html>
27 / 100
As you can see, we created the table tag inside the body tag. After this, we added first the rows tag tr and inside each row
the table data (or cells) with the td tag. Notice we also added a table heading row using the th tag, in the first row.
By default, you should not see anything shown in the browser until now, and that is because we only created the table, but did
not put there any data or styling, so both information and layout are missing. Well add them soon.
4.1.2
Our table needs to be filled, so lets give it some random information in this example. To give it styling and layout attributes we
create a style tag where all the css code is going to be put. To begin, I am giving this table two attributes:
1. width attribute of 40% (a normal width for having a pleasant view of the table, we will add custom width later)
2. border attribute of 0.1em solid to the table, th and td tags.
The code now looks like this:
<style type="text/css">
table {
width: 40%;
border: 0.1em solid;
td {
border: 0.1em solid;
th {
border: 0.1em solid;
</style>
<th>Name</th>
<th>Surname</th>
<th>Age</th>
Figure 4.1: First Look of the Table View with inital attributes applied.
Notice that the table headings are set in a bold style and centered position by default by the browser.
So remember that whatever you put inside the th tag is styled this way everytime, but you can change it.
Other table data, like the cell information is just normal text and aligned left by default.
You probaby see that every cell has its own surrounding/separate border and that looks just bad.
We fix that with the border-collapse:collapse attribute given to the table.
28 / 100
(We are later in this article going to have a closer look to the border-collapse property)
That eleminates the need for border attributes inside the table tag.
Lets also align all the table data (td tag) in the center.
Now we have a code that looks like this in the style section:
<style type="text/css">
table {
width: 40%;
border-collapse:collapse }
td {
border: 0.1em solid;
text-align: center;
th {
border: 0.1em solid;
text-align: center;
</style>
Figure 4.2: Table View with text-align and border-collapse attributes applied.
Now we can say we have created a simple table that looks fine. But there are lots of other styles we can apply to make this table
look a lot more interesting and attractive.
4.2
4.2.1
Spacing
There are several spacing considerations when talking about table/row/cell sizes. Two of them are most popular and shown
below.
1. Set the size of of your table according to the desired width of the cells and height of the rows like this:
29 / 100
<style type="text/css">
table {
/* the width: 40% attribute is removed here */
border-collapse:collapse }
td {
border: 0.1em solid;
text-align: center;
width: 10em;
}
th {
border: 0.1em solid;
text-align: center;
width: 10em;
}
tr {
height: 2em;
</style>
This would make the cells 10em wide and rows 2em high. And wed get this view:
Figure 4.3: Table Width and Spacing using Cells Width and Rows Height
1. Set an inner space called padding to the cells (and/or headings of the table) by adding this attribute to the style like this:
<style type="text/css">
table {
border: 0.1em solid;
border-collapse:collapse }
td {
border: 0.1em solid;
text-align: center;
width: 10em;
padding: 1em;
cells */
30 / 100
th {
border: 0.1em solid;
text-align: center;
width: 10em;
padding: 2em;
}
*/
tr {
height: 2em;
</style>
In this case, I added a 2em padding to the heading cells and 1em to the data cells.
4.2.2
Border Styling
Until now, we have set a 1px solid border style for our table. And that is just good.
But lets see other options and customizations we can apply to the table border.
dotted - dotted border dashed - dashed border solid - solid border double - double border
The way we apply border styles is as in the code below:
<style type="text/css">
table {
border: 0.1em dashed;
border-collapse:collapse }
td {
31 / 100
</style>
For the sake of displaying these four styles in one picture, I have reduced the width and padding of the cells.
32 / 100
<style type="text/css">
table {
border-collapse: collapse;
td {
border: solid #e74f3b;
attribute */
text-align: center;
width: 10em;
border-width: 0.2em;
attribute */
padding: 1em;
th {
border: 0.2em solid;
attribute */
text-align: center;
width: 10em;
padding: 1em;
border-color: #329bd7;
attribute */
tr {
height: 1em;
</style>
This is what the table looks like after these two modifications on the css code:
4.2.3
33 / 100
Cell Styling
Adding a few more lines into our css code will give our cell backgrounds a new look. Lets remove some of the styling we did
before like the border-width and border-color attributes to have a cleaner table.
In the code below, I am giving the heading cells a light gray background and data cells a light green background:
td {
border: 0.1em solid;
text-align: center;
width: 10em;
padding: 1em;
background-color: #d8ffc4;
green) */
th {
border: 0.1em solid;
text-align: center;
width: 10em;
padding: 1em;
background-color: #ededed;
light gray) */
Figure 4.7: Table View after adding the background-color attribute to the cells
4.3
Even though we have covered a lot until now, there are still some interesting layout and styling changes that might be of great
help to our specific cases. So we start with the layout changes we can use.
4.3.1
border-collapse - this property specifies whether the browser should control the appearance of adjacent borders that touch
each other or whether each cell should maintain its style. It can have two possible values:
34 / 100
border-collapse:collapse and border-collapse:separate The code below shows both cases: (duplicated our
table code to have 2 tables in order to show both these two properties):
CSS
<style type="text/css">
table.one {
border-collapse: collapse;
table */
margin-bottom: 5em;
}
table.two {
border-collapse: separate;
table */
caption {
padding-bottom: 1em;
td {
border: 0.1em solid;
text-align: center;
width: 10em;
padding: 1em;
th {
border: 0.1em solid;
text-align: center;
width: 10em;
padding: 1em;
tr {
height: 1em;
</style>
HTML
<caption>Collapse Border Example</caption>
<th>Name</th>
<th>Surname</th>
<th>Age</th>
|Fabio|Cimo|20
|Jasmin|Brown|18
<caption>Separate Border Example</caption>
<th>Name</th>
<th>Surname</th>
<th>Age</th>
|Fabio|Cimo|20
|Jasmin|Brown|18
In this example I also added table captions just by adding the caption tag under the table tag.
And referred to captions in css by giving the caption property attributes.
You can also have captions in the top, bottom, left or right of the table according to your needs.
You can achieve this by adding the caption-side attribute in your css code under the caption class.
The view we are going to get is this:
35 / 100
4.3.2
border-spacing - specifies the distance that separates adjacent cells, borders. It can take one or two values according to the
spacing you need on different sides.
The code below shows both cases:
table.one {
margin-bottom: 5em;
border-collapse:separate;
border-spacing: 1em; }
border spacing */
table.two {
border-collapse:separate;
border-spacing: 0.5em 3em;
left border spacing */
36 / 100
4.3.3
empty-cells - used to indicate whether a cell without any content should have a border displayed. It can take 3 values:
show - given this attribute, all empty cells will show. hide - given this attribute, all empty cells will hide. inherit - given
this attribute, all empty cells will by default show.
All three cases are shown in the code below with the respective comments:
table.one {
37 / 100
margin-bottom: 3em;
border-collapse:separate;
empty-cells: inherit; }
table.two {
margin-bottom: 3em;
border-collapse:separate;
empty-cells: show; }
*/
table.three {
border-collapse:separate;
empty-cells: hide; }
*/
Note that the empty-cells property only works when the border-collapse attribute is set to sepatare.
Here are the 3 results we get:
38 / 100
39 / 100
<th>Surname</th>
<th>Age</th>
4.3.4
4.3.5
40 / 100
Table Design
Professional looking tables come with some simple and eye-catching design.
There is a pseudo-selector in css that we call went we want specific elements (the child) to be the same out of a larger element
(the parent). This is the nth-child(). Inside the brackets we add a condition. Lets see the code below:
<style type="text/css">
table {
font-family: "Lato","sans-serif";
*/
table.one {
margin-bottom: 3em;
border-collapse:collapse;
td {
table data rows */
text-align: center;
width: 10em;
padding: 1em;
}
/* removed the border from the
th {
table heading row */
text-align: center;
padding: 1em;
background-color: #e8503a;
cells */
color: white;
}
to the heading text */
table tr:nth-child(even) {
background-color: #eee;
table tr:nth-child(odd) {
background-color:#fff;
*/
*/
</style>
tr {
height: 1em;
41 / 100
4.3.6
Conclusion
CSS Tables are a great way to organise information inside a webpage as long as we need to structure information.We use them
heavily when we want to have our fully customized properties applied regarding the styles and design.
At other cases, you may also use a framework like Bootstrap to create tables faster and with a default style.For more practice on
css tables and ideas on table design, check the source files below.
4.4
Download
Download You can download the full source code of this example here: CSS Tables
42 / 100
Chapter 5
5.1
Prerequisites
Before we start creating and styling buttons please consider downloading Font Awsome icon font pack. These icons will be
attached to buttons later when we have created the basics.
After downloading, add the folders css and fonts inside your project folder like this:
Figure 5.1: Folder View after adding the Font Awesome Folders
Also, create your basic HTML file that looks like this:
<!DOCTYPE html>
<html>
<head>
<title>CSS Buttons</title>
</head>
<body>
<!-- STYLE SECTION -->
43 / 100
<style type="text/css">
</style>
5.2
Basic Styling
In this section, were going to create a basic button using html and css.
5.2.1
There are 3 main ways you can create a button starting from html.
1. Use the a (anchor) tag to create the link and give it a class, which by default if not styled as a button.
2. Use the button tag that html5 offers and you have a basic styled button with no css at all.
3. Use the input tag and give it a class of button and a type of submit. That will create a pre-styled button.
Your code with these three lines would be:
<!-- HTML SECTION -->
<a class="button" href="#"<Anchor Button</a>
<button class="button">Button Tag</button>
<input class="button" type="submit">
This is what the basic buttons would look like with no css properties applied.
Until now, it looks like just a link, but we will change that with css.
5.2.2
44 / 100
Lets give this button class some initial attributes to get the view of a button:
border:0.1em #333336 solid - gave the border respectively a width, a color and a style.
border-radius:0.2em - gave the border a radius of 0.2em (seems normal).
text-decoration:none - given this attribute, the text will not be underlined as the default browser link underline attribute.
color:black - this will give the text a black color, overriding the default blue color set by the browser
padding:0.5em 1em - gave the text inside the button a padding of 0.5em top and bottom and 1em left and right.
background-color:#f3f3f3 - gave the button a light gray background color.
Lets also place it in a more pleasant position in order to have a better view.
We do that by giving the btn-wrapper class (from the div) margins.
The CSS code will look like this:
.btn-wrapper {
margin-top: 5em;
margin-left: 5em;
}
.button{
border: 0.1em #333336 solid;
border-radius: 0.2em;
text-decoration: none;
color: black;
padding: 0.5em 1em;
background-color: #f3f3f3;
}
Given these attributes, we have created a basic styled button that looks like this in the browser:
5.2.3
Button States
In addition to the default state, buttons can have two other states: hover and active, which respectively mean mouse over
and clicked/pressed. Below we will show the button how to act/change when the cursor is over it and when it is pressed.
1. Hover State
The hover state can be achieved by adding :hover pseudo class like below:
.button:hover {
background-color: #cececc;
}
45 / 100
5.3
From now on, we are going to use a custom font that is Lato and font icons we downloaded. Lets add the necessary links in the
head section:
<!-- ADD CUSTOM FONT LATO -->
<link href=http://fonts.googleapis.com/css?family=Lato&subset=latin,latin-ext rel= stylesheet type=text/css>
<!-- ADD FONTAWESOME ICONS -->
<link rel="stylesheet" href="css/font-awesome.min.css">
46 / 100
Also, add the css code to have one font for everything:
* {
/*
means apply to everything on the page */
font-family: "Lato", "sans-serif";
5.3.1
Start Small
You can have a pretty nice button view with color alternation in aspects of background and text.
One first advice is, you dont always need border for the default state.
Have a look at the following attributes:
.button{
border-radius: 0.5em;
text-decoration: none;
color: white;
padding: 1em 3em;
button */
background-color: #329bd8;
*/
text-transform: uppercase;
font-weight: bold;
/* increased border-radius */
/* changed text color to white */
/* increased padding for a larger
}
.button:hover {
background-color: transparent;
border: 0.15em #329bd8 solid;
color: #329bd8;
color */
}
.button:active {
background-color: transparent;
border: 0.15em #5e8ca5 solid;
color: #5e8ca5;
deeper blue */
}
5.3.2
47 / 100
Icons on Buttons
Going further into whats called a good user experience, this time with buttons, we will add an icon next to the text which will
indicate what the button is for.
Icons are very easy to add, just find the one you want from here and copy the html code of the icon and paste it before the button
text to make it sit right next to the text.
Look how Ive added an upload icon to the button:
<!-- added <i class="fa fa-upload"></i> next to the button text-->
<<,<i class="fa fa-upload"></i>Upload>>
You will also need a few lines of css to make this look fine:
(only the commented lines are new or edited)
.button{
border-radius: 0.5em;
text-decoration: none;
color: white;
padding: 1em;
padding value */
padding-right: 3em;
align the icon left
background-color: #329bd8;
text-transform: uppercase;
font-weight: bold;
*/
}
.fa-upload {
padding-right: 2em;
from the text */
font-size: 1.2em;
button */
}
48 / 100
5.3.3
Gradients on Buttons
Just like you apply background colors to buttons, you can also apply *gradient*s on them.
Gradients make buttons look more like 3D rather than flat like weve seen until now.
Creating gradients yourselves is just time-wasting, so I suggest you generate gradients online in this website
Feel free to choose any gradient you like and copy the css. To be coherent with what weve started Imma choose a blue gradient.
Look at the code below:
/* just removed background-color attribute and added the custom css code */
.button{
border-radius: 0.5em;
text-decoration: none;
color: white;
padding: 1em;
padding-right: 3em;
text-transform: uppercase;
font-weight: bold;
text-shadow: -1px -1px 0 rgba(0,0,0,0.3);
color: #FFFFFF;
background-color: #49c0f0; background-image:
-webkit-gradient(linear, left top, left bottom, from(#49c0f0), to(#2CAFE3));
background-image: -webkit-linear-gradient(top, #49c0f0, #2CAFE3);
background-image: -moz-linear-gradient(top, #49c0f0, #2CAFE3);
background-image: -ms-linear-gradient(top, #49c0f0, #2CAFE3);
background-image: -o-linear-gradient(top, #49c0f0, #2CAFE3);
background-image: linear-gradient(to bottom, #49c0f0, #2CAFE3);
filter:progid:DXImageTransform.Microsoft.gradient
(GradientType=0,startColorstr=#49c0f0, endColorstr=#2CAFE3);
}
.button:hover {
background-color: #1ab0ec; background-image:
-webkit-gradient(linear, left top, left bottom, from(#1ab0ec), to(#1a92c2));
49 / 100
In the code above I did only consider the normal and hover state of the button.
Its up to you to add the active state if you think you need it.
This is the how the button wed get:
5.3.4
Patterns on Buttons
50 / 100
.button:hover {
background:
radial-gradient(black 15%, transparent 16%) 0 0,
radial-gradient(black 15%, transparent 16%) 8px 8px,
radial-gradient(rgba(255,255,255,.1) 15%, transparent 20%) 0 1px,
radial-gradient(rgba(255,255,255,.1) 15%, transparent 20%) 8px 9px;
background-color:#282828;
background-size:16px 16px;
}
In this code I just added pattern backgrounds for the normal and hover state of the button.
This is how the pattern styled button would look like:
51 / 100
5.4
Conclusion
To conclude, we can state that there are various ways you can style and fit buttons according to your needs.
You can add png images instead of icons or images as backgrounds into a button, but after some time practicing you will
understand that keeping it simple and nice is a combinations you can achieve by trying either flat or gradient design, which will
also make your project less cluttery from images on each button.
Below you can find some pre-styled and good designed buttons that you can use for your own projects.
5.5
Download
Download You can download the full source code of this example here: CSS Buttons
52 / 100
Chapter 6
6.1
Prerequisites
First, go ahead and create a html file with its basic syntax inside like this:
<!DOCTYPE html>
<html>
<head>
<title>CSS3 Text Input Styling Example</title>
</head>
<body>
<!-- STYLE SECTION -->
<style type="text/css">
</style>
<!-- HTML SECTION
-->
</body>
</html>
</pre>
Set up an input with a type of text, a class of element and optionally a name of city:
<!-- HTML SECTION
-->
Note that the name will be needed because the text input is going to be used under some form and referred to.
Look at the default styling of a text input:
53 / 100
input[type=text] {
background-color: #ffffff;
border: 1px solid #cccccc;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
-moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
-webkit-transition: border linear 0.2s, box-shadow linear
-moz-transition: border linear 0.2s, box-shadow linear
-ms-transition: border linear 0.2s, box-shadow linear
-o-transition: border linear 0.2s, box-shadow linear
transition: border linear 0.2s, box-shadow linear
}
0.2s;
0.2s;
0.2s;
0.2s;
0.2s;
input[type=text]:focus {
border-color: rgba(82, 168, 236, 0.8);
outline: 0;
outline: thin dotted \9;
/* IE6-9 */
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6) ;
-moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6) ;
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6) ;
}
And the default view of a text input as you might know is:
6.2
In this section, we will style the above element with CSS3 to create a better looking input view. Look at the lines below:
<style type="text/css">
.element {
font-family: "Montserrat", sans-serif;
width: 20em;
height: 3em;
border: 0.1em solid #ccc;
border-radius: 0.5em;
background-color: #feffdc;
*/
padding: 0em 1em;
padding */
54 / 100
}
</style>
55 / 100
6.3
Notice that up until now, we have been styling the text input by giving it a class ( element ), but what if you want to style
all text inputs the same way? Well, there is a way. You can select all input type text like this in css: input[type=text].
First lets add a few more lines create a form like view of the inputs and then apply styling using the selector.
<!-- HTML SECTION -->
<h4>Please, fill the form below to continue:</h4>
<form class="form-group" method="post">
<h4>First Name</h4>
<input type="text" placeholder="First Name" name="first-name"><br />
<h4>Last Name</h4>
<input type="text" placeholder="Last Name" name="last-name"><br />
<h4>E-Mail</h4>
<input type="text" placeholder="E-Mail Address" name="email"><br />
<h4>Country</h4>
<input type="text" placeholder="Country" name="country"><br />
</form>
That gave a form view of several inputs. A more intense styling with advanced shadows and inset would be:
<!-- STYLE
SECTION -->
<style type="text/css">
input[type=text] {
font-family: "Open Sans", sans-serif;
width: 20em;
height: 2em;
The new, smart styled all inputs would look like this:
56 / 100
57 / 100
Figure 6.4: Styling all inputs with a type of Text using the CSS Selector
6.4
Conclusion
To conclude, there is a lot to explore while styling inputs of every type, especially text, and it is recommended to giveyour inputs
a personalized view and keep a standard style that you create for later uses, just like we did with text inputs.
The default input view is kinda creepy and old-fashioned, so consider using your own styling when using inputs.
You could also take advantage of front end frameworks like Bootstrap to have pre styled inputs with nice animated hovers.
6.5
Download
Download You can download the full source code of this example here: CSS3 Text Input Styling
58 / 100
Chapter 7
7.1
Go ahead and create a new html document and add the basic sytnax in it like so:
<!DOCTYPE html>
<html>
<head>
<title>CSS3 Text Decoration Example</title>
</head>
<body>
<!-- STYLE SECTION -->
<style type="text/css">
</style>
<!-- HTML SECTION -->
</body>
</html>
Lets first explain how the property is used and values it can take:
1. Usage: text-decoration:value;
2. Values: underline, overline, line-through
A basic application of the text-decoration property would be the following. Create a some lines of text in html:
59 / 100
-->
SECTION -->
<style type="text/css">
h1 {
text-decoration: underline;
}
h2 {
text-decoration: overline;
}
h3 {
text-decoration: line-through;
}
</style>
The view in the browser for the basic application would be:
7.2
In this sections, we will cover things that you can do with text-decoration and some interesting ways how you can use it.
Notice that the following examples will work best in Firefox, which has full support of them.
7.2.1
In addition to a single attribute value, text-decoration can also take multiple values. Look at the code below:
-HTML
<h2 class="mix">this line will have multiple values</h2>
60 / 100
Lets add all three common values in css: underline, overline and line-through like below:
.mix {
text-decoration: underline overline line-through;
}
Lets add a dotted line style and red color to it to make this underline fancy.
.fancy {
text-decoration: underline dotted red; /* works only on firefox */
}
Currently only supported in Firefox, this styling would look like this in Mozilla Firefox:
7.2.2
The text-decoration property is a shorthand to some specific related properties like text-decoration-color/line/style.
1. text-decoration-color
-HTML
<h2 class="color">This is the color styled line</h2>
-CSS
61 / 100
.color {
text-decoration: underline;
text-decoration-color: #40b7c2;
-CSS
.line {
text-decoration-line: underline;
}
We just underlined the sentence, but in the long way, the view is:
-CSS
.style {
text-decoration: underline;
text-decoration-style: dotted;
}
62 / 100
7.3
Conclusion
To conclude, we can state that text-decoration property is quite handy when working with text, consider it just like the
bold or color property of text, it is obvious that it is part of the text styling family. The property is very easy to use in css.
Some elements, line the anchor tags include pre-styled text with a text-decoration:underline;, so you may want to
do the contrary in those cases, I mean reset text-decoration to none. It always depends on you specific needs for your project.
7.4
Download
Download You can download the full source code of this example here: CSS3 Text Decoration
63 / 100
Chapter 8
8.1
Basic Set Up
First, go ahead and create a new html file and add the basic html and css like this:
<!DOCTYPE html>
<html>
<head>
<title>CSS Buttons</title>
</head>
<body>
<!-- STYLE SECTION -->
<style type="text/css">
</style>
On the html section, add a <h1> tag with a class of text like so:
<!-- HTML SECTION -->
<h1 class="text">Web Code Geeks</h1>
64 / 100
<style type="text/css">
body{
font-family: "Arial", "sans-serif";
}
h1 {
}
</style>
4px - X (horizontal) offset 3px - Y (vertical offset 2px - blur amount #ccc - color
Basically, you can see it like this:
text-shadow: horizontal-offset vertical-offset blur color;
where the color can be either in hex #ccc; code or rgba(0,0,0,0.3); code.
Applying the shadow to our text in css would look like this:
<!-- STYLE SECTION -->
.text {
font-size: 5em;
larger */
text-shadow: 4px 3px 2px #ccc;
}
The view in the browser of this text with shadow would be:
8.2
Variations
From now on, the body background color will be subject to constant change.
This is because certain shadows need specific backgrounds to be noticed.
Also the text is going to be uppercase for better results.
The following will show some great text-shadows you can apply.
8.2.1
65 / 100
Pressed Effect
Set your text color to a slightly darker shade than your background color.
Next, apply a slight white text-shadow with a reduced opacity.
body {
background: #222;
}
.text {
font-size: 5em;
color: rgba(0,0,0,0.6);
added text-color */
text-shadow: 2px 2px 3px rgba(255,255,255,0.1);
/*
/* added shadow */
Using the rgba color code, you will be able to set the opacity of the color applied.
Notice that the text-color has an opacity of 60% (0.6) and shadow 10% (0.1).
8.2.2
Because of their retro nature, hard shadows dont always need to have blur applied.
Take for example this hard text shadow:
body {
background: #fff3cd;
color */
/*
}
.text {
font-size: 5em;
color: white; changed text-color to white */
text-shadow: 6px 6px 0px rgba(0,0,0,0.2);
*/
}
66 / 100
8.2.3
It is interesting to know that you are not limited to one single shadow application.
You can use more than one shadow like this: text-shadow:shadow1, shadow2, shadow3;
Lets add two shadows, one of them with the color of the background, and the other one a slightly darker color than the background color:
.text {
font-size: 5em;
text-shadow: 4px 3px 0px #fff, 9px 8px 0px rgba(0,0,0,0.15);
}
In this case, the background is white, so we dont need a custom color for it.
The view in the browser would be:
8.2.4
67 / 100
body {
background: #fff3cd;
}
.text {
font-size: 5em;
color: white;
text-shadow: 0px 3px 0px #b2a98f,
0px 14px 10px rgba(0,0,0,0.15),
0px 24px 2px rgba(0,0,0,0.1),
0px 34px 30px rgba(0,0,0,0.1);
}
8.2.5
68 / 100
8.2.6
Gordon uses some serious CSS voodoo to pull off not only an outer shadow but a genuine inner shadow as well.
body {
background: #cbcbcb;
}
.text {
font-size: 5em;
color: transparent;
background-color: #666666;
-webkit-background-clip: text;
-moz-background-clip: text;
background-clip: text;
text-shadow: rgba(255,255,255,0.5) 0px 3px 3px;
}
8.2.7
body {
background: #992d23;
69 / 100
}
.text {
font-size: 5em;
color: white;
text-shadow: 0px 0px 6px rgba(255,255,255,0.7);
}
8.2.8
body {
background: #629552;
}
.text {
font-size: 5em;
color: rgba(0,0,0,0.6);
text-shadow: 2px 8px 6px rgba(0,0,0,0.2),
0px -5px 35px rgba(255,255,255,0.3);
}
8.3
Conclusion
As you saw in this example, the text-shadow property is really easy to use.
Whats interesting, is that you can come up with creative effects if you put yourself into it.
These effects are used all over the web (I did not invent them) so be sure you are using qualitative stuff.
You can also download the source html and just edit these examples that you saw above.
8.4
Download
Download You can download the full source code of this example here: CSS Text Shadow
70 / 100
71 / 100
Chapter 9
9.1
Prerequisites
Below, I will present you to the necessary knowledge you should have to get through this.
9.1.1
Basic Set Up
First, go ahead and create a new html file and add the basic html and css like this:
<!DOCTYPE html>
<html>
<head>
<title>CSS Buttons</title>
</head>
<body>
<!-- STYLE SECTION -->
<style type="text/css">
</style>
9.1.2
72 / 100
A pseudo-element creates a phoney element and inserts it before or after the content of the element that youve targeted.
We will use the :before and :after pseudo-elements to add shadows left, right, top, bottom or combinations of these.
See this simple example of its usage so you have a better clue:
HTML
<!-- HTML SECTION -->
<h1 class="text">This is the first item</h1>
CSS
<!-- STYLE SECTION -->
.text:before {
content:"1. ";
}
.text:after {
content: "!";
}
</style>
This would give the number 1. before the text and ! after the text like this:
9.1.3
Along with the pseudo-elements, there are also some properties Id like you to understand:
z-index - specifies the stack order of an element.
position - allows you to position elements within a page.
content - used when appyling :before and :after pseudo-elements.
left, right, top, bottom - defines the left, right, top, bottom edge of an element.
transform - self-explanatory, transforms an object by degree, scale ect.
9.2
73 / 100
9.2.1
9.2.2
We are using multiple classes on an element, in this case two, because the box element will be the same for all examples, while
the other class will contain distinctive attributes according to the effect we want to create.
Now give the body a light gray color and box element these initial attributes to create a standard now.
74 / 100
A basic shadow applied to a box like above will look like this:
9.3
9.3.1
/* EFFECT 1 */
/* Here shadows are styled as elements to give impressive look */
/* However, the box shadow property is applied at the end to make final touches */
75 / 100
.effect1
{
position: relative;
/* relative positioning referring to before and after */
}
.effect1:before, .effect1:after /* apply some mutual properties before and after this element */
{
z-index: -1;
/* send this element backward */
position: absolute;
/* absolute positioning referring to effect2 */
content: "";
/* just required, no need to put anything inside */
bottom: 15px;
/* bottom shadow alignment, less is closer to bottom */
left: 10px;
/* consider this like a margin left */
width: 50%;
/* width of the shadow element background */
top: 80%;
/* consider this as margin top of the shadow element */
max-width:300px;
/* restricts the max width of the shadow element */
background: #777;
/* gives a background color to the shadow element */
-webkit-box-shadow: 0 15px 10px #777; /* compatibility case application */
-moz-box-shadow: 0 15px 10px #777;
/* compatibility case application */
box-shadow: 0 15px 10px #777; /* applied the basic box-shadow property */
/* rotation of shadows/elements gives that 3D look to the box */
-webkit-transform: rotate(-3deg);
-moz-transform: rotate(-3deg);
-o-transform: rotate(-3deg);
-ms-transform: rotate(-3deg);
transform: rotate(-3deg);
}
.effect1:after
{
-webkit-transform: rotate(3deg);
/* aligns the shadow right */
-moz-transform: rotate(3deg);
-o-transform: rotate(3deg);
-ms-transform: rotate(3deg);
transform: rotate(3deg);
right: 10px;
/* consider this like a margin right */
left: auto;
/* leave this auto to automatically set the left */
}
View:
9.3.2
View:
76 / 100
77 / 100
9.3.3
78 / 100
transform: rotate(-8deg);
}
.effect4:after
{
-webkit-transform: rotate(8deg);
-moz-transform: rotate(8deg);
-o-transform: rotate(8deg);
-ms-transform: rotate(8deg);
transform: rotate(8deg);
right: 10px;
left: auto;
}
View:
9.3.4
79 / 100
right:10px;
-moz-border-radius:100px / 10px;
border-radius:100px / 10px;
}
.effect5:after
{
right:10px;
left:auto;
-webkit-transform:skew(8deg)
-moz-transform:skew(8deg)
-ms-transform:skew(8deg)
-o-transform:skew(8deg)
transform:skew(8deg)
}
rotate(3deg);
rotate(3deg);
rotate(3deg);
rotate(3deg);
rotate(3deg);
View:
9.3.5
These two examples show: top and bottom inset shadow, left and right inset shadow.
/* TOP AND BOTTOM INSET SHADOW */
.effect6
{
position:relative;
-webkit-box-shadow:0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1) inset;
-moz-box-shadow:0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1) inset;
box-shadow:0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1) inset;
}
.effect6:before, .effect6:after
{
content:"";
position:absolute;
z-index:-1;
-webkit-box-shadow:0 0 20px rgba(0,0,0,0.8);
-moz-box-shadow:0 0 20px rgba(0,0,0,0.8);
box-shadow:0 0 20px rgba(0,0,0,0.8);
top:0;
80 / 100
bottom:0;
left:10px;
right:10px;
-moz-border-radius:100px / 10px;
border-radius:100px / 10px;
}
.effect6:after
{
right:10px;
left:auto;
-webkit-transform:skew(8deg)
-moz-transform:skew(8deg)
-ms-transform:skew(8deg)
-o-transform:skew(8deg)
transform:skew(8deg)
}
rotate(3deg);
rotate(3deg);
rotate(3deg);
rotate(3deg);
rotate(3deg);
View:
81 / 100
9.4
Conclusion
Generally speaking, shadows are a great way to make some nice touches on elements.
They do create interesting look and feel of elements and thats why you should consider using them.
Sometimes the code will be complicated, but feel free to use effects we used in this example, (just copy and paste) if you like any
of them just the way they are.
However, if you feel professional at this, you can change values according to your needs.
Below you can find all these shadow examples in one html file.
9.5
Download
Download You can download the full source code of this example here: CSS Box-Shadow
82 / 100
Chapter 10
10.1
Basic Setup
First, go ahead and create a html file with its basic syntax inside like so:
<!DOCTYPE html>
<html>
<head>
<title>CSS3 Horizontal Menu Example</title>
</head>
<body>
<!-- STYLE SECTION -->
<style type="text/css">
</style>
<!-- HTML SECTION -->
</body>
</html>
10.2
Below, we will create the html and css for a basic horizontal menu.
1. HTML
The html structure of the menu is going to be like this:
83 / 100
1. A class named menu is going to wrap all menu items under a div.
2. The menu is going to be placed as an unordered list element, that is ul.
3. Each specific menu title is going to be under the li and then a tag.
Coming to the code, it would look like this together with some random menu items inside:
<!-- HTML SECTION
-->
<ul>
<li><a
<li><a
<li><a
<li><a
href="#">Home</a></li>
href="#">Profile</a></li>
href="#">About</a></li>
href="#">Contact</a></li>
</ul>
Well, it is still html, and we got a creepy view where the menu is vertical:
.menu ul {
list-style-type: none;
}
Then, we continue with making the menu items display inline, color, padding etc:
.menu li {
display: inline;
padding-right: 2em;
text-transform: uppercase;
}
.menu a {
color: black;
transition: .5s;
}
.menu a:hover {
color: green;
}
/* display horizontal */
/* item spacing */
/* ALL CAPS */
That was pretty easy. We now have a better looking horizontal menu that looks like this:
84 / 100
10.3
Now we will have a look at some more professional menus that have extra elements added on css.
10.3.1
Example 1
.menu a {
color: #f2f2f2;
transition: .5s;
}
.menu a:hover {
color: white;
}
.menu {
width: 28em;
height: 3em;
border-radius: 0.4em;
padding: 0.5em;
background-color: #56bce7;
}
Now this menu has a background and would look like this:
85 / 100
10.3.2
Example 2
Lets see another great styled menu which now has a more enhanced look.
HTML:
<head>
<meta charset=utf-8>
<link rel="stylesheet" href="styles.css">
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></ script>
<script src="script.js"></script>
<title>CSS MenuMaker</title>
</head>
<body>
<ul>
<li class=active><a href=#>Home</a></li>
<li><a href=#>Products</a></li>
<li><a href=#>Company</a></li>
<li><a href=#>Contact</a></li>
</ul>
CSS:
@import url(http://fonts.googleapis.com/css?family=Raleway);
#cssmenu,
#cssmenu ul,
#cssmenu ul li,
#cssmenu ul li a {
margin: 0;
padding: 0;
border: 0;
list-style: none;
line-height: 1;
display: block;
position: relative;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
#cssmenu:after,
#cssmenu > ul:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
#cssmenu {
width: auto;
border-bottom: 3px solid #47c9af;
font-family: Raleway, sans-serif;
line-height: 1;
}
#cssmenu ul {
background: #ffffff;
}
#cssmenu > ul > li {
float: left;
}
#cssmenu.align-center > ul {
font-size: 0;
text-align: center;
}
#cssmenu.align-center > ul > li {
display: inline-block;
float: none;
}
#cssmenu.align-right > ul > li {
float: right;
}
#cssmenu.align-right > ul > li > a {
margin-right: 0;
margin-left: -4px;
}
#cssmenu > ul > li > a {
z-index: 2;
padding: 18px 25px 12px 25px;
font-size: 15px;
font-weight: 400;
text-decoration: none;
color: #444444;
-webkit-transition: all .2s ease;
-moz-transition: all .2s ease;
-ms-transition: all .2s ease;
-o-transition: all .2s ease;
transition: all .2s ease;
margin-right: -4px;
}
#cssmenu
#cssmenu
#cssmenu
color:
}
86 / 100
87 / 100
position: absolute;
left: 0;
bottom: 0;
right: 0;
z-index: -1;
width: 100%;
height: 120%;
border-top-left-radius: 8px;
border-top-right-radius: 8px;
content: "";
-webkit-transition: all .2s ease;
-o-transition: all .2s ease;
transition: all .2s ease;
-webkit-transform: perspective(5px) rotateX(2deg);
-webkit-transform-origin: bottom;
-moz-transform: perspective(5px) rotateX(2deg);
-moz-transform-origin: bottom;
transform: perspective(5px) rotateX(2deg);
transform-origin: bottom;
}
#cssmenu > ul
#cssmenu > ul
#cssmenu > ul
background:
}
10.4
Conclusion
To conclude, we can say that it is pretty easy to create basic menus with html and style with css. However, if you want professional
and well designed menus consider browsing for templates or using frameworks which make this easier.
Note that to make interactive menus with submenus and dropdowns, you will probably need jQuery or Javascript in general to
achieve animations, dropdowns, left and right menu expansion and so on and so forth.
10.5
Download
Download You can download the full source code of this example here: CSS3 Horizontal Menu
88 / 100
89 / 100
Chapter 11
11.1
First, go ahead and create a blank html document and then add the basic syntax inside like so:
<!DOCTYPE html>
<html>
<head>
<title>CSS3 Image Rotate Example</title>
</head>
<body>
<!-- STYLE SECTION -->
<style type="text/css">
</style>
<!-- HTML SECTION
-->
</body>
</html>
</pre>
Now add an img tag in the html section with an image inside like this:
<!-- HTML SECTION
-->
<img src="image.jpg">
90 / 100
SECTION -->
<style type="text/css">
img {
transform: rotate(10deg);
</style>
11.2
In this section, we will go through some cases where you can use and how the image rotation.
In the html area, I will add 5 images so that we can create a collage like view of the images.
<!-- HTML SECTION
<img
<img
<img
<img
<img
class="img1"
class="img2"
class="img3"
class="img4"
class="img5"
-->
src="image.jpg">
src="image1.jpg">
src="image2.jpg">
src="image3.jpg">
src="image4.jpg">
Lets wrap images inside a border and a shadow and give odd numbers a -10deg rotation and even numbers 10deg:
1. box-shadow - each image is going to have a light gray shadow surrounding it.
2. border - borders are going to be placed around images in a gray color.
3. transform:rotate - first image will have a -10deg rotation, second a 10deg rotation and so on.
Coding the css part would be easy:
<!-- STYLE
SECTION -->
<style type="text/css">
.img1 {
transform: rotate(-10deg);
border: 0.8em solid #f4f4f4;
box-shadow: 0.5em 0.5em 1em #ccc;
}
.img2 {
transform: rotate(10deg);
border: 0.8em solid #f4f4f4;
box-shadow: 0.5em 0.5em 1em #ccc;
}
.img3 {
transform: rotate(-10deg);
border: 0.8em solid #f4f4f4;
box-shadow: 0.5em 0.5em 1em #ccc;
}
.img4 {
transform: rotate(10deg);
border: 0.8em solid #f4f4f4;
box-shadow: 0.5em 0.5em 1em #ccc;
}
.img5 {
transform: rotate(-10deg);
border: 0.8em solid #f4f4f4;
box-shadow: 0.5em 0.5em 1em #ccc;
}
</style>
The view of this nice photo collage in the browser would be:
91 / 100
92 / 100
class="img1"
class="img2"
class="img3"
class="img4"
class="img5"
-->
src="image.jpg">
src="image1.jpg">
src="image2.jpg">
src="image3.jpg">
src="image4.jpg">
Now the CSS will have very slight changes (in this case, only the degree has changed and some size elements):
<!-- STYLE
SECTION -->
<style type="text/css">
body {
font-family: "Montserrat";
}
.img1 {
transform: rotate(7deg);
border: 0.5em solid #f4f4f4;
box-shadow: 0.5em 0.5em 0.5em #ccc;
}
.img2 {
transform: rotate(-8deg);
border: 0.5em solid #f4f4f4;
box-shadow: 0.5em 0.5em 0.5em #ccc;
}
.img3 {
transform: rotate(7deg);
border: 0.5em solid #f4f4f4;
box-shadow: 0.5em 0.5em 0.5em #ccc;
}
.img4 {
transform: rotate(-8deg);
border: 0.5em solid #f4f4f4;
box-shadow: 0.5em 0.5em 0.5em #ccc;
}
.img5 {
transform: rotate(7deg);
border: 0.5em solid #f4f4f4;
box-shadow: 0.5em 0.5em 0.5em #ccc;
}
</style>
So you can have a vertical view of these rotated images and place your description inline:
93 / 100
94 / 100
11.3
Conclusion
It seems like a small part of css, and it is somehow, but the transform property with rotate(xdeg) value applied can fundamentally
change the way you think of images inside a webpage and rethink ways you can organise them.
It is very easy to use and create beautiful gallery-like sets of images placed on various degrees.
A 180 deg rotation would mean an upside down view while a 360 deg would mean no changes in the view, a full rotation.
11.4
Download
Download You can download the full source code of this example here: CSS3 Image Rotate
95 / 100
Chapter 12
12.1
First, go ahead and create a blank html document and then add the basic syntax inside like so:
<!DOCTYPE html>
<html>
<head>
<title>CSS3 Hover Effects Example</title>
</head>
<body>
<!-- STYLE SECTION -->
<style type="text/css">
</style>
<!-- HTML SECTION
-->
</body>
</html>
</pre>
To apply hover effects, you add :hover after the tag or class of html and then open the brackets:
-HTML (create an element, i.e a button, so that we can apply hover)
<button>This is me</button>
96 / 100
}
</style>
We expect this button to change background color to red and text color to white when the mouse is over it:
12.2
Below, we will take elements of html, style them and give different hover effects to show examples.
12.2.1
There are three most important aspects you should remember when stlying buttons with hovers:
1. Choose a light bg color on normal state and a deeper bg color on hover state or vice versa, that will look great.
2. Be careful to notice how the text inside the button looks like while on hover. If not right, change its color.
3. Always add a transition property with a value of .5s to make the hover effect animate (fade) beautifully.
Below, I have added three buttons on the HTML section, those will have the same styling but different hovers.
<!-- HTML SECTION -->
<button class="btn1">button 1</button>
<button class="btn2">button 2</button>
<button class="btn3">button 3</button>
Now notice that I have given same styling by selecting the button tag and different hovers using the respective classes:
<!-- STYLE
SECTION -->
<style type="text/css">
button {
width: 8em;
height: 3em;
border: none;
border-radius: 0.5em;
background-color: #919191;
font-family: "Montserrat";
text-transform: uppercase;
color: white;
margin-right: 2em;
transition: .5s;
97 / 100
}
.btn1:hover {
background-color:#8a3f3f;
text-decoration: underline;
}
.btn2:hover {
background-color: transparent; /* no background color */
text-transform: lowercase;
border: 0.2em solid #ccc;
/* added border color */
color: black;
}
.btn3:hover {
background-color:#3f708a;
/* deep blue color */
box-shadow: .1em .1em 1em #ccc; /* added box shadow */
}
</style>
See how the buttons look in normal and hover state in the browser:
12.2.2
Lets take for example a simple html menu, and give the menu items hover effects:
<a
<a
<a
<a
<a
href="#1">Home</a>
href="#2" class="current">Profile</a>
href="#3">About</a>
href="#4">Contact</a>
href="#5">Sth Else Here</a>
Notice how you can add image effects using hover to the menu, in this case a gradient image:
div.menu2
{
text-align:center;
background-image: url(bg1.gif);
border:1px solid black;
font-size:0;
}
98 / 100
div.menu2 a
{
display: inline-block;
padding: 0 20px;
background-image: url(bg.gif);
color:White;
text-decoration:none;
font: bold 12px Arial;
line-height: 32px;
}
div.menu2 a:hover, div.menu2 a.current
{
background-position:0 -60px;
}
12.2.3
THe following example is something advanced brought in a simple way to you. It is a box that is going to be given attributes
for the normal state and then it is going to expand and show extra lines of text when on hover state.
<span class="element1">Hi Im not here at first, but only on hover.</span> Im here for too
long now.
Applying some simple logics in css that would look like this:
.box {
margin-top: 5em;
width: 15em;
height: 8em;
font-size: 1.2em;
font-family: "Montserrat";
color: white;
background-color: #464646;
transition: width 2s;
}
span {
display: none;
}
.box:hover{
width: 25em;
}
.box:hover .element1 {
display: block;
}
99 / 100
The result, like you may have been wondering, would be:
12.3
Conclusion
To conclude, we can say that hovering elements should be used to have your website better designed.
It comes to usage in an easy form, only by adding the :hover in css, but when considering more advanced stuff like animation
or adding elements after mouse over, it can get complicated.
However, keep in mind to use these hovering effects using only css and not javascript because it is the lightest way not to overload
your website with js.
12.4
Download
Download You can download the full source code of this example here: CSS3 Hover Effects
100 / 100