CSS III Second
CSS III Second
CSS selectors are used to "find" (or select) the HTML elements you want to style.
</body>
</html>
<h1>Hello world!</h1>
<html>
<head>
<style>
h1, h2, p {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<h2>Smaller heading!</h2>
<p>This is a paragraph.</p>
</body>
</html>
External CSS
Internal CSS
Inline CSS
External CSS
With an external style sheet, you can change the look of an entire website
by changing just one file!
Each HTML page must include a reference to the external style sheet file
inside the <link> element, inside the head section.
<html>
<head>
<link rel="stylesheet" href="mystyle.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
"mystyle.css"
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
Internal CSS
An internal style sheet may be used if one single HTML page has a unique
style.
The internal style is defined inside the <style> element, inside the head
section.
<html>
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Inline CSS
An inline style may be used to apply a unique style for a single element.
To use inline styles, add the style attribute to the relevant element. The
style attribute can contain any CSS property.
<html>
<body>
<h1 style="color:blue;text-align:center;">This is a heading</h1>
<p style="color:red;">This is a paragraph.</p>
</body>
</html>
CSS – Fonts:
We can set following font properties of an element −
<html>
<head>
</head>
<body>
<p style = "font-style:italic;">
<p style = "font-variant:small-caps;">
This text will be rendered in italic style
</p>
<p style = "font-weight:bold;">
This font is bold.
</p>
<p style = "font-weight:bolder;">
This font is bolder.
</p>
</body>
</html>
CSS – Text:
A text refers to a piece of written or printed information in the form
of words or characters that can be read and understood. CSS
provides a number of properties to format text in beautiful way.
text-align :
<body>
<h2>Setting Text Alignment using CSS</h2>
<p style="text-align: left;">Text Left Alignment.</p>
<p style="text-align: right;">Text Right Alignment.</p>
<p style="text-align: center;">Text Center Alin.</p>
text-align-last:
LTR (Left-to-Right): Default value, used for languages that are written from
left to right, like English. You don't need to specify this value explicitly unless
you want to override an inherited right-to-left direction.
RTL (Right-to-Left): Used for languages that are written from right to left, such as
Arabic or Hebrew. When using rtl, text will be aligned right by default.
<body>
<p style = "direction: rtl;">
Right to Left
</p>
<p style = "direction: ltr;">
Left to Right
</p>
</body>
text-decoration:
The CSS text-decoration property helps in adding extra decoration to the text, such as, adding a
line (underline, strikethrough, overline) and color, style and thickness to the line.
It is a shorthand property to the following properties:
text-decoration-line: Sets the type of decoration line (underline, strikethrough or
overline).
text-decoration-color: Sets the color to the text decoration.
text-decoration-style: Sets the style to the text decoration (dotted, dashed, solid, wavy,
double, etc.)
text-decoration-thickness: Sets the thickness to the text decoration.
<html>
<head>
<style>
.overline{
text-decoration: overline solid red 5px;
}
.line-through{
text-decoration: line-through solid green 1px;
}
.underline{
text-decoration: underline dashed 2pt blue;
}
</style>
</head>
<body>
<h2>Text Decoration using CSS</h2>
<p class="overline">Overline text decoration.</p>
<p class="line-through">Line-through text decoration.</p>
<p class="underline">Underline text decoration.</p>
</body>
</html>
Text Transform:
The CSS text-transform property is used to change the appearance of text by transforming it in
various ways. It can be used to convert text to uppercase, lowercase, capitalize the first letter of
each word, or even capitalize all letters.
The text-transform property can have one of the following values:
lowercase: Transforms all text to lowercase.
uppercase: Transforms all text to uppercase.
capitalize: Capitalizes the first character of each word.
none: No text transformation is applied.
full-width: Transforms all characters to their full-width variant.
full-size-kana: Transforms all the small Kana characters to full-size Kana characters.
This is specifically used for <ruby> annotation text.
initial: Sets the text-transform value to its default value (none).
inherit: Inherits the text-transform value from its parent element.
unset: Removes any previously set text-transform value.
<html>
<body>
<h2>Text Transform Examples</h2>
<p style="text-transform: capitalize;">This text will be capitalized.</p>
Text Indentation:
Indentation is the space between the margin and the first line of text. Proper indentation enhances
the readability and clarity of text on a page. The following values can be passed to this property:
length: Any specific length {pixels (px), points (pt), ems (em), etc}. Default value is 0.
percentage (%): The value passed is in relation to the percentage of the width of the
parent element.
each-line: Affects the first line of a block of text along with each line after a forced line
break.
hanging: Indentation is inverted, except for the first line.
initial: Sets the text-indent to its default value.
inherit: Allows to inherit the text-indent value from its parent element.
<html>
<head>
</head>
<body>
<h2>Text indentation Examples</h2>
<p style="text-indent: 2cm;">Text indentation at 2 cm.</p>
<p style="text-indent: 2in;">Text indentation at 2 inches.</p>
<p style="text-indent: 20%;">Text indentation at 20%.</p>
</body>
</html>
Output:
<html>
<body>
<h2>Letter Spacing Examples</h2>
<p style="letter-spacing: normal;">Letter spacing normal.</p>
<p style="letter-spacing: 5px;">Letter spacing increased.</p>
<p style="letter-spacing: -1px;">Letter spacing decreased.</p>
</body>
</html>
Output:
Text Shadow:
The text-shadow property is used to add a shadow effect to text. It allows you to specify the
color, offset, blur-radius, and spread-radius of the shadow. Following is the syntax of text-
shadow:
<html>
<head>
</head>
<body>
<h2>Text Shadow Examples</h2>
<p style="text-shadow: 2px 5px yellow;"> Simple Text shadow </p>
<p style="text-shadow: 5px 5px 2px #ff00ff;">Text shadow with blur radius</p>
<p style="text-shadow: 1px 1px 2px green, 0 0 1em yellow, 0 0 0.2em red;">Multiple
shadows</p>
<p style="text-shadow: 0px 0px 10px rgb(26, 69, 105); ">Text shadow with RGB colors</p>
</body>
</html>
Output:
CSS - Using Images:
CSS plays a good role to control image display. You can set the following
image properties using CSS.
<body>
<img style = "border:0px;" src = a1.png"; height:100px; width:150px; />
<br />
<img style = "border:3px dashed red;" src = a1.png" />
<img style = "border:1px solid red; -moz-opacity:0.4; filter:alpha(opacity=40);" src = a1.png" />
</body>
</html>
div.gallery:hover {
border: 1px solid #777;
}
div.gallery img {
width: 100%;
height: auto;
}
div.desc {
padding: 15px;
text-align: center;
}
1
*{
box-sizing: border-box;
}
.responsive {
padding: 0 6px;
float: left;
width: 24.99999%;
}
<div class="responsive">
<div class="gallery">
<a target="_blank" href="img_5terre.jpg">
<img src="im1.jpg" alt="Cinque Terre" width="600" height="400">
</a>
<div class="desc">Add a description of the image here</div>
</div>
</div>
<div class="responsive">
<div class="gallery">
2
<a target="_blank" href="img_forest.jpg">
<img src="im1.jpg" alt="Forest" width="600" height="400">
</a>
<div class="desc">Add a description of the image here</div>
</div>
</div>
<div style="padding:6px;">
<p >This example use media queries to re-arrange the images on different screen sizes: for
screens larger than 700px wide, it will show four images side by side, for screens smaller than
700px, it will show two images side by side. For screens smaller than 500px, the images will
stack vertically (100%).</p>
<p>You will learn more about media queries and responsive web design later in our CSS </p>
</div>
</body>
</html>
Output:
CSS – Links:
<style type = "text/css">
a:link {color: #000000}
a:visited {color: #006600}
a:hover {color: #FFCC00}
a:active {color: #FF00CC}
</style>
3
CSS – Tables:
1.
<html>
<head>
<style>
table {
font-family: Arial, Helvetica, sans-serif;
border-collapse: collapse; width: 100%;
}
td, th {
border: 1px solid #ddd; padding: 8px;
}
tr:nth-child(even){background-color: #f2f2f2;}
tr:hover {background-color: #ddd;}
th {
padding-top: 12px; padding-bottom: 12px;
text-align: left; background-color: #40a944; color: white;
}
</style>
</head>
<body>
<h1>CSS Styled Table</h1>
<table>
<tr>
<th>Name</th>
<th>Address</th>
<th>Country</th>
</tr>
<tr>
<td>Nuha Ali</td>
<td>My Home Bhooja</td>
<td>India</td>
</tr>
<tr>
<td>Zara Ali</td>
<td>Students Roosters</td>
<td>England</td>
</tr>
<tr>
<td>Mahira</td>
<td>Orland Park</td>
<td>Chicago</td>
</tr>
</table>
</body>
</html>
4
2. Collapse Table Border using CSS
The property border-collapse ensures that borders between table cells collapse
into a single border, creating a cleaner look. Property border-collapse can have
values collapse
<html>
<head>
<style>
table {
border-collapse: collapse;
border: 3px solid;
width: 100%; padding:5px;
}
th, td {
border: 1px solid black;
}
</style>
<body>
<table>
<tr>
<th>Name</th>
<th>Address</th>
<th>Country</th>
</tr>
<tr>
<td>Nuha Ali</td>
<td>My Home Bhooja</td>
<td>India</td>
</tr>
<tr>
<td>Zara Ali</td>
<td>Students Roosters</td>
<td>England</td>
</tr>
<tr>
<td>Mahira</td>
<td>Orland Park</td>
<td>Chicago</td>
</tr>
</table>
</body>
</html>
5
3. Setting Table Border Spacing
The border-spacing property specifies the distance that separates adjacent
cells' borders in a table. This property may be specified as either one or two
values.
6
Lists in CSS:
Lists are useful as they present the information in a structured and organized
manner. Lists improve the readability and comprehension of content on a web
page. Lists are commonly used to display items, steps, options, or any other type of
related information that should be presented sequentially or in a group
<html>
<head>
<!—for listing in image form ->
<style>
ul {
list-style-image: url('/images/icon-bullet.png');
}
</style>
<style>
ul.a {
list-style-position: outside;
}
ul.b {
list-style-position: inside;
}
</style>
</head>
<body>
<h2>CSS Lists - Image Marker</h2>
<ul>
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
</body>
</html>
7
Lists - Setting Colors
<html>
<head>
<style>
ol {
list-style: upper-latin; background: Aquamarine; padding:20px;
}
ol li {
background: silver; padding:10px; font-size:20px; margin:10px;
}
ul {
list-style: square inside; background: teal; padding:20px;
}
ul li {
background: olive; color:white; padding:10px; font-size:20px; margin:10px;
}
</style>
</head>
<body>
<h2>CSS Lists - Setting Colors</h2>
<ol>
<li>Java.</li>
<li>HTML.</li>
<li>CSS.</li>
<li>React.</li>
</ol>
<ul>
<li>Java.</li>
<li>HTML.</li>
<li>CSS.</li>
<li>React.</li>
</ul>
</body>
</html>
8
CSS Box
It is a design and layout. The CSS box model is essentially a box that wraps around every HTML
element. It consists of: margins, borders, padding, and the actual content.
Content - The content of the box, where text and images appear
Padding - Clears an area around the content. The padding is transparent
Border - A border that goes around the padding and content
Margin - Clears an area outside the border. The margin is transparent
The box model allows us to add a border around elements, and to define space between
elements.
Example:
<html>
<head>
<style>
div {
background-color: lightgrey;
width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px;
}
</style>
</head>
<body>
9
<html>
<head>
<style>
.button {
background-color: #4CAF50;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
</style>
</head>
<body>
<h2>CSS Buttons</h2>
<button>Default Button</button>
<a href="#" class="button">Link Button</a>
<button class="button">Button</button>
<input type="button" class="button" value="Input Button">
</body>
</html>
Rounded Buttons
Use the border-radius property to add rounded corners to a button:
<html>
10
<head>
<style>
.button {
background-color: #4CAF50; /* Green */
border: none;
color: white;
padding: 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
<h2>Rounded Buttons</h2>
</body>
</html>
Hoverable Buttons
<html>
<head>
11
<style>
.button {
background-color: #4CAF50; /* Green */
border: none;
color: white;
padding: 16px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
transition-duration: 0.3s;
cursor: pointer;
}
.button1 {
background-color: white;
color: black;
border: 2px solid #4CAF50;
}
.button1:hover {
background-color: #4CAF50;
color: white;
}
.button2 {
background-color: white;
color: black;
border: 2px solid #008CBA;
}
.button2:hover {
background-color: #008CBA;
color: white;
}
.button3 {
background-color: white;
color: black;
border: 2px solid #f44336;
}
.button3:hover {
background-color: #f44336;
color: white;
}
.button4 {
background-color: white;
12
color: black;
border: 2px solid #e7e7e7;
}
.button5 {
background-color: white;
color: black;
border: 2px solid #555555;
}
.button5:hover {
background-color: #555555;
color: white;
}
</style>
</head>
<body>
<h2>Hoverable Buttons</h2>
<p>Use the :hover selector to change the style of the button when you move the mouse
over it.</p>
<p><strong>Tip:</strong> Use the transition-duration property to determine the speed
of the "hover" effect:</p>
</body>
</html>
<html>
<head>
<style>
.btn-group .button {
background-color: #4CAF50; /* Green */
border: 1px solid green;
13
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
cursor: pointer;
float: left;
}
.btn-group .button:not(:last-child) {
border-right: none; /* Prevent double borders */
}
.btn-group .button:hover {
background-color: #3e8e41;
}
</style>
</head>
<body>
<div class="btn-group">
<button class="button">Button</button>
<button class="button">Button</button>
<button class="button">Button</button>
<button class="button">Button</button>
</div>
</body></html>
14
cursor: pointer;
width: 150px;
display: block;
}
.btn-group .button:not(:last-child) {
border-bottom: none; /* Prevent double borders */
}
.btn-group .button:hover {
background-color: #3e8e41;
}
</style>
</head>
<body>
<div class="btn-group">
<button class="button">Button</button>
<button class="button">Button</button>
<button class="button">Button</button>
<button class="button">Button</button>
</div>
</body>
</html>
Shadow Buttons
Use the box-shadow property to add shadows to a button:
<html>
<head>
<style>
.button {
background-color: #4CAF50; /* Green */
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
15
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
-webkit-transition-duration: 0.4s; /* Safari */
transition-duration: 0.4s;
}
.button1 {
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
}
.button2:hover {
box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24),0 17px 50px 0 rgba(0,0,0,0.19);
}
</style>
</head>
<body>
<h2>Shadow Buttons</h2>
</body>
</html>
16
}
.button:active {
background-color: #ffadc3;
box-shadow: 0 5px #666;
transform: translateY(4px);
}
</style>
</head>
<body>
</body>
</html>
Image Reflection
The box-reflect property is used to create an image reflection.
The value of the box-reflect property can be: below, above, left , or right.
<html>
<head>
<style>
img {
-webkit-box-reflect: right;
}
</style>
</head>
17
<body>
</body>
</html>
.example {
padding: 20px;
color: white;
}
body {
18
background-color: lightpink;
}
@media screen and (max-width: 420px) {
body {
background-color: lightblue;
}
}
/* Extra large devices (large laptops and desktops, 1200px and up) */
@media only screen and (min-width: 1200px) {
.example {background: pink;}
}
</style>
</head>
<body>
<p class="example"> If screen size is less than 420px, then it will show lightblue
color, or else it will show light pink color </p>
</body>
</html>
<html>
<head>
<style>
body {
color:green;
}
</style>
</head>
<body>
19
<div class = "container">
<p>
Java is a high-level programming language originally developed by Sun
Microsystems and released in 1995. Java runs on a variety of platforms,
such as Windows, Mac OS, and the various versions of UNIX. This tutorial
gives a complete understanding of Java.
</p>
</div>
</div>
</body>
</html>
20
CSS Forms
<html>
<head>
<style>
input[type=text1] {
width: 100%;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 16px;
background-color: white;
background-image: url('searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
padding: 12px 20px 12px 40px;
}
21
input[type=text] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
border: 1px solid red;
border-radius: 8px;
display: inline-block;
// background-color: #3CBC8D;
}
input[type=submit] {
width: 100%;
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type=submit]:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<form>
<label for="fname">First Name</label>
<input type="text1" name="search" placeholder="Search..">
<input type="text" id="fname" name="fname">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lname">
<input type="submit" value="Submit">
</form>
</body>
</html>
22