We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 65
CSS & JQUERY
Practice ExercisesCSS Practice Exercises
1.1.€58 Syntax
Open HIML-Kit Tools(Shortcut on Desktop}. Select File > New Document. Paste the below code,
Observe the syntax for the CSS mentioned for the P tag
1[Page
Hello World!
CSS comments are not shown in the output.
Me too!
<
And I'm not affected by the styling,.te/div>
tag with a random text,after the tag and verify if the styling is applied on the
same,
1.3.2 Selecting an Element with i:
In the Editor, add an id{id = "#parat”) to the first
and add the below dparal. style.
‘The code shauld look similar to below. Verify in the preview. The Styling should be applied to the first
paragraph only.
Hello World!
This paragraph is not affected by the style.
1.3.3 Selecting an element with class narve:
Check the way the class has been defined in the below code.
Copy and paste the below HTML in the editor and observe the styling pattern.
Red and center-aligned headingch1>
“center'>Red and center-aligned paragraph.
‘Add a new
element with random text below the
tag,
name the class of the same as “left”
Create a new Class in the Style tag with name as left and add the text-align attribute as left and color as
blue.
Preview the HTML now and see the difference,
1.3.4 Selecting a specific element with class name:
Now that we were selecting the Elements with a class name, lets select specific elements with the class
name
3 [PageIn the code above in earlier ecercise, beside the dass name, add the tag name as well{suppose p }. Your
code should look similar to as below and observe the Styling affects only the
tag and not the
‘tag though both the elements have same Class name.
shtml>
This heading will not be affecteds/h1>
This paragraph will be red and center-aligned.
sft
Now replace the 'p'added beside the class tag with 1’ and observe the difference in the preview.
1.3.5 Grouping similar style to several tags/classes
Now that we have observed selecting an element with a tag name and class name,
group several elements together.
shall see how to
Observe the below code and see how the grouping has been made Copy and paste the below code in
editor and view the preview.
Hello World!
a|Pageh2»Smaller heading!
‘This is a paragraph.
Now add the class name to each of the elements and then suffix the class names to the respective
elementsin the style tags.
Eg:
and in style tag as below
h1.class1, h2.class2, p {
text-align: center;
color: red;
}
‘Verify in the preview if the respective elements are being styled accordingly.
1.4 CSS Backgrounds
1.4.1. Simple background
Lets see h
1w we can make a simple background to anelement in HTML,
In the below example we shall see how to put background color to the body of the HTML,
shead>
My C55 web page!
Hello world! This is an example of body with background color.
1.4.2 Background to specific elements:
5|PageNow Lets individually give a background color to different elements.
Copy paste the below cade and observe the way different elements are being styled individually with
different background,
Check the preview and observe the difference.
chtml>
CSS background-color example!
‘This is a text inside a div element.
This paragraph has its own background color.
We are still in the div element.
‘Add the below style and check the preview.
body {
background-color: red;
}
1.4.3. background with image
Create a new folder in the desktop, Copy paste the below code in HTML Kit Tools and save the
document in the falder that was created.
6 [Page
Hello World!
Copy the file images.jpg from the labsfimages folder, to the newly created folder where the HTML has
been saved and refer the name of the picture in the style,
Check the preview and the image should be loaded as background with repeated several times
horizontally and vertically,
1.4.4 Repeat image horizontally
Add the line "background-repeat: repeat-x;" in the style and check the preview. Now the image should
be repeated only horizontally.
Hello World!
Change the repeat-x to repeat-y and check the preview. now the background should be repeated
vertically only.
7[Page1.4.5 No repeat image
So now we know how to stop the image getting repeated horizontally/vertically/both. To have the
repeat stopped both horizontally & vertically, set the background-repeat to "no-repeat"
Hello World!
The background image is only showing ance, but itis disturbing the reader!
1.46 Positioning background image
Observe the below style which will say how to position the imge in the body of the HTML page.
Copy the below HTML code and paste in the HTML Editor and observe the positioning of the background
image now.
shtml>
Hello World!
8|Page
W3Schools background no-repeat, set position example.
Now the background image is only shown once, and positioned away from the text.
Now change the position from "right top" to "left top" and preview the HTML.
15 CSS Text
1.5.1 CSS Text color
‘We shall see how to make the color of the text in this exercise.
Observe the color pattern in the below code and see how various colors are put to various texts in
respective elemnts. Copy the below code in HTML editor and preview the same.
This is an ordinary paragraph, Notice that this text is red. The default text-color for a page is defined
in the body selector.
This is a paragraph with cla
‘ex". This text is blue.
9|Page1.5.2 Text alignment
Observe the below code and see haw the text alignment has been made for various parts of the text.
Use the below code in the HTML Editor and see how the text gets aligned.
ehtml>
May, 2009
In my younger and more vulnerable years my father gave me some advice that I've
been turning over in my mind ever since. ‘Whenever you feel like criticizing anyone,’ he told me,
"just remember that all the people in this world haven't had the advantages that you've had.'
cbeNote: Resize the browser window to see how the value "justify" works.
Change the Alignments in the above cade and see the preview.
1.5.3 Text decoration
Now we shall see various text decoration patterns.
Add the below styling to the code in HTML Editor and check the difference,
This is heading 1
This is heading 2
<
This is heading 3
1.5.4 Text transformation
Using the below style code try and set the text transformations.
This is some text.
This is some text.
1.5.5 Text indentation
‘The text identation can be done using the below code,
Change the text indent to 150px and see the preview. Observe the difference now.
1.5.6 CSS Fonts
In the below excercise we shall see how to set various fonts to the text.
Observe the style difference for various elements. Try and set the font family and check the preview,
al, Helvetica, sans-serif;
CSS font-familys/h1>
This is a paragraph, shown in the Times New Roman font.
This is a paragraph, shown in the Arial font.
1.5.7 Selecting the font style:
‘The font style can be set to italic/normal using the below attributes,
Observe the different font styles in the paragraphs,
This is a paragraph in oblique style.
1.5.8 setting the font size
13 [PageFont size can be set using the Font-size attribute. Use the below code and try setting the font size by
increasing the px or in the decimals of em.
shtml>
‘spa This is a paragraph.
This is another paragraph.
1.5.9 Setting with em:
The below format shows how to set the font size Ushg em(unit of measure af font size)
Increase the font size in the decimal values and observe the difference.
1.6 CSS Links
1.6.1 Working with coloring of links upon visiting, visited and hover.
Copy the below code to the HTML Editor and check for the styles on the link
Note: a:hover MUST come after arlink andazvisited in the CSS definition in order ta be
effective.
Note: a:active MUST come after a:hover inthe CSS definition in order to be effective.
17 :€55 Lists:
1.7.1 Styling lists with different bullets
Below styling would set different styles to different ordered lists,
Observe the way each of the list style types are being put up.
ehtml>
16 |Page
Example of unordered lists:
slisTeas/li>
Coca Colac/li>
slisteac/li>
Example of ordered lists:
Some text.
Some text.
Some text.
Nate: The "border-width" property does not work if itis used alone. You must add the
“border-style” property to set the borders first.
<1DOCTYPE himl>
chtml>
chead>
A solid red border
A solid green border
Note: The "border-color" property does not work if itis used alone, Use the "border-style"
property to set the borders first.
1.9.3 Setting Border to individual sides:
Now set the style of the
with below code so that each side of the border would look differently.
pt
border-top-style: dotted;
border-right-style: solid;
border-bottom-style: dotted;
border-left-style: solid
}
2z|Page
This is some text in a paragraph.
1.10 C55 Margin
28 [Page1.10.1 Applying Margin to individual sides:
Create two
tag with some text and apply the below style and see how it differs with and without
margins being set to the element.
pt
background-color: yellow;
t
pcx {
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 50px;
This is a paragraph with no specified margins.
This is @ paragraph with specified margins.
1.11 CSS Padding
1.11.1 Simple C35 padding
Create two
tags and set padding for one of them with the below style.
s="padding" >This is @ paragraph with specified paddings.
Change the padding values in the above style and see the difference in preview.
1.11.2 Setting padding with shorthand:
Set the padding style in a single line with multiple values side by side seperated by a space.
This is a paragraph with specified paddings.
1.12 C55 Display
1.12.1 Hiding elements with visibility
31|PageCreate an element
and set the visiblity of the element to hidden with the below styleand
This is a visible headings/h1>
This is a hidden heading
Notice that the hidden heading still takes up space.
1.12.2 Hiding elements with display: none
Now in the same HTML code, instead of visibility hidden, set Display to none and observe the difference.
html>
This is a visible headings/h1>
This is 2 hidden heading
Natice that the hidden heading does not take upspace.
1.12.3 Display lists in line
32|PageTo display all the list items in a single line instead of line by line, set the display as inline.
_blank"sHIMLs/a>
“ blank">cSS
_blank">Javascripts/a>
Now remove the inline style and observe the difference.
1.13 C35 Align
1.13.1 CSS Align using the margin property
Create a
element with some text and set the style with belo:
center {
width: 70%;
background-color: #b0eOe6;
}
33 [Page
attributes.
Whenever you feel lke criticizing anyone,’ he told me, ‘just remember that all the people in this
world haven't had the advantages that you've had.'s/p>
Note: Using margin-auto will not work in IES, unless a IDOCTYPE is declared.
Now add the margin attribute and see the difference in preview.
Add below twa lines in the style.
margin-left: auto:
margin-right: auto:
1.13.2 Align using Position absolute:
Create a
element and the element by default aligns to top left corner of the HTML page.
Now that we want to move this to the right top corner, apply the style below and verify the difference,
To the style, add below lines.
position: absolute;
right: Opx;
In my younger and more vulnerable years my father gave me some advice that I've been turning
over in my mind ever since.
34|Page
"Whenever you feel lke criticizing anyone,’ he told me, ‘just remember that all the people in this
world haven't had the advantages that you've had.'s/p>
1.13.3 Aligning using the float property.
In the above HTML code, use float:left, instead of position absolute and check the preview.
cheml>
In my younger and more vulnerable years my father gave me some advice that I've been turning
‘over in my mind ever since.
'Whenever you feel like criticizing anyone," be told me, ‘just remember that all the people in this
world haven't had the advantages that you've had.'
fit
Change the float attribute to right and observe the difference in preview.
35|PagejQuery Practice Exercises
For the below exercises, Create a new HTML file and copy paste(replace) the code in the same, for Each
of the exercises use Google Chrome browser to see the results. Edit the HTML files in notepadt+.
2.1 jQuery intro
Below exercise will give an over view of how to indude the jQuery library in the HTML document.
So far in all the CSS exercises the jQuery library was not used but going forward we will be using the
same for all HTML code that is written.
To include the jQuery library take an example HTML basic structure and include the below
To verify ifthe file is loaded to the HTML page or not, in Chrome Browser, right click and select inspect
element, after loading the HTML page.
Navigate to the Sources tab and see if below file is present. if you don't find the same then the jQuery
library is not loaded.
\ttp://ajax googleapis.com/ajax/libs/iquery/1.11.1/iquery.min js
This is aheadings/h2>
2.2 jQuery Selectors
2.2.1 Element selector:
Create @ HTML Button in the webpage and add a
\ttp://a)ax googleapis.com/ajax/libs/iquery/1.11.1/jquery.min,
This is a headings/h2>
“spoThis is a paragraph.
This is another paragraph.
Click mec/button>
37|Page2.2.2 Selecting an Element with id
From the above exercise, assign an id attribute tothe
tag and replace the click function content with
below code where “test” refers to the id of the element.
S("Atest) hide();
Save the HTML page and reload the same in the browser. Click the button and test the result
shead>
http://ajax googleapis.com/ajax/libs/jquery/1.11.1/jquery.min js
This isa heading
This is another paragraph.
Click mes/button>
2.2.3 Selecting an Element with a Class
From the HTML code in previous exercise, add a class attribute to another
element and replace the
click function with the below code where “test” refers to the class name,
S(".testi") hide);
Save the HTML page and reload the same in the browser. Cli
the button and test the result,
fest"> This is a heading
“test">This is a paragraph.
<
\ttp://ajax googleapis.com/ajax/libs/jquery/1.11.1/jquery.min js
h2>This is a headingc/h2>
This is a paragraph.
This is another paragraph.
et Value
53|Page2.6 jQuery Set
2.6.1 Setting attributes:
shtml
ttp://ajax.googleapis.com/ajax/libs/iquery/1.11.1/iquery.min.js">
Google.com
Change href Value
2.6.2 Setting multiple attributes
Observe the below code and see how we can set multiple attributes at the same time, using a single
attr.
ttp://Avww google.com” id="goo">goagle.comChange href and title
Mouse aver the link to see that the href attribute has changed and a title attribute is set.
2.6.3 Set attribute using call back function
Call back function’s get the old and new values tothe event when called upon,
In the Click function add the below code to get the previous value and do some manipulations if
required
${"button"}.clicklfunction(}
St'Hgoo"| attr( "href", function(i,origValue}
return origValue + "/images";
Ne
)
)
chtmi>
https//ajax-googleapis.com/ajax/libs/jquery/1.11.1/jquery.min js'>
origValue}{
55|Page2.7 jQuery Add
2.7. Append an element,
Below example code depicts how to add a line item/text to an existing elements.
Within the click function add the below line of code to make an append to existing element,
5{"p").append{" Newly Appended text.");
‘This isa paragraph.
This is another paragraph.
List item
List iter 2
List item 3
Append list items
‘We can also add a different HTML element instead of
.
Place the below line for the btn2 click function, save the HTML and reload and click the button to see
the results.
${°Ol").append{"
ttp://ajax.googleapis.com/ajax/libs/iquery/1.11.1/iquery.min.js">
This is another paragraph.
List item
List item 2
List item 3¢/li>
Prepend text
Prepend list item
s7|Page2.7.3 after and before
Using after and before, we shall see how to make insert content after and before the elements,
shtmb>
");
)
Ms
Me
-cimg src="images/images jpg" alt="Picture" width
100" height="140">
2.8 jQuery Remove
2.8.1 remove element
Using removel) we shall see how to remove a particular element from the DOM,
Create a new button in the body of the HTML and Write a click function on the same.
Within the Click function, Select an element that has to be removed and bind the remove event,
Slelement}.removel);
neight:100px;width:300px;border-1px solid black;background-color:yellow;">
‘This is some text in the div.
Remove div element
2.8.2 empty an element
Instead of removing the element campletely from the DOM, we can empty its content which will put the
empty element in the DOM.
In the same code from the above exercise, instead of removel} use empty(} in the click function which
will be removing the content and makes the div empty.
S("#divt"}emptyl)
\ttp://ajax.googleapis.com/ajax/libs/iquery/1.11.1/iquery.min.js">
‘This is some text in the div.
59|Page
Empty the div element
ttps//ajax-googleapis.com/ajax/libs/jquery/1.11.1/jquery.min js'>
Now remove the width and height attributes from thestyle of the DIV tag and save the HTML and reload
to test, Observe that the DIV comes with its own dimension in the page now. Click the button to get the
height and width now
61|Page2.9.2 inner width and inner height
Paste the below code in the html editor and inspect the element to observe the inner width and height.
Once the HTML page is loaded, right click on the box and click on inspect element.
In the below pane that opens up, check if you are in the elements tab. On your right, scroll down and
after the styles, you should observe a box which depicts the exact dimensions and padding with margins
as well
ttp://ajax.googleapis.com/ajax/libs/iquery/1.11.1/iquery.min.js">
"height: 100px;width:300px;padding: 1Opxsmargin:3px;border:1px solid
“fd
Display dimensions of dive/button>
2.9.3 Outer width and height
Copy the below html code and reload and repeat the same exercise as above to observe the changes to
the dimensions of the div now.
html>
62|Page
\ttp://a)ax googleapis.com/ajax/libs/iquery/1.11.1/iquery.min js
ivi" style="height: 100px;width:300px;padding:10px;margin:3pxborder: 1px solid
.ckground-color:lightblue;">
Display dimensions of div
Now we shall see how exactly to set the width and height of the div,
Dimensions of the div can be set using the below line of code. Use the same in click function.
${"¥#div1").width{500}.height(500);
63 [Pageeight: 100px;width:300px;padding: 10pxsmargin:3pxsborder:1px solid
blue;background-color:lightblue;">