Skip to main content

CSS


1. CSS Selectors

SelectorExampleDescription
ElementdivAll <div> elements
Class.boxAll elements with class "box"
ID#headerThe element with id "header"
Attributeinput[type="text"]Input elements of type text
Descendantul liAll <li> inside <ul>
Childul > liDirect children <li> of <ul>
Adjacent Siblingh2 + pFirst <p> immediately after <h2>
General Siblingh2 ~ pAll <p> after an <h2> sibling
Pseudo-classa:hover<a> elements on hover
Pseudo-elementp::first-lineFirst line of all <p> elements

2. CSS Specificity & Cascade

  • Inline style > ID selector > Class/Attribute/Pseudo-class selector > Element/Pseudo-element selector > Universal selector
  • Later rules override earlier ones if specificity is equal.

3. Box Model

+-----------------------+
| Margin |
| +-----------------+ |
| | Border | |
| | +-----------+ | |
| | | Padding | | |
| | | +-----+ | | |
| | | | Box | | | |
| | | +-----+ | | |
| | +-----------+ | |
| +-----------------+ |
+-----------------------+
  • width = content width
  • box-sizing: border-box; includes padding & border in width

4. Positioning

PropertyValuesDescription
positionstaticDefault, normal flow
relativeRelative to itself (can use top/right)
absoluteRelative to nearest positioned ancestor
fixedRelative to viewport
stickyScrolls until a threshold, then fixes
top/right/bottom/leftpx/%/emOffsets with positioned elements
z-indexnumberStacking order

5. Display & Visibility

  • display: block | inline | inline-block | flex | grid | none
  • visibility: visible | hidden | collapse
  • opacity: 0 (transparent but clickable)

6. Flexbox

.container {
display: flex;
flex-direction: row; /* or column */
justify-content: space-between; /* main axis */
align-items: center; /* cross axis */
flex-wrap: wrap;
gap: 20px;
}
.child {
flex: 1 1 200px; /* grow shrink basis */
align-self: flex-start;
}
PropertyDescription
flex-directionrow, column
justify-contentflex-start, center, space-between
align-itemsflex-start, center, stretch
flex-wrapwrap, nowrap
flexgrow shrink basis
align-selfOverride align-items per child

7. CSS Grid

.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto 200px;
gap: 10px;
grid-template-areas:
"header header header"
"sidebar main main";
}
.header {
grid-area: header;
}
.sidebar {
grid-area: sidebar;
}
.main {
grid-area: main;
}
PropertyExample/ValuesDescription
grid-template-columns1fr 2frColumn sizes (fractional units)
grid-template-rows100px autoRow sizes
grid-areaheaderAssign area
gap20pxSpace between rows/columns
place-itemscenterAlign items both axes

8. Colors & Backgrounds

  • Colors: hex, rgb(), rgba(), hsl(), hsla(), color names
  • background: color url("img.jpg") repeat center/cover no-repeat;
  • background-size: cover | contain
  • background-blend-mode: multiply | overlay | screen | ...

9. Typography

  • font-family: "Roboto", Arial, sans-serif;
  • font-size: 1rem | 16px | large | ...
  • font-weight: 400 | bold | ...
  • line-height: 1.5
  • letter-spacing, word-spacing
  • text-align: left | center | right | justify
  • text-transform: uppercase | capitalize | lowercase
  • text-shadow: 2px 2px 4px #000;

10. Transitions & Animations

Transition Example:

button {
transition: background 0.3s ease, color 0.3s;
}
button:hover {
background: #222;
color: #fff;
}

Animation Example:

@keyframes slidein {
from {
transform: translateX(-100%);
}
to {
transform: translateX(0);
}
}
.element {
animation: slidein 1s forwards;
}

11. Media Queries (Responsive Design)

@media (max-width: 600px) {
.container {
flex-direction: column;
}
.sidebar {
display: none;
}
}
  • max-width, min-width, orientation, hover, prefers-color-scheme...

12. Common CSS Functions

  • calc(100% - 80px)
  • var(--main-color)
  • clamp(1rem, 2vw, 2rem)
  • rgba(255, 0, 0, 0.5) (red, 50% opacity)
  • linear-gradient(90deg, #333, #eee)

13. Common Pseudo-classes & Pseudo-elements

SelectorDescription
:hoverOn mouse hover
:activeWhile being clicked
:focusWhen focused (e.g. input)
:nth-child(2n)Every even child
:first-childFirst child
:last-childLast child
::beforeInsert content before element
::afterInsert content after element
::placeholderStyle input placeholder text

14. Useful CSS Snippets

Center an element

.parent {
display: flex;
align-items: center;
justify-content: center;
}

Responsive image

img {
max-width: 100%;
height: auto;
display: block;
}

Hide scrollbar (Webkit browsers)

.element::-webkit-scrollbar {
display: none;
}

Text overflow ellipsis

.ellipsis {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}

15. Advanced Topics

  • Custom Properties (CSS Variables):

    :root {
    --main-color: #3498db;
    }
    .btn {
    background: var(--main-color);
    }
  • Selectors Level 4:

    • :is(.a, .b), :where(), :not()
    • [type^="text"] (attribute starts with)
  • Logical Properties:

    • margin-inline-start, padding-block-end, etc.
  • Backdrop Filter (frosted glass):

    .glass {
    backdrop-filter: blur(6px) brightness(0.7);
    background: rgba(255, 255, 255, 0.2);
    }
  • Container Queries:

    @container (max-width: 400px) {
    .card {
    font-size: 1rem;
    }
    }

16. Debugging Tools

  • Browser DevTools: Elements, Styles, Computed, Network
  • outline: 2px solid red; for debugging layouts
  • CSS linting tools (stylelint)

17. Resources & References


Happy Styling! 🎨