skip to Main Content

The Problem

Well … i don’t know if this is even possible. I have a wonderful menu with a gradient and their child elements with transparent background-color and color white: https://nimb.ws/o5xWlZ

In the hover, i want to make the background-color of this child element color white and the color text make it transparent for show that part of gradient.

What I Have Tried

My first reaction was:

child-element{background-color:#fff;color:transparent}

And you can imagine the results( SPOILER: https://nimb.ws/TNrCKJ ) … only white in the button.
Then, i think to apply a box-shadow …

 child-element{-webkit-box-shadow: inset 0px 0px 300px 200px rgba(255,255,255,1);-moz-box-shadow: inset 0px 0px 300px 200px rgba(255,255,255,1);box-shadow: inset 0px 0px 300px 200px rgba(255,255,255,1);}

And wel.. searching and searching … doesn’t find anything … so, here i am.

The Code

This is the HTML, i can’t editing so much, because is a dynamic <ul> in a .tpl

<div id="block_top_menu" class="full-width here-is-the-gradient">
    <ul class="sf-menu transparent">
        <li class="menu-li category transparent"><a href="/257-productos-medicos" title="Productos Médicos">Productos Médicos</a></li>
        ... others li elements ...
    </ul>
</div>

And this is the CSS

#block_top_menu{
    background:#6aa447;background:-moz-linear-gradient(45deg,#6aa447 0%,#4d81bd 90%);
    background:-webkit-gradient(left top,right top,color-stop(0%,#6aa447),color-stop(100%,#4d81bd));
    background:-webkit-linear-gradient(45deg,#6aa447 0%,#4d81bd 90%);
    background:-o-linear-gradient(45deg,#6aa447 0%,#4d81bd 90%);
    background:-ms-linear-gradient(45deg,#6aa447 0%,#4d81bd 90%);
    background:linear-gradient(to right,#6aa447 0%,#4d81bd 90%);
    filter:progid:DXImageTransform.Microsoft.gradient( startColorstr='#6aa447',endColorstr='#4d81bd',GradientType=1);otherthings
}
.sf-menu{
      background:transparent;
   ...otherthings...
}
...
.sf-menu > li > a{
       border:none;color:#fff;
       font: 500 18px Oswald;
       padding:10px 5px;
       text-transform:none;
 }
.sf-menu > li > a:hover {
   /* What do I put here? */
 }

An example:

Like to fill text with an image as in Photoshop:

enter image description here

Thanks you very much for the time 🙂

3

Answers


  1. Chosen as BEST ANSWER

    Finally

    After searching it with differents words for the same problem, I found an interesting article in CSS-Tricks about this. Here i found this codepen from Richard.

    html, body{ background: grey; }
    p{ margin: 0px; }
    
    #clip{
      /*
      Ensure background is added first
      */  
      background: linear-gradient(to bottom, #eee, rgba(222,112,6,0.2), #de7006),url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0naHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmcnIHZlcnNpb249JzEuMScgd2lkdGg9JzQwMCcgaGVpZ2h0PSc0MDAnPgoJPGRlZnMgaWQ9J2RlZnM0Jz4KCQk8ZmlsdGVyIGNvbG9yLWludGVycG9sYXRpb24tZmlsdGVycz0nc1JHQicgaWQ9J2ZpbHRlcjMxMTUnPgoJCQk8ZmVUdXJidWxlbmNlIHR5cGU9J2ZyYWN0YWxOb2lzZScgbnVtT2N0YXZlcz0nMScgYmFzZUZyZXF1ZW5jeT0nMC45JyBpZD0nZmVUdXJidWxlbmNlMzExNycgLz4KCQkJPGZlQ29sb3JNYXRyaXggcmVzdWx0PSdyZXN1bHQ1JyB2YWx1ZXM9JzEgMCAwIDAgMCAwIDEgMCAwIDAgMCAwIDEgMCAwIDAgMCAwIDYgLTMuNzUgJyBpZD0nZmVDb2xvck1hdHJpeDMxMTknIC8+CgkJCTxmZUNvbXBvc2l0ZSBpbjI9J3Jlc3VsdDUnIG9wZXJhdG9yPSdpbicgaW49J1NvdXJjZUdyYXBoaWMnIHJlc3VsdD0ncmVzdWx0NicgaWQ9J2ZlQ29tcG9zaXRlMzEyMScgLz4KCQkJPGZlTW9ycGhvbG9neSBpbj0ncmVzdWx0Nicgb3BlcmF0b3I9J2RpbGF0ZScgcmFkaXVzPScxMCcgcmVzdWx0PSdyZXN1bHQzJyBpZD0nZmVNb3JwaG9sb2d5MzEyMycgLz4KCQk8L2ZpbHRlcj4KCTwvZGVmcz4KCTxyZWN0IHdpZHRoPScxMDAlJyBoZWlnaHQ9JzEwMCUnIHg9JzAnIHk9JzAnIGlkPSdyZWN0Mjk4NScgZmlsbD0nI2VlZWVlZScvPiAgICAgCgk8cmVjdCB3aWR0aD0nMTAwJScgaGVpZ2h0PScxMDAlJyB4PScwJyB5PScwJyBpZD0ncmVjdDI5ODUnIHN0eWxlPSdmaWxsOiNlMDg3Mjg7ZmlsdGVyOnVybCgjZmlsdGVyMzExNSknIC8+Cjwvc3ZnPg==);
      
       background-attachment: fixed;
      -webkit-text-fill-color: transparent;
      -webkit-background-clip: text;
      
      font-size: 28vw;
      font-weight: bold;
      text-align: center;
    }
    <p id="clip">FIXED</p>
    <p id="clip">FIXED</p>
    <p id="clip">FIXED</p>
    <p id="clip">FIXED</p>
    <p id="clip">FIXED</p>
    <p id="clip">FIXED</p>

    Then, complementing this technique with my html and css, I have obtained the expected result:

    @import url('https://fonts.googleapis.com/css?family=Oswald:300,400&display=swap');
    
    /* Styles for the parents and child */
    #block_top_menu{background:#6aa447;background:-moz-linear-gradient(45deg,#6aa447 0%,#4d81bd 90%);background:-webkit-gradient(left top,right top,color-stop(0%,#6aa447),color-stop(100%,#4d81bd));background:-webkit-linear-gradient(45deg,#6aa447 0%,#4d81bd 90%);background:-o-linear-gradient(45deg,#6aa447 0%,#4d81bd 90%);background:-ms-linear-gradient(45deg,#6aa447 0%,#4d81bd 90%);background:linear-gradient(to right,#6aa447 0%,#4d81bd 90%);filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#6aa447',endColorstr='#4d81bd',GradientType=1);display: flex;padding:0}
    .sf-menu{background:transparent;border:none;box-shadow:0 0 #ccc;display:flex;width: inherit;margin:0}
    .menu-li{border:none;float:none;margin:0;padding:10px 20px;}
    .sf-menu > li:hover{background-color:#fff;}
    .sf-menu > li > a{border:none;color:#fff;font: 400 20px Oswald;text-transform:none}
    
    /* Magic */
    .sf-menu > li:hover > a{
      background:#6aa447;background:-moz-linear-gradient(45deg,#6aa447 0%,#4d81bd 90%);background:-webkit-gradient(left top,right top,color-stop(0%,#6aa447),color-stop(100%,#4d81bd));background:-webkit-linear-gradient(45deg,#6aa447 0%,#4d81bd 90%);background:-o-linear-gradient(45deg,#6aa447 0%,#4d81bd 90%);background:-ms-linear-gradient(45deg,#6aa447 0%,#4d81bd 90%);background:linear-gradient(to right,#6aa447 0%,#4d81bd 90%);
      
       background-attachment: fixed;
      -webkit-text-fill-color: transparent;
      -webkit-background-clip: text;
      /*This last 3 lines are important for the magic*/
    }
    
    /* Others */
    ul{list-style:none}
    a{text-decoration: none;}
    body{align-content:center;background-color:#f1f1f1;display:flex;flex-wrap:wrap;font-family:Oswald;height:90vh;justify-content:center;}
    <h1>Fill text with the gradient background of the parent</h1>
    <div id="block_top_menu" class="full-width here-is-the-gradient">
        <ul class="sf-menu transparent">
            <li class="menu-li category transparent"><a href="/257-productos-medicos" title="Productos Médicos">Productos Médicos</a></li>
            <li class="menu-li category transparent"><a href="/257-productos-medicos" title="Productos Médicos">Productos Médicos</a></li>
          <li class="menu-li category transparent"><a href="/257-productos-medicos" title="Productos Médicos">Productos Médicos</a></li>
          <li class="menu-li category transparent"><a href="/257-productos-medicos" title="Productos Médicos">Productos Médicos</a></li>
          <li class="menu-li category transparent"><a href="/257-productos-medicos" title="Productos Médicos">Productos Médicos</a></li>
          <li class="menu-li category transparent"><a href="/257-productos-medicos" title="Productos Médicos">Productos Médicos</a></li>
        </ul>
    </div>

    You can see it in a Full Page View at Codepen

    Thanks a lot to everyone who reads me and suggest me a solution or trick.

    Greetings, V.Alex.


  2. Use the descendant combinator? Like below?

    section {
      background: blue;
    }
    
    section .foo {
      color: red;
    }
    <section>
      <h2>Hello</h2>
      <p class=foo>
        World
      </p>
    </section>
    Login or Signup to reply.
  3. https://www.sitepoint.com/masking-in-the-browser-with-css-and-svg/
    you will need to create a vector mask using SVG’s.

    warning:
    css mask is not currently supported by IE
    https://caniuse.com/#search=mask

    codepen example:
    https://codepen.io/antonietta/pen/zqpBEg

    .masked {
        margin: 20px auto;
        display: block;
        max-width: 100%;
        height: 450px;
        mask-image: url(https://s3-us-west-        
        2.amazonaws.com/s.cdpn.io/234228/trapeze.png),
        url(https://s3-us-west2.amazonaws.com/s.cdpn.io/234228/alpha-star.png);
        mask-position: center center, bottom right;
        mask-repeat: no-repeat, no-repeat;
        mask-size: 350px 350px, 350px 350px;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search