Css (BScCSIT 5th Semester)
Css (BScCSIT 5th Semester)
h1{
color:red;
text-align: center;
}
p{
color: blue;
}
Inserting CSS
We can use style sheets in three different ways in out HTML document. There
are external style sheet, internal style sheet and inline style.
1. 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.
Example:
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
</head>
External CSS
An external style sheet can be written in any text editor. The file should not
contain any html tags. Your style sheet should be saved with a .css extension.
Example:
hr {
color:sienna;
}
p
{
margin-left:20px;
} /*Note: Do not leave space between property value and units*/
body {
background-image:url("images/back40.gif");
}
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.
Example:
<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
<h1 style="color:blue;text-align:center;">This is a
heading</h1>
<p style="color:red;">This is a paragraph.</p>
</body>
Cascading Order
What style will be used when there is more than one style
specified for an HTML element?
All the styles in a page will "cascade" into a new "virtual" style
sheet by the following rules, where number one has the highest
priority:
1. Inline style (inside an HTML element)
2. internal style sheets (in the head section)
3. External style sheet
4. Browser default
So, an inline style has the highest priority, and will override
external and internal styles and browser defaults.
CSS Selectors
CSS selectors are used to "find" (or select) the HTML elements you
want to style.
Example:
The CSS rule below will be applied to the HTML element with id="para1":
<html>
<head>
<style>
#para1 {
text-align: center;
color: red;
}
</style>
</head>
<body>
</body>
</html>
Example
In this example only <p> elements with class="center" will be center-aligned:
<html>
<head>
<style>
p.center {
text-align: center;
color: red;
}
</style>
</head>
<body>
</body>
</html>
CSS Colors
Colors are specified using predefined color names, or RGB, HEX, HSL,
RGBA, HSLA values.
CSS Backgrounds
The CSS background properties are used to define the
background effects for elements.
CSS background properties:
1. background-color
2. background-image
3. background-repeat
4. background-attachment
5. background-position
1.CSS background-color
The background-color property specifies the background
color of an element.
body {
background-color: lightblue;
}
2. CSS background-image
The background-image property specifies an image to use as the
background of an element.
By default, the image is repeated so it covers the entire element.
body {
background-image: url("paper.gif");
}
CSS background-repeat
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
}
CSS background-position
The background-position property is used to specify the
position of the background image.
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
}
CSS background-attachment
The background-attachment property specifies whether the background
image should scroll or be fixed (will not scroll with the rest of the page):
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
background-attachment: fixed;
}
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
background-attachment: scroll;
}
CSS background - Shorthand property
p {
border: 5px solid red;
}
CSS Text
Text Color
The color property is used to set the color of the text. The color is specified by:
a color name - like "red“
a HEX value - like "#ff0000"
an RGB value - like "rgb(255,0,0)"
Text Alignment
The text-align property is used to set the horizontal alignment of a text.
A text can be left or right aligned, centered, or justified.
When the text-align property is set to "justify", each line is stretched so that
every line has equal width, and the left and right margins are straight (like
in magazines and newspapers):
Text Decoration
The text-decoration property is used to set or remove
decorations from text.
The value text-decoration: none; is often used to remove
underlines from links:
<style>
a{
text-decoration: none;
}
</style>
p.sansserif {
font-family: Arial, Helvetica, sans-serif;
}
Font Style
The font-style property is mostly used to specify italic
text.
This property has three values:
normal - The text is shown normally
italic - The text is shown in italics
oblique - The text is "leaning" (oblique is very similar to
italic, but less supported)
Font Size
The font-size property sets the size of the text.
h1 {
font-size: 40px;
}
h2 {
font-size: 30px;
}
p {
font-size: 14px;
}
CSS Lists
Different List Item Markers
The list-style-type property specifies the type of list item marker.
ul.a {
list-style-type: circle;
}
ul.b {
list-style-type: square;
}
ol.c {
list-style-type: upper-roman;
}
ol.d {
list-style-type: lower-alpha;
}
CSS Tables
Table Borders
To specify table borders in CSS, use the border property.
The example below specifies a black border for <table>, <th>,
and <td> elements:
<style>
table, th, td {
border: 1px solid black;
}
CSS Tables
Collapse Table Borders
The border-collapse property sets whether the table borders
should be collapsed into a single border:
table {
border-collapse: collapse;
}
th {
height: 50px;
}
Horizontal Alignment
The text-align property sets the horizontal alignment (like
left, right, or center) of the content in <th> or <td>.
By default, the content of <th> elements are center-
aligned and the content of <td> elements are left-aligned.
The following example left-aligns the text in <th>
elements:
th {
text-align: left;
}
Table Padding
th, td {
padding: 15px;
text-align: left;
}
Hoverable Table
Use the :hover selector on <tr> to highlight table rows on
mouse over:
tr:hover {background-color: #f5f5f5;}
Table Color
The example below specifies the background color and text
color of <th> elements:
th {
background-color: #4CAF50;
color: white;
}
The CSS Box Model
All HTML elements can be considered as boxes. In CSS, the
term "box model" is used when talking about design and
layout.
h1{
display:none;
}
CSS Padding
div.absolute {
position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
border: 3px solid #73AD21;
}
position: sticky;
#example2 {
box-shadow: 5px 10px #888888;
}
CSS text-shadow Property
What is Bootstrap?
Bootstrap is a free front-end framework for faster and
easier web development
Bootstrap includes HTML and CSS based design
templates for typography, forms, buttons, tables,
navigation, modals, image carousels and many other, as
well as optional JavaScript plugins
Bootstrap also gives you the ability to easily create
responsive designs
Why Use Bootstrap?
Advantages of Bootstrap:
Easy to use: Anybody with just basic knowledge of
HTML and CSS can start using Bootstrap
Responsive features: Bootstrap's responsive CSS adjusts
to phones, tablets, and desktops
Mobile-first approach: In Bootstrap 3, mobile-first styles
are part of the core framework
Browser compatibility: Bootstrap is compatible with all
modern browsers (Chrome, Firefox, Internet Explorer,
Edge, Safari, and Opera)