CSS Cheatsheet


Introduction:

Cascading Style Sheet also known as CSS is used to design Web Pages or Websites.

↬ CSS saves time.

↬ Helps the page load faster.

↬ Easy to maintain.

↬ Independent platform.

↬ Extension: Save into .css

Types of CSS:

I. Inline CSS:

<h1 style="color: white; background-color: red;">This is Heading one.</h1>

II. Internal CSS:

<head>
<title>CodeX</title>
<style type="text/css">
    h1{
        color: white;
        background-color: red;
    }
</style>
</head>

III. External CSS:

<head>
<title>CodeX</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
1. Color Properties
h1 {
color: white;
color: rgb(255, 0, 0);
color: rgba(255, 0, 0, 0.5); /* Alpha (Transparent) can be used from 0 to 1 */
}
2. Background Properties
body {
    /* 1. Background Color */
    background-color: #00ff00;
    background-color: red;
    background-color: rgb(255, 0, 0);

    /* 2. Background Image */
    background-image: url("images/bg.png");

    /* 3. Background Position */
    background-position: top;
    background-position: right;
    background-position: left;
    background-position: bottom;
    background-position: center;

    /*4. Background Repeat */
    background-repeat: no-repeat;
    background-repeat: repeat-x;
    background-repeat: repeat-y;
    background-repeat: round;
    background-repeat: space;

    /* 5. Background Size */
    background-size: auto;
    background-size: cover;
    background-size: contain;

    /* 6. Background Origin */
    background-origin: padding-box;
    background-origin: border-box;
    background-origin: content-box;

    /* 7. Background Clip */
    background-clip: padding-box;
    background-clip: border-box;
    background-clip: content-box;

    /* 8. Background Attachment */
    background-attachment: scroll;
    background-attachment: fixed;
    background-attachment: local;

    /* 9. Background Blend Mode */
    background-blend-mode: multiply;
    background-blend-mode: screen;
    background-blend-mode: luminosity;
    background-blend-mode: overlay;
    background-blend-mode: darken;
    background-blend-mode: lighten;
    background-blend-mode: color-dodge;
    background-blend-mode: saturation;
}
3. Border Properties
div {
    height: 300px;
    width: 300px;

    /* 1. Border Style*/
    border-style: solid;
    border-style: dotted;
    border-style: dashed;
    border-style: double;
    border-style: groove;
    border-style: hidden;
    border-style: ridge;
    border-style: inset;
    border-style: outset;
    border-top-style: dotted;

    /* 2. Border Width*/
    border-width: thin;
    border-width: thick;
    border-width: medium;
    border-top-width: thin;

    /* 3. Border Color*/
    border-color: red;
    border-top-color: blue;

    /* 4. Border Radius*/
    border-radius: 10px;

    /* 5. Border Collapse*/
    border-collapse: separate;
    border-collapse: collapse;

    /* 6. Border Image*/
    border-image-source: url("images/arrow.png");
    border-image-slice: 30;
    border-image-width: 10px;
    border-image-outset: 20px;

    border-image-repeat: round;
    border-image-repeat: stretch;
    border-image-repeat: space;
    border-image-repeat: repeat;

    /* Shortcut Writing */
    border-image: url("images/arrow.png") 30 10px 20px round;

    /* border-image generator (FireFox Browser Only.) */
}
4. Text Properties
h1 {
    /* 1. Text Color */
    color: red;

    /* 2. Letter Spacing */
    letter-spacing: 5px;

    /* 3. Word Spacing */
    word-spacing: 20px;

    /* 4. Text Indent (First Line Before Space Like DAWN Newspaper) */
    text-indent: 20px;

    /* 5. Text Align */
    text-align: left;
    text-align: right;
    text-align: center;
    text-align: justify;

    /* 6. Text Decoration */
    text-decoration: underline;
    text-decoration: overline;
    text-decoration: line-through;
    text-decoration: none;

    /* 7. Text Transform */
    text-transform: uppercase;
    text-transform: lowercase;
    text-transform: capitalize;

    /* 8. Text Shadow */
    /* Text Shadow: Horizental    Vertical   Blur    Color; */
    text-shadow: 2px 2px 4px gray;
}
5. Font Properties
h1 {
    /* If Font Name Contains More Than One Words Then Use Like This: "Times New Roman"*/
    font-family: Arial;

    font-style: italic;
    font-style: oblique;

    font-weight: bold;

    /* All Fonts In Capital */
    font-variant: small-caps;

    /* Space Between Lines */
    line-height: 100px;

    font-size: 100px;

    /* One Line Code: Shortcut */
    /* font: style      variant     weight      size/line       height    family */
    font: italic small-caps bolder 100px/100px Arial;
}
6. Text Decoration Properties
h1 {
    /* 1. Text Decoration Line */
    text-decoration-line: underline;
    text-decoration-line: overline;
    text-decoration-line: line-through;
    text-decoration-line: none;

    /* 2. Text Decoration Color */
    text-decoration-color: red;
    text-decoration-color: #ff00cc;
    text-decoration-color: rgb(255, 255, 0);
    text-decoration-color: rgb(255, 255, 0, 0.2);

    /* 3. Text Decoration Style */
    text-decoration-style: solid;
    text-decoration-style: dotted;
    text-decoration-style: dashed;
    text-decoration-style: double;
    text-decoration-style: wavy;

    /* 4. Text Decoration Thickness */
    text-decoration-thickness: 10px;
    /* Supported By FireFox and Safari Only. */
}
8. List Style Properties
ul {
    list-style-type: disc;
    list-style-type: circle;
    list-style-type: square;
    list-style-type: none;
}
                        
