1 CSS-Starting Upto
1 CSS-Starting Upto
What is CSS?
CSS stands for Cascading Style Sheets. CSS is the standard language for
defining styles on web pages.
Styles are set using CSS Properties. For example, you can set font properties
(size, colors, style etc), background images, border styles, and much more.
HTML vs CSS
It's not really "HTML vs CSS". In fact HTML and CSS are the best of friends! CSS is
a supplementary extension to HTML. Here's what each does:
HTML
The purpose of HTML is to provide document structure and meaning. You use
HTML to specify what elements go on the page (eg, headings, paragraphs,
tables, images, etc). Each of these elements (as well as their attributes and
attribute values) has a certain meaning.
CSS
We use CSS to specify how those HTML elements look. But not just how
they look, how they are presented. So, you can write an HTML document
without being concerned with its presentation, and then use CSS to specify
how it will be presented within any given context.
Styling is an essential property for any website. It increases the standards and
overall look of the website that makes it easier for the user to interact with it
CSS Code Format: It is the basic structure of the CSS style of a webpage.
body {
background-color: lightgray;
h1 {
color: green;
text-align: center;
p {
font-family: sans-serif;
font-size: 16px;
Types of CSS: There are three types of CSS which are given below.
Inline: Inline CSS contains the CSS property in the body section attached with the
Internal or Embedded: The CSS ruleset should be within the HTML file in the head
External: External CSS contains a separate CSS file that contains only style property
The Major key feature of CSS includes styling rules which are interpreted by the client
Using CSS, you can control the color text, the style of fonts, spacing between elements,
how columns are sized, variation in display for different devices and screen size as well
Time-saving: You can easily use any single CSS script at multiple places.
Cache storing: CSS can store web applications locally with the help of offline cache so
you can see the web site when you are offline.
Native front-end: CSS contains a huge list of attributes and function that is helpful to
Selectors: In CSS, there are lots of selectors (ID selectors, Class Selectors, etc.) that will
Advantages of CSS:
CSS make the website faster and enhances search engine capabilities to crawl the
web pages
Disadvantages of CSS:
In CSS, there is a cross browsers issue if you design anything and check on chrome it
looks perfect but that does not mean it will look the same in the other browsers. Then
The CSS syntax consists of a set of rules. These rules have 3 parts: a selector, a property, and
a value.
Syntax
This code tells the browser to render all occurrences of the HTML <h1> element in blue.
Grouping Selectors
You can apply a style to many selectors if you like. Just separate the selectors with a comma.
To apply more than one property separates each declaration with a semi-colon.
h1 { color:blue; font-family: arial, helvetica, "sans serif" }
Readability
You can make your CSS code more readable by spreading your style declarations across
multiple lines. You can also indent your code if you like. This doesn't affect how your code is
rendered - it just makes it easier for you to read.
h1 {
color: blue;
font-family: arial, helvetica, "sans serif";
font-size: 150%;
}
CSS Properties
Class: CSS classes give you extra flexibility for specifying which elements are to receive a
particular style.
CSS ID Syntax
The syntax for declaring a CSS ID is the same as for classes, except that instead of using a
dot, you use a hash (#).
IDs vs Classes
Given classes and IDs are very similar, you may be wondering which one to use. This depends
on the situation.
Example: live
You should use classes when your style needs to be applied multiple times on
the same page. For example, you might have many <h1> elements that need
the same style applied.
You should use IDs if only one element on the page should have the style
applied, and/or you need a unique identifier for that element.
Another useful thing about IDs is that you can use the Document Object
Model (DOM) to refer to them. This enables you to use JavaScript/DHTML
techniques to build a much more interactive web site
Font-Family
CSS font properties enable you to change the look of your text. For example,
you can assign a font family, apply bold or italic formatting, change the size
and more.
Example:
So, if the user doesn't have your first choice font, the second choice will be
used for that user. If they don't have the second choice, the third choice will be
used, and so on.
It's good practice to make your last choice a generic font family such
as serif or sans-serif
Font Size
The CSS font-size property is used for specifying the size of a font.
You can specify the font size as an absolute size, relative size, length, or
percentage. See below.
Also see the font-size-adjust property to ensure that any fallback fonts are
rendered at an appropriate size.
Syntax
❖ xx-small
❖ x-small
❖ small
❖ medium
❖ large
❖ x-large
font-size: medium|large|x-large|xx-large|xx-small|x-small|small
Relative-size: It is used to set the size of the text relative to its neighboring
elements. With relative-size, it is possible to change the size of the text in
browsers
❖ larger
❖ smaller
Font-size with pixels: When we set the size of text with pixels, then it
provides us the full control over the size of the text.
Font-size with em: It is used to resize the text. Most of the developers
prefer em instead of pixels. It is recommended by the World Wide Web
consortium (W3C). As stated above, the default text size in browsers is 16px.
So, we can say that the default size of 1em is 16px.
Example: font-size-px-em
Length: Specifies an absolute font size using a valid length value (for
example, 18px, 1.5em, 14pt, etc). This value is independent of the user agent's
font table. Negative lengths are illegal.
NOTE: If we do not define a font-size, then for the normal text such as
paragraphs, the default size is 16px, which is equal to 1em
Text Color: The color property is used to set the color of the text. The color is
specified by
Text Align Last: The text-align-last property specifies how to align the last
line of a text.
Text Indent: The text-indent property specifies the indentation of the first
line in a text-block.
Example
Text Transform
Syntax
full-width: Puts all characters in fullwidth form. If the character does not have
The CSS height and width properties are used to set the height and width of an
element. The height and width properties do not include padding, borders, or
margins. It sets the height and width of the area inside the padding, border, and
margin of the element.
17-05-2023
4. Background
The <color> data type allows for many different ways of specifying a color. For
example, you can use the hex value, rgba() functional notation, a named color, a
keyword such as transparent, or any of the other choices available.
Example: Clickhere
Syntax
Syntax
Scroll: The "scroll" value is set, the background-image will scroll with the page.
This is the default value.
Fixed: The "fixed" value is applied, the background-image will remain fixed to the
viewport. Even if an element has a scrolling mechanism, the background doesn't
move with the page.
Local: The local value is set; the background-image will scroll with the element's
contents.
Example: ClickHere
Syntax
Auto: This is the default value, which displays the background image in its
original size.
Length: It is used to set the width and height of the background image. This
value stretches the image in the corresponding dimension of the given
length. Its single value specifies the width of the image, and the height sets to
auto. If two values are given, the first value sets the width, and the second
value sets the height. It does not allow negative values
Percentage: This value defines the width and height of the background image
to the percentage (%) of the background positioning area. Negative values
are not allowed.
Cover: This value is used to resize the background image to cover the entire
container. Sometimes, it crops the little bit off one of the edges or stretches
the image. It resizes the image to ensure the element is completely covered.
CSS allows you to set styles for the border of any HTML element. It also
provides you with a way of setting border styles for one or more sides of an
element.
Borders on all Sides: To set border styles for all sides of an element, you
use the border-width, border-style, and border-color properties.
border-bottom-color
border-bottom-style
border-bottom-width
border-left-color
border-left-style
border-left-width
border-right-color
border-right-style
border-right-width
border-top-color
border-top-style
border-top-width
Each Side Boarder Shorthand: The following properties provide you with
a more concise way of specifying border properties for each side.
border-bottom
border-left
border-right
border-top
Example: Click Here
Border Radius: You can give your borders rounded corners by using
the border-radius property.
Margins define the space around the element. CSS margins are specified in a
similar way to borders - they can be set individually for each side, or all sides
at once.
If you don't want the margin settings to be applied to all four sides, or if you
want each side to have different margins applied, you can use the following
properties
❖ margin-top
❖ margin-right
❖ margin-bottom
❖ margin-left
Variations
You don't need to provide different values for all four sides. You can provide
one, two, three, or four values.
If there is only one value, it applies to all sides. If there are two values, the top
and bottom margins are set to the first value and the right and left margins
are set to the second. If there are three values, the top is set to the first value,
the left and right are set to the second, and the bottom is set to the third. If
there are four values, they apply to the top, right, bottom, and left,
respectively.
margin:10px;
margin:10px 20px;
• Top is 10px
• Left and right are 20px
• Bottom is 30px
• Top is 10px
• Right is 20px
• Bottom is 30px
• Left is 40px
7. Padding Properties
Padding defines the space between the element's border and its content. CSS
padding is specified just like margins — it can be set individually for each side,
or all sides at once.
This example uses the padding shorthand property to set the same padding on
all sides of an element.
If you don't want the same padding settings to be applied to all four sides, or
if you want each side to have different padding, you can use the following
properties:
❖ padding-top
❖ padding-right
❖ padding-bottom
❖ padding-left
Shorthand Property
Again, as with margin, you don't need to provide different values for all four
sides. You can provide one, two, three, or four values.
If there is only one value, it applies to all sides. If there are two values, the top
and bottom paddings are set to the first value and the right and left paddings
are set to the second. If there are three values, the top is set to the first value,
the left and right are set to the second, and the bottom is set to the third. If
there are four values, they apply to the top, right, bottom, and left,
respectively.
padding:10px;
padding:10px 20px;
• Top is 10px
• Left and right are 20px
• Bottom is 30px
• Top is 10px
• Right is 20px
• Bottom is 30px
• Left is 40px
8. CSS Lists
HTML lists can have a number of different styles applied using CSS. Here are
some of the commonly used ones.
List Style Type: The list-style-type property specifies the type of list item
marker.
The list-style-image property allows you to create your own custom bullets to
use in lists.
Syntax
9. CSS Positioning
Example
Example
Elements are floated only horizontally. So it is possible only to float elements left
or right, not up or down
Then we can add overflow: auto; to the containing element to fix this
problem:
12. Layers
CSS gives you opportunity to create layers of various divisions. The CSS layers
refer to applying the z-index property to elements that overlap with each
other.
The z-index property is used along with the position property to create an
effect of layers. You can specify which element should come on top and
which element should come at bottom.
A z-index property can help you to create more complex webpage layouts
Layers with Opacity: You can use the opacity property to provide some
transparency between the layers.
13. FLEXBOX
Flexbox (flexible box) is a layout mode of CSS3. Using this mode, you can easily
create layouts for complex applications and web pages. Flexbox layout gives
complete control over the direction, alignment, order, size of the boxes.
Cascading Style Sheets (CSS) is a simple design language intended to simplify the
process of making web pages presentable. CSS handles the look and feel part of a
web page.
Using CSS, you can control the color of the text, the style of fonts, the spacing
between paragraphs, how columns are sized and laid out, what background images
or colors are used, layout designs, variations in display for different devices and
screen sizes as well as a variety of other effects.
To determine the position and dimensions of the boxes, in CSS, you can use one of
the layout modes available –
What is Flexbox?
In addition to the above-mentioned modes, CSS3 provides another layout mode
Flexible Box, commonly called as Flexbox.
Using this mode, you can easily create layouts for complex applications and web
pages. Unlike floats, Flexbox layout gives complete control over the direction,
alignment, order, size of the boxes.
Features of Flexbox
Following are the notable features of Flexbox layout –
❖ Direction − You can arrange the items on a web page in any direction such as
left to right, right to left, top to bottom, and bottom to top.
❖ Order − Using Flexbox, you can rearrange the order of the contents of a web
page.
❖ Wrap − In case of inconsistent space for the contents of a web page (in single
line), you can wrap them to multiple lines (both horizontally) and vertically.
❖ Alignment − Using Flexbox, you can align the contents of the webpage with
respect to their container.
❖ Resize − Using Flexbox, you can increase or decrease the size of the items in
the page to fit in available space.
Container
A Flexible Layout must have a parent element with the display property set to flex.
Basically, the flexbox properties define how flex items are sized, how they wrap, and
which direction they go in. The alignment properties define how the flex items are
aligned, both horizontally and vertically, within their container.
To use Flexbox in your application, you need to create/define a flex container using
the display property.
Syntax: display: flex | inline-flex
FLEX
Now, we will see how to use the display property with examples.
On passing this value to the display property, a block level flex container will be
created. It occupies the full width of the parent container (browser).
INLINE-FLEX
On passing this value to the display property, an inline level flex container will be
created. It just takes the place required for the content. Note:
Flex-Direction
The flex-direction property is used to specify the direction in which the elements of
flex container (flex-items) are needed to be placed.
The flex-direction property is used to specify the direction in which the elements of
usage −
❖ row − Arranges the elements of the container horizontally from left to right.
❖ row-reverse − Arranges the elements of the container horizontally from right
to left.
❖ column − Arranges the elements of the container vertically from left to right.
❖ column-reverse − Arranges the elements of the container vertically from right
to left.
❖
Now, we will take a few examples to demonstrate the use of the direction property.
ROW: On passing this value to the direction property, the elements of the container
are arranged horizontally from left to right.
Row-reverse: On passing this value to the direction property, the elements of the
container are arranged horizontally from right to left as shown below.
.container{
Display:inline-flex;
border:3px solid black;
flex-direction:row-reverse;
Flex-Wrap
Generally, in case of insufficient space for the container, the rest of the flex items will
be hidden as shown below.
The flex-wrap property is used to specify the controls whether the flex-container is
single-line or multi-line.
usage −
• wrap − In case of insufficient space for them, the elements of the container
(flexitems) will wrap into additional flex lines from top to bottom.
• wrap-reverse − In case of insufficient space for them, the elements of the
container (flex-items) will wrap into additional flex lines from bottom to top.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flex-Wrap</title>
<!---->
<style>
.parent{
display: flex;
flex-direction: row;
height: 500px;
width: 700px;
border:2px solid gray;
/*flex-wrap: wrap;*/
/*flex-wrap:wrap-reverse;/*
/*flex-direction: column;*/
}
.child{ width:100px; height: 100px; }
#c1{ background-color: crimson; }
#c2{ background-color:aqua; }
#c3{ background-color:chartreuse; }
</style>
</head>
<body>
<div class="parent">
<div id="c1" class="child">1</div>
<div id="c2" class="child">2</div>
<div id="c3" class="child">3</div>
<div id="c1" class="child">4</div>
<div id="c2" class="child">5</div>
<div id="c3" class="child">6</div>
<div id="c1" class="child">7</div>
<div id="c2" class="child">8</div>
<div id="c3" class="child">9</div>
</div>
</body>
</html>
On passing the value wrap to the property flex-wrap and the value column to the
property flex-direction, the elements of the container are arranged horizontally from
left to right
wrap (column)
On passing the value wrap to the property flex-wrap and the value column to the
property flex-direction, the elements of the container are arranged horizontally from
left to right
wrap-reverse (column)
Justifying Contents
Using the property justify-content, you can align the contents along the main axis
by distributing the extra space as intended. You can also adjust the alignment of the
flexitems, in case they overflow the line.
space-evenly;
flex-start
On passing this value to the property justify-content, the flex-items are
placed at the start of the container.
flex-end
On passing this value to the property justify-content, the flex-items are
placed at the end of the container.
On passing this value to the property justify-content, the flex-items are placed at
the center of the container, where the extra space is equally distributed at the start
and at the end of the flex-items.
space-between
On passing this value to the property justify-content, the extra space is
equally distributed between the flex items such that the space between any
two flex-items is the same and the start and end of the flex-items touch the
edges of the container.
space-around
On passing this value to the property justify-content, the extra space is equally
distributed between the flex-items such that the space between any two flex-items
is the same. However, the space between the edges of the container and its contents
(the start and end of the flex-items) is half as the space between the flex items.
space-evenly
On passing this value to the property justify-content, the extra space is
equally distributed between the flex-items such that the space between any
two flex-items is the same (including the space to the edges).
Usage –
• flex-start − The flex items were aligned vertically at the top of the
container.
• flex-end − The flex items were aligned vertically at the bottom of the
container.
• flex-center − The flex items were aligned vertically at the center of the
container.
• stretch − The flex items were aligned vertically such that they fill up the
whole vertical space of the container.
• baseline − The flex items were aligned such that the baseline of their
text align along a horizontal line.
flex-start: On passing this value to the property align-items, the flex items were
aligned vertically at the top of the container .
flex-end: On passing this value to the property align-items, the flex-items are
aligned vertically at the bottom of the container.
Center: On passing this value to the property align-items, the flex-items are aligned
vertically at the center of the container.
Stretch: On passing this value to the property align-items, the flex-items are aligned
vertically such that they fill up the whole vertical space of the container.
Baseline: On passing this value to the property align-items, the flex-items are
aligned such that the baseline of their text aligns along a horizontal line.
In case the flex-container has multiple lines (when, flex-wrap: wrap), the align-
content property defines the alignment of each line within the container.
Usage –
align-content: flex-start | flex-end | center | space-between | space-around |
stretch;
This property accepts the following values −
• stretch − The lines in the content will stretch to fill up the remaining space.
• flex-start − All the lines in the content are packed at the start of the container.
• flex-end − All the lines in the content are packed at the end of the container.
• center − All the lines in the content are packed at the center of the container.
• space-around − The extra space is distributed between the lines evenly with
equal space around each line (including the first and last lines)
stretch: On passing this value to the property align-content, the lines will stretch to
fill up the remaining space.
flex-start: On passing this value to the property align-content, all the lines are
packed at the start of the container.
flex-end: On passing this value to the property align-content, all the lines are
packed at the end of the container.
Center: On passing this value to the property align-content, all the lines are packed
space-around: On passing this value to the property align-content, the extra space
is distributed between the lines evenly with equal space around each line (including
space is distributed between the lines evenly, where the first line will be at the top
The following example demonstrates the order property. Here we are creating six
colored boxes with the labels one, two, three, four, five, six, arranged in the same
order, and we are reordering them in the order one, two, five, six, three, four, using the
flex-order property.
flex-basis
We use the flex-basis property to define the default size of the flex-item before the
space is distributed.
A link has four different states — link, visited, active and hover. These four states of a link
can be styled differently through using the following anchor pseudo-class selectors.
You can specify any CSS property you'd like e.g. color, font, background, border, etc.
to each of these selector to customize the style of links, just like you do with the
normal text.
a:link { /* unvisited link */
color: #ff0000;
text-decoration: none;
border-bottom: 1px solid;
}
Note: In general, the order of the pseudo classes should be the following —
:link, :visited, :hover, :active, :focus in order for these to work properly.
The following example shows how to remove or set underline for different states of a
link.
a:link, a:visited {
text-decoration: none;
}
a:hover, a:active {
text-decoration: underline;
}
Making Text Links Look Like Buttons - Example
You can also make your ordinary text links look like button using CSS. To do this
we need to utilize few more CSS properties such as background-
color, border, display, padding, etc.
a:link, a:visited {
color: white;
background-color: #1ebba3;
display: inline-block;
padding: 10px 20px;
border: 2px solid #099983;
text-decoration: none;
text-align: center;
font: 14px Arial, sans-serif;
}
a:hover, a:active {
background-color: #9c6ae1;
border-color: #7443b6;
}
One common use for display: inline-block is to display list items horizontally instead
of vertically.
CSS Tables
When you create an HTML Table without any styles or attributes, browsers display
them without any border. With CSS you can greatly improve the appearance your
tables.
CSS provides several properties that allow you to control the layout and presentation
of the table elements.
Now, we will set a black border for the <table>, <th>, and <td> elements.
By default, browser draws a border around the table, as well as around all the cells,
with some space in-between, which results in double border. To get rid of this double
border problem you can simply collapse the adjoining table cell borders and create
clean single line borders.
There are two distinct models for setting borders on table cells in
CSS: separate and collapse.
In the separate border model, which is the default, each table cell has its own distinct
borders, whereas in the collapsed border model, adjacent table cells share a
common border. You can set the border model for an HTML table by using the
CSS border-collapse property.
table {
border-collapse: collapse;
}
th, td {
border: 1px solid black;
}
By default, a table will render just wide and tall enough to contain all of its contents.
However, you can also set the width and height of the table as well as its cells
explicitly using the width and height CSS property.
table {
width: 100%;
}
th {
height: 40px;
}
You can do this with the help of CSS table-layout property. This property defines the
algorithm to be used to layout the table cells, rows, and columns. This property takes
one of two values:
auto — Uses an automatic table layout algorithm. With this algorithm, the widths of
the table and its cells are adjusted to fit the content. This is the default value.
fixed — Uses the fixed table layout algorithm. With this algorithm, the horizontal
layout of the table does not depend on the contents of the cells; it only depends on
the table's width, the width of the columns, and borders or cell spacing. It is normally
faster than auto.
table {
width: 300px;
table-layout: fixed;
}
Handling Empty Cells: In tables that uses separate border model, which is
default, you can also control the rendering of the cells that have no visible content
using the empty-cells CSS property.
This property accepts a value of either show or hide. The default value is show, which
renders empty cells like normal cells, but if the value hide is specified no borders or
backgrounds are drawn around the empty cells.