4. CSS (2)
4. CSS (2)
Muhammad Taufiq
Aisyah Binti Mohd. Hanifiah
Siti Nurul Akmal Binti Mohd
Nur Hidayu Binti ‘Aris
• Develop the foundation skills, confidence and ability to build a website from scratch
What is CSS?
Stand for Cascading Style Sheet.
• In addition, CSS are use to alter the font, color, size, and spacing
of a web content, split it into multiple columns, or user may add
animations and other decorative features.
• The stylesheet can be a separate file which all HTML pages on your entire site
can link to.
• Only have to specify the style once for your ENTIRE SITE
• Can change the style for your entire site by editing only ONE FILE.
CSS Syntax
• CSS Syntax rule consists of a selector, property, and its value.
• The selector points to the HTML element where the CSS style is to be
applied.
• The CSS property is separated by semicolons. It is a combination of the
selector name followed by the property: value pair that is defined for the
specific selector.
CSS Syntax - Continue
Output:
In the code on the left, h1 is the selector
of h1 tags, it select the h1 element that
you want to style. The color is a property
and green is the value of that property,
similar text-align is the property and
value of that property is center.
Three ways to insert CSS
• Inline: the “style” attribute
<p style=“font-color:red;font-size:10px;”>Content</p>
Note, the selector for inline CSS is the tag which contains the style
attribute.
Three ways to insert CSS
• Internal: the <style> markup tag
<html><head><style>
p{ background-color: Red;
font-family: serif;
font-color: White; }
</style></head><body>
<p>Content</p>
</body></html>
Three ways to insert CSS
• External: the .css stylesheet file
Output
CSS ID Selectors
• The id selector selects the id attribute of an HTML element
to select a specific element.
• An id is always unique within the page so it is chosen to
select a single, unique element.
It is written with the hash character (#), followed by the id
of the element.
CSS ID Selectors
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. #para1 {
Output
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>
CSS Class Selectors
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
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. .center { Output
6. text-align: center;
7. color: blue;
8. }
9. </style>
10.</head>
11.<body>
12.<h1 class="center">This heading is blue and ce
nter-aligned.</h1>
13.<p class="center">This paragraph is blue and ce
nter-aligned.</p>
14.</body>
15.</html>
CSS Universal Selectors
The universal selector is used as a wildcard character. It selects
all the elements on the pages.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. * { Output
6. color: green;
7. font-size: 20px;
8. }
9. </style>
10.</head>
11.<body>
12.<h2>This is heading</h2>
13.<p>This style will be applied on every paragraph.</
p>
14.<p id="para1">Me too!</p>
15.<p>And me!</p>
16.</body>
17.</html>
CSS Group Selectors
The grouping selector is used to select all the elements with the same style definitions.
Grouping selector is used to minimize the code. Commas are used to separate each selector in grouping.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
Output
5. h1, h2, p {
6. text-align: center;
7. color: blue;
8. }
9. </style>
10. </head>
11. <body>
12. <h1>Hello Javatpoint.com</h1>
13. <h2>Hello Javatpoint.com (In smaller font)</h2>
14. <p>This is a paragraph.</p>
15. </body>
16. </html>
CSS Properties
Specific aspect or characteristic of an element that you want to modify.
Text and Font
• Text and font properties in CSS are used to format the appearance of words and
text on a webpage.
• The font-size property sets the size of the text. Font size can be set with pixels
(px), em, percentage (%) or by using keywords.
Using em
• An em is is equal to the current font size. If the element is not a child of any
element, the current font size is the browser’s default font size. As mentioned
above, the default font size used on most browsers is 16px. Hence by default 1em
= 16px.
Text and Font
Using percentage
• Percentage is similar to em. 200% simply means 2 times the current font size. Hence, 200% =
2em.
Using Keywords
Commonly used keywords are xx-small, x-small, small, medium (this is the default), large,
x-large and xx-large.
Examples:
• font-size: 40px;
• font-size: 1.5em;
• font-size: 120%;
• font-size: large;
Text Style
Source from
https://x.com/Div_pradeep/status/15519474434318
04930
Text Property : Letter-Spacing
• Letter spacing is used to increase or decrease the spacing between letters in a word. User
at first specify the amount of spacing in pixels.
• To increase the spacing, user may insert a positive value. To decrease it, use a negative
value.
Text Property : Letter-Spacing
• <!DOCTYPE html>
• <html>
• <head> Output
• <title>The letter-spacing property in CSS</title>
• <style type="text/css">
• .normal { letter-spacing: normal; }
• .em-wide { letter-spacing: 0.4em; }
• .em-wider { letter-spacing: 1em; }
• .em-tight { letter-spacing: -0.05em; }
• .px-wide { letter-spacing: 6px;
• color: green;
• }
• </style>
• </head>
• <body>
Text Property : Word-Spacing
• This element is used to increase or decrease the spacing between words in text. Similar to
letter-spacing, you specify the amount of spacing in pixels, using positive to
increase the spacing and negative to decrease it.