ol {
    list-style-type: decimal;
    list-style-type: decimal-leading-zero;
    list-style-type: upper-alpha;
    list-style-type: lower-alpha;
    list-style-type: upper-roman;
    list-style-type: lower-roman;
}

ul {
    list-style-position: outside;
    list-style-position: inside;
}

ul {
    list-style-image: url("images/arrow.png");
}
9. Table Style Properties
table {
    width: 100%;
    padding: 5px 10px;
}
                        
table,
th,
td {
    border: 1px solid black;
}

table {
    border-collapse: separate;  /* Default */
    border-collapse: collapse;  /* Remove Border Spaces */

    border-spacing: 5px 10px;    /* 5px For Column and 10px For Row */
    
    empty-cells: show;
    empty-cells: hide;

    table-layout: auto;
    table-layout: fixed;
}

td {
    vertical-align: top;
    vertical-align: bottom;
    vertical-align: middle;
}

caption {
    caption-side: top;
    caption-side: bottom;
}

tr:hover {
    background-color: gray;
}

tr:nth-child(even) {
    color: white;
    background-color: green;
}
10. Overflow Properties
div {
    overflow: visible;
    overflow: hidden;
    overflow: scroll;
    overflow: auto;
    overflow-x: hidden;
    overflow-y: hidden;
}
11. Position Properties
div {
    position: relative;
    position: absolute; /* Most Used and Helpful Than Others */
    position: sticky;
    position: static;
    position: fixed;
    top: 5px;
    left: 5px;
}
12. Display Properties
div {
    display: none;
    display: inline;
    display: block;
    display: inline-block;
    display: list-item;
    display: initial;
    display: inherit;
    display: contents;
    display: flex;
    display: inline-flex;
    display: grid;
    display: inline;
    display: table;
    display: inline-table;
    display: table-caption;
    display: table-column-group;
    display: table-header-group;
    display: table-footer-group;
    display: table-row-group;
    display: table-cell;
    display: table-column;
    display: table-row;
    display: run-in;
}
13. Cursor Properties
div {
    cursor: alias;
    cursor: context-menu;
    cursor: all-scroll;
    cursor: auto;
    cursor: cell;
    cursor: col-resize;
    cursor: copy;
    cursor: crosshair;
    cursor: default;
    cursor: e-resize;
    cursor: ew-resize;
    cursor: grab;
    cursor: grabbing;
    cursor: help;
    cursor: move;
    cursor: n-resize;
    cursor: ne-resize;
    cursor: nesw-resize;
    cursor: ns-resize;
    cursor: nw-resize;
    cursor: nwse-resize;
    cursor: no-drop;
    cursor: none;
    cursor: not-allowed;
    cursor: pointer;
    cursor: progress;
    cursor: row-resize;
    cursor: s-resize;
    cursor: se-resize;
    cursor: sw-resize;
    cursor: text;
    cursor: url("images/arrow.jpg");
    cursor: w-resize;
    cursor: wait;
    cursor: zoom-in;
    cursor: zoom-out;
}
14. Visibility Properties
div {
    visibility: hidden;
    visibility: visible;
    visibility: collapse;
}
15. Float Properties
div {
    float: right; /* Adjustment From Right */
    float: left; /* Adjustment From Left */
}
16. Z-Index Properties
div {
   z-index: auto;
   z-index: 1;
   z-index: 2;
   z-index: -1; /* If We Use -1 Then It Will Be In Background Side. */
}
17. Box Shadow Properties
div {
    /* Box Shadow: Horizental   Vertical    Blur    Spread      Color  */
    box-shadow: 2px 2px 3px 5px gray;

    /* If We Use Values In Minus Then It Will Be Applied For Upperside.  */
    box-shadow: -2px -2px 3px 5px gray;
}
18. CSS Selectors

