CSS Notes
CSS Notes
Introduction
What is CSS?
CSS defines styles of your web pages including design, layout and
variations in display across devices and screen sizes.
CSS was first proposed in 1994, by Norwegian technologist
Håkon Wium Lie.
CSS Syntax: -
Grouping Selector
The group selector in CSS allows you to apply the same styles to
multiple elements at once by separating them with a comma (,).
Comments
Comments can be used to explain CSS codes and make it more
readable. They are not rendered by browsers.
CSS Inserting
There are three simple ways to insert CSS into an HTML Document : -
● Inline Styling
● Internal Style Sheet
● External Style Sheet
Internal Style Sheet : An internal style sheet is useful for styling a single
HTML document. CSS rules should be placed inside the <style>
element, which must be within the <head> section.
External Style Sheet : An external style sheet is useful for applying the
same styles to multiple HTML pages. It is saved with a .css extension
(e.g., filename.css) and included in HTML using the <link>
element. A CSS file should not contain a <style> element.
Color property : The color property in CSS is used to set the text
color of an element.
● Color Name
● Hexadecimal Color Value
● RGB Color Value
Background property : The background property in CSS is used to
set the background of an element. It allows you to define colors, images,
positions, and more.
Background properties : -
● background-color
● background-image
● background-repeat
● background-attachment
● background-position
● background-clip
Valid Values :
● Top, bottom, left, right, center
● Pixels
● Percentage
● And more.
Background attachment property : this property sets whether a
background image position is fixed within the viewport, or scroll with its
containing block.
Valid values : -
● scroll : Default value. The background image scrolls with the
page.
● fixed : The background image stays fixed while the page
scrolls.
●
● background-color
● background-image
● background-repeat
● background-attachment
● background-position
Width property : The width property in CSS is used to define the width
of an element.
Border Style : the border-style property sets the linestyle for all four
sides of an element’s border.
Valid values : -
● none : displays no border
● hidden : displays no border
● dotted : displays a series of rounded dots
● dashed : displays a series of short square-ended dashes or line
segments
● solid : displays a single, straight, solid line
● double : displays two straight lines • groove : displays a border
with a carved appearance • ridge: displays a border with an
extruded appearance
● inset : displays a border that makes the element appear
embedded
● outset : displays a border that makes the element appear
embossed
Border Width : we can specify the width of an element's borders using
the border-width CSS property.
Valid values :-
● Thick
● Medium
● Thin
Box Model : The CSS Box Model is the fundamental concept that
defines how elements are structured and displayed on a webpage. Every
element in CSS is treated as a rectangular box, consisting of four parts:
CSS Box's width = left border's width + left padding's width + content
box's width + right padding's width + right border's width.
CSS Box's height = top border's height + top padding's height + content
box's height + bottom padding's height + bottom border's height.
CSS Text
Color property : the color CSS property sets the color of a text.
Text Align property : the text-align CSS property sets the horizontal
alignment of a text.
Valid values : -
● left : aligns text to the left edge.
● center : aligns text to the center.
● right : aligns text to the right edge.
● justify : aligns text its left and right edges to the left and right
edges of the line box except for the last line.
Valid Values :-
● none : prevents the case of the text from being changed
● capitalize : converts the first letter of each word to uppercase;
other characters are unchanged
● uppercase : converts a text to uppercase
● lowercase : converts a text to lowercase
Text Indent property : The text-indent CSS property sets the length of
the indentation of a text.
Usage
Valid Values :
Valid Values :-
● Normal : sets the normal letter spacing for the current font.
● <length>
Valid Values : -
● normal : sets the normal word spacing as defined by the browser
● <length>
● <percentage>
Valid Values :-
● normal : depends on the user agent
● <number> : the value is multiplied by the element's current font
size; this is the preferred may to set line-height
● <length>
● <percentage>
Valid Values : -
● visible (default)
● hidden
● scroll
● auto
● clip
White Space property : The white-space CSS property sets how the
white space is handled inside and element .
Valid Values : -
Text Direction property : The direction CSS property sets the direction
of a text (and more).
Valid Values : -
● ltr : sets text and other elements go from left to right.
● rtl : sets text and other elements go from right to left.
CSS Fonts
CSS fonts define the font family, size, style, weight or boldness and
variant of a text.
Font Family property : There are two types of font family names in
CSS
● <generic-name> : a group of font families which have similar looks
eg. San-serif, Monospace.
● <family-name> : a specific font family name e.g. Lucida Console,
Verdana.
Let’s learn how to add Google fonts
Icons : we can add icons to our HTML page by using icons libraries
such as font awesome, Bootstrap icons and Material icons by google.
Link : in CSS we can style links by changing their properties (color,
background-color, text-decoration etc.)
Link Defaults :
● Links are underlined
● Unvisited links are blue
● Visited links are purple
● Hovering a link makes the mouse a little hand icon
● Focused links have an outline around them
● Activate links are red
Valid Values:
● Outside
● Inside
Images
Responsive Image
CSS Layout
Valid values :-
● Inline
● Block
● Inline-block
● None
●
Hiding an Element
1) Set the display CSS property to none.
2) Set visibility CSS property to hidden.
Note : Understand the different between display none and visibility
hidden.
The top, right, bottom , left properties set the location of positioned
elements.
Absolute Lengths
The absolute length units are fixed and a length expressed in any of
these will appear as exactly that size.
Absolute length units are not recommended for use on screen, because
screen sizes vary so much. However, they can be used if the output
medium is known, such as for print layout.
Relative Lengths
Relative length units specify a length relative to another length property.
Relative length units scale better between different rendering medium.
Overflow property : Content usually overflows when it is bigger than its
container (parent element).
The overflow-x CSS property sets what to do when content overflows a
block-level element's left and right edges.
The overflow-y CSS property sets what to do when content overflows a
block-level element's top and bottom edges.
Valid Values:
● visible : default; content is not clipped, overflowing content is
rendered outside the box's edges
● hidden : content is clipped if necessary and invisible
● scroll: content is clipped if necessary to fit the padding box;
browsers will display a scroll bar
● auto : if content fits inside the padding box it works the same as
visible ; if content overflows the the padding box it works like scroll
The overflow CSS property is a shorthand for the overflow-x and
overflow-y CSS properties.
It can have one or two values:
● When one value is specified, the value applies to both overflow-x
and overflow-y properties
● When two values are specified, the first value applies to the
overflow-x property while the second value applies to the
overflow-y property
Valid Values:
● none: default; the element does not float
● left: the element floats on the left side of its container
● right: the element floats on the left side of its container
Clear property : The clear CSS property sets whether an element can
have floating elements around it.It can be applied to floating and
non-floating elements.
Valid Values:
● none: default; elements are allowed to float on both sides
● left: elements are not allowed to float on the left side
● right: elements are not allowed to float on the right side
● both: elements are not allowed to float on both sides
CSS combinators :
● Descendent combinators (वंशज) : The descendant combinator
matches all elements that are descendants of a specified element.
Note : border-radius CSS property does not need the border properties
to work.
Valid values : -
● border-box (default)
● padding-box
● content-box
Background origin property : The background-origin property specifies
the origin position (the background positioning area) of a background
image.
Valid values : -
● border-box
● padding-box
● content-box
Valid values : -
● cover
● contain
● <length>
● <percentage>
Gradients : CSS gradients are made of a progressive transition
between two or more colors.
The transition between colors are usually smooth.
2D Transform methods
● translate()
● translateX()
● translateY()
● rotate()
● scale()
● scaleX()
● scaleY()
● skew()
● skewX()
● skeY()
● matrix()
It is specified with two values, the first value represents the horizontal
direction while the second value represents the vertical directions.
translateX() : translateX() function repositions or moves an element
from its original position in the horizontal directions.
3D transform methods :
● rotateX()
● rotateY()
● rotateZ()
Valid Values : -
● Ease : default; the transition effect start slowly, then fast,then
slowly ends.
● Linear : the transition effect has the same speed from start to end.
● Ease-in : the transition effect starts slowly.
● Ease-out : the transition effect ends slowly.
● Ease-in-out : the transition effect both starts and ends slowly.
Transition-delay : the transition-delay CSS property specifies a
duration to wait before the transition effect starts.
Transition shorthand
● transition-property
● transition-duration
● transition-timing-function
● transition-delay
Valid Values:
● none: default; the animation will not apply styles to the element
while it's not executing
● forwards: the element will retain the styles set by the last keyframe
● backwards: the element will retain the styles set by the last
keyframe
● Both : the element will retain the styles set by the first and last
keyframes
Animation play state : The animation-play-state CSS property defines
whether an animation is running or paused.
Valid Values:
● running: default; the animation is playing
● paused: the animation is paused
Animation shorthand
● Animation-name
● Animation-duration
● Animation-timing-function
● Animation-delay
● Animation-iteration-count
● Animation-direction
● Animation-fill-mode
● Animation-play-state
Valid Values:
● none: the element does not offer any user-controllable method for
resizing it
● horizontal: the element provides a control allowing it to be resized
horizontally
● vertical: the element provides a control allowing it to be resized
vertically
● both: the element provides a control allowing it to be resized both
horizontally and vertically
Valid Values:
● content-box: gives the default box-sizing behavior where the
widths of the padding and border are added to the size of an
element
● border-box: the defined width and height include any border and
padding, it causes the content-box to shrink
by default, the padding and border widths add up to the total render
width and height of an element.
Media queries : CSS3 Media Queries are useful when you want to
modify your web page depending on a device's general type or specific
characteristics and parameters.
For instance, you can set distinct styles for device's with different:
● width and height of the viewport
● orientation (i.e. portrait or landscape in mobile devices)
● screen resolution
CSS media type : CSS media types describe the category of a device.
Media feature expressions test for their presence or value, and are
entirely optional. Each media feature expression must be surrounded by
parentheses.
The @media Rule : the @media at-rule is used to target media types
and media features.
If true, it means that the expressions match the media type of the device
the web page is being displayed on.
When that is the case then all the corresponding style sheet (e.g. CSS
declaration blocks) are applied to the document.
The media type is optional except when using the not or only operators.
If they are not used, the all media type will be implied.
Example:
Viewport is the area of the window in which web contents can be seen.
Or in simplest terms, it is the visible area of a webpage that a user can
see.
The viewport size depends on the device of the user. In mobile phones
the viewport size is much smaller compared to laptops or desktops
which are much wider.
Before, web pages were not mobile-optimized making them look bad in
mobile phones or tablets since they are only designed for computer
screens.
Additionally, web pages before are defined with fixed sizes, for instance,
a web page can have wrappers or containers with a width of 1000px
which causes web pages to look bad on narrow screen devices.
Viewport meta tag
The viewport meta tag is an HTML tag that helps control how a web
page is displayed on mobile devices. It ensures that the page scales
correctly to fit different screen sizes, improving usability and
responsiveness.