Is there a way to have CSS in an html widget only affect the elements contained therein?

 I've created an html widget with some custom CSS that is affecting the "label" element in the Admin tools.

Here's the sample code which is something I may use to create a custom navigation and help users find specific content.

Note that I have the labels styled with a background color, when you hover over the label.

Here's what's happening in the Admin tools, as a result ➞

Parents Reply Children
  • Hi . Just to add a little more signal to this reply, 's approach is definitely the preferred way to do this and the way in which all out-of-the-box default widgets also implement custom widget-specific styling.

  • Hey guys... thanks for your explanations. I now understand what that custom field is supposed to do. I'm still not clear on how to apply it to all of my various styles outlined in my inline CSS.

    I'm using the "unfiltered html" widget, which is a generic html widget. I've placed my CSS within <style> tags, at the top, and then included my html below.

    If I've added a custom class that I want to use to wrap JUST this container, as "customstyle" then the first step is to add this to the  that custom CSS class input box that you've recommended.

    What is the next step? How can I apply that custom style to all the other existing styles in my inline CSS?

    Here's a snippet of some code I'm tinkering with (which I could not get to work)...

    <style>
    
    div.customstyle {
      width: 100%;
      margin: 0;
      padding: 0;
    }
    
    .button {
      border-radius: 4px;
      background-color: #BF403A;
      border: none;
      color: #FFFFFF;
      text-align: center;
      font-size: 16px;
      font-weight: heavy;
      padding: 15px 20px;
      width: auto;
      transition: all 0.5s;
      cursor: pointer;
      margin: 0 auto;
    }
    
    .button span {
      cursor: pointer;
      display: inline-block;
      position: relative;
      transition: 0.5s;
    }
    
    .button span:after {
      content: '\00bb';
      position: absolute;
      opacity: 0;
      top: 0;
      right: -20px;
      transition: 0.5s;
    }
    
    .button:hover span {
      padding-right: 25px;
    }
    
    .button:hover span:after {
      opacity: 1;
      right: 0;
    }
    
    img {
    cursor: default;
    pointer-events: none;
    }
    
    </style>
    
    
    
    
    
    
    
    <div class="customstyle" style="padding-bottom: 30px;">
    <img style="margin-bottom:-10px; height: auto; width: 77%; text-align:center; margin-right: auto; margin-left: auto; display: block;" alt=" " src="https://community.canopytax.com/resized-image/__size/1040x240/__key/widgetcontainerfiles/3fc3f82483d14ec485ef92e206116d49-g-NYiokL8eH0SigrPSOiJu2g-page-1home/3_5F00_step_5F00_process.png" />
    <p></p>
    <table style="border-bottom-style: hidden; height: auto; width: 100%;">
    <tbody>
    <tr>
    <td style="height: auto; padding: 12px; text-align: center; width: 33%;" valign="top">
    <p><span style="color: #e7713a; font-size: 200%;"><strong>SEARCH</strong></span></p>
    <p style="text-align: left;">Use the top search bar to see if another community member has posted a similar question. If so, please add a comment to that post. If not, move to step 2!</p>
    </td>
    <td style="height: auto; padding: 12px; text-align: center; width: 33%;" valign="top">
    <p><span style="color: #bf403a; font-size: 200%;"><strong>POST</strong></span></p>
    <p style="text-align: left;">Click the button below to post your question. Add a descriptive title and clearly outline question details. Use applicable tags. Don't forget step 3!</p>
    </td>
    <td style="height: auto; padding: 12px; text-align: center; width: 33%;" valign="top">
    <p><span style="color: #4494c0; font-size: 200%;"><strong>RESOLVE</strong></span></p>
    <p style="text-align: left;">Thanks for posting your question! You will receive an email as soon as someone responds to your message. Be sure to come back to mark the correct reply!</p>
    </td>
    </tr>
    </tbody>
    </table>
    <p></p>
    
    <div style="text-align:center;"><button formaction="https://community.canopytax.com/questions-discussions/f/questions-discussions/p/addpost" class="button hvr-glow"><span>Ask the Community </span></button>
    </div>
    </div>
    

  • The way to do this would be to give a widget-specific class name in the "CSS Class Name" property. It doesn't matter what it is, as long as it's unique to your widget. So, if the widget was named "My Custom Wiget", you could maybe name the CSS class "my-custom-widget".

    Then, in your CSS, you can target any HTML elements in the widget as follows. Say you have a div with class "hello"

    <div class="hello">
        Hello World
    </div>

    Your custom CSS could be:

    .my-custom-widget .hello {
        font-weight: bold;
        color: green;
    }

    Note the selector, ".my-custom-widget .hello". This tells the browser to style an element with class "hello" that is inside an element with class "my-custom-widget". Since all of your elements would be inside "my-custom-widget", all you need to do is make sure you prefix all your selectors with ".my-custom-widget ".

    If, alternatively, you are using a custom LESS stylesheet in your theme, you could simplify the CSS selectors further. But it's not necessary, and it doesn't seem you are doing that.

  • Thank you Michael!

    Since all of your elements would be inside "my-custom-widget", all you need to do is make sure you prefix all your selectors with ".my-custom-widget ".

    This was the part I was trying to avoid and what I was struggling with. After adding that custom style to all of my elements, what you suggested worked perfectly. what I was hoping for was a way to just add my custom style one time, and have it apply to all my styles in the CSS... i.e. if you have a really long CSS file and want to apply this custom style to all the elements in the CSS, it would be nice to apply the new custom style to ALL CSS instead of manually adding it to each individual CSS style

  • For that you would need to use something like LESS and reference it via the theme, or add the compiled CSS to the widget.

  • To do this, I'd recommend using a custom LESS stylesheet in your theme instead. The theme supports LESS pre-processing, so you can write simpler styles, such as:

    So, instead of writing something like:

    .my-custom-widget .hello {
      /* styles */
    }
    
    .my-custom-widget .hello2 {
      /* styles */
    }
    
    .my-custom-widget .hello3 {
      /* styles */
    }

    You could write:

    .my-custom-widget {
      .hello {
        // styles 
      }
    
      .hello2 {
        // styles 
      }
    
      .hello3 {
        // styles 
      } 
    }
    
    

    ...which then gets converted to CSS.

  • gtk! This conversation has been really insightful. I still have a lot to learn but we're making great strides. Thanks for all the help, here!

  • I tried this and some of html/css button reverted back to a styleless generic html button. The other solution worked perfectly. I'll have to try this solution again in the future and see if I can get it to work!

  • This needs to be in a LESS file and added to the theme, it won't work if you add it to the widget cSS

  • Thanks Luke. Sounds like I need to learn more about LESS!