Targeting dynamic ID names in CSS style sheets

I am currently utilizing WordPress and have integrated a slideshow from my Smugmug website.

Within the slideshow, there is a cookie notification that I am looking to conceal. However, the challenge lies in the randomness of the id attribute, apart from the first and last characters (which begin with yui_ and end with _32). Although the Class remains constant, applying it has not led to any change in the display of the Cookie Warning. Moreover, there seems to be dynamically loaded javascript running as part of the embedded slideshow, raising uncertainty over whether this affects the capability of local CSS to modify the code.

HTML:

<div class="sm-eu-cookie-message" id="yui_3_8_0_1_1478749888956_32"> = $0
            “ FYI: This site uses cookies to make sure your visit is as awesome as possible.  “
           <a class"headermessagelink"="" target="_blank" href="http://help.smugmug.com/customer/portal/articles/93251">What and why?</a>
      <div class="sm-eu-cookie-close">
      <button type="button" class="sm-button sm-button-size-small sm-button-skin-default sm-button-nochrome">
      <span class="sm-fonticon sm-button-fonticon sm-fonticon-small sm-fonticon-XCross"></span>
      </button>
</div>
</div>

Despite numerous attempts to find a solution, I have been unsuccessful thus far.

Is there a way to hide the entire <div> element using CSS?

EDIT: It's important to note that I do not have control over the server where the embedded code originates from, and most of the code mentioned above is generated dynamically upon page loading. My editing capabilities are limited to the local CSS on my Wordpress theme and the theme CSS on my Smugmug hosted galleries (which I believe does not impact the slideshow displayed on external embeds). You can view the page code as seen by the user at .

Answer №1

CSS Solution

To hide the cookie notification, use the following CSS code if the class is consistent:

.sm-eu-cookie-message{
  display: none; /* Use !important if needed for persistence */
}

Alternatively, with the !important flag:

.sm-eu-cookie-message{
  display: none!important; 
}

If the class varies but the attribute id values are constant, you can utilize substring matching attribute selectors like ^ or $.

  • [attribute^=value]: Selects elements where the attribute starts with specified value.
  • [attribute$=value]: Selects elements where the attribute ends with specified value.

[id^=yui_],
[id^=yui_ i] {  
  color: red;
}
[id$=_32] {
  color: blue;
}
<div class="sm-eu-cookie-message" id="yui_3_8_0_1_1478749888956_changedfordemo">
  Id starts with <em>yui_</em>
</div>

<div class="sm-eu-cookie-message" id="changedfordemo_3_8_0_1_1478749888956_32">
  Id ends with <em>_32</em>
</div>


You can broaden coverage by using the * substring matching attribute selector as well.

  • [attribute*=value]: Selects elements with at least one instance of the specified value in the attribute.

[id^=yui_],
[id^=yui_ i],
[id*=yui_],
[id*=yui_ i]{ 
  color: red;
}
[id$=_32],
[id*=_32]{
  color: blue;
}
<div class="sm-eu-cookie-message" id="yUI_3_8_0_1_1478749888956_changedfordemo">
  Id starts with <em>yUI_</em>
</div>

<div class="sm-eu-cookie-message" id="changedfordemo_3_8_0_1_1478749888956_32">
  Id contains an instance of <em>_32</em>
</div>


For more information on these selectors, refer to this source from W3C.


Note that external sources may change the notification suddenly, rendering previous selectors ineffective and requiring updates.


EDIT:

JavaScript Method

This method requires jQuery library, which can be found here.

Two options are available:

  1. Manually create a jQuery Multiple selector and use remove().
  2. Implement a function to automate the process.

1) Manually create a jQuery Multiple selector:

