Css
Css
CSS Uses
• Solves a big problem
• Saves a lot of time
• Provide more attributes
• Pages load faster
• Easier Website maintenance
• Multiple device compatibility
CSS Syntax
Selector {Declaration; Declaration; …}
Universal Selector
Group Selector
• The id selector selects the id attribute of an HTML element to select a specific element.
#para1 {
text-align: center;
Applies to element having id attribute value
color: blue;
} as para1 in HTML document.
• The class selector selects HTML elements with a specific class attribute.
.center {
text-align: center; Applies to all the elements having class
color: blue; attribute value as center in HTML document.
}
• The universal selector is used as a wildcard character. It selects all the elements on
the pages.
* {
color: green;
font-size: 20px; Applies to all the elements in HTML document.
}
• The group selector is used to select group of elements on which same style is to be
applied.
h1,h2,p {
text-align: center;
color: blue; Applies to only listed elements in HTML document.
}
CSS Colors
color: rgb(R, G, B); Red(R), Green(G) and Blue(B) values that can either be in
percentage or integer in the range 0 to 255
color: rgba(R, G, B, A); Same as RGB. Alpha(A) specifies elements transparency.
Value of A ranges from 0.0 to 1.0.
0.0 fully transparent & 1.0 opaque.
• The CSS background properties are used to define the background effects for elements.
background-color:color-name;
background-image: url("url");
background-repeat: repeat-x/repeat-y/repeat/no-repeat;
background-position: left/right/top/bottom/center;
background-attachment: fixed;
• The CSS font property is used to set the fonts content of HTML element.
font-family: "font family name";
font-style: normal/italic;
font-weight: normal/bold/lighter/bolder;
font-variant: normal/small-caps;
text-align: left/right/center/justify;
text-decoration: underline/overline/line-through/none;
text-transform: uppercase/lowercase/capitalise;
text-indent: size;
letter-spacing: size;
line-height: size;
word-spacing: size;
CSS Borders
• The CSS border properties allow you to specify the style, width, and color of an
element's border.
• The border-style property specifies what kind of border to display.
border-style: dotted/dashed/solid/double/groove/ridge/inset/outset/none/hidden;
• The width can be set as a specific size (in px, pt, cm, em, etc) or by using one of the
three pre-defined values: thin, medium, or thick
• The border-color property is used to set the color of the four borders.
• Links can be styled differently depending on what state they are in.
• a:link - a normal, unvisited link
• a:visited - a link the user has visited
• a:hover - a link when the user moves mouse over it
• a:active - a link the moment it is clicked
• The text-decoration property is mostly used to remove underlines from links
text-decoration: underline;
text-decoration: none;
• The background-color property can be used to specify a background color for links
background-color: yellow;
CSS Lists
• The list-style-type property specifies the type of list item marker.
list-style-type: circle;
For <ul>
list-style-type: square;
list-style-type: upper-roman;
For <ol>
list-style-type: lower-alpha;
list-style-image: url('sqpurple.gif');
• To span table on entire screen, add width: 100% to the <table> element.
table {
width: 100%;
}
• To collapse table border into a single border add border-collapse: collapse to <table>
element.
table {
border-collapse: collapse;
}
• If you want a border only around the table, specify the border property for <table>
table {
border: 1px solid black;
}
• The width and height of a table are defined by the width and height properties.
table {
width: 100%;
}
th {
height: 70px;
}
• The text-align property sets the horizontal alignment (like left, right, or center) of the
content in <th> or <td>
td {
text-align: center;
}
• The vertical-align property sets the vertical alignment (like top, bottom, or middle) of
the content in <th> or <td>.
td {
height: 50px;
vertical-align: bottom;
}
• To control the space between the border and the content in a table, use
the padding property on <td> and <th> elements
th, td {
padding: 15px;
text-align: left;
}
• Add the border-bottom property to <th> and <td> for horizontal divider.
th, td {
border-bottom: 1px solid #ddd;
}
• Use the :hover selector on <tr> to highlight table rows on mouse over
tr:hover {background-color: #f5f5f5;}
• For zebra-striped tables, use the nth-child() selector and add a background-color to all
even (or odd) table rows
• CSS has properties for specifying the margin for each side of an element:
• margin-top
• margin-right
• margin-bottom
• margin-left
• Set the margin property to auto to horizontally center the element within its
container.
CSS Padding
• Padding is used to create space around an element's content, inside of any defined
borders.
• The CSS padding properties are used to generate space around an element's content,
inside of any defined borders.
• There are properties for setting the padding for each side of an element (top, right,
bottom, and left).
• CSS has properties for specifying the padding for each side of an element:
• padding-top
• padding-right
• padding-bottom
• padding-left
input {
width: 100%; Applies to all <input> elements
}
• Use the padding property to add space inside the input field.
• Use the border property to change the border size, style and color of input elements.
• By default, some browsers will add a blue outline around the input when it gets
focus (clicked on). You can remove this behavior by adding outline: none; to the
input.
• Use the :focus selector to do something with the input field when it gets focus
• If you want an icon inside the input, use the background-image property and
position it with the background-position property. Also notice that we add a large
left padding to reserve the space of the icon
input[type=text] {
background-color: white;
background-image: url('searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
padding-left: 40px;
}
• Use the CSS transition property to animate the width of the input when it gets
focus.
input[type=text] {
transition: width 0.4s ease-in-out;
}
input[type=text]:focus {
width: 100%;
}
• Use the resize property to prevent textarea from being resized (disable the
"grabber" in the bottom right corner)
Styling Select Menus Styling Input Buttons
• The CSS box model is a box that wraps around every HTML element.
div {
width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px;
}
Transparent area around the content.
The content of the box, where text A border that goes around the padding
and images appear and content
Total element width = width + left padding + right padding + left border + right border
+ left margin + right margin
div {
width: 320px;
padding: 10px;
border: 5px solid gray;
margin: 0;
}
320px (width)
+ 20px (left + right padding)
+ 10px (left + right border)
+ 0px (left + right margin)
= 350px
• Create a webpage which displays "Hello World" with font size 20 pixels, bold format,
in "Times New Roman" font and green in color using inline CSS, embedded CSS and
external CSS.
• Create a webpage which displays the class time table and apply the following effects
on the table:
• For the table header apply blue as the background color and white for the color
of the text in the table header.
• Display day names (Mon, Tue etc...) in bold format with the first letter in the day
name in uppercase.
• Display lunch slightly in bigger font other than the remaining text.
Period 1 2 3 4 5 6 7
12.30PM-
8.30AM- 9.30AM- 10.40AM- 11.30AM- 1.30PM 1.30PM- 2.30PM- 3.30PM-
Day
9.30AM 10.30AM 11.30AM 12.30AM 2.30PM 3.30PM 4.30PM
Mon AJWT ACN DAA UP IPR2 CA MS
Tue ACN MS UP DAA L AJWT LAB
Wed CA MS DAA ACN U IPR2 AJWT UP
N
Thu UP DAA AJWT CA C CN&UNIX LAB
Fri AJWT ACN CA MS H DAA ACN CRT
Sat MS AJWT CA UP CRT
• Create a webpage to manage personal details like name, class, qualifications,
photo, address etc., using tables and other suitable HTML tags. Apply the following
style information:
• Display the heading of the page in Times New Roman font and with 24px size.
• Align all the field names like Name, Class, Photo etc to right in the table.
• Apply light blue as background color for the left side cells in the table which
contains field names like Name, Class etc.
• Also display your college logo as background image in the top right position of
the web page.
CSS display property
• Every HTML element has a default display value depending on what type of element it is.
• Always starts on a new line and takes up • Doesn’t start on a new line and only
the full width available (stretches out to takes up as much width as
the left and right as far as it can) necessary.
<div> <span>
<h1> - <h6> <a>
<p> <img>
<form>
<header>
<footer>
<section>
Changing an inline element to a block element, or vice versa
li { span { a{
display: inline; display: block; display: block;
} } }
• Set the display property to none to hide an element. The element will be hidden,
and the page will be displayed as if the element is not there
• Setting visibility: hidden; also hides an element. However, the element will still take
up the same space as before.
h1.hidden { h1.hidden {
display: none; visibility: hidden;
} }
CSS overflow property
• The overflow property specifies whether to clip the content or to add scrollbars when
the content of an element is too big to fit in the specified area.
• visible - Default. The overflow is not clipped. The content renders outside the
element's box
• hidden - The overflow is clipped, and the rest of the content will be invisible
• scroll - The overflow is clipped, and a scrollbar is added to see the rest of the content
• Note that the overflow property only works for block elements with a specified
height.
CSS float property
• none - The element does not float (will be displayed just where it occurs in the text).
This is default
• The clear property specifies what should happen with the element that is next to a
floating element.
• none - The element is not pushed below left or right floated elements. This is default
• both - The element is pushed below both left and right floated elements
• inherit - The element inherits the clear value from its parent
<body> <style>
<div class="div1">div1</div> .div1 {
<div class="div2">div2</div> float: left;
<div class="div3">div3</div> padding: 10px;
<div class="div4">div4</div> border: 3px solid blue;
</body> }
.div2 {
padding: 10px;
border: 3px solid red;
}
.div3 {
float: left;
padding: 10px;
border: 3px solid blue;
}
.div4 {
padding: 10px;
border: 3px solid red;
clear: left;
}
</style>
CSS box-sizing property
• Defines how the width and height of an element are calculated: should they include
padding and borders, or not.
• content-box - The width and height properties includes only the content. Border and
padding are not included
• border-box - The width and height properties includes content, padding and border.
Style an element when a user
CSS Pseudo Classes
moves mouse over it
Used to define a special state of an element.
Style visited and unvisited links
differently
p:first-child i {
color: blue;
}
Style the first letter, or line, of an
CSS Pseudo elements
element
Used to style specified parts of an element.
Insert content before, or after, the
content of an element
selector :: pseudo-element { Used to add a special style to the first letter of a text.
property: value;
} p::first-letter {
color: #ff0000;
font-size: xx-large;
The ::first-line pseudo-element }
p::first-line {
color: #ff0000;
font-variant: small-caps;
}
The ::before pseudo-element
h1::before {
content: url(smiley.gif);
}
CSS Flexbox allows users to arrange and organize their webpage horizontally or vertically
The flex container becomes flexible by setting the display property to flex
.flex-container {
display: flex;
}
<div class="flex-container"> <style>
<div>1</div> .flex-container {
<div>2</div> display: flex;
<div>3</div> background-color: DodgerBlue;
</div> }
.flex-container div {
background-color: #f1f1f1;
margin: 10px;
padding: 20px;
font-size: 30px;
}
</style>
flex-direction property
Defines in which direction the container wants to stack the flex items.
.flex-container {
display: flex;
flex-direction: row | row-reverse | column | column-reverse;
}
The column value stacks the flex items vertically (from top to bottom)
The column-reverse value stacks the flex items vertically (but from bottom to top)
The row value stacks the flex items horizontally (from left to right)
The row-reverse value stacks the flex items horizontally (but from right to left)
flex-wrap property
The wrap value specifies that the flex items will wrap if necessary
The nowrap value specifies that the flex items will not wrap (this is default)
The wrap-reverse value specifies that the flexible items will wrap if necessary, in reverse
order
flex-flow property
Shorthand property for setting both the flex-direction and flex-wrap properties
.flex-container {
display: flex;
flex-flow: row wrap;
}
justify-content property Used to align the flex items
.flex-container {
display: flex;
justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly;
}
.flex-container {
display: flex;
align-items: stretch | flex-start | flex-end | center | baseline;
}
.flex-container {
display: flex;
align-content: flex-start | flex-end | center | space-between | space-around | space-evenly | stretch;
}
Header
Usually located at the top of the website. It often contains a logo or the website name.
nav { <nav>
overflow: hidden; <a href="#">Home</a>
background-color: #333; <a href="#">About</a>
} <a href="#">Contact</a>
</div>
nav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
nav a:hover {
background-color: #ddd;
color: black;
}
Content
often used for mobile often used for tablets only used for
browsers and laptops desktops
<div class="row"> *{
<div class="column"> box-sizing: border-box;
<h2>Column</h2> }
<p>…</p>
</div> body {
margin: 0;
<div class="column"> }
<h2>Column</h2>
<p>…</p> .column {
</div> float: left;
width: 33.33%;
<div class="column"> padding: 15px;
<h2>Column</h2> }
<p>…</p>
</div>
</div>
Footer
Placed at the bottom of your page. Often contains information like copyright and contact
info.
footer {
background-color: #F1F1F1;
text-align: center;
padding: 10px;
}