1. Simple Selectors

  1. Element Selector:
    h1 {
        /* Code */
    }
    
    p {
        /* Code */
    }
    
    span {
        /* Code */
    }
  2. ID Selector:
    #navbar {
        /* Code */
    }
  3. Class Selector:
    .navbar {
        /* Code */
    }
  4. Universal Selector:
    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
  5. Grouping Selector:
    h1,
    p,
    span {
        /* Code */
    }

2. Combinator Selectors

  1. Descendent Selectors (Space)
  2. Child Selectors (>)
  3. Adjacent Sibling Selectors (+)
  4. General Sibling Selectors (~)

3. Attribute Selectors

input[type="text"] {
    border: 2px solid red;
}
19. Pseudo Classes

Link

a:link {
    /* Code */
}

a:hover {
    /* Code */
}

a:visited {
    /* Code */
}

a:active {
    /* Code */
}

a:target {
    /* Code */
}

Common Used

div p:first-child {
    /* Code */
}

div p:last-child {
    /* Code */
}

div p:nth-child(even) {
    /* Code */
}

div p:nth-child(odd) {
    /* Code */
}

div p:nth-last-child(even) {
    /* Code */
}

div p:nth-of-type(even) {
    /* Code */
}

div p:nth-last-of-type() {
    /* Code */
}

div p:nth-last-child(even) {
    /* Code */
}

div p:only-child {
    /* Code */
}

div p:only-of-type {
    /* Code */
}

div p:empty {
    /* Code */
}

div p:not() {
    /* Code */
}

div p:lang() {
    /* Code */
}

Form Used

input:focus {
    /* Code */
}

input:checked {
    /* Code */
}

input:disabled {
    /* Code */
}

input:enabled {
    /* Code */
}

input:required {
    /* Code */
}

input:optional {
    /* Code */
}

input:in-range {
    /* Code */
}

input:out-of-range {
    /* Code */
}

input:read-only {
    /* Code */
}

input:read-write {
    /* Code */
}

input:valid {
    /* Code */
}

input:invalid {
    /* Code */
}

input:default {
    /* Code */
}

Variables

:Root
  • The :root class is used to define variables in CSS.
  • A variable is a data container that stores data and it is defined by :root class.
:root {
    --bgcolor: orange;
}

body {
    background-color: var(--bgcolor);
}

div {
    background-color: var(--bgcolor);
}