Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save carlosvarela/fbb0fc8ca53f3bd63ed45bfd788a26c2 to your computer and use it in GitHub Desktop.

Select an option

Save carlosvarela/fbb0fc8ca53f3bd63ed45bfd788a26c2 to your computer and use it in GitHub Desktop.

Revisions

  1. @tunguskha tunguskha revised this gist Mar 24, 2018. No changes.
  2. @tunguskha tunguskha created this gist Mar 24, 2018.
    133 changes: 133 additions & 0 deletions Gradient shadow in pure CSS.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,133 @@
    # Gradient shadow in pure CSS

    ![alt text](https://lab.tungu.me/external/img/gradient-shadow-btn.jpg)


    ###### HTML
    ```html
    <button>Let's Go !</button>
    ```

    ---

    ###### CSS
    *Declare colors*
    ```css
    html, :root {
    --purple: #7f00ff;
    --pink: #e100ff;
    }
    ```

    *Styling our button*
    ```css
    button {
    font-family: "Roboto", sans-serif;
    font-size: 16px;
    position: relative;
    outline: none;
    border: none;
    -webkit-appearance: none;
    -webkit-tap-highlight-color: transparent;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    padding: 0.75em 1.75em;
    border-radius: 50px;
    display: inline-block;
    color: #fff;

    /* Our gradient */
    background: -webkit-gradient(linear, left top, right top, from(var(--purple)), to(var(--pink)));
    background: linear-gradient(to right, var(--purple), var(--pink));
    }
    ```

    *Now, let's create our shadow*
    ```css
    button::after {
    content: "";
    position: absolute;
    z-index: -1;
    bottom: -10px;
    left: 5%;
    height: 110%;
    width: 90%;
    opacity: 0.8;
    border-radius: 50px;

    /* Our gradient */
    background: -webkit-gradient(linear, left top, right top, from(var(--purple)), to(var(--pink)));
    background: linear-gradient(to right, var(--purple), var(--pink));

    /* Blurring the element for shadow effect */
    -webkit-filter: blur(6px);
    -moz-filter: blur(6px);
    -o-filter: blur(6px);
    -ms-filter: blur(6px);
    filter: blur(6px);

    /* Transition for the magic */
    -webkit-transition: all 0.2s;
    transition: all 0.2s;
    }
    ```

    *We have our button and her shadow, now change the style for hover*
    ```css
    button:hover::after {
    /* Changing blur effect */
    -webkit-filter: blur(4px);
    -moz-filter: blur(4px);
    -o-filter: blur(4px);
    -ms-filter: blur(4px);
    filter: blur(4px);

    /* And change the style properties */
    width: 100%;
    bottom: -5px;
    left: 0;
    }
    ```

    *For more interactivity, change the active effect*
    ```css
    button:hover:active::after {
    -webkit-filter: blur(10px);
    -moz-filter: blur(10px);
    -o-filter: blur(10px);
    -ms-filter: blur(10px);
    filter: blur(10px);
    }
    ```

    ### Easy no ?
    *For some bug and impovements on IE and Edge*
    ```css
    /* Edge does not support blur effect, so we can just delete the shadow */

    @supports (-ms-ime-align: auto) {
    button::after, button:hover::after, button:active::after {
    display: none;
    }
    }


    /* And IE does not support CSS variables and blur effect, so.. */

    @media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
    /* Fix css var by calling the hexa colors */
    button {
    background: -webkit-gradient(linear, left top, right top, from(#7f00ff), to(#e100ff));
    background: linear-gradient(to right, #7f00ff, #e100ff);
    }
    /* Deleting the shadow */
    button::after, button:hover::after, button:active::after {
    display: none;
    }
    }
    ```

    [A live version is here](https://codepen.io/tunguska/pen/KovRbg)