CSSNotesForProfessionals 201 244
CSSNotesForProfessionals 201 244
Code
<div id="multi-columns">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
laborum</div>
.multi-columns {
-moz-column-count: 2;
-webkit-column-count: 2;
column-count: 2;
}
Result
Code:
<div id="multi-columns">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit anim id est laborum
.multi-columns {
-moz-column-width: 100px;
-webkit-column-width: 100px;
column-width: 100px;
}
Result
CSS
.content {
-webkit-column-count: 3; /* Chrome, Safari, Opera */
-moz-column-count: 3; /* Firefox */
column-count: 3;
}
<section>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et
justo duo dolores et ea rebum.</p>
<p> Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem
ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore
et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea
rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et
justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum
dolor sit amet.</p>
</section>
With the following CSS applied the content is split into three columns separated by a gray column rule of two pixels.
section {
columns: 3;
column-gap: 40px;
column-rule: 2px solid gray;
}
HTML
<nav>
<ul>
<li>abc</li>
<li>abcdefghijkl</li>
<li>abcdef</li>
</ul>
</nav>
CSS
nav {
width: 100%;
line-height: 1.4em;
}
ul {
list-style: none;
display: block;
width: 100%;
margin: 0;
padding: 0;
text-align: justify;
margin-bottom: -1.4em;
}
ul:after {
content: "";
display: inline-block;
width: 100%;
}
li {
display: inline-block;
}
Notes
The nav, ul and li tags were chosen for their semantic meaning of 'a list of navigation (menu) items'. Other
tags may also be used of course.
The :after pseudo-element causes an extra 'line' in the ul and thus an extra, empty height of this block,
pushing other content down. This is solved by the negative margin-bottom, which has to have the same
magnitude as the line-height (but negative).
If the page becomes too narrow for all the items to fit, the items will break to a new line (starting from the
right) and be justified on this line. The total height of the menu will grow as needed.
Common properties that are automatically inherited are: font, color, text-align, line-height.
#myContainer {
color: red;
padding: 5px;
}
This will apply color: red not only to the <div> element but also to the <h3> and <p> elements. However, due to
the nature of padding its value will not be inherited to those elements.
<div id="myContainer">
<h3>Some header</h3>
<p>Some paragraph</p>
</div>
However, sometimes inheritance is desired anyway. To achieve this, we can apply the inherit value to the property
that should be inherited. The inherit value can be appied to any CSS property and any HTML element.
#myContainer {
color: red;
padding: 5px;
}
#myContainer p {
padding: inherit;
}
This will apply color: red to both the <h3> and <p> elements due to the inheritance nature of the color property.
However, the <p> element will also inherit the padding value from its' parent because this was specified.
<div id="myContainer">
<h3>Some header</h3>
<p>Some paragraph</p>
</div>
An image sprite is a single asset located within an image sprite sheet. An image sprite sheet is an image file that
contains more than one asset that can be extracted from it.
For example:
The image above is an image sprite sheet, and each one of those stars is a sprite within the sprite sheet. These
sprite sheets are useful because they improve performance by reducing the number of HTTP requests a browser
might have to make.
HTML
CSS
.icon {
background: url(“icons-sprite.png”);
display: inline-block;
height: 20px;
width: 20px;
}
.icon1 {
background-position: 0px 0px;
}
.icon2 {
background-position: -20px 0px;
}
.icon3 {
background-position: -40px 0px;
}
By using setting the sprite's width and height and by using the background-position property in CSS (with an x and y
value) you can easily extract sprites from a sprite sheet using CSS.
Clipping
Clips are vector paths. Outside of this path the element will be transparent, inside it's opaque. Therefore you can
define a clip-path property on elements. Every graphical element that also exists in SVG you can use here as a
function to define the path. Examples are circle(), polygon() or ellipse().
The element will be only visible inside of this circle, which is positioned at the center of the element and has a
radius of 100px.
Masking
Masks are similar to Clips, but instead of defining a path you define a mask what layers over the element. You can
imagine this mask as an image what consist of mainly two colors: black and white.
Luminance Mask: Black means the region is opaque, and white that it's transparent, but there is also a grey area
which is semi-transparent, so you are able to make smooth transitions.
Alpha Mask: Only on the transparent areas of the mask the element will be opaque.
This image for example can be used as a luminance mask to make for an element a very smooth transition from
right to left and from opaque to transparent.
The mask property let you specify the the mask type and an image to be used as layer.
Example
mask: url(masks.svg#rectangle) luminance;
An element called rectangle defined in masks.svg will be used as an luminance mask on the element.
HTML
<div></div>
In the above example there is an element with an image as its background. The mask that is applied on the image
(using CSS) makes it look as though it is fading out from left to right.
The masking is achieved by using a linear-gradient that goes from white (on the left) to transparent (on the right)
as the mask. As it is an alpha mask, image becomes transparent where the mask is transparent.
Note: As mentioned in remarks, the above example would work in Chrome, Safari and Opera only when used with
the -webkit prefix. This example (with a linear-gradient as mask image) is not yet supported in Firefox.
HTML
<div></div>
This example shows how to clip a div to a circle. The element is clipped into a circle whose radius is 30% based on
the dimensions of the reference box with its center point at the center of the reference box. Here since no <clip-
geometry-box> (in other words, reference box) is provided, the border-box of the element will be used as the
reference box.
The circle shape needs to have a radius and a center with (x,y) coordinates:
circle(radius at x y)
View Example
Output:
HTML:
<div></div>
In the above example, a polygonal clipping path is used to clip the square (200 x 200) element into a triangle shape.
The output shape is a triangle because the path starts at (that is, first coordinates are at) 0 0 - which is the top-left
corner of the box, then goes to 0 100% - which is bottom-left corner of the box and then finally to 100% 50% which is
nothing but the right-middle point of the box. These paths are self closing (that is, the starting point will be the
ending point) and so the final shape is that of a triangle.
View Example
HTML
In the above example, a transparent circle is created at the center using radial-gradient and this is then used as a
mask to produce the effect of a circle being cut out from the center of an image.
HTML
<div></div>
In the above example, three linear-gradient images (which when placed in their appropriate positions would
cover 100% x 100% of the container's size) are used as masks to produce a transparent triangular shaped cut at the
bottom of the image.
it prevents a page break inside any p tags, meaning a paragraph will never be broken in two pages, if
possible.
it forces a page-break-before in all h1 headings, meaning that before every h1 occurrence, there will be a
page break.
it prevents page-breaks right after any h2
Finally, insert a background-image rule for the body element at the end of the stylesheet:
stylesheet.insertRule("body { background-image:
url('http://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico'); }", end);
1. The web browser examines your HTML and builds the DOM (Document Object Model).
2. The web browser examines your CSS and builds the CSSOM (CSS Object Model).
3. The web browser combines the DOM and the CSSOM to create a render tree. The web browser displays your
webpage.
In terms of syntax, @supports is very similar to @media, but instead of detecting screen size and orientation,
@supports will detect whether the browser can handle a given CSS rule.
Rather than doing something like @supports (flex), notice that the rule is @supports (display: flex).
For the ultimate @supports experience, try grouping logical expressions with parenthesis:
@supports ((display: block) and (zoom: 1)) or ((display: flex) and (not (display: table-cell))) or
(transform: translateX(1px)) {
/* ... */
}
Root
DIV #1
DIV #2
DIV #3
DIV #4
DIV #5
DIV #6
It is important to note that DIV #4, DIV #5 and DIV #6 are children of DIV #3, so stacking of those elements is
completely resolved within DIV#3. Once stacking and rendering within DIV #3 is completed, the whole DIV #3
element is passed for stacking in the root element with respect to its sibling's DIV.
HTML:
<div id="div1">
<h1>Division Element #1</h1>
<code>position: relative;<br/>
z-index: 5;</code>
</div>
<div id="div2">
<h1>Division Element #2</h1>
<code>position: relative;<br/>
z-index: 2;</code>
</div>
<div id="div3">
<div id="div4">
CSS:
* {
margin: 0;
}
html {
padding: 20px;
font: 12px/20px Arial, sans-serif;
}
div {
opacity: 0.7;
position: relative;
}
h1 {
font: inherit;
font-weight: bold;
}
#div1,
#div2 {
border: 1px dashed #696;
padding: 10px;
background-color: #cfc;
}
#div1 {
z-index: 5;
margin-bottom: 190px;
}
#div2 {
z-index: 2;
}
#div3 {
z-index: 4;
opacity: 1;
position: absolute;
top: 40px;
left: 180px;
width: 330px;
border: 1px dashed #900;
background-color: #fdd;
padding: 40px 20px 20px;
}
#div4,
Result:
Source:
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context.
https://jsfiddle.net/MadalinaTn/qkwwmu6m/2/
Using the overflow property with a value different to visible (its default) will create a new block formatting
context. This is technically necessary — if a float intersected with the scrolling element it would forcibly
rewrap the content.
This example that show how a number of paragraphs will interact with a floated image is similar to this example, on
css-tricks.com.
2: https://developer.mozilla.org/en-US/docs/Web/CSS/overflow MDN
<div class="wrapper">
<div class="outer">
<div class="inner">
centered
</div>
</div>
</div>
CSS:
.wrapper {
height: 600px;
text-align: center;
}
.outer {
display: table;
height: 100%;
width: 100%;
}
.outer .inner {
display: table-cell;
text-align: center;
vertical-align: middle;
}
<div class="container">
<div class="child"></div>
</div>
CSS:
.container {
height: 500px;
width: 500px;
display: flex; // Use Flexbox
align-items: center; // This centers children vertically in the parent.
justify-content: center; // This centers children horizontally.
background: white;
}
.child {
width: 100px;
height: 100px;
background: blue;
}
<div class="wrapper">
<div class="centered">
centered
</div>
</div>
CSS:
.wrapper {
position: relative;
height: 600px;
}
.centered {
position: absolute;
z-index: 999;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
}
<div class="container">
<span>vertically centered</span>
</div>
CSS:
.container{
height: 50px; /* set height */
line-height: 50px; /* set line-height equal to the height */
vertical-align: middle; /* works without this rule, but it is good having it explicitly set */
}
Note: This method will only vertically center a single line of text. It will not center block elements correctly and if the
text breaks onto a new line, you will have two very tall lines of text.
<div class="wrapper">
<img
src="http://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a">
</div>
CSS:
.wrapper{
position:relative;
If you want to center other then images, then you must give height and width to that element.
HTML:
<div class="wrapper">
<div class="child">
make me center
</div>
</div>
CSS:
.wrapper{
position:relative;
height: 600px;
}
.wrapper .child {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
width: 200px;
height: 30px;
border: 1px solid #f00;
}
<div class="wrapper">
<div class="content"></div>
</div>
CSS:
.wrapper{
min-height: 600px;
}
.wrapper:before{
content: "";
display: inline-block;
height: 100%;
vertical-align: middle;
}
This method is best used in cases where you have a varied-height .content centered inside .wrapper; and you
want .wrapper's height to expand when .content's height exceed .wrapper's min-height.
FILL
object-fit:fill;
Fill stretches the image to fit the content box without regard to the image's original aspect ratio.
CONTAIN
object-fit:contain;
Contain fits the image in the box's height or width while maintaining the image's aspect ratio.
COVER
object-fit:cover;
NONE
object-fit:none;
SCALE-DOWN
object-fit:scale-down;
Scale-down either sizes the object as none or as contain. It displays whichever option results in a smaller image
size.
These examples are NOT for documenting CSS frameworks like Bootstrap or Foundation.
As the same implies, BEM metholology is all about componentization of your HTML and CSS code into three types
of components:
Elements: Part of blocks that have no standalone meaning and are semantically tied to their blocks.
Examples are menu item, list item, checkbox caption & header title
Examples are disabled, highlighted, checked, fixed, size big & color yellow
The goal of BEM is to keep optimize the readability, maintainability and flexibility of your CSS code. The way to
achieve this, is to apply the following rules.
Code example
If you apply BEM to your form elements, your CSS selectors should look something like this:
.form { } // Block
.form--theme-xmas { } // Block + modifier
.form--simple { } // Block + modifier
.form__input { } // Block > element
.form__submit { } // Block > element
.form__submit--disabled { } // Block > element + modifier
-khtml- Konquerer
Because these default styles are given some leeway by the language specifications and because browsers may not
follow the specs properly they can differ from browser to browser.
This is where normalize.css comes into play. It overrides the most common inconsistencies and fixes known bugs.
What does it do
So, by including normalize.css in your project your design will look more alike and consistent across different
browsers.
Difference to reset.css
You may have heard of reset.css. What's the difference between the two?
While normalize.css provides consistency by setting different properties to unified defaults, reset.css achieves
consistency by removing all basic styling that a browser may apply. While this might sound like a good idea at first,
this actually means you have to write all rules yourself, which goes against having a solid standard.
Normalize CSS on the other and deals with many of these separately. The following is a sample from the version
(v4.2.0) of the code.
/**
* 1. Change the default font family in all browsers (opinionated).
* 2. Correct the line height in all browsers.
* 3. Prevent adjustments of font size after orientation changes in IE and iOS.
*/
/* Document
========================================================================== */
html {
font-family: sans-serif; /* 1 */
line-height: 1.15; /* 2 */
-ms-text-size-adjust: 100%; /* 3 */
-webkit-text-size-adjust: 100%; /* 3 */
}
/* Sections
========================================================================== */
/**
* Remove the margin in all browsers (opinionated).
*/
body {
margin: 0;
}
/**
* Add the correct display in IE 9-.
*/
article,
aside,
footer,
header,
nav,
section {
display: block;
}
/**
* Correct the font size and margin on `h1` elements within `section` and
* `article` contexts in Chrome, Firefox, and Safari.
*/
Normalize CSS
The display property with the value of inline-block is not supported by Internet Explorer 6 and 7. A work-around
for this is:
zoom: 1;
*display: inline;
The zoom property triggers the hasLayout feature of elements, and it is available only in Internet Explorer. The
*display makes sure that the invalid property executes only on the affected browsers. Other browsers will simply
ignore the rule.
The -ms-high-contrast selector has 3 states: active, black-on-white, and white-on-black. In IE10+ it also had a
none state, but that is no longer supported in Edge going forward.
Examples
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: black-on-white) {
.header{
background: #fff;
color: #000;
}
}
This will change the header background to white and the text color to black when high contrast mode is active and
it is in black-on-white mode.
Similar to the first example, but this specifically selects the white-on-black state only, and inverts the header colors
to a black background with white text.
More Information:
.hide-on-ie6-and-ie7 {
*display : none; // This line is processed only on IE6 and IE7
}
Non-alphanumeric prefixes (other than hyphens and underscores) are ignored in IE6 and IE7, so this hack works for
any unprefixed property: value pair.
@media \0 screen {
.hide-on-ie8 {
display : none;
}
}
DON'T
#box {
left: 0;
top: 0;
transition: left 0.5s, top 0.5s;
position: absolute;
width: 50px;
height: 50px;
background-color: gray;
}
#box.active {
left: 100px;
top: 100px;
}
#box {
left: 0;
top: 0;
position: absolute;
width: 50px;
height: 50px;
background-color: gray;
#box.active {
Demo same animation, took 1.3ms for rendering, 2.0ms for painting.
AB Chapter 20
A.J Chapter 4
Aaron Chapter 4
abaracedo Chapter 4
Abhishek Singh Chapter 22
adamboro Chapter 1
Aeolingamenfel Chapters 27 and 55
Ahmad Alfy Chapters 4, 5 and 16
Alohci Chapter 15
amflare Chapters 13 and 17
Andre Lopes Chapter 44
andre mcgruder Chapter 54
andreas Chapters 15 and 38
Andrew Chapters 12, 19 and 53
Andrew Myers Chapter 47
Anil Chapter 4
animuson Chapters 4, 50 and 53
apaul Chapters 6 and 27
Araknid Chapter 4
Arif Chapter 11
Arjan Einbu Chapters 4, 7, 8, 15 and 17
Ashwin Ramaswami Chapters 1 and 4
Asim K T Chapters 5 and 16
AVAVT Chapter 50
awe Chapter 1
bdkopen Chapter 3
Ben Rhys Chapter 5
Bipon Chapter 40
BiscuitBaker Chapter 7
Boris Chapter 5
Boysenb3rry Chapter 1
brandaemon Chapter 17
Brett DeWoody Chapters 18, 38 and 39
CalvT Chapters 5 and 9
Casey Chapter 11
Cassidy Williams Chapters 10 and 22
cdm Chapters 5 and 8
Charlie H Chapters 4 and 28
Chathuranga Jayanath Chapters 11, 13 and 23
Chiller Chapter 38
Chris Chapters 1, 4, 23, 25, 42 and 50
Chris Spittles Chapters 8 and 24
Christiaan Maks Chapter 28
CocoaBean Chapter 5
coderfin Chapter 3
cone56 Chapters 31 and 36
CPHPython Chapter 4