QUIZGUM

Coding Class

Quizgum : control css

Changing css value with jQuery

You can also change the css value using jQuery.

For example, a div with a horizontal value of 100px can be changed to 500px. using jQuery !!!!

  1. $('SELECTOR').css();

If you are watching this course, I am going through the html css course at quizgum, and I am looking at this jQuery course, so I think you know css.

How do I specify font color values ​​in css?

  1. selector{color:red}

So in jQuery?

  1. $('SELECTOR').css('color','red');

It is expressed as above. css ('CSS attribute', 'value');

So let's learn by example.

situation 1

Let's make the phrase 100px, 200px, 300px and make the horizontal value of a specific div change to 100px, 200px, 300px when clicked ^^

Now that we have studied variable declarations, let's declare them.

  1. $(function(){
  2. var box = $('.box');
  3. var btn_100px = $('.btn_100px');
  4. var btn_200px = $('.btn_200px');
  5. var btn_300px = $('.btn_300px');
  6.  
  7. btn_100px.click(function(){
  8. box.css('width','100px');
  9. });
  10.  
  11. btn_200px.click(function(){
  12. box.css('width','200px');
  13. });
  14.  
  15. btn_300px.click(function(){
  16. box.css('width','300px');
  17. });
  18. });

Write the source as above.

So let's size the box 50px wide by 20px high and the background yellow.

  1. css source :
  2.  
  3. .box{width:50px;height:20px;background:yellow}
  4. .btn{cursor:pointer}
  5.  
  6. html source :
  7. <div class="box"></div>
  8. <p class="btn btn_100px">100px</p>
  9. <p class="btn btn_200px">200px</p>
  10. <p class="btn btn_300px">300px</p>


Let's test and learn through hands-on examples.

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>jQuery</title>
  6. <style>
  7. .box{width:50px;height:20px;background:yellow}
  8. .btn{cursor:pointer}
  9. </style>
  10. <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.0.min.js" ></script>
  11. <script type="text/javascript">
  12. $(function(){
  13. var box = $('.box');
  14. var btn_100px = $('.btn_100px');
  15. var btn_200px = $('.btn_200px');
  16. var btn_300px = $('.btn_300px');
  17.  
  18. btn_100px.click(function(){
  19. box.css('width','100px');
  20. });
  21.  
  22. btn_200px.click(function(){
  23. box.css('width','200px');
  24. });
  25.  
  26. btn_300px.click(function(){
  27. box.css('width','300px');
  28. });
  29. });
  30. </script>
  31. </head>
  32. <body>
  33. <div class="box"></div>
  34. <p class="btn btn_100px">100px</p>
  35. <p class="btn btn_200px">200px</p>
  36. <p class="btn btn_300px">300px</p>
  37. </body>
  38. </html>

It is possible to control css with jQuery like this.
float, display, width, height, color, background, margin, padding, etc. Available.

Practice by controlling various css elements yourself. ^^