0

Following is the code

<html>
<head>
    <title>Facing issue while applying css tranform properties to DOM object using multiple css classes</title>
    <!-- PUT A PATH OF JQUERY LIBRARY -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
    window.onload = function () {
        var flag = true;
        setInterval(function () {
            if (flag) {
                flag = false;
                $("#spelling_game_score_box_score").addClass("active_score");
            }
            else {
                flag = true;
                $("#spelling_game_score_box_score").removeClass("active_score");
            }
        },2000);
    }
</script>
<style>
    #spelling_game_score_box_score {
        position:absolute;
        top:200px;
        left:200px;
        height: 100px;
        width: 100px;
        transform: rotate(-30deg);
        -webkit-transform: rotate(-30deg);
        -moz-transform: rotate(-30deg);
        -ms-transform: rotate(-30deg);
        font-size: 36px;
        background-color: orange;
    }
    .active_score {
        transform: scale(2);
        -webkit-transform: scale(2);
        -moz-transform: scale(2);
        -ms-transform: scale(2);
    }
    </style>
</head>
<body>
    <div id="spelling_game_score_box_score">100</div>
</body>
</html>

Now, I run the code I can not find scale() effect after interval of 2000 ms. But when I comment following css under #spelling_game_score_box_score selector

/*transform: rotate(-30deg);
  -webkit-transform: rotate(-30deg);
  -moz-transform: rotate(-30deg);
  -ms-transform: rotate(-30deg);*/

I can have scale effect but can not find rotate effect. I want to know that is there some sub properties for css tranform like border has border-color, border-style etc. so I can have some solution regarding the problem. Please help me if you have some idea about it. Thank you.

1 Answer 1

1

There are two problems here:

  1. CSS Specificity
  2. transform css code

First, you use id to specify the transform functions:

#spelling_game_score_box_score {
    ...
    transform: rotate(-30deg);
    ...
}

Then, you try to override it by specifying a class:

.active_score {
    transform: scale(2);
}

which wouldn't work because of the CSS specificity problem.

You can solve this by making it more specific:

#spelling_game_score_box_score.active_score {
    transform: scale(2);
}

But then we come to the second problem where transform: scale(2); overrides the original transformation.

To solve this you can use both rotate and scale in the same transformation.

#spelling_game_score_box_score.active_score {
    transform: rotate(-30deg) scale(2);
}

Here is a working fiddle: https://jsfiddle.net/uopfapd3/

Sign up to request clarification or add additional context in comments.

2 Comments

I need to apply .active_score on multiple location so rotate effect will be applied on every dom where i apply .active_score class. So there will be another issue i will face.
Then I would suggest that instead of using an id earlier, you use another class. e.g. .game_score_box_score

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.