[CSS] 客製化 HTML 元件樣式

  1. <!DOCTYPE html>
  2. <html>
  3. <style>
  4. /* The container */
  5. .container {
  6. display: block;
  7. position: relative;
  8. padding-left: 35px;
  9. margin-bottom: 12px;
  10. cursor: pointer;
  11. font-size: 22px;
  12. -webkit-user-select: none;
  13. -moz-user-select: none;
  14. -ms-user-select: none;
  15. user-select: none;
  16. }
  17.  
  18. /* Hide the browser's default checkbox */
  19. .container input {
  20. position: absolute;
  21. opacity: 0;
  22. cursor: pointer;
  23. height: 0;
  24. width: 0;
  25. }
  26.  
  27. /* Create a custom checkbox */
  28. .checkmark {
  29. position: absolute;
  30. top: 0;
  31. left: 0;
  32. height: 25px;
  33. width: 25px;
  34. background-color: #eee;
  35. }
  36.  
  37. /* On mouse-over, add a grey background color */
  38. .container:hover input ~ .checkmark {
  39. background-color: #ccc;
  40. }
  41.  
  42. /* When the checkbox is checked, add a blue background */
  43. .container input:checked ~ .checkmark {
  44. background-color: #2196F3;
  45. }
  46.  
  47. /* Create the checkmark/indicator (hidden when not checked) */
  48. .checkmark:after {
  49. content: "";
  50. position: absolute;
  51. display: none;
  52. }
  53.  
  54. /* Show the checkmark when checked */
  55. .container input:checked ~ .checkmark:after {
  56. display: block;
  57. }
  58.  
  59. /* Style the checkmark/indicator */
  60. .container .checkmark:after {
  61. left: 9px;
  62. top: 5px;
  63. width: 5px;
  64. height: 10px;
  65. border: solid white;
  66. border-width: 0 3px 3px 0;
  67. -webkit-transform: rotate(45deg);
  68. -ms-transform: rotate(45deg);
  69. transform: rotate(45deg);
  70. }
  71. </style>
  72. <body>
  73.  
  74. <h1>Custom Checkboxes</h1>
  75. <label class="container">One
  76. <input type="checkbox" checked="checked">
  77. <span class="checkmark"></span>
  78. </label>
  79. <label class="container">Two
  80. <input type="checkbox">
  81. <span class="checkmark"></span>
  82. </label>
  83. <label class="container">Three
  84. <input type="checkbox">
  85. <span class="checkmark"></span>
  86. </label>
  87. <label class="container">Four
  88. <input type="checkbox">
  89. <span class="checkmark"></span>
  90. </label>
  91.  
  92. </body>
  93. </html>
