I am making a navigation bar for my app. There are currently 3 main items on my navigation bar (Menu, Search and Cart indicator). And they are nicely evenly spread using flexbox
and space-between
. My app needs to support multiple languages and the problem is that when the translation for "Cart" changes then there is a huge content shifting happening and it looks like my app is broken.
Is there some way I can prevent this content shifting and still have my navigation items evenly spread out and centered? I have thought about adding fixed widths to my flex items but then I can't get my items to be centered (what space-between does).
Any help would be much appreciated.
Here is a working fiddle demo: https://jsfiddle.net/queeeeenz/mf6jak05/15/
Here you can see how my whole navigation bar jumps around when I change the language.
Here is my code (I am using tailwind for styling):
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<div class="flex justify-between items-center bg-gray-50 p-2 border border-gray-200">
<div class="bg-gray-200 p-2 rounded">Menu</div>
<div class="bg-gray-200 p-2 rounded w-[150px] text-center">Search</div>
<div class="bg-gray-200 p-2 rounded" id="cart">Cart (2)</div>
</div>
<button class="bg-teal-100 mt-2 py-2 px-4 rounded-lg border border-teal-300" id="changeLanguage">
Change Language
</button>
<script>
document.querySelector('#changeLanguage').addEventListener('click', () => {
if (document.querySelector('#cart').innerText === 'Cart (2)') {
document.querySelector('#cart').innerText = 'Kariton Ti Panaggatang (2)'
} else {
document.querySelector('#cart').innerText = 'Cart (2)'
}
})
</script>
</body>
</html>