0
I came up with a question that can be of help to many, the point is that in an html form we make a lot of use of style class (css) to make life easier, it turns out that creating classes for some situations gets boring and we often repeat the class but with another name for not remembering that we had already created something similar, for example
<style>
.w30px{ width: 30px; }
</style>
We do this because it is much easier than creating style inside the tag
<div class="w30px">TEXTO</div>
The natural sequence of creation is: we create the style file (css) then set the classes in the objects within the page as needed.
Great, it makes it a lot easier, but now comes the question:
Can you create the class at runtime according to the name of the class in the object? I would like to automate the creation process only by giving a name to the class and a function would create the style.
Example on onload page:
<script>
function CriaEstilos(objeto){
var w = substr($(objeto).prop("class"), 1, 2);
$(objeto).css("width", w);
}
</script>
I have not tested the above idea, it is merely illustrative, rsrs
But the idea is to automate the creation of simple classes with a common denominator, in this case the width of the object.
What would be the difference between this method and the direct inline specification of an element’s style (
style="width:10px"
)?– OnoSendai
There is no difference if we create a stylesheet (.css file) or place it directly on the object. What I want to do is facilitate development and avoid typos for example. Imagine that I have several formularies along the pages, and these formularies are for different properties but with fields of common sizes, so the automated creation of styles would make life much easier, as well as creating sheet styles instead of placing directly on the object. The idea is not to worry whether a style is created or not, simply put the name of the class.
– Marcelo
I understand, but if the class "w30px" has only one property ( "width:30px;") wouldn’t it be costly to create a class just for that? Its development time cost to quote
class="w30px"
would basically be the same asstyle="width:30px"
. It may be the lack of coffee, but I don’t see a direct benefit. Perhaps an example serves to illustrate your proposal.– OnoSendai
SASS and LESS do just what you are looking for, put their syntax is not so simple!
– Adriano Luz
My example is in the post, is that in day to day to develop Fields we adjust the sizes of the fields to arrive at a more elaborate layout, for that need to be adjusting field sizes and if I set one I have to go there and adjust another, It happens that when mecho in the style sheet changes all, including some that I do not remember, the use of a function for that would change only that form, however I would not need to keep typing much see this example of form: http://mvsoftware.com.br/exempleformulario-css.png
– Marcelo
@Adriano Luz, killed the cock, I think I was not able to express myself, but that’s it, pass variables through the class, example class="width(10px)" and in css . width{ width: $value }
– Marcelo