Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I made this UI which I intend utilising in my website and I want it to move just like a marquee, however, while it loops through each item I want to get rid of that nasty ***extra gap*** (you will observe this after the last item displays).

Also, when you hover your mouse on the carousel (or marquee maybe), the motion stops.

Please help me resolve the extra gap after the last pill item. Thank you. :)

What I have tried:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>marquee</title>
	<style>
		*{
			margin: 0;
			padding: 0;
			box-sizing: border-box;
		}
		body{
			display: flex;
			align-items: center;
			justify-content: center;
			width: 100%;
			height: 100vh;
			font-family: 'Maven Pro';
			background-color: #8a5bcb;
		}
		#categories{
			border: 1px solid #000;
			display: flex;
			align-items: center;
			background-color: #30b090;
			overflow: hidden;
			height: 40px;
		}

		#inner_categories{
			width: 1000px;
			display: flex;
			white-space: nowrap;
			animation-name: marquee;
			animation-duration: 10000ms;
			animation-timing-function: linear;
			animation-iteration-count: 999;
		}

		#categories:hover #inner_categories{
			animation-play-state: paused;
		}

		@keyframes marquee {
			0%{
				transform: translateX(0%);
			}
			100%{
				transform: translateX(-100%);
			}
		}

		.category{
			font-size: 12px;
			text-transform: capitalize;
			background-color: #ef00ef;
			padding: 2.5px 5px;
			border-radius: 5px;
		}

		.category:not(:last-child){
			margin-right: 6px;
		}

		.category:hover{
			cursor: pointer;
		}

		span{
			overflow: hidden;
			padding: 0;
			margin: 0;
		}
	</style>
</head>
<body>
	<!-- ========== begin ========== MARQUEE TABS ========== begin ========== -->
	<div id="categories">
        <div id="inner_categories">
            <!-- ========== begin ========== CATEGORY TABS ========== begin ========== -->
            <div class="category">
            	<span>START item</span>
            </div>
            <div class="category">
            	<span>grocery item</span>
            </div>
            <div class="category">
            	<span>grocery item</span>
            </div>
            <div class="category">
            	<span>grocery item</span>
            </div>
            <div class="category">
            	<span>grocery item</span>
            </div>
            <div class="category">
            	<span>grocery item</span>
            </div>
            <div class="category">
            	<span>grocery item</span>
            </div>
            <div class="category">
            	<span>grocery item</span>
            </div>
            <div class="category">
            	<span>grocery item</span>
            </div>
            <div class="category">
            	<span>grocery item</span>
            </div>
            <div class="category">
            	<span>grocery item</span>
            </div>
            <div class="category">
            	<span>grocery item</span>
            </div>
            <div class="category">
            	<span>grocery item</span>
            </div>
            <div class="category">
            	<span>grocery item</span>
            </div>
            <div class="category">
            	<span>grocery item</span>
            </div>
            <div class="category">
            	<span>grocery item</span>
            </div>
            <div class="category">
            	<span>grocery item</span>
            </div>
            <div class="category">
            	<span>grocery item</span>
            </div>
            <div class="category">
            	<span>grocery item</span>
            </div>
            <div class="category">
            	<span>grocery item</span>
            </div>
            <div class="category">
            	<span>END item</span>
            </div>
            <!-- ========== end ========== CATEGORY TABS ========== end ========== -->
        </div>
    </div>
    <!-- ========== end ========== MARQUEE TABS ========== end ========== -->
</body>
</html>
Posted
Updated 16-Apr-22 18:06pm

1 solution

Open the browser web tools and inspect the element with hte gap. You will quickly see where the issue is. Here is how using:
1. Chrome: Open Chrome DevTools - Chrome Developers[^]
2. FireFox: Firefox DevTools User Docs — Firefox Source Docs documentation[^]

To answer your questions:
1. I want to get rid of that nasty ***extra gap*** ... after the last pill item.

Your design is heavily flawed.

If you resize the browser window, the number of elements that are displayed is varied. Reduce the width to the minimum the browser will allow (mobile device size) you will only see the first 12 items. I have a 4k screen and when I maximise the browser window, I do see the gap that you describe.

This is working 100% as you have designed it. Here is why:
CSS
@keyframes marquee {
    0%{
        transform: translateX(0%);
    }
    100%{
        transform: translateX(-100%);
    }
}

The above code takes into consideration the width of the element and will move it left 100% of it's width from where it started.

Your design takes into consideration the width of the elements, not the width of the viewport (visible area).

Also, for a marquee to work, you need to join the start to the end, like a stamp wheel, for the information to seem seemlessly scroll.

Next is the animation...

SOLUTION
If you want to use your style of marquee and have a gap, chec out this example: Marquee infinite div scroll pure css[^]

But if you want to remove the gap, be mindful of the viewport size and look at this example: Pure CSS marquee - Infinite Horizontal Text Scroll Without Break[^]

2. I made this UI ... when you hover your mouse on the carousel... the motion stops

If you created this, then you would know why the motion stops. This is why:
CSS
#categories:hover #inner_categories{
    animation-play-state: paused;
}

Google Search is your friend - all links provided in this answer were from using Google Search. I would suggest learning how to use it. Here is the search that I used to find the two above examples: pure css scolling marquee[^]
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900