Image ALT Caption

Description

  • add caption text under Image Block, use ALT

574 (free) 1

#1. Install Code

#1.1. Personal Plan/Basic Plan

Note: You can also use this code for Business Plan/higher.

Edit Page where you use Image Blocks > Add a Markdown Block

574 (free) 4

Paste this code

<script src="https://code.beaverhero.com/file?filename=1762422598547altcaption.js"></script>
<link rel="stylesheet" href="https://code.beaverhero.com/file?filename=1762422637315altcaption.css"/>

574 (free) 5

#1.2. Business Plan/higher

Hover on page where you use Image Blocks > Click Gear icon

574 (free) 2

Click Advanced > Paste this code

<!-- @tuanphan - Image ALT Caption -->
<script>
document.addEventListener('DOMContentLoaded', function() {
    var imageBlocks = document.querySelectorAll('.image-block');
    
    imageBlocks.forEach(function(block) {
        var img = block.querySelector('img');
        if (img) {
            var alt = img.getAttribute('alt');
            if (alt && alt.includes('###')) {
                var text = alt.replace(/###/g, '').trim();
                console.log(text);
                var span = document.createElement('span');
                span.className = 'tp-alt-caption';
                span.textContent = text;
                var imageContent = block.querySelector('.sqs-image-content');
                if (imageContent) {
                    imageContent.parentNode.insertBefore(span, imageContent.nextSibling);
                }
            }
        }
    });
});
</script>
<style>
.sqs-image:has(.tp-alt-caption) {
    flex-direction: column;
}
.tp-alt-caption {
    font-size: 15px;
    line-height: 20px;
    display: block;
    color: #000;
}
</style>

574 (free) 3

#2. Usage

Make sure you entered ALT

574 (free) 6

Add ### to last of ALT

###

574 (free) 7

#3. Customize

#3.1. To align caption left, you can use this to Custom CSS

div.sqs-image:has(.tp-alt-caption) {
    align-items: flex-start !important;
}

574 (free) 8

#3.2. To change caption style, use this to Custom CSS

span.tp-alt-caption {
    font-size: 15px;
    line-height: 20px;
    display: block;
    color: #000;
}

574 (free) 9

Buy me a coffee