$(document).ready(function() {

  $("[id^=yui_], [id$=_32], [id*=yui_], [id*=_32], [id^=yui_ i], [id$=_32 i], [id*=yui_ i], [id*=_32 i], [id^=yui_][id$=_32], [id^=yui_ i][id$=_32 i]").remove();

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

2. Utilize a function for automation:

$(document).ready(function() {

  function removeByAttSubstring(att, beginsWith, endsWith, bContains, bCaseInsensitive, bBeginsAndEndsWith) {

    var sBw = att + "^=" + beginsWith,
      sEw = att + "$=" + endsWith,
      sCbw = att + "*=" + beginsWith,
      sCew = att + "*=" + endsWith;

    var aSelectors = [sBw, sEw, sCbw, sCew];

    if (bCaseInsensitive === true) {
      var sBwCi = att + "^=" + beginsWith + " i",
        sEwCi = att + "$=" + endsWith + " i",
        sCbwCi = att + "*=" + beginsWith + " i",
        sCewCi = att + "*=" + endsWith + " i";
      aSelectors.push(sBwCi, sEwCi, sCbwCi, sCewCi);
    }

    if (bBeginsAndEndsWith === true) {
      var sBwaew = sBw + "][" + sEw;
      aSelectors.push(sBwaew);
    }

    if (bCaseInsensitive === true && bBeginsAndEndsWith === true) {
      var sBwaewCi = sBw + " i][" + sEw + " i";
      aSelectors.push(sBwaewCi);
    }

    for (var i = 0; i < aSelectors.length; i++) {
      aSelectors[i] = "[" + aSelectors[i] + "]";
    }
    
    var sSelectors = aSelectors.join(", ");
    $(sSelectors).remove();

  }

  removeByAttSubstring("id", "yui_", "_32", true, true, true);

});

Parameters:

  1. att: The target attribute (type: string).
  2. beginsWith: The starting value of the attribute (type: string).
  3. endsWith: The ending value of the attribute (type: string).
  4. bContains: Boolean indicating the use of * for both start and end values (type: boolean).
  5. bCaseInsensitive: True if case-insensitive match should be applied (type: boolean).
  6. bBeginsAndEndsWith: True for stacking attribute selectors (type: boolean).

Example Call:

  removeByAttSubstring("id", "yui_", "_32", true, true, true);

jsFiddle Demo


Notes:

  • Level 4 CSS selectors are employed here; browser support details can be checked here.

Answer №2

Here's a neat trick: combine attribute selectors in CSS. I made sure to include the !important directive in case this HTML is generated by a plugin with existing CSS styles. By using !important, you can prioritize your own CSS rules over others, unless they also use a more specific selector with !important. If they end up overriding CSS with JavaScript, you may need to come up with a JavaScript solution yourself.

div[id^="yui_"][id$="_32"] {
  display: none!important;
}
<div class="sm-eu-cookie-message" id="yui_3_8_0_1_1478235091986_32">= $0 “ FYI: This site uses cookies to make sure your visit is as awesome as possible. “
  <a class="headermessagelink" target="_blank" href="http://help.smugmug.com/customer/portal/articles/93251">What and why?</a>
  <div class="sm-eu-cookie-close">
    <button type="button" class="sm-button sm-button-size-small sm-button-skin-default sm-button-nochrome">
      <span class="sm-fonticon sm-button-fonticon sm-fonticon-small sm-fonticon-XCross"></span>
    </button>
  </div>
</div>
<div class="sm-eu-cookie-message" id="yui_3_8_0_1_14782349638956_32">= $0 “ FYI: This site uses cookies to make sure your visit is as awesome as possible. “
  <a class="headermessagelink" target="_blank" href="http://help.smugmug.com/customer/portal/articles/93251">What and why?</a>
  <div class="sm-eu-cookie-close">
    <button type="button" class="sm-button sm-button-size-small sm-button-skin-default sm-button-nochrome">
      <span class="sm-fonticon sm-button-fonticon sm-fonticon-small sm-fonticon-XCross"></span>
    </button>
  </div>
</div>
<div class="sm-eu-cookie-message" id="yui_3_8_0_1_147834534532_32">= $0 “ FYI: This site uses cookies to make sure your visit is as awesome as possible. “
  <a class="headermessagelink" target="_blank" href="http://help.smugmug.com/customer/portal/articles/93251">What and why?</a>
  <div class="sm-eu-cookie-close">
    <button type="button" class="sm-button sm-button-size-small sm-button-skin-default sm-button-nochrome">
      <span class="sm-fonticon sm-button-fonticon sm-fonticon-small sm-fonticon-XCross"></span>
    </button>
  </div>
</div>
<div class="sm-eu-cookie-message" id="yui_3_8_0_1_147834534532_33">NOT HIDDEN
  <a class="headermessagelink" target="_blank" href="http://help.smugmug.com/customer/portal/articles/93251">What and why?</a>
  <div class="sm-eu-cookie-close">
    <button type="button" class="sm-button sm-button-size-small sm-button-skin-default sm-button-nochrome">
      <span class="sm-fonticon sm-button-fonticon sm-fonticon-small sm-fonticon-XCross"></span>
    </button>
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing the malfunction of my Superfish menu in Firefox?

I am currently experimenting with the Superfish jQuery plugin to improve a drop-down menu on my website. Unfortunately, in Firefox browser (v. 21.0), the drop-down menu does not open when hovering over it as expected. However, it works fine in Chrome and O ...

Shorten a data point in JSON code

I'm currently in the process of developing a stock widget that utilizes JSON to retrieve data from the Yahoo API/YQL console. I am specifically working with values extracted from the key ChangePercentRealtime, however, the values are longer than what ...

Discover the art of implementing linear gradient backgrounds in Tailwind css effortlessly

Is there a way to incorporate this specific color scheme into my background? linear-gradient(355.45deg, #FFFFFF 11.26%, rgba(255, 255, 255, 0) 95.74%) I have knowledge on how to implement this color using regular CSS, but I'm curious about how it can ...

Executing a function every time a prop is updated within the component

I have a prop named transcript in one of my components. Whenever I speak a voice intent, it gets updated. I want to execute a function every time the transcript changes and pass the transcript as an argument. In this code snippet, I attempted to use an On ...

Implementing dynamic title insertion into a popover element using jQuery

My goal is to assign a title to my popover object in a local project. I have already included the following files: bootstrap.css v4.2.1 jquery.min.js v2.2.0 bootstrap.min.js v4.2.1 popper.min.js v1.11.0 Initially, there was a basic button present. <i ...

How can you align the login form fields in a single row?

I am looking to align the login fields in my form so that they are on the same row, similar to the layout seen here. I have tried various CSS codes, but have not been able to achieve the alignment I desire. This is how my current login form looks: https: ...

Please send the element that is specifically activated by the onClick function

This is the HTML code I am working with: <li class="custom-bottom-list"> <a onClick="upvote(this)"><i class="fa fa-thumbs-o-up"></i><span>upvote</span></a> </li> Here is my JavaScript function for Upvot ...

Issue with div:after transition not functioning in Safari

The transition effect on div:after does not function properly in Safari. Below is the code for reference: HTML: .bbtn { width: 151px; height: 48px; background: #ef2e41; margin: auto; margin-top: 32px; -webkit-transition: all 0.3s ease-in-ou ...

Conceal the page until it has completely finished loading

Similar Question: How can I delay displaying a webpage until it is fully loaded? I have a basic website and I want to delay its opening for X milliseconds to ensure that everything, such as images, is fully loaded. Is there a way to do this for the en ...

The XPath for the element that comes before a specific text

Looking for the XPath of the following code snippet: <a class="account-name" long-text="Sample Text" ng-click="accountClick(account)" ng-href="sample URL" href="sample URL1"> <span>Plan Name</span></a> I attempted this: //span[te ...

What is the easiest way to import a CSS file into just one specific page in Ionic2?

Take, for instance, my desire to import the following: <link rel="stylesheet" href="mycss.css"> into example_page. I attempted importing it from index.html, but I fear that by doing so, the CSS will be loaded on every page each time I run my app. I ...

Fix a bug in IE6 by ensuring that an element takes up 100% of the body when contained within

It appears that this issue is specific to IE6, as the code functions correctly in all other browsers. Check out the jsFiddle link with the HTML code: <div style="background:blue; width:600px; margin-left:auto; margin-right:auto;"> BLUE < ...

Having trouble finding a match between the local storage string and the URL string

Let me explain the concept I'm working on: Imagine you have a simple nested list: <ul> <li>chevy</li> <li>dodge</li> <li id="trucks">ford <ul> <li>ranger</li> <l ...

When a Vue.js datepicker is set as required, it can be submitted even if

When using the vuejs-datepicker, setting the html required attribute on input fields may not work as expected. This can lead to the form being submitted without an input value. <form> <datepicker placeholder="Select Date" required></datep ...

Should I use CSS, Bootstrap, or a combination of both for a complicated design in a React app

I am currently developing a react app and utilizing react-bootstrap to incorporate various components. As someone who is new to javascript, react, and web development as a whole, I find it challenging at times. My goal was to create a layout that seemed q ...

The CSS stylesheets are not compatible with IE7 and IE8

I am encountering an issue with my local site where the CSS links are not being included in the page, particularly in IE7 and IE8. The technologies I am using include WordPress 3.6, Bootstrap 3, Modernizr, and jQuery. Source Code <!DOCTYPE HTML PUBLI ...

"Adjusting the background color based on the current day using PHP and CSS

Check out the screenshot of my result here. I'm currently experiencing an issue with the code while trying to modify a calendar. The error seems to be originating from tablerow.php, as I've indicated in the comments. I am attempting to change the ...

Creating a custom CSS counter-style reminiscent of the toLocaleString function

Is there a way to customize the list counter style to display commas in the thousands place, similar to number.toLocaleString()? Or perhaps even adjust based on locale to show either 1,234 or 1.234? I have checked the Predefined counter styles but did not ...

What is the simplest method for generating a datatable?

What is the best way to set up a datatables table? I've tried implementing datatables features like search, pagination, dropdowns, and sorting in my existing table, but it's not functioning properly. Even though I'm using the CDN versions o ...

What's the best way to eliminate letter-spacing only for the final letter of an element using CSS?

Displayed above is the image in question from my HTML page. The text menu is contained within a right-aligned div, with a letter spacing of 1.2em. Is there a way to target this specific element with a pseudo-selector? I'd prefer not to use relative po ...