0

Developing my own grid system, I decided to use jQuery instead of adding classes for every width or height. I found the code below from here to change the width of divs with jQuery, based on their classname:

$("[class*='tul-']").each(function(){
    $(this).css("width", 100 / $(this).attr("class").substring(4,5) + "%");
});

So, something like: <div class="tul-5"></div> should have a 100 / 5 + "%" = 20% width.

The problem is that jQuery adds inline-css instead of changing the value of width property in class (tul-5) itself. The <div class="tul-5"></div> becomes <div class="tul-5" style="width: 20%"></div>. What I want is:

tul-5 {
    width: 20%; /*change this*/
}

I want to get rid of inline-css because of SEO and the mess it makes.

Things I tried:

I tried creating classes with a default width size, but no luck:

.tul-1, .tul-2, .tul-3 {
    width: 50px;
}

I've also tried pure Js code from the page mentioned above, but it also adds inline-css.

P.S.: I haven't declared any classes starting with tul.

7
  • 3
    If you're developing your own grid system you don't need javascript. This can be done with SASS or LESS. Look how it's implemented in Bootstrap or Material-UI, there are source files in sass and less Commented May 6, 2018 at 11:59
  • 1
    What does inline css have to do with SEO? Commented May 6, 2018 at 12:14
  • can you share complete code? Commented May 6, 2018 at 15:14
  • 1
    Possible duplicate of How modify a property of external CSS file using jQuery / Javascript Commented May 6, 2018 at 15:16
  • @lomboboo I was thinking about Javascript because I have a lot of classes with same syntax (like bg-{color}) and generating them automatically helps a lot. Due to your recommendation, I checked both SASS and LESS and found ways in both to generate classes, so I'm going to use SASS. Many Thanks! Commented May 7, 2018 at 15:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.