{"id":147262,"date":"2023-04-13T07:55:01","date_gmt":"2023-04-13T07:55:01","guid":{"rendered":"https:\/\/developer.wordpress.org\/block-editor\/reference-guides\/block-api\/block-selectors\/"},"modified":"2024-10-30T17:09:45","modified_gmt":"2024-10-30T17:09:45","slug":"block-selectors","status":"publish","type":"blocks-handbook","link":"https:\/\/developer.wordpress.org\/block-editor\/reference-guides\/block-api\/block-selectors\/","title":{"rendered":"Selectors"},"content":{"rendered":"
Block Selectors is the API that allows blocks to customize the CSS selector used
\nwhen their styles are generated.<\/p>\n
A block may customize its CSS selectors at three levels: root, feature, and
\nsubfeature.<\/p>\n
The root selector is the block’s primary CSS selector.<\/p>\n
All blocks require a primary CSS selector for their style declarations to be Feature selectors relate to styles for a block support, e.g. border, color, A block may wish to apply the styles for specific features to different These selectors relate to individual styles provided by a block support e.g. A subfeature can have styles generated under its own unique selector. This is A great example of this is Rather than specify a CSS selector for every subfeature, you can set a single A selector that hasn’t been configured for a specific feature will fall back to Rather than repeating selectors for multiple subfeatures, you can set the The For a subfeature such as
\nincluded under. If one is not provided through the Block Selectors API, a
\ndefault is generated in the form of .wp-block-<name><\/code>.<\/p>\n
Example<\/h3>\n
{\n ...\n \"selectors\": {\n \"root\": \".my-custom-block-selector\"\n }\n}\n<\/code><\/pre>\n
Feature selectors<\/h2>\n
\ntypography, etc.<\/p>\n
\nelements within a block. An example might be using colors on the block’s wrapper
\nbut applying the typography styles to an inner heading only.<\/p>\nExample<\/h3>\n
{\n ...\n \"selectors\": {\n \"root\": \".my-custom-block-selector\",\n \"color\": \".my-custom-block-selector\",\n \"typography\": \".my-custom-block-selector > h2\"\n }\n}\n<\/code><\/pre>\n
Subfeature selectors<\/h2>\n
\nbackground-color<\/code><\/p>\n
\nespecially useful where one block support subfeature can’t be applied to the
\nsame element as the support’s other subfeatures.<\/p>\ntext-decoration<\/code>. Web browsers render this style
\ndifferently, making it difficult to override if added to a wrapper element. By
\nassigning text-decoration<\/code> a custom selector, its style can target only the
\nelements to which it should be applied.<\/p>\nExample<\/h3>\n
{\n ...\n \"selectors\": {\n \"root\": \".my-custom-block-selector\",\n \"color\": \".my-custom-block-selector\",\n \"typography\": {\n \"root\": \".my-custom-block-selector > h2\",\n \"text-decoration\": \".my-custom-block-selector > h2 span\"\n }\n }\n}\n<\/code><\/pre>\n
Shorthand<\/h2>\n
\nselector as a string value for the relevant feature. This is the approach
\ndemonstrated for the color<\/code> feature in the earlier examples above.<\/p>\n
Fallbacks<\/h2>\n
\nthe block’s root selector. Similarly, if a subfeature hasn’t had a custom
\nselector set, it will fall back to its parent feature’s selector and, if unavailable, fall back further to the block’s root selector.<\/p>\n
\ncommon selector as the parent feature’s root<\/code> selector and only define the
\nunique selectors for the subfeatures that differ.<\/p>\nExample<\/h3>\n
{\n ...\n \"selectors\": {\n \"root\": \".my-custom-block-selector\",\n \"color\": {\n \"text\": \".my-custom-block-selector p\"\n },\n \"typography\": {\n \"root\": \".my-custom-block-selector > h2\",\n \"text-decoration\": \".my-custom-block-selector > h2 span\"\n }\n }\n}\n<\/code><\/pre>\n
color.background-color<\/code> subfeature isn’t explicitly set in the above
\nexample. As the color<\/code> feature also doesn’t define a
root<\/code> selector,
\ncolor.background-color<\/code> would be included under the block’s primary root
\nselector, .my-custom-block-selector<\/code>.<\/p>\n
typography.font-size<\/code>, it would fallback to its parent
\nfeature’s selector given that is present, i.e. .my-custom-block-selector > h2<\/code>.<\/p>\n","protected":false},"author":0,"featured_media":0,"parent":134406,"menu_order":69,"template":"","meta":{"footnotes":""},"class_list":["post-147262","blocks-handbook","type-blocks-handbook","status-publish","hentry","type-handbook"],"revision_note":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/blocks-handbook\/147262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/blocks-handbook"}],"about":[{"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/types\/blocks-handbook"}],"version-history":[{"count":5,"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/blocks-handbook\/147262\/revisions"}],"predecessor-version":[{"id":152851,"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/blocks-handbook\/147262\/revisions\/152851"}],"up":[{"embeddable":true,"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/blocks-handbook\/134406"}],"wp:attachment":[{"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/media?parent=147262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}