Demo


  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <style>
  6. /*the container must be positioned relative:*/
  7. .custom-select {
  8. position: relative;
  9. font-family: Arial;
  10. }
  11.  
  12. .custom-select select {
  13. display: none; /*hide original SELECT element:*/
  14. }
  15.  
  16. .select-selected {
  17. background-color: DodgerBlue;
  18. }
  19.  
  20. /*style the arrow inside the select element:*/
  21. .select-selected:after {
  22. position: absolute;
  23. content: "";
  24. top: 14px;
  25. right: 10px;
  26. width: 0;
  27. height: 0;
  28. border: 6px solid transparent;
  29. border-color: #fff transparent transparent transparent;
  30. }
  31.  
  32. /*point the arrow upwards when the select box is open (active):*/
  33. .select-selected.select-arrow-active:after {
  34. border-color: transparent transparent #fff transparent;
  35. top: 7px;
  36. }
  37.  
  38. /*style the items (options), including the selected item:*/
  39. .select-items div,.select-selected {
  40. color: #ffffff;
  41. padding: 8px 16px;
  42. border: 1px solid transparent;
  43. border-color: transparent transparent rgba(0, 0, 0, 0.1) transparent;
  44. cursor: pointer;
  45. user-select: none;
  46. }
  47.  
  48. /*style items (options):*/
  49. .select-items {
  50. position: absolute;
  51. background-color: DodgerBlue;
  52. top: 100%;
  53. left: 0;
  54. right: 0;
  55. z-index: 99;
  56. }
  57.  
  58. /*hide the items when the select box is closed:*/
  59. .select-hide {
  60. display: none;
  61. }
  62.  
  63. .select-items div:hover, .same-as-selected {
  64. background-color: rgba(0, 0, 0, 0.1);
  65. }
  66. </style>
  67. </head>
  68.  
  69. <body>
  70.  
  71. <h2>Custom Select</h2>
  72.  
  73. <!--surround the select box with a "custom-select" DIV element. Remember to set the width:-->
  74. <div class="custom-select" style="width:200px;">
  75. <select>
  76. <option value="0">Select car:</option>
  77. <option value="1">Audi</option>
  78. <option value="2">BMW</option>
  79. <option value="3">Citroen</option>
  80. <option value="4">Ford</option>
  81. <option value="5">Honda</option>
  82. <option value="6">Jaguar</option>
  83. <option value="7">Land Rover</option>
  84. <option value="8">Mercedes</option>
  85. <option value="9">Mini</option>
  86. <option value="10">Nissan</option>
  87. <option value="11">Toyota</option>
  88. <option value="12">Volvo</option>
  89. </select>
  90. </div>
  91.  
  92. <script>
  93. var x, i, j, selElmnt, a, b, c;
  94. /*look for any elements with the class "custom-select":*/
  95. x = document.getElementsByClassName("custom-select");
  96. for (i = 0; i < x.length; i++) {
  97. selElmnt = x[i].getElementsByTagName("select")[0];
  98. /*for each element, create a new DIV that will act as the selected item:*/
  99. a = document.createElement("DIV");
  100. a.setAttribute("class", "select-selected");
  101. a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
  102. x[i].appendChild(a);
  103. /*for each element, create a new DIV that will contain the option list:*/
  104. b = document.createElement("DIV");
  105. b.setAttribute("class", "select-items select-hide");
  106. for (j = 1; j < selElmnt.length; j++) {
  107. /*for each option in the original select element,
  108. create a new DIV that will act as an option item:*/
  109. c = document.createElement("DIV");
  110. c.innerHTML = selElmnt.options[j].innerHTML;
  111. c.addEventListener("click", function(e) {
  112. /*when an item is clicked, update the original select box,
  113. and the selected item:*/
  114. var y, i, k, s, h;
  115. s = this.parentNode.parentNode.getElementsByTagName("select")[0];
  116. h = this.parentNode.previousSibling;
  117. for (i = 0; i < s.length; i++) {
  118. if (s.options[i].innerHTML == this.innerHTML) {
  119. s.selectedIndex = i;
  120. h.innerHTML = this.innerHTML;
  121. y = this.parentNode.getElementsByClassName("same-as-selected");
  122. for (k = 0; k < y.length; k++) {
  123. y[k].removeAttribute("class");
  124. }
  125. this.setAttribute("class", "same-as-selected");
  126. break;
  127. }
  128. }
  129. h.click();
  130. });
  131. b.appendChild(c);
  132. }
  133. x[i].appendChild(b);
  134. a.addEventListener("click", function(e) {
  135. /*when the select box is clicked, close any other select boxes,
  136. and open/close the current select box:*/
  137. e.stopPropagation();
  138. closeAllSelect(this);
  139. this.nextSibling.classList.toggle("select-hide");
  140. this.classList.toggle("select-arrow-active");
  141. });
  142. }
  143. function closeAllSelect(elmnt) {
  144. /*a function that will close all select boxes in the document,
  145. except the current select box:*/
  146. var x, y, i, arrNo = [];
  147. x = document.getElementsByClassName("select-items");
  148. y = document.getElementsByClassName("select-selected");
  149. for (i = 0; i < y.length; i++) {
  150. if (elmnt == y[i]) {
  151. arrNo.push(i)
  152. } else {
  153. y[i].classList.remove("select-arrow-active");
  154. }
  155. }
  156. for (i = 0; i < x.length; i++) {
  157. if (arrNo.indexOf(i)) {
  158. x[i].classList.add("select-hide");
  159. }
  160. }
  161. }
  162. /*if the user clicks anywhere outside the select box,
  163. then close all select boxes:*/
  164. document.addEventListener("click", closeAllSelect);
  165. </script>
  166.  
  167. </body>
  168. </html>
Demo

沒有留言:

技術提供:Blogger.