Author: Saim Khalid

  • Hover Effect

    <!DOCTYPE html>
    <html>
    <head>
      <style>
    
    button {
      background: green;
      color: white;
      padding: 10px;
      border: none;
      cursor: pointer;
    }
    button:hover {
      background: darkgreen; /* Changes color when hovered */
    }
    </style> </head> <body> <button>Hover Me</button> </body> </html>

    Explanation:

    • :hover applies style when the mouse is over the element.
  • Box Model Example

    <!DOCTYPE html>
    <html>
    <head>
      <style>
    
    .box {
      width: 200px;
      height: 100px;
      background: lightblue;
      padding: 20px;
      border: 5px solid black;
      margin: 15px;
    }
    </style> </head> <body> <div class="box">Box Model Example</div> </body> </html>

    Explanation (Box Model):

    • Content → main text/image.
    • Padding → space between content and border.
    • Border → outer line.
    • Margin → space between elements.
  • Borders

    <!DOCTYPE html>
    <html>
    <head>
      <style>
    
    div {
      border: 2px solid red; /* Thickness, style, color */
      padding: 10px; /* Space inside border */
      margin: 15px; /* Space outside border */
    }
    </style> </head> <body> <div>This is a CSS border example.</div> </body> </html>

    Explanation:

    • border: creates a border with thickness, style (solid, dashed, dotted), and color.
    • padding: space inside border.
    • margin: space outside border.
  • Fonts and Text Styling

    <!DOCTYPE html>
    <html>
    <head>
      <style>
    
    p {
      font-family: Arial, sans-serif; /* Sets font type */
      font-size: 18px; /* Controls text size */
      font-weight: bold; /* Makes text bold */
      text-align: center; /* Aligns text in the middle */
    }
    </style> </head> <body> <p>Styled Text Example</p> </body> </html>

    Explanation:

    • font-family: changes font style.
    • font-size: controls text size.
    • font-weight: bold, normal, light.
    • text-align: left, right, center, justify.
  • Background Color

    <!DOCTYPE html>
    <html>
    <head>
      <style>
    
    body {
      background-color: lightgray; /* Sets page background */
    }
    h2 {
      background-color: yellow; /* Sets only heading background */
    }
    </style> </head> <body> <h2>CSS Background Example</h2> </body> </html>

    Explanation:

    • You can style either the entire page (body) or specific elements (like h2).
  • Changing Text Color

    <!DOCTYPE html>
    <html>
    <head>
      <style>
    
    h1 {
      color: blue; /* Changes heading text color */
    }
    p {
      color: green; /* Changes paragraph text color */
    }
    </style> </head> <body> <h1>Hello CSS</h1> <p>This is a CSS color example.</p> </body> </html>

    Explanation:

    • color property sets the text color.
    • Headings will appear blue, and paragraphs green.
  • PX to EM Conversion

    This simple online pixel (PX) values to EM units converter tool calculate precise EM values from pixels for better CSS scaling and accessibility. In CSS, pixel and em are units used to define sizes for elements dimensions, fonts and layout structure.

    • Pixels (px) are a fixed unit of measurement, which represent the exact number of dots on a screen. It does not adapt to other elements on the page or user display settings.
    • Em units (em) are relative units based on the font size of the element or its parent element. It is used in responsive and flexible layouts. One em unit is equal to current font-size.

    Pixel to Em Conversion Formula

    Following is formula to convert pixel to em.

    em = px / font-size
    

    For example, if base font-size is 16px, then setting an element to 1.5 em will make it 24px ( 1.5 * 16 ). This way em units ensure responsiveness.

    Pixel to Em Converter

    Following is a digital converter for converting pixel to em and vice-versaFont Size (px):Pixels:EM:

    Note: Default font-size is 16px. So while converting the px to em, you need to select your base value (default is 16) for the pixel and use it in the formula to calculate.

    Benefits of Using EM Units

    • Responsive Scaling: Em units are based on fontsize of parent, So any adjustments to the base fontsize will automatically resize all em based elements.
    • Consistent Proportionality: When we set entire layout in em units, changing base font will proportionally adjust all the sizes in page.
    • User Accessibility: Some visually impaired user may increase font-size in browser settings for better visibility, in this cases our layout defined in em units will also scale accordingly.

    To learn about units in CSS, checkout article on CSS measurement units.

    CSS PX to EM Conversion Table

    Following table shows the corresponding em values to px values, considering the base pixel value as 16px:

    PXEM
    5px0.3125em
    6px0.3750em
    7px0.4375em
    8px0.5000em
    9px0.5625em
    10px0.6250em
    11px0.6875em
    12px0.7500em
    13px0.8125em
    14px0.8750em
    15px0.9375em
    16px1.0000em
    17px1.0625em
    18px1.1250em
    19px1.1875em
    20px1.2500em

  • Units

    CSS Units define the measurement system used to specify the values. CSS offers a number of different units for expressing length and measurement. CSS unit is used to specify the property size for a page element or its content.

    There are a number of ways to specify and measure length in CSS. It is used to specify margins, padding, font size, width, height, border, etc.

    For example – font-size: 50px, here number 50 has a suffix px i.e., pixel, it is a CSS measurement unit.

    There should be no whitespace between the number and the unit. The unit can be left out when the value is 0.

    Length Units

    Length units can be categorized into two types:

    • Absolute units: Fixed unit lengths that does not depend on screen width.
    • Relative units: Responsive unit lengths that changes according to screen width.

    Absolute Length Units

    These units are categorized as fixed-length units, which means that lengths specified with absolute units maintain an exact, unchanged size on the screen.

    These units prove to be very effective when the browser has comprehensive information about the properties of the screen, the printer being used, or other appropriate user agents.Height: 2pxHeight: 2cmHeight: 2inHeight: 2pt

    The following table contains all the types of absolute units:

    UnitDescriptionEquivalent valueExample
    mmRefers to millimeter, it is used to specify the measurements in millimeters.1mm = 1/10th of 1cmfont-size: 10mm;
    cmRefers to centimeter, it is used to specify the measurements in centimeters.1cm = 37.8px = 25.2/64infont-size: 5cm;
    QRefers to Quarter-millimeters, it is used to specify the measurements in centimeters.1Q = 1/40th of 1cmfont-size: 5Q;
    inRefers to inches, it is used to specify the measurement in inches.1in = 2.54cm = 96pxfont-size: 1in;
    ptRefers to point, it is used to specify the measurements in points.1pt = 1/72 of 1infont-size: 20pt;
    pcRefers to picas, it is used to specify the measurement in picas.1pc = 1/6th of 1inwidth: 6pc;
    pxRefers to pixels, it is used to specify the measurement in pixels.1px = 1/96th of 1infont-size: 15px;

    Absolute units prove valuable for projects where responsiveness is not a priority. However, they are less beneficial for responsive websites because they do not adjust when screen dimensions change.

    Example

    Here is an example of absolute units (mm, cm, in, Q) used for font sizes:

    <html><head><style>
    
        .unit-mm {
            font-size: 5mm;
        }
        .unit-cm {
            font-size: 1cm;
        }
        .unit-inch {
            font-size: 0.5in;
        }
        .unit-quarter {
            font-size: 40Q;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h1 class="unit-mm"&gt; Font size 5mm &lt;/h1&gt;&lt;h1 class="unit-cm"&gt; Font size 1cm &lt;/h1&gt;&lt;h1 class="unit-inch"&gt; Font size 0.5inch &lt;/h1&gt;&lt;h1 class="unit-quarter"&gt; Font size 40Q &lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Relative Length Units

    Relative length units are measured in relation to other elements or viewport of the screen.

    Relative units are great for styling responsive websites because they can be adjusted proportionally based on window size or parent elements. These units define lengths relative to other length properties.Height: 3emHeight: 3exHeight: 3lhHeight: 3vh

    The following table contains all the types of relative units:

    UnitDescriptionExample
    emRelative to the font-size of the element.font-size: 4em;
    exRelative to the x-height of the current font.font-size: 4ex;
    chRelative to width of the "0".font-size: 4ch;
    remRelative to font-size of the root element.font-size: 2rem;
    lhIt is relative to the line height of the element.font-size: 4lh;
    rlhIt is relative to the line height of the root element.font-size: 4rlh;
    vhIt is relative to the height of the viewport. 1vh = 1% or 1/100 of the height of the viewport.font-size: 4vh;
    vwIt is relative to the width of the viewport. 1vw = 1% or 1/100 of the width of viewport.width: 4vw;
    vminIt is relative to the smaller dimension of the viewport. 1vmin = 1% or 1/100 of the viewport's smaller dimension.width: 4vmin;
    vmaxIt is relative to the larger dimension of the viewport. 1vmax = 1% or 1/100 of the viewport's larger dimension.width: 4vmax;
    vbIt is relative to the size of the initial containing block in the direction of the root element's block axis. 1vb = 1% of containing block's size (block axis).font-size: 4vb;
    viIt is relative to the size of the initial containing block in the direction of the root element's inline axis. 1vb = 1% of containing block's size (inline axis).font-size: 4vi;
    svw, svhIt is relative to the width and height of the smaller viewport. 1svw = 1% or 1/100 of the smaller viewport's width and 1svh = 1% or 1/100 of the smaller viewport's height.width: 40svw; height: 40svh;
    lvw, lvhIt is relative to the width and height of the larger viewport. 1lvw = 1% or 1/100 of the larger viewport's width and 1lvh = 1% or 1/100 of the larger viewport's height.width: 40lvw; height: 40lvh;
    dvw, dvhIt is relative to the width and height of the dynamic viewport. 1dvw = 1% or 1/100 of the dynamic viewport's width and 1dvh = 1% or 1/100 of the dynamic viewport's height.width: 40dvw; height: 40dvh;

    Example

    Here is an example of relative units (em, rem, vw, vh, %) used for font sizes:

    <html><head><style>
    
        .unit-em {
            font-size: 2em;
        }
        .unit-rem {
            font-size: 1.5rem;
        }
        .unit-vw {
            font-size: 5vw;
        }
        .unit-vh {
            font-size: 5vh;
        }
        .unit-percent {
            font-size: 150%;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h1 class="unit-em"&gt;Font size 2em &lt;/h1&gt;&lt;h1 class="unit-rem"&gt;Font size 1.5rem &lt;/h1&gt;&lt;h1 class="unit-vw"&gt;Font size 5vw &lt;/h1&gt;&lt;h1 class="unit-vh"&gt;Font size 5vh &lt;/h1&gt;&lt;h1 class="unit-percent"&gt;Font size 150% &lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Units px (Pixel) and em (indicates size relative to the size of the font) are two of the measurement units of length. In order to convert px to em, try our free CSS - PX to EM Converter.

  • Color References

    The following table shows the 16 color names that were introduced in HTML 3.2, to support the 16 colors that 8-bit graphics cards offered. Same set of color can be used in CSS −

    Color NameHex ValueColorShow
    aqua#00ffffDemo
    black#000000Demo
    blue#0000ffDemo
    fuchsia#ff00ffDemo
    green#008000Demo
    gray#808080Demo
    lime#00ff00Demo
    maroon#800000Demo
    navy#000080Demo
    olive#808000Demo
    purple#800080Demo
    red#ff0000Demo
    silver#c0c0c0Demo
    teal#008080Demo
    white#ffffffDemo
    yellow#ffff00Demo

    There are other colors, which are not part of HTML or XHTML but they are supported by most of the versions of IE or Netscape. These color names can be used in CSS as well.

    Color NameHex ValueColorShow
    aliceblue#f0f8ffDemo
    antiquewhite#faebd7Demo
    aquamarine#7fffd4Demo
    azure#f0ffffDemo
    beige#f5f5dcDemo
    bisque#ffe4c4Demo
    blanchedalmond#ffebcdDemo
    blueviolet#8a2be2Demo
    brown#a52a2aDemo
    burlywood#deb887Demo
    cadetblue#5f9ea0Demo
    chartreuse#7fff00Demo
    chocolate#d2691eDemo
    coral#ff7f50Demo
    cornflowerblue#6495edDemo
    cornsilk#fff8dcDemo
    crimson#dc143cDemo
    cyan#00ffffDemo
    darkblue#00008bDemo
    darkcyan#008b8bDemo
    darkgoldenrod#b8860bDemo
    darkgray#a9a9a9Demo
    darkgreen#006400Demo
    darkkhaki#bdb76bDemo
    darkmagenta#8b008bDemo
    darkolivegreen#556b2fDemo
    darkorange#ff8c00Demo
    darkorchid#9932ccDemo
    darkred#8b0000Demo
    darksalmon#e9967aDemo
    darkseagreen#8fbc8fDemo
    darkslateblue#483d8bDemo
    darkslategray#2f4f4fDemo
    darkturquoise#00ced1Demo
    darkviolet#9400d3Demo
    deeppink#ff1493Demo
    deepskyblue#00bfffDemo
    dimgray#696969Demo
    dodgerblue#1e90ffDemo
    firebrick#b22222Demo
    floralwhite#fffaf0Demo
    forestgreen#228b22Demo
    gainsboro#dcdcdcDemo
    ghostwhite#f8f8ffDemo
    gold#ffd700Demo
    goldenrod#daa520Demo
    gray#808080Demo
    greenyellow#adff2fDemo
    honeydew#f0fff0Demo
    hotpink#ff69b4Demo
    indianred#cd5c5cDemo
    indigo#4b0082Demo
    ivory#fffff0Demo
    khaki#f0e68cDemo
    lavender#e6e6faDemo
    lavenderblush#fff0f5Demo
    lawngreen#7cfc00Demo
    lemonchiffon#fffacdDemo
    lightblue#add8e6Demo
    lightcoral#f08080Demo
    lightcyan#e0ffffDemo
    lightgoldenrodyellow#fafad2Demo
    lightgreen#90ee90Demo
    lightgrey#d3d3d3Demo
    lightpink#ffb6c1Demo
    lightsalmon#ffa07aDemo
    lightseagreen#20b2aaDemo
    lightskyblue#87cefaDemo
    lightslategray#778899Demo
    lightsteelblue#b0c4deDemo
    lightyellow#ffffe0Demo
    limegreen#32cd32Demo
    linen#faf0e6Demo
    magenta#ff00ffDemo
    mediumblue#0000cdDemo
    mediumorchid#ba55d3Demo
    mediumpurple#9370dbDemo
    midnightblue#191970Demo
    mistyrose#ffe4e1Demo
    moccasin#ffe4b5Demo
    oldlace#fdf5e6Demo
    orange#ffa500Demo
    orchid#da70d6Demo
    peachpuff#ffdab9Demo
    peru#cd853fDemo
    pink#ffc0cbDemo
    plum#dda0ddDemo
    purple#800080Demo
    rosybrown#bc8f8fDemo
    royalblue#4169e1Demo
    salmon#fa8072Demo
    sandybrown#f4a460Demo
    seagreen#2e8b57Demo
    sienna#a0522dDemo
    skyblue#87ceebDemo
    slateblue#6a5acdDemo
    steelblue#4682b4Demo
    tan#d2b48cDemo
    thistle#d8bfd8Demo
    tomato#ff6347Demo
    violet#ee82eeDemo
    wheat#f5deb3Demo
    whitesmoke#f5f5f5Demo
    yellow#ffff00Demo
    yellowgreen#9acd32Demo

  • Functions Reference

    This chapter is a CSS functions reference page and lists down each functions used in CSS. For detailed information about any function, visit respective function pages.

    FunctionDescription
    acos()CSS acos() function returns the inverse cosine of a value in the range -1 and 1.
    asin()CSS asin() function returns the inverse sine of a number between -1 and 1.
    atan()CSS atan() function returns the inverse tangent of a number between -infinity and infinity.
    atan2()CSS atan2() function returns the inverse tangent of two values between -infinity and infinity.
    attr()CSS attr() function returns the value of an attribute of the selected element.
    blur()CSS blur() function applies a Gaussian blur to the elements on which it is applied.
    brightness()CSS brightness() function sets the brightness of an element.
    calc()CSS calc() function performs calculations to determine CSS property values.
    circle()CSS circle() function defines a circle.
    clamp()CSS clamp() function sets a value for adjusting responsively between a minimum and maximum
    color()CSS color() function allows a color to be specified in a particular, specified color space.
    color-mix()CSS color-mix() function is used to mix two color values by a given amount.
    conic-gradient()CSS conic-gradient() is used to creates a conic gradient.
    contrast()CSS contrast() function sets the contrast of an element.
    cos()CSS cos() function returns the cosine of an angle.
    counter()CSS counter() function returns the current value of the counter.
    counters()CSS counters() function returns the current values of the nested and named counters.
    cubic-bezier()CSS cubic-bezier() function defines a custom cubic bezier curve for animations.
    drop-shadow()CSS drop-shadow() function applies a drop shadow effect to an image.
    ellipse()CSS ellipse() function is used to define an ellipse.
    env()CSS env() function adds a value to a user-defined environment variable in your CSS.
    exp()CSS exp() function returns the value of e raised to the power of a given number.
    fit-content()CSS fit-content() function resizes an element based on its content.
    grayscale()CSS grayscale() function sets the grayscale of an image.
    hsl() / hsla()CSS hsl() function defines a color using the Hue-Saturation-Lightness model, with an optional alpha that represents the opacity.
    hue-rotate()CSS hue-rotate() function rotates the hue of an element’s colors.
    hwb()CSS hwb() function defines a color using the Hue-Whiteness-Blackness model, with an optional alpha that represents the opacity.
    hypot()CSS hypot() function returns the square root of the sum of the squares of the numbers.
    inset()CSS inset() function defines a rectangle at the specified inset distances from each side of the reference box.
    invert()CSS invert() function inverts the colors of an element.
    lab()CSS lab() function expresses a color in CIE L*a*b color space.
    lch()CSS lch() function expresses a color in LCH(Lightness Chroma Hue) color space.
    linear-gradient()CSS linear-gradient() function is used to create a linear gradient.
    log()CSS log() function returns the natural logarithm (e) of a given number.
    matrix()CSS matrix() function defines a 2D transformation with homogeneous coordinates.
    matrix3d()CSS matrix3d() function defines a 3D transformation using a 4×4 matrix of 16 values.
    max()CSS max() function specify the largest (most positive) value from a list of comma-separated expressions.
    min()CSS min() function specify the lowest value from a list of comma-separated expressions.
    minmax()CSS minmax() function defines a size range for CSS grid columns or rows.
    mod()CSS mod() function calculates the modulus of two numbers when divided.
    opacity()CSS opacity() function function sets the transparency level of an element.
    oklab()CSS oklab() function expresses a color in the oklab color space.
    oklch()CSS oklch() function expresses a color in oklch color space.
    perspective()CSS perspective() function defines a perspective view for a 3D transformed element.
    polygon()CSS polygon() function is used to define a polygon.
    pow()CSS pow() function returns the value of a base raised to the power of a number.
    radial-gradient()CSS radial-gradient() function is used to create a radial gradient.
    repeat()CSS repeat() function is used for repeating columns or rows in a CSS grid.
    repeating-conic-gradient()CSS repeating-conic-gradient() repeats a conic gradient.
    repeating-linear-gradient()CSS repeating-linear-gradient() repeats a linear gradient.
    repeating-radial-gradient()CSS repeating-radial-gradient() repeats a radial gradient.
    rgb()CSS rgb() function sets the colors using the Red-Green-Blue model (RGB) value.
    rotate()CSS rotate() function defines a 2D rotation of an element.
    rotate3d()CSS rotate3d() function rotates an element around a fixed axis on the three-dimensional surface.
    rotateX()CSS rotateX() function rotates an element around the x-axis.
    rotateY()CSS rotateY() function rotates an element around the y-axis.
    rotateZ()CSS rotateZ() function rotates an element around the z-axis.
    round()CSS round() function returns a rounded number.
    saturate()CSS saturate() function sets the saturation level of an element.
    scale()CSS scale() function applies a scaling transformation to an element on the two-dimensional plane.
    scale3d()CSS scale3d() function applies a scaling transformation to an element on the three-dimensional plane.
    scaleX()CSS scaleX() function applies a scaling transformation to an element along the x-axis.
    scaleY()CSS scaleY() function applies a scaling transformation to an element along the y-axis.
    scaleZ()CSS scaleZ() function applies a scaling transformation to an element along the z-axis.
    sepia()CSS sepia() function applies and sets a sepia (warm, yellowish/brownish) effect to an element.
    sin()CSS sin() function calculates the sine of a given number and returns a result in the range -1 to 1.
    skew()CSS skew() function specifies a transformation that slants an element on the 2D plane.
    skewX()CSS skewX() function specifies a transformation that slants an element horizontally on the 2D plane.
    skewY()CSS skewY() function specifies a transformation that slants an element vertically on the 2D plane.
    sqrt()CSS sqrt() function returns the square root of a given number.
    tan()CSS tan() function returns the tangent of a number.
    translate()Re-positions an element along the x- and y-axis
    translateX()CSS translateX() function translates, or moves, an element horizontally along the X-axis.
    translateY()CSS translateY() function translates, or moves, an element vertically along the Y-axis.
    url()CSS url() function is used to include a file in the HTML document.
    var()CSS var() function is used to insert the value of a custom property.