Can a dynamic class be styled using CSS? [duplicate]

Yes it is possible just by using CSS only.

Option #1 – Match by prefix value

  • Use CSS Class selector ^="class" which select all elements whose class is prefixed by “box-
[class^="box-"] {
  background: red;
  height: 100px;
  width: 100px;
  margin: 10px 0;
  display:block
}
<div class="box-159"></div>
<span class="box-147"></span>
<article class="box-76878"></article>

Option #2 – Match by contains at least one value

  • Use another CSS class selector *="class" (equivalent to CSS attribute selector) which select all elements whose class contains at least one substring “box-“.
[class*="box-"] {
  background: red;
  height: 100px;
  width: 100px;
  margin: 10px 0;
  display:block
}
<div class="box-159"></div>
<span class="box-147"></span>
<article class="box-76878"></article>

Leave a Comment