Introduction
Before moving on with this session, I would strongly recommend you to check Part 1 of this series.
The following are the topics that we will discuss in this article.
- Background properties
- Text properties
- Font properties
- Link properties
Background properties
To define or add a background, we can use the background properties.
- Background-color
To specify a background color for an element or a group of an element, we can use the background-color property.
Let's look at an example of it.
Step 1
Create a new Index.html file and in the body section of it, add a div container tag and inside this tag add a paragraph.
Step 2
The next is to create an internal stylesheet and for that write the following in the head section of this current Index.html file.
In the internal stylesheet, we created a rule of type “Descendant Selector” that we already discussed in Part 1 of this series.
We want the background color of the paragraph to be Green and for that, we can use the background-color property and set its value to Green.
The following is the entire code snippet.
- <html>
- <head>
- <title></title>
- <style type="text/css">
- div p
- {
- background-color: green;
- }
- </style>
- </head>
- <body>
- <div>
- <p>Welcome to CSS Tutorial</p>
- </div>
- </body>
- </html>
Open Index.html in a browser.
- Background-image
To add a background image for an element, use the background-image property.
Let's look at an example of it.
Step 1
Create a new IndexTwo.html file and add the following to the body section.
Step 2
Create an internal stylesheet in the head section of IndexTwo.html. Create a new element selector rule of div and add the following.
Pass the address in the URL block.
The following is the entire code snippet.
- <html>
- <head>
- <title></title>
- <style type="text/css">
- div{
- width: 200px;
- height: 100px;
- background-image: url(Images/1.jpg);
- }
- </style>
- </head>
- <body>
- <div>
- <p>Background Image property</p>
- </div>
- </body>
- </html>
Open in a browser.
NOTE: By default, the background images are repeated horizontally(x) as well as vertically(y). But we can manipulate this rule using the background-repeat property.
- Background-repeat
To repeat a background vertically or horizontally or both, we can use the background-repeat property.
Currently, the image repeats both vertically and horizontally.
Let's say we want the background image to be repeated only in the horizontal direction and for that, all we need to do is pass repeat-x as a value for the background-repeat property.
If you don't want to repeat the background, pass no-repeat as a value.
- Background-position
Let's say for some reason we don't want to repeat the background but we want the Orange part of the background image to be displayed as a background for the paragraph.
Set the background-position property to the bottom left.
- background-attachment
To make a background fixed or to scroll with other content, we use the background-attachment property.
Let's look at an example.
Step 1
Create a new IndexThree.html file and add thirteen paragraphs to it.
Step 2
Create two different rules in the internal stylesheet using element selectors.
In the body element, we set a background image. We passed no-repeat for the background-repeat property since we don't want this image to be repeated in the body and we passed fixed as a value for the background-attachment. That means that whenever we scroll the page down, this background image remains fixed at where it is displayed.
In the paragraph element, all we are doing is assigning White as a text color.
Open the file in a browser.
Look at the output we got. We just got one and two paragraphs displayed with a background image.
But when we scroll the page down, we get the rest of the paragraphs on the same background image because the image is fixed.
And so on.
Entire code snippet
- <html>
- <head>
- <title></title>
- <style type="text/css">
- body
- {
- background-image: url(Images/1.jpg);
- background-repeat: no-repeat;
- background-attachment: fixed;
- }
- p
- {
- color: white;
-
- }
- </style>
- </head>
- <body>
- <p>One</p>
- <p>Two</p>
- <p>Three</p>
- <p>Four</p>
- <p>Five</p>
- <p>Six</p>
- <p>Seven</p>
- <p>Eight</p>
- <p>Nine</p>
- <p>Ten</p>
- <p>Eleven</p>
- <p>Twelve</p>
- <p>Thirteen</p>
- </body>
- </html>
Text Properties
To change the fore-color of a paragraph we use the color property that is nothing but a text property. Using the text property we can decorate the text using the Text-Decoration property or we can change the alignment of the text using the Text-Align property and many more.
Let's look at each of them.
- Color
Create a new HTML file and add the following paragraph.
- <html>
- <head>
- <title></title>
- </head>
- <body>
- <p>This paragraph color is orange</p>
- </body>
- </html>
Open in a browser.
Let's say we want to change the fore-color of this paragraph to Orange and for that, we need to create a new CSS rule.
Create an internal stylesheet in the head section.
- <style type="text/css">
- p{
- color: orange;
- }
- </style>
Refresh the page.
- Text-Alignment
To set the horizontal alignment to the text, in other words, left, right, center or justify, use the text-align property.
Let's look at an example.
In the previous example, we saw how to fill in color in a text. Now let's change its alignment.
- <style type="text/css">
- p{
- color: orange;
- text-align: center;
- }
- </style>
Refresh the page.
So, now the paragraph is centered.
If you want to align text to the right, pass right as a value for the text-align property.
- <style type="text/css">
- p{
- color: orange;
- text-align: right;
- }
- </style>
- Text-Decoration
To add an underline or line-through the text or over the text, use the text-decoration property.
Let's look at an example.
Add another paragraph to the HTML file.
- <body>
- <p id="para">This paragraph color is orange</p>
- <p id="para1">This is <span>another</span> a new paragraph </p>
- </body>
Now we have two paragraphs and both of these paragraphs have different Ids.
Let's add some decorations in para1.
Create a new CSS rule in the internal stylesheet and this time we will create a Descendent selector rule.
Let's say for some reason we want to strike out the span element from the para1, for that we can set the text-decoration property to line-through.
- #para1 span{
- text-decoration: line-through;
- }
Refresh the page.
In a similar way, if you want to add a line under a text then set the text-decoration property to underline.
Add another span element in para1.
- <p id="para1">This is <span>another</span> a new <span id="under">paragraph</span></p>
Wrap the paragraph text with span and provide an id of under.
In the internal stylesheet, create a new CSS rule.
- #para1 span#under{
- text-decoration: underline;
- }
Refresh the page.
In a similar way, if you want to add a line over a text or the entire paragraph, set the text-decoration property to overline.
- Text: Transform
To change the case of a text or the entire paragraph to uppercase, lowercase or want to capitalize on every first of a text, use text-transformation.
Let's look at an example.
Create a new HTML file and add three paragraphs as in the following:
- <html>
- <head>
- <title></title>
- </head>
- <body>
- <p id="para1">This is a uppercase paragraph</p>
- <p id="para2">THIS IS A LOWERCASE PARAGRAPH</p>
- <p id="para3">This is a capitalized paragraph</p>
- </body>
- </html>
Open in a browser.
Now add an internal stylesheet to the head section and create three different CSS rules using the Id selector.
- <head>
- <title></title>
- <style type="text/css">
- #para1{
-
- }
- #para2{
-
- }
- #para3{
-
- }
- </style>
- </head>
In the first Id selector, set the text-transform property value to uppercase.
In the second Id selector, set the text-transform property value to lowercase.
In the third Id selector, set the text-transform property value to capitalize.
- <head>
- <title></title>
- <style type="text/css">
- #para1{
- text-transform: uppercase;
- }
- #para2{
- text-transform: lowercase;
- }
- #para3{
- text-transform: capitalize;
- }
- </style>
Refresh the page.
- Letter-Spacing
To add or remove spaces between each character, use letter-spacing.
To add a space use a positive value and to remove a space use a negative value.
Let's say for some reason we want to add a space of positive 5px in para1 and a negative 5px in para3.
In the output, para1 has 5px of space between each character and in para3, space is reduced between each character by 5px.
NOTE
To add a space between each character we use the letter-spacing property.
Just like that, to add a space between each word, we can use the word-spacing property.
To increase or decrease the height of a text, we can use the line-height property.
Font Properties
The font properties can be classified into the following:
- Font-family
- Font-size
- Font-style
- Font-variant
- Font-weight
- Font
Let's look at each of them practically.
Create a new HTML file and write the following inside the body element.
- <body>
- <p id="para1">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. </p>
- <br/>
- <br/>
- <p id ="para2">Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
- </body>
Note: The paragraph content is just a snippet provided by the sublime text editor.
Open the file in a browser.
In the same HTML file, create a new internal stylesheet in the head section and write the following in it.
- <head>
- <title></title>
- <style type="text/css">
- #para1{
-
-
- #para2{
-
- }
- </style>
- </head>
So, until now what we have done is, we have created a new HTML file in which we added two paragraphs with an id of para1 and para2 respectively.
In the head section of the same HTML file, we created an internal stylesheet in which we created two new CSS rules using an id selector.
Now let's say we want to change the font family of para1 to Verdana and font family of para2 to Garamond. For that, we can use the font-family property and pass the value as Verdana for para1 and pass the value as Garamond for para2.
- <style type="text/css">
- #para1{
- font-family: Verdana;
- }
- #para2{
- font-family: Garamond;
- }
- </style>
Currently, our HTML page looks like this.
Refresh the page.
So, now the font of the paragraphs is changed respectively.
- Font-Size
Now let's increase the font size of both of these paragraphs and for that, we can use the font-size property.
The default text size in the browser is 16px and the font-size value can be in percentage, or pixels, or em.
Let's say we want to give a font size of 32px in para1 and 2em in para2.
- <style type="text/css">
- #para1{
- font-family: Verdana;
- font-size: 32px;
- }
- #para2{
- font-family: Garamond;
- font-size: 2em;
- }
- </style>
As we know, the default size of the text is 16px and just like that the default size of 1em is 16px and in the rule we passed the font-size as 2 em, which means 32px.
Refresh the page.
- Font- style
If you want to change the text font from normal to italic, then use this property and pass italic as a value.
Let's say we want to change the font style of para1 to italic and for that write the following in #para1 CSS rule.
- #para1{
- font-family: Verdana;
- font-size: 32px;
- font-style: italic;
- }
Refresh the page.
- Font-Variant
The font-variant property can be used to convert the lowercase letters to upper case. But the converted uppercase letters appear in a smaller font size than the original uppercase letter size.
DEMO
Create a new HTML file and add two paragraphs to the body section.
- <p id="para1">Paragraph with uppercase transformation</p>
- <p id="para2">Paragraph with small-caps font variant</p>
Create an internal stylesheet in the head section of your HTML file and write the following.
- <style type="text/css">
- #para1{
- text-transform: uppercase;
- }
- #para2{
- font-variant: small-caps;
- }
- </style>
For paragraph one, we used the text-transform property and passed the value “uppercase” that will change the lower case text to uppercase.
For paragraph two, we used the font-variant property and passed the value “small-caps” that will change the lowercase text to uppercase.
Open the file in a browser.
In the output we can see, the second paragraph in which we passed small-caps is converted to uppercase but the converted uppercase letters appear in smaller font sizes than the original uppercase letter size.
- Font-weight
To set the thickness or thinness of a paragraph or text, use the font-weight property and pass the value of bold, bolder, or lighter.
DEMO
Create a new HTML file and add three paragraphs.
- <body>
- <p id="para1">This paragraph is lighter</p>
- <p id="para2">This paragraph is bold</p>
- <p id="para3">This pargraph is bolder</p>
- </body>
In the head section of this HTML file, add an external stylesheet and write the following.
- <style type="text/css">
- #para1{
- font-weight: lighter;
- }
- #para2{
- font-weight: bold;
- }
- #para3{
- font-weight: bolder;
- }
- </style>
Open the file in a browser.
Link Property
Whenever you want to add styles to hyperlinks then use the link properties.
There are four states of link:
- a:link: it is a normal state meaning the link is unvisited.
- a:visited: a visited link.
- a:hover: a state where the user hovers the mouse over the link.
- a:active: link that is clicked.
NOTE
a:hover must come after a:link and a:visited whereas a:active must come after a:hover.
Let's look at an example of it.
DEMO
Create a new HTML file and in the body section, add an anchor tag element.
- <body>
- <a href="http://www.c-sharpcorner.com/">CSharpCorner</a>
- </body>
Open the file in a browser.
In the head section of the same file, create an internal stylesheet.
In this stylesheet create the rules for all four states in the same order as shown below.
- <style type="text/css">
- a:link{
-
- }
- a:visited{
-
- }
- a:hover{
-
- }
- a:active{
-
- }
- </style>
Now let's say our business requirements are such that we don't want to display an underline in the link.
In the first statem a:link rule, write the following.
- a:link{
- /*To remove the underline, pass none in the text-decoration property*/
- text-decoration: none;
- }
Refresh the page.
Whenever a link is visited, we want to change the color from default to Black and for that pass the value Black in the color property in the visited state, in other words, a:visited.
- a:visited{
- color: black;
- }
Refresh the page and click on the link.
Now whenever a user hovers the mouse on the link, we want to change the color of the link from default to Yellow and pass the color property value as Yellow in the hover state of the link.
- a:hover{
- color: yellow;
- }
Refresh the page.
Now let's say that whenever the user clicks the link, meaning when the link is active, we want to change the background color of the link to Black and the text color to Red and for that, we can use the background-color and color property in the a:active state.
- a:active{
- color: red;
- background-color: black;
- }
Refresh the page.
Summary
This article explained the four most commonly used properties Background, Text, Font, and Link.
The next article of this series will explain more about properties.
Until then keep learning.
Thank you.