How to Target SVG Images Without Direct Class Access in WordPress
Content
Many WordPress users rely on the SVG Support plugin to safely use and inline SVG files. A common challenge arises when you need to target an SVG image for inline rendering but cannot directly add the required CSS class to the <img> tag itself. This is often the case when working with third-party page builders like Visual Composer, specific themes, or other plugins that generate the HTML output automatically.
Understanding the Problem
By default, the SVG Support plugin uses a specific CSS class (usually style-svg) applied directly to an <img> tag to identify which images should be replaced with inline SVG code. The problem occurs when your theme or builder does not provide an option to add classes to individual image tags, only to their parent containers. For example, your HTML might look like this:
<div class="svg-icon">
<img src="image-source.svg"/>
</div>
Without direct access to the <img> tag, you couldn't historically trigger the inline SVG replacement.
The Solution: Parent Element Targeting (Available in v2.4+)
Good news! The SVG Support team addressed this exact limitation in a significant update. As of version 2.4, the plugin's functionality was expanded. You can now apply the target CSS class (e.g., style-svg) to any parent element. The plugin will then search through all children of that element for any <img> tags with an SVG source and perform the inline replacement.
This means for the example above, you would simply add the class style-svg to the <div>:
<div class="svg-icon style-svg">
<img src="image-source.svg"/>
</div>
The plugin will find the image within the div and process it. This makes the plugin compatible with almost any theme or page builder that allows you to add classes to wrapping elements.
How to Implement This on Your Site
- Update the Plugin: Ensure you are running SVG Support version 2.4 or higher. You can check your current version and update it from your WordPress admin dashboard under Plugins > Installed Plugins.
- Identify the Parent Element: Use your browser's inspector tool to find the class or ID of the element wrapping your SVG image. This could be a
<div>,<figure>, or any other container. - Add the Target Class: Using your theme, page builder, or custom HTML block, add the class
style-svgto that parent element. The exact method for adding a class will depend on your specific builder (look for an "Advanced" or "CSS Class" field in the module's settings).
Important Considerations
- This method only works for inlining SVGs. It is not needed if you simply want to use SVG files like standard images without inline replacement.
- When the
<img>tag is replaced by the inline<svg>code, any classes that were on the original<img>tag are not preserved. If you need to style the final SVG, you should add those classes directly to the SVG file's code or to the parent container that remains on the page. - This feature is designed for static content. If you are dynamically adding images to the page using JavaScript after it has loaded, the plugin may not be able to target them automatically.
This enhancement provides a powerful and flexible way to control SVG inlining, overcoming one of the most frequent obstacles users faced with page builders and restrictive themes.
Related Support Threads Support
-
CSS Class to targethttps://wordpress.org/support/topic/css-class-to-target/
-
Get elements by wrapping classhttps://wordpress.org/support/topic/get-elements-by-wrapping-class/
-
Target multiple class names?https://wordpress.org/support/topic/target-multiple-class-names/
-
Target element created by jQueryhttps://wordpress.org/support/topic/target-element-created-by-jquery/
-
[Suggestion] Add more “CSS Class to target” optionshttps://wordpress.org/support/topic/suggestion-add-more-css-class-to-target-options/
-
Add SVG to buttonshttps://wordpress.org/support/topic/add-svg-to-buttons-2/
-
Prevent srcsethttps://wordpress.org/support/topic/prevent-srcset/
-
Target child img tags within parent?https://wordpress.org/support/topic/target-child-img-tags-within-parent/
-
Preserve other CSS classeshttps://wordpress.org/support/topic/preserve-other-css-classes/