Css Stands For Cascading Style Sheets: Styling Can Be Added To HTML Elements in 3 Ways
Css Stands For Cascading Style Sheets: Styling Can Be Added To HTML Elements in 3 Ways
</body>
</html>
Internal Styling (Internal CSS)
An internal style sheet can be used to define a common style for all HTML elements on a
page.
Internal styling is defined in the <head> section of an HTML page, using a <style>
element:
<html>
<head>
<title>internal css</title>
<style>
body {
Background-color: lightgrey;
}
h1{
color:blue;
}
p{
color:green;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
CSS Selector
CSS selectors are used to select the content you want to style. Selectors are the part of
CSS rule set. CSS selectors select HTML elements according to its id, class, type, attribute
etc.
There are several different types of selectors in CSS.
1. CSS Element Selector
2. CSS Id Selector
3. CSS Class Selector
4. CSS Universal Selector
5. CSS Group Selector
h1,h2,p {
text-align: center;
color: blue;
}
CSS Color
Colors are specified using predefined color names, or RGB, HEX, HSL, RGBA, HSLA values.
<html>
<body>
<h1 style="background-color:Tomato;">Tomato</h1>
<h1 style="background-color:Orange;">Orange</h1>
<h1 style="background-color:DodgerBlue;">DodgerBlue</h1>
<h1 style="background-color:MediumSeaGreen;">MediumSeaGreen</h1>
<h1 style="background-color:Gray;">Gray</h1>
<h1 style="background-color:SlateBlue;">SlateBlue</h1>
<h1 style="background-color:Violet;">Violet</h1>
<h1 style="background-color:LightGray;">LightGray</h1>
</body>
</html>
The CSS Background
The CSS background properties are used to define the background
effects for elements.
In these chapters, you will learn about the following CSS background properties:
• background-color
• background-image
• background-repeat
• background-attachment
• background-position
1. Background-image
<html>
<head>
<style>
body {
background-image: url("paper1.gif");
margin-left:100px;
}
</style>
</head>
<body>
<h1>Hello Javatpoint.com</h1>
</body>
</html>
2. background-repeat
the background-image property repeats the background image horizontally and
vertically. Some images are repeated only horizontally or vertically.
<html>
<head>
<style>
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
}
</style>
</head>
<body>
<h1>Hello Javatpoint.com</h1>
</body>
</html>
3. Background-attachment
The background-attachment property is used to specify if the background image is fixed
or scroll with the rest of the page in browser window. If you set fixed the background
image then the image will not move during scrolling in the browser.
<html>
<head>
<style>
body {
background: white url('bbb.gif');
background-repeat: no-repeat;
background-attachment: fixed;
margin-left:200px;
}
</style>
</head>
<body>
<p>This is a fixed background-image. Scroll down the page.</p>
<p>This is a fixed background-image. Scroll down the page.</p>
<p>This is a fixed background-image. Scroll down the page.</p>
<p>This is a fixed background-image. Scroll down the page.</p>
<html>
<head>
<style>
body {
background: white url('good-morning.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
}
</style>
</head>
<body>
<p>This is a fixed background-image. Scroll down the page.</p>
<p>This is a fixed background-image. Scroll down the page.</p>
<p>This is a fixed background-image. Scroll down the page.</p>
<p>This is a fixed background-image. Scroll down the page.</p>
<p>This is a fixed background-image. Scroll down the page.</p>
<p>This is a fixed background-image. Scroll down the page.</p>
<p>This is a fixed background-image. Scroll down the page.</p>
<p>This is a fixed background-image. Scroll down the page.</p>
<p>This is a fixed background-image. Scroll down the page.</p>
<p>This is a fixed background-image. Scroll down the page.</p>
<p>This is a fixed background-image. Scroll down the page.</p>
<p>This is a fixed background-image. Scroll down the page.</p>
<p>This is a fixed background-image. Scroll down the page.</p>
<p>This is a fixed background-image. Scroll down the page.</p>
<p>If you do not see any scrollbars, Resize the browser window.</p>
</body>
</html>
CSS Border
The CSS border is a shorthand property used to set the border on an element.
The CSS border properties are use to specify the style, color and size of the border of an
element. The CSS border properties are given below
o border-style
o border-color
o border-width
o border-radius
Value Description
• padding-top
• padding-right
• padding-bottom
• padding-left
Shorthand Property: padding: 25px 50px 75px 100px;
• top padding is 25px
• right padding is 50px
• bottom padding is 75px
• left padding is 100px
CSS Margins
Margins are used to create space around elements, outside of any defined borders.
Margin - Individual Sides
CSS has properties for specifying the margin for each side of an element:
• margin-top
• margin-right
• margin-bottom
• margin-left
• 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
<html>
<head>
<style>
div {
background-color: lightgrey;
width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px;
}
</style>
</head>
<body>
<h2>Demonstrating the Box Model</h2>
<p>The CSS box model is essentially a box that wraps around every HTML element. It
consists of: borders, padding, margins, and the actual content.</p>
<div>This text is the content of the box. We have added a 50px padding, 20px margin and
a 15px green border. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
officia deserunt mollit anim id est laborum.</div>
</body>
</html>
CSS Text
CSS has a lot of properties for formatting text.
1. Text Color
The color property is used to set the color of the text. The color is specified by:
• a color name - like "red"
• a HEX value - like "#ff0000"
• an RGB value - like "rgb(255,0,0)"
2. Text Color and Background Color
In this example, we define both the background-color property and the color property:
3. CSS Text Alignment
The text-align property is used to set the horizontal alignment of a text. A text can be left
or right aligned, centered, or justified.
4. Text Direction
The direction and Unicode-bidi properties can be used to change the text direction of an
element:
direction: rtl;
unicode-bidi: bidi-override;
5. Vertical Alignment
The vertical-align property sets the vertical alignment of an element. like baseline, text-
top, text-bottom, sub, super.
vertical-align: baseline;
6. Text Decoration
The text-decoration property is used to set or remove decorations from text.
The value text-decoration: none; is often used to remove underlines from links:
• Overline
• line-through
• underline
7. Text Transformation
The text-transform property is used to specify uppercase and lowercase letters in a text.
• text-transform: uppercase;
• text-transform: lowercase;
• text-transform: capitalize;
div.third {
opacity: 0.6;
}
</style>
</head>
<body>
<h1>Transparent Box</h1>
<p>When using the opacity property to add transparency to the background of an
element, all of its child elements become transparent as well. This can make the text
inside a fully transparent element hard to read:</p>
<div class="first">
<h1>opacity 0.1</h1>
</div>
<div class="second">
<h1>opacity 0.3</h1>
</div>
<div class="third">
<h1>opacity 0.6</h1>
</div>
</body>
</html>
CSS filter
CSS filters are used to set visual effects to text, images, and other aspects of a webpage.
The CSS filter property allows us to access the effects such as color or blur, shifting on
the rendering of an element before the element gets displayed.
Syntax
filter: none | invert() | drop-shadow() | brightness() | saturate() | blur() | hue-rotate() |
contrast() | opacity() | grayscale() | sepia() | url();
CSS Gradients
CSS gradients let you display smooth transitions between two or more specified colors
SS defines three types of gradients:
• Linear Gradients (direction, color-stop1, color-stop2, ...)
• Radial Gradients (defined by their center)
• Conic Gradients (rotated around a center point)
CSS list
The list-style-type property specifies the type of list item marker.
• list-style-type: circle;
• list-style-type: square;
• list-style-type: upper-roman;
• list-style-type: lower-alpha;
• list-style-type: none
Position The List Item Markers
• list-style-position: outside
• list-style-position: inside
CSS Tables
Table Borders
To specify table borders in CSS, use the border property. The example below specifies a
black border for <table>, <th>, and <td> elements:
Full-Width Table
The table above might seem small in some cases. If you need a table that should
span the entire screen (full-width), add width: 100% to the <table> element:
Collapse Table Borders
The border-collapse property sets whether the table borders should be collapsed into
a single border:eight
The width and height of a table are defined by the width and height properties.
The example below sets the width of the table to 100%, and the height of the <th>
elements to 70px:
• <span>
• <a>
• <img>
<html>
<head>
<title>Display</title>
</head>
<body>
<p>SQL Tutorial.</p>
</body>
</html>
Inline-block
The CSS display inline-block element is very similar to inline element but the difference is
<html>
<head>
<style>
p{
display: inline-block;
width: 150px;
</style>
</head>
<body>
<p>Java Tutorial.</p>
<p>SQL Tutorial.</p>
<p>HTML Tutorial.</p>
<p>CSS Tutorial.</p>
</body>
</html>
<html>
<head>
<style>
h1.hidden {
display: none;
}
</style>
</head>
<body>
<h1>This heading is visible.</h1>
<h1 class="hidden">This is not visible.</h1>
<p>You can see that the hidden heading does not contain any space.</p>
</body>
</html>
The position Property
The position property specifies the type of positioning method used for an element
(static, relative, fixed, absolute or sticky).
The position Property
The position property specifies the type of positioning method used for an element.
There are five different position values:
• Static-default position
• Relative-own position but not change own space
• Absolute-depend on parents position
• fixed
layer and z-index
The z-index property specifies the stack order of an element. When elements are
positioned, they can overlap other elements.
The z-index property specifies the stack order of an element (which element should be
placed in front of, or behind, the others).
An element can have a positive or negative stack order:
CSS Overflow
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.
overflow property has the following values:
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
auto - Like scroll, but it adds scrollbars only when necessary
Note: The overflow property only works for block elements with a specified height.
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: #eee;
width: 200px;
height: 50px;
border: 1px dotted black;
overflow:properties;
}
</style>
</head>
<body>
<h2>Overflow: visible</h2>
<p>text</p>
<div>texyt.</div>
</body>
</html>
Float Property
The float property is used for positioning and formatting content e.g. let an image float
left to the text in a container.
The float property can have one of the following values:
left - The element floats to the left of its container.
right - The element floats to the right of its container.
none - The element does not float (will be displayed just where it occurs in the text).
inherit - The element inherits the float value of its parent.
Example: image text
Clear Property
The clear property specifies what elements can float beside the cleared element and on
which side.
both - No floating elements allowed on either the left or the right side
inherit - The element inherits the clear value of its parent
CSS hover
The :hover selector is for selecting the elements when we move the mouse on them.
The hover feature includes the following effects:
• Change the color of the background and font.
• Modify the opacity of the image.
• Text embedding.
• Create image rollover effects.
• Swapping of images.
Syntax
:hover {
css declarations;
}
Example
<html>
<head>
<style>
body{
text-align:center;
}
p:hover, h1:hover, a:hover{
background-color: yellow;
}
</style>
</head>
<body>
<h1>Hello World</h1>
<p>Welcome to the TECHNICIA</p>
<a href="#">CSS Grid</a>
</body>
</html>
CSS 2D Transforms
CSS transforms allow you to move, rotate, scale, and skew elements.
Mouse over the element below to see a 2D transformation:
Transforms Methods
• translate()-moves an element from its current position (according to the
parameters given for the X-axis and the Y-axis.
transform: translate(50px, 100px);
• rotate()-method rotates an element clockwise or counter-clockwise according to a
given degree.
transform: rotate(20deg)
• scaleX()-The scaleX() method increases or decreases the width of an element.
transform: scaleX(2);
• scaleY()-The scaleY() method increases or decreases the height of an element.
transform: scaleY(2);
• scale()-Th
•
•
• e scale() method increases or decreases the size of an element (according to the
parameters given for the width and height).
transform: scale(2, 3);
• skewX()-The skewX() method skews an element along the X-axis by the given
angle.
transform: skewX(20deg);
• skewY()-The skewY() method skews an element along the Y-axis by the given angle.
transform: skewY(20deg);
• skew()-The skew() method skews an element along the X and Y-axis by the given
angles.
transform: skew(20deg, 10deg);
• matrix()-The matrix() method combines all the 2D transform methods into one.
The parameters are as follow:
matrix(scaleX(),skewY(),skewX(),scaleY(),translateX(),translateY())
transform: matrix(1, -0.3, 0, 1, 0, 0);
CSS Transitions
CSS transitions allows you to change property values smoothly, over a given duration.
• Transition(p,du,ti,de)
• transition-delay
• transition-duration
• transition-property
• transition-timing-function
Note: If the duration part is not specified, the transition will have no effect, because the
default value is 0.