CSS Fundamentals
CSS Fundamentals
tag { .class{
//code //code
} }
*{
//code #id{
} //code
// ‘*’ selects }
every element
Box Model
Content area
The content area, bounded by the content edge, contains the "real" content of the element, such as text, an image, or a video player.
Its dimensions are the content width (or content-box width) and the content height (or content-box height). It often has a background
colour or background image.
If the box-sizing property is set to content-box (default) and if the element is a block element, the content area's size can be
explicitly defined with the width, min-width , max-width , height, min-height , and max-height properties.
Padding area
The padding area, bounded by the padding edge, extends the content area to include the element's padding. Its dimensions are the
padding-box width and the padding-box height.
The thickness of the padding is determined by the padding-top , padding-right , padding-bottom , padding-left , and
shorthand padding properties.
Border area
The border area, bounded by the border edge, extends the padding area to include the element's borders. Its dimensions are the border-box width and the
border-box height.
The thickness of the borders are determined by the border-width and shorthand border properties. If the box-sizing property is set to border-box, the border
area's size can be explicitly defined with the width, min-width, max-width, height, min-height, and max-height properties. When there is a background (background-color
or background-image) set on a box, it extends to the outer edge of the border (i.e. extends underneath the border in z-ordering). This default behavior can be
altered with the background-clipCSS property.
Margin area
The margin area, bounded by the margin edge, extends the border area to include an empty area used to separate the element from its neighbors. Its
dimensions are the margin-box width and the margin-box height.
The size of the margin area is determined by the margin-top, margin-right, margin-bottom, margin-left, and shorthand margin properties. When margin collapsing
occurs, the margin area is not clearly defined since margins are shared between boxes.
Finally, note that for non-replaced inline elements, the amount of space taken up (the contribution to the height of the line) is determined by the line-height
property, even though the borders and padding are still displayed around the content.
Units
CSS has several different units that can be used to specify dimensions such as height and width. Here's a brief
overview of each unit:
1. Pixels (px): The pixel is the most common unit used for specifying dimensions in CSS. A pixel is a single dot on
a computer screen, and the number of pixels in a given area determines the resolution of the display. In CSS, the
pixel unit is fixed, which means that 1px is always equal to one physical pixel on the screen.
2. Rem: The rem unit stands for "root em," and is based on the font-size of the root element (usually the <html>
element). One rem is equal to the font-size of the root element. This means that if the font-size of the root
element is set to 16px(which is the default case), then 1rem is equal to 16px. You can change the root HTML
document's default size. It doesn't always have to be 16px. You can change it to any number you like, and that
will be the new value of rem. html{font-size : 10px;} //1rem = 10px
3. Percent (%): The percent unit is based on the size of the containing element. For example, if the width of a div is
set to 50%, it will take up half of the width of its parent element.
3. Viewport Width (vw): The viewport width unit is based on the width of the viewport, which is the
visible area of the browser window. One viewport width unit (1vw) is equal to 1% of the viewport width. For
example, if the viewport width is 1000px, then 1vw is equal to 10px.
4. Viewport Height (vh): The viewport height unit is based on the height of the viewport, which is the
visible area of the browser window. One viewport height unit (1vh) is equal to 1% of the viewport height. For
example, if the viewport height is 800px, then 1vh is equal to 8px.
Read More
Positioning
The position property specifies the type of positioning method used for an element.
● 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.
1. How to explain CSS
Positioning to a 5-year-old
If you add items to your backpack without arranging them, the items automatically position themselves inside
based on the space available. On a web page, the same thing is happening. When you add elements to a web page,
they will arrange themselves based on the rules defined by the normal flow.
There are two types of elements on a web page. Block-level elements and inline elements.
Block-level elements such as<h1> , <div> , <p> are contained on their own line. Because block-level elements start with
a line break, two block-level elements can't exist on the same line without styling.
Inline elements such as <a> , <img> , <span> don't form their own blocks. Instead, they are displayed within lines. They
line up next to one another horizontally; when there isn't enough space left on the line, the content moves to a new
line.
position: static; position: relative;
HTML elements are positioned static by default. An element with position: relative; is
positioned relative to its normal position.
Static positioned elements are not affected by the
top, bottom, left, and right properties. Setting the top, right, bottom, and left properties
of a relatively-positioned element will cause it to
An element with position: static; is not be adjusted away from its normal position. Other
positioned in any special way; it is always content will not be adjusted to fit into any gap left
positioned according to the normal flow of the by the element.
page:
This <div> element has position: relative;
This <div> element has position: static;
position: absolute;
An element with position: absolute; is
position: fixed; positioned relative to the nearest positioned
ancestor (instead of positioned relative to
An element with position: fixed; is the viewport, like fixed).
positioned relative to the viewport, which means
it always stays in the same place even if the However; if an absolute positioned element
page is scrolled. The top, right, bottom, and left has no positioned ancestors, it uses the
properties are used to position the element. document body, and moves along with page
scrolling.
A fixed element does not leave a gap in the page
where it would normally have been located.
Note: Absolute positioned elements are
Notice the fixed element in the lower-right removed from the normal flow, and can
corner of the page. Here is the CSS that is used: overlap elements.
z-index:
is a CSS property that controls the
vertical stacking order of HTML elements
Here are some basic typography-related CSS properties and their syntax:
line-height: Sets the vertical spacing between lines of text within an element.
Syntax: line-height: 1.5 ;
text-decoration: Sets the text decoration for an element (e.g., underline, strikethrough).
Syntax: text-decoration: underline ;
RGB: Uses red, green, and blue values to create a color. Each value ranges from 0 to 255.
Syntax: color: rgb(255, 0, 0) ;
RGBA: Same as RGB, but with an additional "alpha" channel for opacity. The alpha value ranges from 0 to 1.
Syntax: color: rgba(255, 0, 0, 0.5) ;
HSL: Uses hue, saturation, and lightness values to create a color. Hue is measured in degrees from 0 to 360,
saturation and lightness are measured as percentages from 0% to 100%.
Syntax: color: hsl(0, 100%, 50%) ;
HSLA: Same as HSL, but with an additional "alpha" channel for opacity. The alpha value ranges from 0 to 1.
Syntax: color: hsla(0, 100%, 50%, 0.5) ;
Box Sizing
In CSS, the box-sizing property allows you to define how the total width and
height of an element is calculated.
content-box: This is the default value. The width and height of the element
do not include padding, borders, or margins. In other words, the content box
is calculated independently of the border box and padding box.
border-box: The width and height of the element include padding and
borders, but not margins. In other words, the border box is calculated as a
sum of the content box, padding, and borders.
Margin
margin: Sets all margins of an element at once, in the order top, right, bottom, left.
Syntax: margin: 10px 20px 15px 30px;
block: This value creates a rectangular box that takes up the full width (100% width) of its parent container and pushes
down any elements that come after it in the document flow. Elements with display: block can have their width, height,
margin, and padding adjusted.
inline: This value creates an inline element that flows with the surrounding text and only takes up as much width as it
needs. Elements with display: inline can have their margin and padding adjusted, but not their width and height.
inline-block: This value is similar to inline, but it allows elements to have their width, height, margin, and padding
adjusted. Elements with display: inline-block take up only as much width as they need and don't push down any
elements that come after them in the document flow.
flex: This value enables a flexible box layout. The children of an element with display: flex are laid out in a row or
column, and can be flexed to adjust their width or height.
grid: This value enables a grid layout. The children of an element with display: grid are laid out in a grid, with columns
and rows that can be defined and manipulated. It is probably the most trickiest layout to use.
none: This value hides an element from the page entirely. The element's space is collapsed, and it does not affect the
layout or flow of other elements on the page.
Display block, inline and inline-block
Links: CSS Trick; Youtube 1 MDN
display: flex;
LINKS: CSS Tricks W3 Schools MDN Youtube 1 Youtube 2
display: grid;
Responsiveness & Media Queries