0% found this document useful (0 votes)
9 views

Cascading Style Sheet Lecture 4

The document explains the CSS position property, detailing five positioning values: static, relative, fixed, absolute, and sticky, along with their behaviors. It also covers the z-index property for overlapping elements, the overflow property for handling content that exceeds its container, and the float property for positioning elements. Lastly, it introduces CSS combinators, including descendant, child, adjacent sibling, and general sibling selectors, with examples for each.

Uploaded by

caretronics77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Cascading Style Sheet Lecture 4

The document explains the CSS position property, detailing five positioning values: static, relative, fixed, absolute, and sticky, along with their behaviors. It also covers the z-index property for overlapping elements, the overflow property for handling content that exceeds its container, and the float property for positioning elements. Lastly, it introduces CSS combinators, including descendant, child, adjacent sibling, and general sibling selectors, with examples for each.

Uploaded by

caretronics77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

LECTURE 4

THE POSITION PROPERTY


THE 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
THE POSITION PROPERTY

Elements are then positioned using the top, bottom, left, and right
properties. However, these properties will not work unless the position
property is set first. They also work differently depending on the position
value.

position: static;
 HTML elements are positioned static by default.
 Static positioned elements are not affected by the top, bottom, left,
and right properties.
 An element with position: static; is not positioned in any special way; it
is always positioned according to the normal flow of the page:
THE POSITION PROPERTY

position: relative;
 An element with position: relative; 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;
 An element with position: fixed; 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.
THE POSITION PROPERTY

 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.
 Note: A "positioned" element is one whose position is anything except
static
THE POSITION PROPERTY

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).
OVERLAPPING
OVERLAPPING

 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:
OVERLAPPING

 An element with greater stack order is always in front of an element


with a lower stack order.
Note: If two positioned elements overlap without a z-index specified, the
element positioned last in the HTML code will be shown on top.
img {
position: absolute;
left: 0px;
top: 0px;
z-index: -1;
}
OVERFLOW
OVERLAPPING

CSS Overflow
 The CSS overflow property controls what happens to content that is too
big to fit into an area.
 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.
The 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
OVERLAPPING

 scroll The overflow is clipped, and a scrollbar is added to see the rest of
the content
 Auto Similar to scroll, but it adds scrollbars only when necessary
FLOAT
FLOAT

The 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). This is default
 inherit - The element inherits the float value of its parent
INLINE-BLOCK
INLINE-BLOCK

The display: inline-block Value


 Compared to display: inline the major difference is that display: inline-
block allows to set a width and height on the element.
 Also, with display: inline-block the top and bottom margins/paddings
are respected, but with display: inline they are not.
 Compared to display: inline the major difference is that display: inline-
block does not add a line-break after the element, so the element can
sit next to other elements.
COMBINATORS
COMBINATORS

CSS Combinators
 A CSS selector can contain more than one simple selector. Between the
simple selectors, we can include a combinator.
There are four different combinators in CSS:
 descendant selector (space)
 child selector (>)
 adjacent sibling selector (+)
 general sibling selector (~)
COMBINATORS

Descendant Selector
 The descendant selector matches all elements that are descendants of
a specified element.
 The following example selects all <p> elements inside <div> elements:
div p {
background-color: yellow;
}
COMBINATORS

Child Selector
 The child selector selects all elements that are the children of a
specified element.
 The following example selects all <p> elements that are children of a
<div> element:
div > p {
background-color: yellow;
}
COMBINATORS

Adjacent Sibling Selector


 The adjacent sibling selector selects all elements that are the adjacent
siblings of a specified element..
 Sibling elements must have the same parent element, and "adjacent"
means "immediately following".
 The following example selects all <p> elements that are placed
immediately after <div> elements:
div + p {
background-color: yellow;
}
COMBINATORS

General Sibling Selector


 The general sibling selector selects all elements that are siblings of a
specified element.
 The following example selects all <p> elements that are siblings of
<div> elements:
div ~ p {
background-color: yellow;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy