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

The CSS Box Model: Explanation of The Different Parts: Content - The Content of The Box, Where Text and Images Appear

The document discusses the CSS box model which describes how HTML elements are represented as boxes. It explains that each element is wrapped in a box that includes margins, borders, padding, and content. It also defines common CSS positioning properties like static, relative, absolute, and fixed that position elements on a page.

Uploaded by

Harshit singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

The CSS Box Model: Explanation of The Different Parts: Content - The Content of The Box, Where Text and Images Appear

The document discusses the CSS box model which describes how HTML elements are represented as boxes. It explains that each element is wrapped in a box that includes margins, borders, padding, and content. It also defines common CSS positioning properties like static, relative, absolute, and fixed that position elements on a page.

Uploaded by

Harshit singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

The CSS Box Model

All HTML elements can be considered as boxes. 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

The box model allows us to add a border around elements, and to


define space between elements. 

CLASS
The .class selector selects elements with a specific
class attribute.
To select elements with a specific class, write a
period (.) character, followed by the name of the
class.

You can also specify that only specific HTML


elements should be affected by a class. To do this,
start with the element name, then write the period
(.) character, followed by the name of the class
(look at Example 1 below).

DIV
The <div> tag defines a division or a section in an HTML
document.

The <div> element is often used as a container for other


HTML elements to style them with CSS or to perform
certain tasks with JavaScript.
Block-level Elements

A block-level element always starts on a new line


and takes up the full width available (stretches out
to the left and right as far as it can).

The <div> element is a block-level element.

Inline Elements

An inline element does not start on a new line and


only takes up as much width as necessary.
This is  an inline <span> element inside  a
paragraph.

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: block, 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.

The following example shows the different behavior


of display: inline, display: inline-
block and display: block:
CSS Layout - The position Property

❮ PreviousNext ❯

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
 relative
 fixed
 absolute
 sticky

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:

This <div> element has position: static;


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.

This <div> element has position: relative;

Here is the CSS that is used:


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.

A fixed element does not leave a gap in the page


where it would normally have been located.

Notice the fixed element in the lower-right corner


of the page. Here is the CSS that is used:

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.

Here is a simple example:

This <div> element has position: relative;


This <div> element has position: absolute;

Here is the CSS that is used:


All CSS Positioning Properties

Property Description

bottom Sets the bottom margin edge for a positioned box

clip Clips an absolutely positioned element

left Sets the left margin edge for a positioned box

position Specifies the type of positioning for an element

right Sets the right margin edge for a positioned box

top Sets the top margin edge for a positioned box

z-index Sets the stack order of an element

You might also like