当前位置:   article > 正文

css例子:按钮代码的修改_button class css

button class css

按钮代码的修改并因此引发的问题

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>money</title>
  6. <style>
  7. button{
  8. padding:6px 16px;
  9. border:1px solid #446d88;
  10. background:#58a linear-gradient(#77a0bb,#58a);
  11. border-radius:4px;
  12. box-shadow:0 1px 5px gray;
  13. color:white;
  14. text-shadow:0-1px 1px #335166;
  15. font-size:20px;
  16. line-height:30px;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <button>yes</button>
  22. </body>
  23. </html>

存在情况1:假设当我需要更改字体大小时,会发现图像的效果存在了问题

字体大小的更改用f12那个地方进行调整演示,而不是要实际更改数值,会更明确发现它存在的缺陷

为了解决这个问题,我们需要调整的有两个地方,行高和字体大小。

因此有了一个结论,当某些值相互依赖的时候,应该把他们的相互关系用代码表示出来

根据代码可以计算出来,行高是字体的1.5倍(30/20=1.5)

因此,现在可以把line-height的值设为1.5(这里其实不是很明白)

但是,还是存在了问题,把值设为固定值,每改一次,就需要重新进行更改了一次。所有用百分比或者em会好很多。把长度值改为em单位,效果的值就变成可以缩放的了。

这样,就可以在一处控制按钮的所有尺寸样式。

现在的效果就比较ok了

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>money</title>
  6. <style>
  7. /* 代码里的单位换算还没懂如何换算的 */
  8. button{
  9. padding:.3em .8em;
  10. border:1px solid #446d88;
  11. background:#58a linear-gradient(#77a0bb,#58a);
  12. border-radius:.2em;
  13. box-shadow:0 .05em .25em gray;
  14. color:white;
  15. text-shadow:0-0.5em 0.5em #335166;
  16. /* 假设父级的字号是16px */
  17. font-size:125%;
  18. line-height:1.5;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <button>yes</button>
  24. </body>
  25. </html>

存在问题2:颜色更改问题

假设需要创造一个红色的取消按钮,或者一个绿色的确定按钮

难点在于:border-color background box-shadow text-shaow四个值的调整

                  根据按钮的亮面和暗面相对于主色调的变亮和变暗程度来推导其他颜色的亮色和暗色版本。

                 把按钮放在非白色的背景上如何设置呢?

解题思路如下:把半透明的黑色或者白色叠加在主色调上,就可以产生主色调的亮色和暗色变体了。

也没有很理解

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>money</title>
  6. <style>
  7. button{
  8. padding:.3em .8em;
  9. border:1px solid rgba(0,0,0,.1);
  10. /* 推荐使用hsla,而不是rgba来产生半透明的白色,因为它的字符长度更短,敲起来也更快,这归功于它的重复度更低 */
  11. /* linear-gradient 线性渐变 transparent 全透明 */
  12. background:#58a linear-gradient(hsla(0,0%,100%,.2),transparent);
  13. border-radius:.2em;
  14. /* 边框阴影 */
  15. box-shadow:0 .05em .25em rgba(0,0,0,0.5);
  16. color:white;
  17. /* 文本阴影 */
  18. text-shadow:0-0.5em 0.5em rgba(0,0,0,.5);
  19. font-size:125%;
  20. line-height:1.5;
  21. }
  22. /* 更改背景色,得到其他颜色版本的按钮 */
  23. button.cancel{
  24. background-color: #c00;
  25. }
  26. button.ok{
  27. background-color: #6b0;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <button>yes</button>
  33. <button class="ok">ok</button>
  34. <button class="cancel">cancel</button>
  35. </body>
  36. </html>

代码易维护和代码量少不可兼得

eg:为每一个元素添加10px宽的边框,但是左侧不加边框

分开写后,只用更改border-width的值

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>money</title>
  6. <style>
  7. button{
  8. padding:.3em .8em;
  9. border:1px solid rgba(0,0,0,.1);
  10. background:#58a linear-gradient(hsla(0,0%,100%,.2),transparent);
  11. border-radius:.2em;
  12. box-shadow:0 .05em .25em rgba(0,0,0,0.5);
  13. color:white;
  14. text-shadow:0-0.5em 0.5em rgba(0,0,0,.5);
  15. font-size:125%;
  16. line-height:1.5;
  17. /* 目的 为一个元素添加一个10px宽的边框 */
  18. /* 但下面这种方式日后改变边框的宽度,需要同时改变3个地方 */
  19. /* border-width:10px 10px 10px 0; */
  20. /* 下面这样改变会方便些 */
  21. border-width:10px;
  22. border-left-width:0;
  23. }
  24. button.cancel{
  25. background-color: #c00;
  26. }
  27. button.ok{
  28. background-color: #6b0;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <button>yes</button>
  34. <button class="ok">ok</button>
  35. <button class="cancel">cancel</button>
  36. </body>
  37. </html>

内容来自书css揭秘

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/297208
推荐阅读
相关标签
  

闽ICP备14008679号