SVG Test: foreignObject
Using SVG to Create Responsive Thumbnail Images for Wizara Forms
The <foreignObject> element in SVG serves the purpose of embedding non-SVG content, primarily HTML and XML, into an SVG document. Its utility becomes apparent when there's a need to integrate HTML-based elements such as text, images, forms, or even entire web pages within SVG graphics. This feature offers versatility to SVG creators, allowing them to combine the strengths of SVG with other web technologies like CSS and JavaScript.
In terms of syntax, <foreignObject> is typically placed within an <svg> element. It functions as a container for the embedded HTML content. It allows specifying the width and height of the <foreignObject>, thereby determining the size of the content it encapsulates.
The content placed within <foreignObject> can consist of standard HTML and XML elements. When rendered by the user agent, which is usually a web browser, this content behaves as if it were an integral part of the SVG. This means it can be styled and interacted with using CSS and JavaScript, opening up a wide range of possibilities for creating interactive and data-rich visualizations.
In our first SVG-foreignObject test, we're utilizing the <foreignObject> element to seamlessly embed HTML forms from Wizara. This approach allows us to create responsive thumbnail images that serve as interactive previews of the forms.
The HTML forms are nested within the <foreignObject> tags, and when a user hovers their mouse over a thumbnail, we employ CSS to trigger a smooth scaling effect. As a result, the thumbnail expands, and the embedded form becomes more prominent, providing users with an enhanced and interactive viewing experience. The provided CSS code defines styles for an image within an .image-wrapper element and specifies how the image should behave when a user hovers over it.
We've implemented additional CSS styling to ensure that the thumbnails are visually appealing, responsive, and user-friendly. This includes padding, border styling, border-radius for rounded corners, and a background color with a slight transparency effect.
CSS: .image-wrapper { margin-bottom: 1rem; width: 100%; height: 200px; overflow: hidden; border-radius: 0.25rem; border: 1px solid #fff; background-color: rgba(255, 255, 255, 0.5); @media (min-width: 992px) { margin-bottom: 0; } } .image-wrapper img { width: 400%; height: 400%; transform: scale(0.25); transform-origin: 0% 0%; transition: all 3s ease; } .image-wrapper img:hover { width: 200%; height: 200%; transform: scale(0.5); transform-origin: 0% 0%; transition: all 1s ease; }
HTML: <div class="wrap mb-5"> <div class="row"> <div class="col-12 col-lg-3"> <div class="image-wrapper"> <img src="bill-of-lading-form.svg" alt="Form Thumbnail"> </div> </div> <div class="col-12 col-lg"> <div class="text-wrapper"> <h3>Demo: Cascade Bill of Lading Form</h3> <p><b>Fields (41): </b>htmlHeading |billTo ... |consigneeName</p> </div> </div> </div> </div>
This combination of SVG, HTML, and CSS techniques results in an engaging user interface where users can interact with the forms by simply hovering over the thumbnail images.
Last Updated: Oct 10, 2023