css advanced and basic
css advanced and basic
Inline Styles
Internal Stylesheets
Internal stylesheets are defined within the <style> tag inside the
HTML document's <head> section. This method is useful for styling
a single webpage.
<!DOCTYPE html>
<html>
<head>
<title>List of Fruits</title>
<style>
body {
background-color: lightgrey;
h1 {
color: blue;
p{
font-size: 20px;
</style>
</head>
<body>
<h1>Welcome to My Website</h1>
</body>
</html>
External Stylesheets
body {
background-color: lightgrey;
h1 {
color: blue;
p{
font-size: 20px;
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
CSS selectors enable you to specify which HTML elements to style. There
are different types of selectors, each serving a specific purpose:
This CSS block makes all <p> (paragraph) elements display blue text.
1. Here, the. intro class applies a font size of 20 pixels to any element
that has this class.
1. This CSS rule makes the element with the id mainHeader have
green text.
1. Here, * selects all elements and sets their margins and padding to
zero, with red text color.
This CSS selector makes all links (<a> tags) that open in a new tab
(target="_blank") have red text.
This will make the text in all <p> elements 20 pixels high.
This will add 15 pixels of space inside all <div> elements, between
the content and the border.
This will add a 1-pixel solid black border around all <p> elements.
This will set the width of all <div> elements to 200 pixels and the
height to 100 pixels.
1. Content: The innermost area of the box where text or images are
displayed.
2. Padding: Space between the content and the border. Padding adds
internal spacing within the element, around its content.
3. Border: The line that surrounds the padding and content. Borders
can have various styles, widths, and colors, defining the outer edge
of the element.
Visual Representation:
Turn On Audio
1. Margin The margin property controls the space outside the border
of an element. You can set margins individually for each side or use
shorthand notation to set them all at once.
2. div {
3. margin-top: 20px;
4. margin-right: 15px;
5. margin-bottom: 20px;
6. margin-left: 15px;
Shorthand notation:
div {
8. div {
9. padding-top: 20px;
Shorthand notation:
div {
13. Border The border property allows you to define the style,
width, and color of the border around an element. You can set these
properties individually for each side or use shorthand notation.
14. div {
Shorthand notation:
div {
18. Width and Height The width and height properties control
the size of the content area of an element, excluding padding,
borders, and margins.
19. div {
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 250px;
height: 150px;
padding: 20px;
margin: 30px;
background-color: lightblue;
</style>
</head>
<body>
</body>
</html>
CSS Positioning
<style>
#mydiv {
position: static;
left: 10px;
</style>
<div>First Div</div>
<div>Third Div</div>
can use the top, right, bottom, and left properties to adjust its
position.
<style>
#mydiv {
position: relative;
top: 2px;
left: 30px;
</style>
<div>First Div</div>
<div>Third Div</div>
<style>
#mydiv {
position: absolute;
top: 50px;
left: 100px;
</style>
<div>First Div</div>
<div>Third Div</div>
<style>
#mydiv {
position: fixed;
top: 0;
right: 0;
</style>
The float property allows you to position elements to the left or right
within their container, while the clear property ensures proper
alignment of subsequent elements.
<style>
div {
float: right;
width: 50%;
</style>
In this example, the div element is floated to the right within its
container, taking up 50% of the container's width.
<style>
.left {
float: left;
width: 50%;
background-color: lightblue;
.right {
float: right;
width: 50%;
background-color: lightgreen;
.clear {
clear: right;
</style>
Here, the .clear class ensures that the paragraph appears below
both the left-floated and right-floated elements.
<style>
.flex-container {
display: flex;
/* or */
display: inline-flex;
</style>
<div class="flex-container">
</div>
In this example:
By default, flex items flow horizontally in a row. You can change this
direction using the flex-direction property to create a vertical
column layout if needed.
<style>
.flex-container {
display: flex;
</style>
<div class="flex-container">
</div>
Main Axis: This axis runs in the primary direction defined by flex-
direction. It determines how flex items are laid out horizontally (row)
or vertically (column).
Cross Axis: Perpendicular to the main axis, the cross axis allows for
additional alignment and spacing control of flex items.
Use align-items to align items along the cross axis (vertically for
rows, horizontally for columns).
<style>
.flex-container {
height: 200px;
display: flex;
</style>
<div class="flex-container">
</div>
Flexbox also supports wrapping of items onto new lines with flex-
wrap, allowing for responsive layouts that adjust automatically
based on available space.
Benefits of Flexbox:
<style>
.outer-container {
display: flex;
justify-content: space-between;
.inner-container {
display: flex;
flex-direction: column;
</style>
<div class="outer-container">
<div class="inner-container">
</div>
<div class="inner-container">
</div>
</div>
Flex items inherit properties from their parent container and can be
individually tailored for precise layout control:
<style>
.flex-container {
display: flex;
justify-content: space-around;
align-items: center;
background-color: #f0f0f0;
padding: 20px;
.flex-item {
margin: 10px;
padding: 20px;
background-color: #3498db;
color: white;
text-align: center;
.flex-item:nth-child(odd) {
background-color: #2ecc71;
</style>
<div class="flex-container">
</div>