Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
653 views
in Technique[技术] by (71.8m points)

html - Can I programmatically determine a property value for a series of CSS classes?

I need help understanding an easier way to write this.

I have classes that have a number between 1-100 at the end of it. So for example:

.select1 {
    padding: 1em;
}

.select2 {
    padding: 2em;
}

.select3 {
    padding: 3em;
}

etc.

I want to match the number with the amount I want to add, but have no idea how to go about it in CSS. Instead of actually writing them out is there a more object oriented way of doing this in CSS?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use a CSS Pre-processor like SASS or LESS, with variables.

For example, with SASS you could do a for loop:

$class-slug: select !default

@for $i from 1 through 100
  .#{$class-slug}-#{$i}
    padding: $i + 0em

Which would output what you're looking for.

It's not a pure CSS solution, but it beats doing each one by hand. Otherwise you could also do this with Javascript, but that's not ideal.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...