Core Web Development: CW412 Programming in CSS
Core Web Development: CW412 Programming in CSS
Core Web Development: CW412 Programming in CSS
CW412
Programming in CSS
Contents
1. INTRODUCTION TO CSS
2. CSS PROPERTIES
2020
3.
4.
5.
CSS ADVANCE
CSS DESIGN
CSS QUESTIONS
Formative Assessment-1
Formative Assessment-2
Formative Assessment-3
Prescribed Book
JAVASCRIPT
CSS Tutorial
It is a style sheet language which is used to describe the look and formatting of a
document written in markup language.
It is generally used with HTML to change the style of web pages and user
interfaces.
It can also be used with any kind of XML documents including plain XML, SVG
and XUL.
CSS is used along with HTML and JavaScript in most websites to create user
interfaces for web applications and user interfaces for many mobile applications.
What does CSS do
•You can completely change the look of your website with only a few
changes in CSS code.
Why use CSS
These are the three major benefits of CSS:
For example: If you are developing a large website where fonts and color information
are added on every single page, it will be become a long and expensive process.
CSS style definitions are saved in external CSS files so it is possible to change the
entire website by changing just one file.
CSS provides more detailed attributes than plain HTML to define the look and feel of
the website.
CSS Syntax
1.color: yellow;
2.font-size: 11 px;
1.Selector{Property1: value1; Property2: value2; ..........;}
CSS Selector
CSS selectors select HTML elements according to its id, class, type, attribute
There are several different types of selectors in CSS.
2.CSS Id Selector
It is written with the hash character (#), followed by the id of the element.
Let?s take an example with the id "para1".
1.<!DOCTYPE html>
2.<html>
3.<head>
4.<style>
5.#para1 {
6. text-align: center;
7. color: blue;
8.}
9.</style>
10.</head>
11.<body>
12.<p id="para1">Hello Javatpoint.com</p>
13.<p>This paragraph will not be affected.</p>
14.</body>
15.</html>
The class selector selects HTML elements with a specific class attribute.
It is used with a period character . (full stop symbol) followed by the class name.
As you can see, you need to define CSS properties for all the elements.
1.Inline CSS
2.Internal CSS
3.External CSS
1) Inline CSS
For example:
1.<p style="color:blue">Hello CSS</p>
For example:
1.<style>
2.p{color:blue}
3.</style>
For example:
The link tag must be used inside head section of html.
1.p{color:blue}
You need to link this style.css file to your html pages like this:
1.<link rel="stylesheet" type="text/css" href="style.css">
The inline CSS is also a method to insert style sheets in HTML document.
This method mitigates some advantages of style sheets so it is advised to use this method
sparingly.
If you want to use inline CSS, you should use the style attribute to the relevant tag.
Syntax:
1.<htmltag style="cssproperty1:value; cssproperty2:value;"> </htmltag>
Example
<h2 style="color:red;margin-
left:40px;">Inline CSS is applied on this heading.
</h2>
1.<p>This paragraph is not affected.</p>
Internal CSS
The internal style sheet is used to add a unique style for a single document.
It is defined in <head> section of the HTML page inside the <style> tag.
Example
1.<!DOCTYPE html>
2.<html>
3.<head>
4.<style>
5.body {
6. background-color: linen;
7.}
8.h1 {
9. color: red;
10. margin-left: 80px;
11.}
12.</style>
13.</head>
14.<body>
15.<h1>The internal style sheet is applied on this heading.</
h1>
16.<p>This paragraph will not be affected.</p>
17.</body>
18.</html>
External CSS
The external style sheet is generally used when you want to make changes on
multiple pages.
It is ideal for this condition because it facilitates you to change the look of the entire
web site by changing just one file.
It uses the <link> tag on every pages and the <link> tag should be put inside the head
section.
Example
1.<head>
2.<link rel="stylesheet" type="text/css" href="mystyle.css">
3.</head>
CSS Background
The CSS background property is used to define the background effects
on the element.
2.background-image
3.background-repeat
The external style sheet may be written in any text editor but must be saved
with a .css extension.
You can set the background image for a page like this.
1.<!DOCTYPE html>
2.<html>
3.<head>
4.<style>
5.body {
6.background-image: url("paper1.gif");
7.margin-left:100px;
8.}
9.</style>
10.</head>
11.<body>
12.<h1>Hello Javatpoint.com</h1>
13.</body>
14.</html>
background-repeat: repeat-x;
1.<!DOCTYPE html>
2.<html>
3.<head>
4.<style>
5.body {
6. background-image: url("gradient_bg.png");
7. background-repeat: repeat-x;
8.}
9.</style>
10.</head>
11.<body>
12.<h1>Hello Javatpoint.com</h1>
13.</body>
14.</html>
3) CSS background-repeat
1.<!DOCTYPE html>
2.<html>
3.<head>
4.<style>
5.body {
6. background-image: url("gradient_bg.png");
7. background-repeat: repeat-y;
8.}
9.</style>
10.</head>
11.<body>
12.<h1>Hello Javatpoint.com</h1>
13.</body>
14.</html>
4) CSS background-attachment
The background-attachment property is used to specify if the background image is
fixed or scroll with the rest of the page in browser window.
If you set fixed the background image then the image will not move during
scrolling in the browser.
1.background: white url('bbb.gif');
2.background-repeat: no-repeat;
3.background-attachment: fixed;
5) CSS background-position
The background-position property is used to define the initial position of the
background image.
2.Top
3.Bottom
4.Left
5.right
1.background: white url('good-morning.jpg');
2.background-repeat: no-repeat;
3.background-attachment: fixed;
4.background-position: center;
CSS Border
The CSS border is a shorthand property used to set the border on an
element.
The CSS border properties are use to specify the style, color and size of the border
of an element.
•border-color
•border-width
•border-radius
1) CSS border-style
The Border style property is used to specify the border type which you want to
display on the web page.
There are some border style values which are used with border-style property to
define a border.
Value Description
none It doesn't define any border.
dotted It is used to define a dotted border.
dashed It is used to define a dashed border.
solid It is used to define a solid border.
double It defines two borders wIth the same border-
width value.
groove It defines a 3d grooved border. effect is
generated according to border-color value.
ridge It defines a 3d ridged border. effect is
generated according to border-color value.
inset It defines a 3d inset border. effect is
generated according to border-color value.
outset It defines a 3d outset border. effect is
generated according to border-color value.
1.<!DOCTYPE html>
2.<html>
3.<head>
4.<style>
5.p.none {border-style: none;}
6.p.dotted {border-style: dotted;}
7.p.dashed {border-style: dashed;}
8.p.solid {border-style: solid;}
9.p.double {border-style: double;}
10.p.groove {border-style: groove;}
11.p.ridge {border-style: ridge;}
12.p.inset {border-style: inset;}
13.p.outset {border-style: outset;}
14.p.hidden {border-style: hidden;}
15.</style>
16.</head>
17.<body>
18.<p class="none">No border.</p>
19.<p class="dotted">A dotted border.</p>
20.<p class="dashed">A dashed border.</p>
21.<p class="solid">A solid border.</p>
22.<p class="double">A double border.</p>
23.<p class="groove">A groove border.</p>
24.<p class="ridge">A ridge border.</p>
25.<p class="inset">An inset border.</p>
26.<p class="outset">An outset border.</p>
27.<p class="hidden">A hidden border.</p>
28.</body>
29.</html>
2) CSS border-width
The border-width property is used to set the border's width.
It is set in pixels.
You can also use the one of the three pre-defined values, thin, medium or
thick to set the width of the border.
1.<!DOCTYPE html>
2.<html>
3.<head>
4.<style>
5.p.one {
6. border-style: solid;
7. border-width: 5px;
8.}
9.p.two {
10. border-style: solid;
11. border-width: medium;
12.}
13.p.three {
14. border-style: solid;
15. border-width: 1px;
16.}
17.</style>
18.</head>
19.<body>
20.<p class="one">Write your text here.</p>
21.<p class="two">Write your text here.</p>
22.<p class="three">Write your text here.</p>
23.</body>
24.</html>
3) CSS border-color
There are three methods to set the color of the border.
•RGB: It specifies the RGB value of the color. For example: "rgb(255,0,0)".
•Hex: It specifies the hex value of the color. For example: "#ff0000".
If the border color is not set it is inherited from the color property of the element.
1.<!DOCTYPE html>
2.<html>
3.<head>
4.<style>
5.p.one {
6. border-style: solid;
7. border-color: red;
8.}
9.p.two {
10. border-style: solid;
11. border-color: #98bf21;
12.}
13.</style>
14.</head>
15.<body>
16.<p class="one">This is a solid red border</p>
17.<p class="two">This is a solid green border</p>
18.</body>
19.</html>
CSS border-radius property
This CSS property sets the rounded borders and provides the rounded corners
around an element, tags, or div.
We can specify the border for all four corners of the box in a single declaration using the
border-radius.
Property Description
border-top-left-radius It is used to set the border-radius
for the top-left corner
Let's see what happens when we provide a single value, two values, three values, and
four values to this property.
•If we provide a single value (such as border-radius: 30px;) to this property, it will
set all corners to the same value.
•When we specify two values (such as border-radius: 20% 10% ;), then the first
value will be used for the top-left and bottom-right corners, and the second value will
be used for the top-right and bottom-left corners.
•When we use three values (such as border-radius: 10% 30% 20%;) then the
first value will be used for the top-left corner, the second value will be applied
on top-right, and bottom-left corners and the third value will be applied to the
bottom-right corner.
•Similarly, when this property has four values (border-radius: 10% 30% 20%
40%;) then the first value will be the radius of top-left, the second value will be
used for the top-right, the third value will be applied on bottom-right, and the
fourth value is used for bottom-left.
Syntax
1.border-radius: 1-4 length | % / 1-4 length | % | inherit | initial;
Property values
CSS Display
CSS display is the most important property of CSS which is used to control the
layout of the element.
Every element on the webpage is a rectangular box and the CSS property defines the
behavior of that rectangular box.
Other CSS display values
Property-value Description
flex It is used to display an element as an block-level flex
container. It is new in css3.
inline-flex It is used to display an element as an inline-level flex
container. It is new in css3.
inline-table It displays an element as an inline-level table.
list-Item It makes the element behave like a <li> element.
table It makes the element behave like a <table> element.
table-caption It makes the element behave like a <caption> element.
table-column-group It makes the element behave like a <colgroup> element.
table-header-group It makes the element behave like a <thead> element.
table-footer-group It makes the element behave like a <tfoot> element.
table-row-group It makes the element behave like a <tbody> element.
table-cell It makes the element behave like a <td> element.
table-row It makes the element behave like a <tr> element.
table-column It makes the element behave like a <col> element.
Basic styling in Buttons
There are multiple properties available that are used to style the button element.
background-color
As we have discussed earlier, this property is used for setting the background color of the
button element.
Syntax
1.element {
2. // background-color style
3.}
border
It is used to set the border of the button.
Syntax
1.element {
2. // border style
3.}
border-radius
Syntax
1.element {
2. // border-radius property
3.}
box-shadow
As its name implies, it is used to create the shadow of the button box.
It is used to add the shadow to the button. We can also create a shadow during the
hover on the button.
Syntax
1.box-shadow: [horizontal offset] [vertical offset] [blur radius]
2. [optional spread radius] [color];
padding
It is used to set the button padding.
1.element {
2. // padding style
3.}
CSS Float
It is used to push an element to the left or right, allowing other element to wrap
around it.
In the print display, image is set into the page such that text wraps around it as
needed.
Its web layout is also just similar to print layout.
CSS Float Properties
By the use of CSS font property you can change the text size, color, style and
more.
Here, you will also know how to resize your font using percentage.
These are some important font attributes:
1.CSS Font color: This property is used to change the color of the text. (standalone
attribute)
2.CSS Font family: This property is used to change the face of the font.
1.CSS Font style: This property is used to make the font bold, italic or
oblique.
5.CSS Font style: This property is used to make the font bold, italic or
oblique.
1.<!DOCTYPE html>
2.<html>
3.<head>
4.<style>
5.body {
6. font-size: 100%;
7.}
8.h1 { color: red; }
9.h2 { color: #9000A1; }
10.p { color:rgb(0, 220, 98); }
11.}
12.</style>
13.</head>
14.<body>
15.<h1>This is heading 1</h1>
16.<h2>This is heading 2</h2>
17.<p>This is a paragraph.</p>
18.</body>
19.</html>
2) CSS Font Family
CSS font family can be divided in two types:
•Font family: It specifies the font family name like Arial, New Times Roman
Serif: Serif fonts include small lines at the end of characters. Example of serif: Times
new roman, Georgia
Sans-serif: A sans-serif font doesn't include the small lines at the end of characters.
Example of Sans-serif: Arial, Verdana
1.<!DOCTYPE html>
2.<html>
3.<head>
4.<style>
5.body {
6.font-size: 100%;
7.}
8.h1 { font-family: sans-serif; }
9.h2 { font-family: serif; }
10.p { font-family: monospace; }
11.}
12.</style>
13.</head>
14.<body>
15.<h1>This heading is shown in sans-serif.</h1>
16.<h2>This heading is shown in serif.</h2>
17.<p>This paragraph is written in monospace.</p>
18.</body>
19.</html>
3) CSS Font Size
CSS font size property is used to change the size of the font.
These are the possible values that can be used to set the font size:
Font Size Value Description
xx-small used to display the extremely small text size.
1.<!DOCTYPE html>
2.<html>
3.<head>
4.<style>
5.body {
6.font-size: 100%;
7.}
8.h2 { font-style: italic; }
9.h3 { font-style: oblique; }
10.h4 { font-style: normal; }
11.}
12.</style>
13.</head>
14.<body>
15.<h2>This heading is shown in italic font.</h2>
16.<h3>This heading is shown in oblique font.</h3>
17.<h4>This heading is shown in normal font.</h4>
18.</body>
19.</html>
5) CSS Font Variant
CSS font variant property specifies how to set font variant of an element.
CSS font weight property defines the weight of the font and specify that how bold
a font is.
The possible values of font weight may be normal, bold, bolder, lighter or number
(100, 200..... upto 900).
1.<!DOCTYPE html>
2.<html>
3.<body>
4.<p style="font-weight:bold;">This font is bold.</p>
5.<p style="font-weight:bolder;">This font is bolder.</p>
6.<p style="font-weight:lighter;">This font is lighter.</p>
7.<p style="font-weight:100;">This font is 100 weight.</p>
8.<p style="font-weight:200;">This font is 200 weight.</p>
9.<p style="font-weight:300;">This font is 300 weight.</p>
10.<p style="font-weight:400;">This font is 400 weight.</p>
11.<p style="font-weight:500;">This font is 500 weight.</p>
12.<p style="font-weight:600;">This font is 600 weight.</p>
13.<p style="font-weight:700;">This font is 700 weight.</p>
14.<p style="font-weight:800;">This font is 800 weight.</p>
15.<p style="font-weight:900;">This font is 900 weight.</p>
16.</body>
17.</html>
CSS Font-size
The font-size property in CSS is used to specify the height and size of the
font.
Syntax
1.font-size: medium|large|x-large|xx-large|xx-small|x-small|small|;
Using absolute-size, it is not possible to change the size of the text in all browsers.
Relative-size
It is used to set the size of the text relative to its neighboring elements.
As stated above, the default text size in browsers is 16px. So, we can say that the
default size of 1em is 16px.
We can set the size of the text by using a vw unit, which stands for the 'viewport
width’.
If the width of the viewport is 50cm, then the 1vw is equal to 0.5 cm.
Example
1.<!DOCTYPE html>
2.<html>
3.<meta name="viewport" content="width=device-width, initial-
scale=1.0">
4.<body>
5.
6.
7.<p style="font-
size:5vw;">First paragraph having the width of 5vw.</p>
8.<p style="font-
size:10vw;">Second paragraph having the width of 10vw.</p>
9.
10.</body>
11.</html>
Font-size with the length property
It is used to set the size of the font in length.
The length can be in cm, px, pt, etc. Negative values of length property are not
allowed in font-size.
Syntax
1.font-size: length;
Example
1.<!DOCTYPE html>
2.<html>
3. <head>
4. <style>
5. .length {
6. color:red;
7. font-size: 5cm;
8. }
9. </style>
10. </head>
11.
12. <body>
13. <h1>font-size property</h1>
14.
15. <p class = "length">A paragraph having length 5cm.</
p>
16. </body>
17.</html>
Thank You