Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gurbaxani's avatar

How Do I Fix This Jumpy UI?

I'm sorry I don't have the right words to express this problem. So I recorded a quick (<90 seconds) video about it instead. Watch: https://youtu.be/THnilTfdH9g

The problem: The UI is jumpy (buttons change positions when they should be fixed), and the text should shrink if it's multi-line.

What I'm building: https://improv-suggestions.gurbax.click/

Source code: https://github.com/gurbaxani/instant-improv-suggestions

The view:


<div class="h-full grid place-items-center mt-20">

    <div class="block max-w-sm p-6 bg-green-100 border-gray-900 rounded-lg shadow hover:bg-gray-100 w-3/4 items-stretch text-center border-4 border-b-8 border-r-8 py-20">

        <h5 class="mb-2 text-2xl font-bold tracking-tight text-gray-900 capitalize"> {!! nl2br($suggestion) !!}</h5>

    </div>
    <div class="grid grid-cols-2 gap-4 sm:grid-cols-4 xl:grid-cols-5 bg-red-500pr-4 mt-6">

        <button wire:click="object" class="relative inline-block text-lg group">
            <span class="relative z-10 block px-5 py-3 overflow-hidden font-medium leading-tight text-gray-800 transition-colors duration-300 ease-out border-2 border-gray-900 rounded-lg group-hover:text-white">
                <span class="absolute inset-0 w-full h-full px-5 py-3 rounded-lg bg-gray-50"></span>
                <span class="absolute left-0 w-48 h-48 -ml-2 transition-all duration-300 origin-top-right -rotate-90 -translate-x-full translate-y-12 bg-gray-900 group-hover:-rotate-180 ease"></span>
                <span class="relative">šŸ“¦ Object</span>
            </span>
            <span class="absolute bottom-0 right-0 w-full h-12 -mb-1 -mr-1 transition-all duration-200 ease-linear bg-gray-900 rounded-lg group-hover:mb-0 group-hover:mr-0" data-rounded="rounded-lg"></span>
        </button>

        ... (the rest of the buttons)

:))

  • Gurbax
0 likes
6 replies
LaryAI's avatar
Level 58

To address the jumpy UI issue, we need to ensure that the elements have a consistent size regardless of their content. For the text that should shrink if it's multi-line, we can use CSS to control the text size and ensure it fits within the designated area.

Here's a solution that you can try:

  1. To prevent the buttons from changing positions, make sure that all buttons have the same height and width, or at least a minimum height and width. You can achieve this by setting a min-height and min-width on the button elements.

  2. For the text that should shrink on multi-line, you can use the CSS property white-space: nowrap to keep the text on a single line, along with overflow: hidden and text-overflow: ellipsis to handle overflow. However, if you want the text to wrap and reduce in size to fit the container, you might need to use JavaScript to dynamically adjust the font size based on the content.

Here's an example of how you can modify your CSS:

.button {
  min-width: 150px; /* Adjust as needed */
  min-height: 50px; /* Adjust as needed */
  /* Other styles */
}

.suggestion-text {
  font-size: 1.5rem; /* Starting font size */
  line-height: 1.2;
  max-height: 4.8rem; /* line-height * number of lines you want to allow, e.g., 1.2 * 4 */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Responsive font size (optional) */
@media (max-width: 768px) {
  .suggestion-text {
    font-size: 1.2rem;
  }
}

And apply the classes to your HTML elements:

<h5 class="mb-2 text-2xl font-bold tracking-tight text-gray-900 capitalize suggestion-text">
  {!! nl2br($suggestion) !!}
</h5>

<!-- ... -->

<button wire:click="object" class="relative inline-block text-lg group button">
  <!-- Button content -->
</button>

If you want the text to wrap and adjust its size dynamically, you might need a JavaScript solution to calculate the text size and adjust it accordingly. Here's a simple example using jQuery:

function adjustTextSize(selector, maxLines) {
  $(selector).each(function() {
    var $el = $(this);
    var origFontSize = parseInt($el.css('font-size'), 10);
    var lineHeight = parseInt($el.css('line-height'), 10);
    var maxHeight = lineHeight * maxLines;

    while ($el.height() > maxHeight && origFontSize > 0) {
      origFontSize--;
      $el.css('font-size', origFontSize + 'px');
    }
  });
}

// Call this function on window load and resize
$(window).on('load resize', function() {
  adjustTextSize('.suggestion-text', 4); // Adjust '.suggestion-text' and '4' as needed
});

Remember to include jQuery in your project if you decide to use the JavaScript solution. Also, adjust the selectors and values according to your specific needs.

gurbaxani's avatar

@LaryAI Thank you AI, but this does not produce the desired behavior. While it does fix the buttons in place, it cuts off the text and adds an ellipsis. Instead, the text should shrink or be scrollable.

Not an ellipsis like this.

JussiMannisto's avatar
Level 50

The scrollable option is easy with css. You can fix the box to a specific or maximum height and apply overflow-y: scroll to it. That's overflow-y-scroll in Tailwind, I think.

Actually overflow-y: auto is probably better.

Shrinkable text is possible too, but I can't recall off the top of my head. I try to avoid auto-resizing text because it's not good for accessibility.

2 likes
gurbaxani's avatar

@JussiMannisto Thank you so much! I implemented your solution and used overflow-y:auto. Works like a charm.

Thanks for teaching me about the overflow property :))

gych's avatar

@gurbaxani Like @jussimannisto already said, specify maximum height and apply overflow y scroll is a good solution.

But personally I would look into improving the overall UX/UI of the interface. I think you have room for improvement where the text could be shown without a scrollbar. This ofcourse depends on the length of other texts that might be bigger but the less scrolling the better the experience wil be.

1 like
Snapey's avatar

i would investigate paginating the text based on the length of the string. Instead of overflow and scrollbars you could show a down or up arrow to show next or previous sections of text if they exist.

just an alternate idea to see what feels right in your context

1 like

Please or to participate in this conversation.