Box Sizing
- The CSS box-sizing property allows us to include the padding and border in an element's total width and height.
-
Without the CSS box-sizing Property
By default, the width and height of an element is calculated like this:- width + padding + border = actual width of an element
- height + padding + border = actual height of an element
The box-sizing property solves this problem. -
The box-sizing property defines how the width and height of an element are calculated: should they include padding and borders, or not.
Syntax : box-sizing: content-box|border-box|initial|inherit;
Value Description content-box Default. The width and height properties (and min/max properties) includes only the content. Border and padding are not included border-box The width and height properties (and min/max properties) includes content, padding and border initial Sets this property to its default value. inherit Inherits this property from its parent element. -
EX:
This div occupies the left halfThis div occupies the right half
Try this code once, and remove box-sizing: border-box; and try again to see the difference.
CSS Shadow Effects
-
With CSS you can add shadow to text and to elements with following properties.
text-shadowbox-shadow
CSS Box Shadow
- The CSS box-shadow property applies shadow to elements.
-
Syntax : box-shadow: none|h-offset v-offset blur spread color |inset|initial|inherit;
Value Description none Default value. No shadow is displayed h-offset Required. The horizontal offset of the shadow. A positive value puts the shadow on the right side of the box, a negative value puts the shadow on the left side of the box v-offset Required. The vertical offset of the shadow. A positive value puts the shadow below the box, a negative value puts the shadow above the box blur Optional. The blur radius. The higher the number, the more blurred the shadow will be spread Optional. The spread radius. A positive value increases the size of the shadow, a negative value decreases the size of the shadow color Optional. The color of the shadow. The default value is the text color. inset Optional. Changes the shadow from an outer shadow (outset) to an inner shadow initial Sets this property to its default value. inherit Inherits this property from its parent element. -
Examples:
/* shadow with blur effect */ #example1 { box-shadow: 10px 10px 8px #888888; } /* Define the spread radius of the shadow */ #example2 { box-shadow: 10px 10px 8px 10px #888888; } /* Define multiple shadows */ #example3 { box-shadow: 5px 5px blue, 10px 10px red, 15px 15px green; }
CSS Display
- The display property is the most important CSS property for controlling layout.
- The display property specifies if/how an element is displayed.
- Every HTML element has a default display value depending on what type of element it is. The default display value for most elements is block or inline.
-
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).
Examples of block-level elements:<div><h1> - <h6><p><form><header><footer><section>
span { display: block; } -
Inline Elements
An inline element does not start on a new line and only takes up as much width as necessary.
Examples of inline elements:<span><a><img>
li { display: inline; } -
Display: none;
Hiding an element can be done by setting the display property to none. The element will be hidden, and the page will be displayed as if the element is not there.
visibility:hidden; also hides an element.
EX:
However, the element will still take up the same space as before. The element will be hidden, but still affect the layout.li { display: none; } h1 { visibility: hidden; } -
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.
-
EX:
display: inline
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam venenatis gravida nisl sit amet facilisis.display: inline-block
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam venenatis gravida nisl sit amet facilisis.display: block
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam venenatis gravida nisl sit amet facilisis.
CSS Position
- The position property specifies the type of positioning method used for an element.
-
There are five different position values:
staticrelativefixedabsolutesticky
- 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.
-
EX:
div.static { position: static; border: 3px solid #73AD21; }
-
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.
-
EX:
div.relative { position: relative; left: 30px; top:20px; } -
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.
-
EX:
div.fixed { position: fixed; bottom: 0; right: 0; width: 300px; }
-
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.
This div element has position: relative;This div element has position: absolute; -
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).
-
EX:
div.sticky { position: -webkit-sticky; /* Safari */ position: sticky; top: 0; background-color: #149ddd; }
-
CSS z-index
- The z-index property specifies the stack order of an element.
- An element with greater stack order is always in front of an element with a lower stack order.
- z-index only works on positioned elements (position: absolute, position: relative, position: fixed, or position: sticky) and flex items (elements that are direct children of display:flex elements).
-
Syntax : z-index: auto|number|initial|inherit;
Value Description auto Sets the stack order equal to its parents. This is default number Sets the stack order of the element. Negative numbers are allowed initial Sets this property to its default value. inherit Inherits this property from its parent element. -
EX:
img { position: absolute; left: 0px; top: 0px; z-index: -1; }
-
All CSS Positioning Properties
Property Description bottom Sets the bottom margin edge for a positioned box 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
CSS float and clear
- The CSS float property specifies how an element should float.
- The CSS clear property specifies what elements can float beside the cleared element and on which side.
-
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
-
-
EX:
Div 1Div 2Div 3
Clear Property
- When we use the float property, and we want the next element below (not on right or left), we will have to use the clear property.
- The clear property specifies what should happen with the element that is next to a floating element.
-
The clear property can have one of the following values :
-
none- The element is not pushed below left or right floated elements. This is default -
left- The element is pushed below left floated elements -
right- The element is pushed below right floated elements -
both- The element is pushed below both left and right floated elements -
inherit- The element inherits the clear value from its parent
-
- When clearing floats, you should match the clear to the float: If an element is floated to the left, then you should clear to the left.
-
EX:
Without clear
div1div2 - Notice that div2 is after div1 in the HTML code. However, since div1 floats to the left, the text in div2 flows around div1.
With clear
div3div4 - Here, clear: left; moves div4 down below the floating div3. The value "left" clears elements floated to the left. You can also clear "right" and "both".
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 boxhidden- The overflow is clipped, and the rest of the content will be invisiblescroll- The overflow is clipped, and a scrollbar is added to see the rest of the contentauto- Similar toscroll, but it adds scrollbars only when necessary
-
overflow values
Value Description Example overflow: visible By default, the overflow is visible, meaning that it is not clipped and it renders outside the element's box. div { width: 200px; height: 50px; background-color: #eee; overflow: visible; }overflow: hidden With the hidden value, the overflow is clipped, and the rest of the content is hidden. div { overflow: hidden; }overflow: scroll Setting the value to scroll, the overflow is clipped and a scrollbar is added to scroll inside the box. Note that this will add a scrollbar both horizontally and vertically (even if you do not need it). div { overflow: scroll; }overflow: auto The auto value is similar to scroll, but it adds scrollbars only when necessary. div { overflow: auto; }overflow-x
overflow-yThe overflow-x and overflow-y properties specifies whether to change the overflow of content just horizontally or vertically (or both). - overflow-x specifies what to do with the left/right edges of the content.
- overflow-y specifies what to do with the top/bottom edges of the content.
div { /* Hide horizontal scrollbar */ overflow-x: hidden; /* Add vertical scrollbar */ overflow-y: scroll; }
CSS Flexbox
-
Before the Flexbox Layout module, there were four layout modes:
- Block, for sections in a webpage
- Inline, for text
- Table, for two-dimensional table data
- Positioned, for explicit position of an element
-
EX :
123456
Try resizing the browser window.
-
CSS Flex Container
The flex container becomes flexible by setting the display property to flex
EX :The flex container properties are:.flex-container { display: flex; }flex-directionflex-wrapflex-flowjustify-contentalign-itemsalign-content
Property Description Example flex-direction The flex-directionproperty defines in which direction the container wants to stack the flex items.- The
columnvalue stacks the flex items vertically (from top to bottom). - The
column-reversevalue stacks the flex items vertically (but from bottom to top). - The
rowvalue stacks the flex items horizontally (from left to right). - The
row-reversevalue stacks the flex items horizontally (but from right to left).
.flex-container { display: flex; flex-direction: column; }flex-wrap The flex-wrapproperty specifies whether the flex items should wrap or not.- The
wrapvalue specifies that the flex items will wrap if necessary. - The
nowrapvalue specifies that the flex items will not wrap (this is default). - The
wrap-reversevalue specifies that the flexible items will wrap if necessary, in reverse order.
.flex-container { display: flex; flex-wrap: wrap-reverse; }flex-flow The flex-flowproperty is a shorthand property for setting both theflex-directionandflex-wrapproperties..flex-container { display: flex; flex-flow: row wrap; }justify-content The justify-contentproperty is used to align the flex items.- The
flex-startvalue aligns the flex items at the beginning of the container (this is default). - The
flex-endvalue aligns the flex items at the end of the container. - The
space-aroundvalue displays the flex items with space before, between, and after the lines. - The
space-betweenvalue displays the flex items with space between the lines.
.flex-container { display: flex; justify-content: space-between; }align-items The align-items property is used to align the flex items. - The
centervalue aligns the flex items in the middle of the container. - The
flex-startvalue aligns the flex items at the top of the container. - The
flex-endvalue aligns the flex items at the bottom of the container. - The
stretchvalue stretches the flex items to fill the container (this is default). - The
baselinevalue aligns the flex items such as their baselines aligns.
.flex-container { display: flex; height: 200px; align-items: center; }align-content The align-contentproperty is used to align the flex lines.- The
space-betweenvalue displays the flex lines with equal space between them. - The
space-aroundvalue displays the flex lines with space before, between, and after them. - The
stretchvalue stretches the flex lines to take up the remaining space (this is default). - The
centervalue displays display the flex lines in the middle of the container. - The
flex-startvalue displays the flex lines at the start of the container. - The
flex-endvalue displays the flex lines at the end of the container.
.flex-container { display: flex; height: 600px; flex-wrap: wrap; align-content: center; } -
CSS Flex Items
The direct child elements of a flex container automatically becomes flexible (flex) items.
EX:1234
The flex item properties are:orderflex-growflex-shrinkflex-basisflexalign-self
Property Description Example align-self Specifies the alignment for a flex item (overrides the flex container's align-items property).
It has three values (center, flex-start, flex-end).div{ align-self: flex-end }flex A shorthand property for the flex-grow,flex-shrink, and theflex-basisproperties.div{ flex: 0 0 200px }Make the third flex item not growable (0), not shrinkable (0), and with an initial length of 200 pixels
flex-basis Specifies the initial length of a flex item. div{ flex-basis: 200px }flex-grow Specifies how much a flex item will grow relative to the rest of the flex items inside the same container.
The value must be a number, default value is 0.div{ flex-grow: 1 }flex-shrink Specifies how much a flex item will shrink relative to the rest of the flex items inside the same container.
The value must be a number, default value is 1.div{ flex-shrink: 0 }order Specifies the order of the flex items inside the same container.
The order value must be a number, default value is 0.div{ order: 3 }
CSS Media Queries
- The @media rule, introduced in CSS2, made it possible to define different style rules for different media types.
- Examples: You could have one set of style rules for computer screens, one for printers, one for handheld devices, one for television-type devices, and so on.
- Media queries in CSS3 extended the CSS2 media types idea: Instead of looking for a type of device, they look at the capability of the device.
-
Media queries can be used to check many things, such as :
- width and height of the viewport
- width and height of the device
- orientation (is the tablet/phone in landscape or portrait mode?)
- resolution
- Using media queries are a popular technique for delivering a tailored style sheet to desktops, laptops, tablets, and mobile phones (such as iPhone and Android phones).
-
Media Query Syntax
@media not|only mediatype and (expressions) { CSS-Code; }- The result of the query is true if the specified media type matches the type of device the document is being displayed on and all expressions in the media query are true. When a media query is true, the corresponding style sheet or style rules are applied, following the normal cascading rules.
- Unless you use the not or only operators, the media type is optional and the all type will be implied.
-
CSS Media Types
Value Description all Used for all media type devices print Used for printers screen Used for computer screens, tablets, smart-phones etc. speech Used for screenreaders that "reads" the page out loud -
Media Queries Examples
@media screen and (min-width: 480px) { #leftsidebar {width: 200px; float: left;} #main {margin-left: 216px;} }
CSS Combinators
- A combinator is something that explains the relationship between the selectors.
- 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 in CSS
Selector Description Example Descendant Selector (space) The descendant selector matches all elements that are descendants of a specified element. div p { background-color: yellow; }The following example selects all <p> elements inside <div> elements.
Child Selector (>) The child selector selects all elements that are the children of a specified element. div > p { background-color: yellow; }The following example selects all <p> elements that are children of a <div> element.
Adjacent Sibling Selector (+) The adjacent sibling selector is used to select an element that is directly after another specific element.
Sibling elements must have the same parent element, and "adjacent" means "immediately following".div + p { background-color: yellow; }The following example selects the first <p> element that are placed immediately after <div> elements.
General Sibling Selector (~) The general sibling selector selects all elements that are next siblings of a specified element. div ~ p { background-color: yellow; }The following example selects all <p> elements that are next siblings of <div> elements.
CSS Attribute Selectors
It is possible to style HTML elements that have specific attributes or attribute values.
| Attribute Selectors | Description | Example |
|---|---|---|
| CSS [attribute] Selector | The [attribute] selector is used to select elements with a specified attribute. |
The following example selects all elements with a target attribute:
|
| CSS [attribute="value"] Selector | The [attribute="value"] selector is used to select elements with a specified attribute and value. |
The following example selects all elements with a target="_blank" attribute:
|
| CSS [attribute~="value"] Selector | The [attribute~="value"] selector is used to select elements with an attribute value containing a specified word. |
The following example selects all elements with a title attribute that contains a space-separated list of words, one of which is "flower":
|
| CSS [attribute|="value"] Selector | The [attribute|="value"] selector is used to select elements with the specified attribute starting with the specified value. |
The following example selects all elements with a class attribute value that begins with "top":
|
| CSS [attribute^="value"] Selector | The [attribute^="value"] selector is used to select elements whose attribute value begins with a specified value. |
The following example selects all elements with a class attribute value that begins with "top":
|
| CSS [attribute$="value"] Selector | The [attribute$="value"] selector is used to select elements whose attribute value ends with a specified value. |
The following example selects all elements with a class attribute value that ends with "test":
|
| CSS [attribute*="value"] Selector | The [attribute*="value"] selector is used to select elements whose attribute value contains a specified value. |
The following example selects all elements with a class attribute value that contains "te":
|
Example:
CSS Pseudo-classes
- A pseudo-class is used to define a special state of an element.
-
Syntax : The syntax of pseudo-classes:
selector:pseudo-class { property: value; } -
EX:
Hover over me to show the p element
Here I am!
-
All CSS Pseudo Classes
Selector Example Example description :active a:active Selects the active link :checked input:checked Selects every checked <input> element :disabled input:disabled Selects every disabled <input> element :empty p:empty Selects every <p> element that has no children :enabled input:enabled Selects every enabled <input> element :first-child p:first-child Selects every <p> elements that is the first child of its parent :first-of-type p:first-of-type Selects every <p> element that is the first <p> element of its parent :focus input:focus Selects the <input> element that has focus :hover a:hover Selects links on mouse over :in-range input:in-range Selects <input> elements with a value within a specified range :invalid input:invalid Selects all <input> elements with an invalid value :lang(language) p:lang(it) Selects every <p> element with a lang attribute value starting with "it" :last-child p:last-child Selects every <p> elements that is the last child of its parent :last-of-type p:last-of-type Selects every <p> element that is the last <p> element of its parent :link a:link Selects all unvisited links :not(selector) :not(p) Selects every element that is not a <p> element :nth-child(n) p:nth-child(2) Selects every <p> element that is the second child of its parent :nth-last-child(n) p:nth-last-child(2) Selects every <p> element that is the second child of its parent, counting from the last child :nth-last-of-type(n) p:nth-last-of-type(2) Selects every <p> element that is the second <p> element of its parent, counting from the last child :nth-of-type(n) p:nth-of-type(2) Selects every <p> element that is the second <p> element of its parent :only-of-type p:only-of-type Selects every <p> element that is the only <p> element of its parent :only-child p:only-child Selects every <p> element that is the only child of its parent :optional input:optional Selects <input> elements with no "required" attribute :out-of-range input:out-of-range Selects <input> elements with a value outside a specified range :read-only input:read-only Selects <input> elements with a "readonly" attribute specified :read-write input:read-write Selects <input> elements with no "readonly" attribute :required input:required Selects <input> elements with a "required" attribute specified :root root Selects the document's root element :target #news:target Selects the current active #news element (clicked on a URL containing that anchor name) :valid input:valid Selects all <input> elements with a valid value :visited a:visited Selects all visited links
CSS Pseudo-elements
- A CSS pseudo-element is used to style specified parts of an element.
-
Syntax : The syntax of pseudo-elements:
selector::pseudo-element { property: value; } -
EX:
This is a heading
-
All CSS Pseudo Elements
Selector Example Example description ::after p::after Insert something after the content of each <p> element ::before p::before Insert something before the content of each <p> element ::first-letter p::first-letter Selects the first letter of each <p> element ::first-line p::first-line Selects the first line of each <p> element ::marker ::marker Selects the markers of list items ::selection p::selection Selects the portion of an element that is selected by a user
CSS Links
- Links can be styled with any CSS property (e.g. color, font-family, background, etc.).
-
The four links states are:
a:link- a normal, unvisited linka:visited- a link the user has visiteda:hover- a link when the user mouses over ita:active- a link the moment it is clicked
-
Examples:
a:link { text-decoration: none; } a:visited { text-decoration: none; background-color: cyan; } a:hover { text-decoration: underline; color: #0000ff; background-color: cyan; cursor: pointer } a:active { text-decoration: underline; background-color: cyan; cursor: progress } -
Cursor Values :
cursor: autocursor: crosshaircursor: defaultcursor: e-resizecursor: helpcursor: movecursor: n-resizecursor: ne-resizecursor: nw-resizecursor: pointercursor: progresscursor: s-resizecursor: se-resizecursor: sw-resizecursor: textcursor: w-resizecursor: wait