Full Stack Unit - 1 (CSS)
Full Stack Unit - 1 (CSS)
CSS
Prepared by:
Dr. Susheela
Cascading Style Sheets
• CSS is the language we use to style an HTML
document.
• CSS provides a set of style rules for defining the layout
of HTML documents.
• CSS describes how HTML elements should be displayed.
• HTML largely determines textual content, CSS
determines visual structure, layout, and aesthetics.
HTML is a markup language, and CSS is a style sheet
language.
• CSS makes it easier to enhance the look of the different
elements on a web page.
Need for CSS
1. Faster Page Speed / Reduce redundancy and increase usability
• CSS enables you to use less code. By using CSS, you can use one CSS rule and apply it to all
occurrences of a certain tag within an HTML document.
2. Better User Experience
• CSS not only makes web pages easy on the eye, it also allows for user-friendly formatting.
When buttons and text are in logical places and well organized, user experience improves.
3. Quicker Development Time
• With CSS, you can apply specific formatting rules and styles to multiple pages with one string
of code. One cascading style sheet can be replicated across several website pages.
4. Easy Formatting Changes /Easier to Maintain the site
• If you need to change the format of a specific set of pages, there’s no need to change for
every individual page. Just edit the corresponding CSS stylesheet and the changes will be
applied to all the pages that are using that style sheet. site
5. Compatibility Across Devices
• Whether mobile or tablet, desktop, or even smart TV, CSS combines with HTML to make
responsive design possible
6. Supported by all browsers
7. Presentation style of document separated from content
Basic Syntax and Structure
• A CSS rule consists of a selector and a declaration block.
*
{
font-family: Calibri;
}
Nested Selector: Descendant
• <a> inside <div class="items">
div.items a
{
color: green;
font-weight: bold;
}
<div class="items">
<a href="#">Item1</a>
<a href="#">Item2</a>
<a href="#">Item3</a>
<ul>
<li><a href="#">Item4</a></li>
<li><a href="#">Item5</a></li>
<li><a href="#">Item6</a></li>
</ul>
</div>
Nested Selector: Adjacent Sibling
• <p> coming after <p>
p+p
{
font-style: italic;
font-weight: bold;
}
<div>
<p>I'm a paragraph</p>
<p>I am selected!</p>
</div>
<p>I am selected!</p>
<h2>Monkey hair</h2>
<p>I am NOT selected</p>
Grouping Selector
• You can apply a style to many selectors if you like.
• Just separate the selectors with a comma, as given in the
following example:
h1, h2, h3
{
color: #36C;
font-weight: normal;
font-size:20px;
}
Pseudo Classes Selectors
• A way of accessing HTML items that are not a part of the document
tree.
• One of its main uses is to control the appearance of unvisited and
visited links on a Webpage.
• Types of pseudo-class selectors
• a:link – an unvisited link
• a:visited – a visited link
• a:hover – a link you have your mouse over
• CSS example:
a: hover
{
text-decoration: none;
}
<!DOCTYPE html>
<html>
HTML Link Colors Using CSS
<head>
<style>
a:link {
color: green;
background-color: transparent;
}
a:visited {
color: pink;
background-color: transparent;
}
a:hover {
color: red;
background-color: transparent;
}
a:active {
color: yellow;
background-color: transparent;
}
</style>
•a:link - a normal, unvisited link
<h2>Link Colors</h2>
•a:visited - a link the user has visited
<p>You can change the default colors of links</p>
•a:hover - a link when the user mouses over it
<a href="html_images.asp" target="_blank">HTML Images</a>
•a:active - a link the moment it is clicked
</body>
</html>
CSS Length Units
• There are two types of length units: absolute and relative.
• Absolute Length Units
• 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 Length Units
• Relative length units specify a length relative to another length property.
• Relative length units scale better between different rendering mediums.
Absolute Length Units
Relative Length Units
CSS Box Model
• In CSS, the term "box model" is used when talking about
design and layout.
• The CSS box model is essentially a box that wraps around
every HTML element.
• It consists of margins, borders, padding, and the actual
content. The image below illustrates the box model:
• Explanation of the different parts:
• Content - The content of the box, where text and images
appear
• Padding - Clears an area around the content. The padding
is transparent
• Border - A border that goes around the padding and
content
• Margin - Clears an area outside the border. The margin is
transparent
CSS Box Model
• box-sizing property is used to define the size of the Box
using width property.
• By default, width property is used to define the width
of the content box.
• It consists of margins, borders, padding, and the actual
content. The image below illustrates the box model:
CSS Box Model
• overflow property is used in-case when the content of
the box takes more space than the specified size of the
box (height & width of the box).
• Possible values: auto, hidden, inherit, scroll, visible
• Default value is visible.
CSS - background property
• The background property is a shorthand property for:
• background-color
• background-image
• background-position
• background-size
• background-repeat
• background-origin
• background-clip
• background-attachment
background-color
• With CSS, a color is most often
specified by:
• a valid color name - like "red"
• a HEX value - like "#ff0000"
• an RGB value - like "rgb(255,0,0)"
• Opacity / Transparency
• The opacity property specifies the
opacity/transparency of an
element. It can take a value from
0.0 - 1.0.
• The lower value, the more
transparency
Transparency using RGBA
background-image
• The background-image property sets one or more background
images for an element.
• By default, a background-image is placed at the top-left corner
of an element, and repeated both vertically and horizontally.
• Possible property values:
• url - The URL to the image. To specify more than one image, separate
the URLs with a comma
• none - No background image will be displayed. This is default
• initial - Sets this property to its default value.
• inherit - Inherits this property from its parent element.
background-position
• The background-position property sets the starting position of a
background image.
• By default, a background-image is placed at the top-left corner of an
element, and repeated both vertically and horizontally.
• The top left corner is 0 0 or 0% 0%. The right bottom corner is 100 100 0r
100% 100%.
• Possible values:
• left top, left center, left bottom, right top, right center, right bottom, center top,
center center, center bottom - If you only specify one keyword, the other value will
be "center”
• x% y% - x is the horizontal position and y is the vertical position.
• xpos ypos – xpos is the horizontal position and ypos is the vertical position.
• initial - Sets this property to its default value.
• inherit - Inherits this property from its parent element.
p.normal {
font-style: normal;
}
p.italic {
font-style: italic;
}
p.oblique {
font-style: oblique;
}
CSS Font Weight
• The font-weight property specifies the weight of a font: normal
or bold
p.normal {
font-weight: normal;
}
p.thick {
font-weight: bold;
}
CSS Font Variant
• The font-variant property specifies whether or not a text should
be displayed in a small-caps font.
• In a small-caps font, all lowercase letters are converted to
uppercase letters. However, the converted uppercase letters
appears in a smaller font size than the original uppercase letters
in the text.
p.normal {
font-variant: normal;
}
p.small {
font-variant: small-caps;
}
CSS Font Variant
• The font-size property sets the size of the text.
• Always use the proper HTML tags, like <h1> - <h6> for headings
and <p> for paragraphs.
• The font-size value can be an absolute, or relative size.
h1 {
font-size: 40px;
}
h2 {
font-size: 1.875em; /* 30px/16=1.875em */
}
body {
font-size: 100%;
}
p {
font-size: 10vw;
}
CSS Font with Shorthand Declaration
• The font property is a shorthand property for:
• font-style
• font-variant
• font-weight
• font-size/line-height
• font-family
• The font-size and font-family values are required. If one of the other values
is missing, their default value are used.
Note: The line-height property sets the space between lines.
• if you specify background-color specifically and then you go ahead and
override it with the background, but you don't specify the color or you
don't specify whatever the specific property is, whether it's background-
image or background-repeat or background-position and so on.
• If you specify font without any dash subproperty, whatever properties that
are inherited with a dash will be overridden unless you actually call them
out directly, straight in the font property.
CSS Border
• The ”border-style” property specifies what kind of border to display.
Padding
The CSS padding properties are used to generate space around an element's
content, inside of any defined borders.If the padding property has four values:
•padding: 25px 50px 75px 100px;
• top padding is 25px
• right padding is 50px
• bottom padding is 75px
• left padding is 100px
CSS Lists
• The CSS list properties allow you to:
• Set different list item markers for ordered lists
• Set different list item markers for unordered lists
• Set an image as the list item marker
• Add background colors to lists and list items
• CSS Lists Properties
• list-style-type - specifies the type of list item marker (circle,
square, upper-roman, lower-alpha).
• list-style-image - specifies an image as the list item marker.
• list-style-position - specifies the position of the list-item markers
(bullet points).
• "list-style-position: outside;" means that the bullet points will be
outside the list item.
• "list-style-position: inside;" means that the bullet points will be inside
the list item.
• list-style-type:none - can also be used to remove the
markers/bullets. Default margin and padding will remains in the list.
To remove this, add margin:0 and padding:0 to <ul> or <ol>
CSS List - Shorthand property
• The list-style property is a shorthand property. It is used to
set all the list properties in one declaration:
ul {
list-style: square inside url(“gehu.jpg”);
}
• When using the shorthand property, the order of the
property values are:
• list-style-type (if a list-style-image is specified, the value of this
property will be displayed if the image for some reason cannot be
displayed)
• list-style-position (specifies whether the list-item markers should
appear inside or outside the content flow)
• list-style-image (specifies an image as the list item marker)
• If one of the property values above is missing, the default value
for the missing property will be inserted, if any.
Position Property
• The position property specifies the type of
positioning method used for an element.
• There are five different position values:
• static
• relative
• fixed
• absolute
• sticky
position: static
• HTML elements are positioned static by default
• Static positioned elements are not affected by the top,
bottom, left, and right properties.
position: relative
• HTML element is positioned relative to its normal
position. Setting the top, right, bottom, and left
properties of a relatively-positioned element will cause
it to be adjusted away from its normal position.
• Other content will not be adjusted to fit into any gap
left by the element
position: fixed
• HTM element is positioned relative to the viewport, which means it
always stays in the same place even if the page is scrolled. The top,
right, bottom, and left properties are used to position the element.
• A fixed element does not leave a gap in the page where it would
normally have been located.
position: absolute
• An element with position: absolute; is positioned
relative to the nearest positioned ancestor (instead of
positioned relative to the viewport, like fixed).
• However; if an absolute positioned element has no
positioned ancestors, it uses the document body, and
moves along with page scrolling.
position: sticky
• An element with position: sticky; is positioned based on
the user's scroll position.
• A sticky element toggles between relative and fixed,
depending on the scroll position. It is positioned relative
until a given offset position is met in the viewport -
then it "sticks" in place (like position:fixed).
Output After Scrolling
Bootstrap
• Bootstrap is an open-source and free CSS framework that helps in directing
a responsive device-friendly mobile-first front-end webpage development
tool.
• Bootstrap mainly includes CSS (Cascading Style Sheets) and an optional
JavaScript-supported design template (plug-ins) that deals with typography,
buttons, forms, and other user interface components. This Bootstrap
framework helps rapid web development and supports developers in
creating responsive web pages.
• Twitter Blueprint was the first name for Bootstrap and was developed on
Twitter by Mr. Mark Otto and Jacob Thornton. It was released as an open-
source product on GitHub in August 2011. The framework is primarily built
to encourage design uniformity and reliability of web pages across
applications. Before its existence, developers used various external libraries
to perform responsive web development, leading to incompatibilities in
web development and heavy maintenance burdens.
Benefits of Bootstrap
• Browser supportive: Every browser supports this Bootstrap Framework.
• Mobile-first approach: The Bootstrap framework has a preexisting mobile-first style all through the library and not as separate
files.
• Simple and easy to start: If you know HTML and CSS, you can quickly start working with Bootstrap, and its documentation is
available on the official site.
• Responsive design and looks: Web pages designed using the Bootstrap framework have responsive CSS that can adjust to the
screen size of large desktops, notebooks, tablets, and mobiles.
• Easy customization: It provides some built-in components and functionalities that are easy to customize.
• Clean interface or Developers: The bootstrap framework provides a new and consistent result for building user interfaces on web
pages.
• It is an open-source framework with web-based customization.
• Benefits of Bootstrap Framework
• It produces fewer cross-browser bugs.
• It is a consistent framework supported by all web browsers and CSS-based compatibility improvements.
• It is a lightweight and hence widely used framework for creating responsive sites.
• It's easily customizable.
• It has a simple and effective grid system.
•
Media Query
• Media Query is used to design a responsive device-friendly front-end
webpage.
Program (Media Query)
OUTPUT (When Page width is
in between 500 – 1000px