Css
Css
CSS (Cascading Style Sheets) is a style sheet language used for describing the presentation of a document
written in HTML or XML, including colors, layout, and fonts. CSS describes how elements should be rendered on the
screen, on paper, in speech, or on other media.
History of CSS:
• CSS1 (1996): The first version, CSS1, was introduced to separate style from structure in web documents.
• CSS2 (1998): CSS2 brought new features and capabilities, including absolute, relative, and fixed positioning,
z-index, media types, and more.
• CSS2.1 (2011): An updated and refined version, CSS2.1, addressed inconsistencies and errors in the earlier
specifications.
• CSS3 (2001-present): Rather than a single monolithic specification, CSS3 is a collection of individual modules,
each adding new features. It includes modules like Flexbox, Grid, Transitions, Animations, and more.
1. Basic Concepts:
• Purpose of CSS: CSS, or Cascading Style Sheets, is a stylesheet language used to control the presentation and
layout of web documents written in HTML. It separates the structure (HTML) from the presentation (CSS) of a
web page.
• Syntax: CSS uses a syntax that consists of selectors and declaration blocks. A selector targets HTML elements,
and the declaration block contains the properties and their values.
• Cascading: CSS rules can cascade, meaning that styles from multiple sources can apply to an element, and a
specific set of rules determines the final styling. The order of specificity and source (e.g., user-defined, author,
browser default) plays a role in this.
3. Text Decoration:
5. Text Transformation:
6. Text Shadow:
8. Text Direction:
9. Text Break:
4. Background Properties:
• background-color: [Color Value]
2. Hexadecimal Notation:
3. RGB Function:
• You can use the rgb() function to specify a color using red, green, and blue values.
4. RGBA Function:
• You can use the rgba() function to specify a color with an alpha (transparency) value.
5. HSL Function:
• You can use the hsl() function to specify a color using hue, saturation, and lightness.
6. HSLA Function:
• You can use the hsla() function to specify a color with an alpha (transparency) value.
1.Position property
The position property in CSS is used to control the positioning of an element within its containing
element or the viewport. It works in conjunction with other properties like top, right, bottom, and left
to precisely place the element.
1. static:
• This is the default value. Elements with position: static; are positioned according to
the normal flow of the document. The top, right, bottom, and left properties have no
effect when position is set to static.
p {
position: static;
}
2. relative:
• The element is positioned relative to its normal position in the document flow, but it
can be moved using the top, right, bottom, and left properties. Other content will not
be adjusted to fit into any gap left by the element.
div {
position: relative;
top: 10px;
left: 20px;
}
3. absolute:
h1 {
position: absolute;
top: 50px;
left: 100px;
}
4. fixed:
• The element is positioned relative to the viewport, meaning it always stays in the same
place even if the page is scrolled. It is not affected by the position of other elements.
div {
position: fixed;
top: 10px;
right: 10px;
5. sticky:
• The element is treated as relative positioned until it crosses a specified point, then it
is treated as fixed positioned.
div {
position: sticky;
top: 0;
Understanding and using these values of the position property is crucial for creating complex
layouts and achieving desired positioning effects on a webpage. The choice of which value to use
depends on the specific layout requirements and the desired behaviour of the element.
2.Display property
The display property in CSS is used to define how an HTML element should be displayed on
the page. It influences the layout and rendering of elements, determining whether an element
generates a block-level box, an inline-level box, or something else. The display property can take
various values, each serving a specific purpose:
1. block:
• The element generates a block-level box. It starts on a new line and takes up the full
width available. Examples of block-level elements include <div>, <p>, <h1> to <h6>,
and <form>.
div {
display: block;
}
2. inline:
• The element generates an inline-level box. It does not start on a new line and only
takes up as much width as necessary. Examples of inline-level elements include
<span>, <a>, <strong>, and <img>.
span {
display: inline;
}
3. inline-block:
• The element generates an inline-level box, but it behaves like a block-level box in terms
of layout.
• It does not start on a new line and allows other elements to sit next to it. It also
respects width and height properties. Useful for creating inline elements with block-
level styling.
span {
display: inline-block;
}
4. flex:
• It allows you to use the flexbox layout model, providing a powerful way to distribute
space and align items.
div {
display: flex;
}
5. grid:
• It allows you to use the CSS Grid layout model, providing a two-dimensional grid for
arranging items.
div {
display: grid;
}
6. none:
• It is removed from the normal flow of the document, and it does not take up space.
div {
display: none;
}
These values provide flexibility in designing the layout of a webpage, allowing developers to
create complex structures and achieve desired visual effects. Choosing the right display value depends
on the desired layout and the behaviour you want for the specific element.
3.Float
The float property in CSS is used to specify whether
an element should be floated to the left or right of its
container, allowing content to flow around it. It was
commonly used for creating basic layouts before the
advent of more modern layout techniques like Flexbox
and CSS Grid. The float property can take the following
values:
1. none:
img {
float: none;
}
2. left:
• Content following the floated element will flow around it on the right side.
img {
float: left;
}
3. right:
• Content following the floated element will flow around it on the left side.
img {
float: right;
}
4. inherit:
• The element inherits the float value from its parent element.
img {
float: inherit;
}
It's important to note that when an element is floated, it is taken out of the normal document flow,
which means that other elements may flow around it. This behaviour can lead to issues with clearing
and layout, especially when there are elements with different heights.
7. Box Model and Spacing:
The box model is a fundamental concept in CSS (Cascading Style
Sheets) that describes how elements are rendered on a web page in
terms of their dimensions and spacing. It consists of four main
components: content, padding, border, and margin. These components
work together to determine the overall size and layout of an element.
1. Content:
2. Padding:
• Padding is the space between the content and the border. It provides inner spacing within the element.
• You can set the padding using the padding property in CSS, which can be a specific value (e.g., padding:
10px;) or individual properties for each side (e.g., padding-top, padding-right, padding-bottom,
padding-left).
3. Border:
• The border surrounds the padding, defining the outer edge of the element.
• You can set the border using the border property, which includes properties for the border width, style,
and color.
• Like padding, you can also set individual border properties for each side (e.g., border-top, border-
right, border-bottom, border-left).
4. Margin:
• Margin is the space outside the border, creating separation between the element and other elements
on the page.
• You can set the margin using the margin property, similar to padding. It can have specific values or
individual
properties for
each side (e.g.,
margin-top,
margin-right,
margin-
bottom,
margin-left).
9. Transitions and Animations:
1.Transition:
In CSS, a transition is a way to smoothly animate changes in CSS properties. It allows you to control the
timing and easing of the transition between the old and new values of a property. Transitions are commonly
used to create more visually appealing and interactive user interfaces.
• transition-property:
• transition-duration:
• transition-timing-function:
• transition-delay:
2. HTML Example:
2.Animations
1. CSS Animation:
CSS animation is a technique that allows elements to gradually change from one style to
another. It enables the creation of smooth and visually appealing transitions or movements on a
webpage without relying on JavaScript or other scripting languages.
2. Properties of Animation:
Here is a list of key properties related to CSS animations along with their possible values:
iii)Animation Shorthand
The animation shorthand property in CSS allows you to specify multiple animation-related properties
in a single declaration. The general syntax for the animation shorthand property is as follows:
element {
.element {
In this example:
A Practice program:
10.Flexbox in CSS
Flexbox, or the Flexible Box Layout, is a layout model in CSS that allows the design of complex and responsive
layouts with a more efficient and predictable way than traditional models. It is particularly well-suited for designing
dynamic and single-dimensional layouts.
1. Purpose of Flexbox:
• Flexbox is designed to provide an efficient way to layout, align, and distribute space among items in
a container, even when their size is unknown or dynamic.
• It helps to create a more responsive design, allowing for the construction of complex layouts with a
flexible and easily adaptable structure.
1. display
• Purpose:
• Defines a flex container.
• Possible Values:
• flex: Establishes a flex container, inline-flex: Establishes an inline flex container.
• Example
2. flex-direction
• Purpose:
• Defines the direction of the main axis.
• Possible Values:
• row, row-reverse, column, column-reverse.
• Example
3. flex-wrap
• Purpose:
• Defines whether the items should wrap or not.
• Possible Values:
• nowrap, wrap, wrap-reverse.
• Example:
4. justify-content
• Purpose:
• Defines how items are aligned along the main axis.
• Possible Values:
• flex-start, flex-end, center, space-between, space-around, space-evenly.
• Example:
5. align-items
• Purpose:
• Defines how items are aligned along the cross axis.
• Possible Values:
• flex-start, flex-end, center, baseline, stretch.
• Example:
Cheat Sheet:
11. Responsive Web Design (RWD):
1. Responsive Web Design
Responsive web design (RWD) is an approach to web design that makes web pages render well on a variety
of devices and window or screen sizes. The goal of responsive design is to build web pages that detect the visitor's
screen size and orientation and change the layout accordingly. This ensures that the same website can provide an
optimal viewing experience across various devices, such as desktops, laptops, tablets, and smartphones.
2. Using RWD
• Fluid Grids: Use relative units like percentages instead of fixed units like pixels for layout components.
• Flexible Images: Ensure images are also sized in relative units, preventing them from overflowing their
containing elements.
• Media Queries:
A media query in CSS allows you to apply different styles to a document based on various conditions,
such as the characteristics of the device or the viewport size. The syntax for a media query is as follows:
/* CSS rules to apply when the media query conditions are met */
• @media: This is the at-rule that defines a media query. It signals to the browser that enclosed styles
should be applied only under specific conditions.
• media_type: Specifies the type of media for which the styles are intended. Common values include:
• all: Applies to all devices.
• screen: Applies to computer screens, tablets, and smartphones.
• print: Applies to printers and print preview.
• and: A logical operator used to combine multiple media features into a single media query. It
requires that all conditions be true for the styles to be applied.
• media_feature: Specifies the specific condition or feature that should be met for the styles to be
applied. Some examples of media features include:
• width: The width of the viewport.
• height: The height of the viewport.
• min-width and max-width: Minimum and maximum width of the viewport.
• orientation: The orientation of the device (landscape or portrait).