Skip to content

How to set opacity of background color given hex css variable #1987

Answered by c0g1t8
StevenTCramer asked this question in Q&A
Discussion options

You must be logged in to vote

@StevenTCramer

Another approach is to use color-mix() with transparency which will give you an equivalent effect. This function is newly available as of 2023.

background-color: color-mix(in srgb, var(--neutral-layer-1), transparent 70%);

Below is the experiment I did to confirm

<style>
    :root{
        --rgb-Color: 251, 251, 251;
        --hex-Color: #fbfbfb;
        --hex-ColorWithAlpha: #fbfbfb4d; /* 4d is equivalent to 0.3 for opacity */
    }

    .container1 {
        background-color: rgb(var(--rgb-Color));
    }

    .container2 {
        background-color: rgba(var(--rgb-Color), 0.3);
    }

    .container3 {
        background-color: var(--hex-Color);
    }

    .container4 {

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@StevenTCramer
Comment options

Comment options

You must be logged in to vote
1 reply
@StevenTCramer
Comment options

Answer selected by vnbaaij
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants