This solution uses SVG's <mask> element to create a text mask over an image. You'll be able to change the font size and the text content dynamically using JavaScript.
Key Concepts:
- SVG
<mask>: An SVG element that defines a mask. Elements inside the mask (like our <text>) determine the shape of the mask.
fill="white" in mask: For a mask, white areas are opaque (show through the masked content), and black areas are transparent (hide the masked content). Grayscale values create semi-transparency.
mask="url(#maskId)": The attribute used on the element you want to mask, pointing to the ID of your <mask> definition.
dominant-baseline="middle" and text-anchor="middle": Crucial for centering text in SVG, especially when its size changes, ensuring it stays visually centered at x="50%" and y="50%".
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SVG Text Mask with Dynamic Font Size</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0;
background-color: #f0f0f0;
color: #333;
}
.container {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
width: 90%;
max-width: 800px;
text-align: center;
}
h1 {
color: #2c3e50;
margin-bottom: 20px;
}
/* SVG Container Styling */
.mask-container {
width: 100%;
padding-bottom: 56.25%; /* 16:9 Aspect Ratio (9 / 16 * 100) */
position: relative;
background-color: #e0e0e0; /* Placeholder background if image fails */
border-radius: 5px;
overflow: hidden;
margin-bottom: 20px;
}
.mask-container svg {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: block; /* Remove extra space below SVG */
}
/* Controls Styling */
.controls {
display: flex;
flex-direction: column;
gap: 15px;
width: 100%;
max-width: 500px;
margin-top: 20px;
}
.control-group {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 5px;
}
label {
font-weight: bold;
color: #555;
}
input[type="range"],
input[type="text"] {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box; /* Include padding and border in the element's total width and height */
}
input[type="range"] {
-webkit-appearance: none; /* Remove default styling for Chrome/Safari */
appearance: none;
height: 8px;
background: #ddd;
outline: none;
opacity: 0.7;
transition: opacity .2s;
border: none;
border-radius: 5px;
}
input[type="range"]:hover {
opacity: 1;
}
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 20px;
height: 20px;
border-radius: 50%;
background: #007bff;
cursor: pointer;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
input[type="range"]::-moz-range-thumb {
width: 20px;
height: 20px;
border-radius: 50%;
background: #007bff;
cursor: pointer;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
/* Basic responsiveness */
@media (max-width: 600px) {
.container {
padding: 15px;
width: 95%;
}
.controls {
max-width: 100%;
}
}
</style>
</head>
<body>
<div class="container">
<h1>Dynamic SVG Text Mask</h1>
<div class="mask-container">
<svg viewBox="0 0 800 450" preserveAspectRatio="xMidYMid slice">
<!--
The <defs> tag is used to store definitions of graphical objects
that will be used later. Here, we define our mask.
-->
<defs>
<mask id="textMask">
<!--
The text element defines the shape of our mask.
- 'fill="white"' is crucial: white areas of the mask are opaque (show through).
- 'x' and 'y' define the starting point of the text.
- 'text-anchor="middle"' centers the text horizontally on the 'x' coordinate.
- 'dominant-baseline="middle"' centers the text vertically on the 'y' coordinate.
This combination ensures the text stays centered when its size changes.
-->
<text
id="maskedText"
x="400" y="225"
font-family="Impact, 'Arial Black', sans-serif"
font-size="120px"
font-weight="bold"
text-anchor="middle"
dominant-baseline="middle"
fill="white"
>
AWESOME!
</text>
</mask>
</defs>
<!--
The image element that will be masked.
- 'href' points to your image URL.
- 'width' and 'height' should match the SVG's viewBox or be 100%.
- 'mask="url(#textMask)"' applies the mask defined above.
-->
<image
href="https://picsum.photos/800/450?random=1"
width="800"
height="450"
mask="url(#textMask)"
alt="Background image for text mask"
/>
</svg>
</div>
<div class="controls">
<div class="control-group">
<label for="fontSizeSlider">Font Size:</label>
<input
type="range"
id="fontSizeSlider"
min="20"
max="200"
value="120"
step="1"
aria-label="Change font size"
>
</div>
<div class="control-group">
<label for="textInput">Text Content:</label>
<input
type="text"
id="textInput"
value="AWESOME!"
aria-label="Edit text content"
>
</div>
</div>
</div>
<script>
// Get references to SVG text element and control inputs
const maskedText = document.getElementById('maskedText');
const fontSizeSlider = document.getElementById('fontSizeSlider');
const textInput = document.getElementById('textInput');
// Function to update font size
function updateFontSize() {
maskedText.style.fontSize = fontSizeSlider.value + 'px';
}
// Function to update text content
function updateTextContent() {
maskedText.textContent = textInput.value;
}
// Set initial values for controls based on SVG's initial state
fontSizeSlider.value = parseInt(maskedText.getAttribute('font-size'));
textInput.value = maskedText.textContent;
// Add event listeners for dynamic updates
fontSizeSlider.addEventListener('input', updateFontSize);
textInput.addEventListener('input', updateTextContent);
// Initial update in case JS loads after DOM content is painted
updateFontSize();
updateTextContent();
</script>
</body>
</html>
How to Use and Customize:
- Save the Code: Save the entire code block above as an
.html file (e.g., text_mask.html).
- Image Source:
- Replace
https://picsum.photos/800/450?random=1 in the <image href="..."> with the actual URL or relative path to your desired image. Make sure the image dimensions match the width and height attributes of the <image> element, or adjust width and height (and the viewBox) to fit your image.
- For testing, I've used
picsum.photos which provides random placeholder images.
- Font Customization:
- Change
font-family="Impact, 'Arial Black', sans-serif" to any font installed on your system or a web font you've imported (e.g., via Google Fonts).
- Adjust
font-weight="bold" if needed.
- Initial Text & Size:
- Modify
"AWESOME!" within the <text> tag to your initial desired text.
- Change
font-size="120px" to set the initial font size.
- Ensure the
value attributes of the fontSizeSlider and textInput in the HTML match the initial SVG text settings.
- ViewBox & Centering:
- The
viewBox="0 0 800 450" sets the internal coordinate system of the SVG. The image and text are sized and positioned relative to this viewBox.
x="400" and y="225" place the text's center at the exact center of an 800x450 viewBox. If you change viewBox dimensions, adjust these x and y values accordingly (e.g., viewBox="0 0 1000 600" would mean x="500" and y="300").
preserveAspectRatio="xMidYMid slice" ensures the SVG scales nicely within its container, similar to background-size: cover; in CSS, maintaining the aspect ratio and cropping if necessary.
- Open in Browser: Open the
.html file in your web browser. You'll see the image masked by the text, along with the controls to change its size and content.