web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.5 KiB

  1. # CSS Has Pseudo [<img src="http://jonathantneal.github.io/js-logo.svg" alt="" width="90" height="90" align="right">][CSS Has Pseudo]
  2. [![NPM Version][npm-img]][npm-url]
  3. [![Build Status][cli-img]][cli-url]
  4. [![Support Chat][git-img]][git-url]
  5. [CSS Has Pseudo] lets you style elements relative to other elements in CSS,
  6. following the [Selectors Level 4] specification.
  7. ```css
  8. a:has(> img) {
  9. /* style links that contain an image */
  10. }
  11. h1:has(+ p) {
  12. /* style level 1 headings that are followed by a paragraph */
  13. }
  14. section:not(:has(h1, h2, h3, h4, h5, h6)) {
  15. /* style sections that don’t contain any heading elements */
  16. }
  17. body:has(:focus) {
  18. /* style the body if it contains a focused element */
  19. }
  20. ```
  21. ## Usage
  22. From the command line, transform CSS files that use `:has` selectors:
  23. ```bash
  24. npx css-has-pseudo SOURCE.css TRANSFORMED.css
  25. ```
  26. Next, use your transformed CSS with this script:
  27. ```html
  28. <link rel="stylesheet" href="TRANSFORMED.css">
  29. <script src="https://unpkg.com/css-has-pseudo/browser"></script>
  30. <script>cssHasPseudo(document)</script>
  31. ```
  32. That’s it. The script is 765 bytes and works in all browsers, including
  33. Internet Explorer 11. With a [Mutation Observer polyfill], the script will work
  34. down to Internet Explorer 9.
  35. ## How it works
  36. The [PostCSS plugin](README-POSTCSS.md) clones rules containing `:has`,
  37. replacing them with an alternative `[:has]` selector.
  38. ```css
  39. body:has(:focus) {
  40. background-color: yellow;
  41. }
  42. section:not(:has(h1, h2, h3, h4, h5, h6)) {
  43. background-color: gray;
  44. }
  45. /* becomes */
  46. body[\:has\(\:focus\)] {
  47. background-color: yellow;
  48. }
  49. body:has(:focus) {
  50. background-color: yellow;
  51. }
  52. section[\:not-has\(h1\,\%20h2\,\%20h3\,\%20h4\,\%20h5\,\%20h6\)] {
  53. background-color: gray;
  54. }
  55. section:not(:has(h1, h2, h3, h4, h5, h6)) {
  56. background-color: gray;
  57. }
  58. ```
  59. Next, the [JavaScript library](README-BROWSER.md) adds a `[:has]` attribute to
  60. elements otherwise matching `:has` natively.
  61. ```html
  62. <body :has(:focus)>
  63. <input value="This element is focused">
  64. </body>
  65. ```
  66. [cli-img]: https://img.shields.io/travis/csstools/css-has-pseudo/master.svg
  67. [cli-url]: https://travis-ci.org/csstools/css-has-pseudo
  68. [git-img]: https://img.shields.io/badge/support-chat-blue.svg
  69. [git-url]: https://gitter.im/postcss/postcss
  70. [npm-img]: https://img.shields.io/npm/v/css-has-pseudo.svg
  71. [npm-url]: https://www.npmjs.com/package/css-has-pseudo
  72. [CSS Has Pseudo]: https://github.com/csstools/css-has-pseudo
  73. [Mutation Observer polyfill]: https://github.com/webmodules/mutation-observer
  74. [Selectors Level 4]: https://drafts.csswg.org/selectors-4/#has-pseudo