Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="latest-news" class="marquee">
    <span style="white-space:nowrap;">
        <span>&nbsp;&bull;&nbsp;</span>
        <a href="#">Explore the hidden wonders of nature</a>
        <span>&nbsp;&bull;&nbsp;</span>
        <a href="#">Ignite your passion for creativity</a>
        <span>&nbsp;&bull;&nbsp;</span>
        <a href="#">Embrace the journey, not just the destination</a>
        <span>&nbsp;&bull;&nbsp;</span>
        <a href="#">Discover the magic in everyday moments</a>
        <span>&nbsp;&bull;&nbsp;</span>
        <a href="#">Chase your dreams with fearless enthusiasm</a>
    </span>
</div>

<p><a href="https://getbutterfly.com/javascript-marquee-a-collection-of-scrolling-text-snippets/">See more marquee scripts here.</a></p>
              
            
!

CSS

              
                .marquee {
    position: relative;
    overflow: hidden;
    text-align: center;
    margin: 0 auto;
    width: 100%;
    height: 30px;
    display: flex;
    align-items: center;
    white-space: nowrap;
    font-size: 24px;
}

              
            
!

JS

              
                function initializeMarquee() {
    createMarqueeContainer('latest-news');
    rotateMarquee(marqueeContainers);
}

window.onload = initializeMarquee;

function getObjectWidth(obj) {
    if (obj.offsetWidth) return obj.offsetWidth;
    if (obj.clip) return obj.clip.width;
    return 0;
}

const marqueeContainers = [];

function createMarqueeContainer(id) {
    const container = document.getElementById(id);
    const itemWidth = getObjectWidth(container.getElementsByTagName("span")[0]) + 5;
    const fullWidth = getObjectWidth(container);
    const textContent = container.getElementsByTagName("span")[0].innerHTML;
    container.innerHTML = "";
    const height = container.style.height;

    container.onmouseout = () => rotateMarquee(marqueeContainers);
    
    container.onmouseover = () => cancelAnimationFrame(marqueeContainers[0].animationID);

    container.items = [];
    const maxItems = Math.ceil(fullWidth / itemWidth) + 1;

    for (let i = 0; i < maxItems; i++) {
        container.items[i] = document.createElement("div");
        container.items[i].innerHTML = textContent;
        container.items[i].style.position = "absolute";
        container.items[i].style.left = itemWidth * i + "px";
        container.items[i].style.width = itemWidth + "px";
        container.items[i].style.height = height;
        container.appendChild(container.items[i]);
    }

    marqueeContainers.push(container);
}

function rotateMarquee(containers) {
    if (!containers) return;

    for (let j = containers.length - 1; j > -1; j--) {
        const maxItems = containers[j].items.length;

        for (let i = 0; i < maxItems; i++) {
            const itemStyle = containers[j].items[i].style;
            itemStyle.left = parseInt(itemStyle.left, 10) - 1 + "px";
        }

        const firstItemStyle = containers[j].items[0].style;

        if (parseInt(firstItemStyle.left, 10) + parseInt(firstItemStyle.width, 10) < 0) {
            const shiftedItem = containers[j].items.shift();
            shiftedItem.style.left = parseInt(shiftedItem.style.left) + parseInt(shiftedItem.style.width) * maxItems + "px";
            containers[j].items.push(shiftedItem);
        }
    }

    containers[0].animationID = requestAnimationFrame(() => rotateMarquee(containers));
}

              
            
!
999px

Console