When we write CSS, sometimes we forget about some edge cases in the designs. For example, when the content is longer than we expected and we didn’t account for that possibility, our design could break. We can’t guarantee that our CSS will always work as expected, but at least we can reduce that by testing different types of content.
When you code CSS, you’re writing abstract rules to take unknown content and organize it in an unknown medium. – Keith J. Grant
In this article, we will go through different UI bugs from real-world websites so we can account for them from the beginning. Ready? Let’s go!
A button with an icon placed on the right/left side

This is a toggle button for an accordion. There is an icon on the right side to emphasize that it is clickable. However, when the area is not big enough, the text will overlap the icon. This might happen when we don’t account for long content.
A solution would be to add an enough padding to the right side to accommodate for the size of the icon:
.button {
padding-right: 50px;
}
Notice how increasing the padding creates a safe area for the icon. Now we can be sure that it won’t break if the text gets longer.

See the Pen A button with an icon by Ahmad Shadeed (@shadeed) on CodePen.
Input Placeholder
When applying the float label pattern for our forms, especially with a button on the right side. We should test this thoroughly to avoid any issues when the label is too long.

A solution would be to add position: relative
for the button. This will move it above the label.
See the Pen Long placeholder by Ahmad Shadeed (@shadeed) on CodePen.
Long Names

In this design, the image is floated to the left and we have an author name on the right. What happens when the name is longer than expected? The UI will break.
The issue there is that we only floated the image to the left side. This might cause the author name to move beside it. However, this will only work if the name is not long.
To make it more robust, we should float
the author image and add overflow: hidden
to the author name wrapper. That way, we will get the advantage of block-formatting context (Thanks to Thierry Koblentz for pointing this out in the comments). Or, another solution, use flexbox since it’s suitable for that small component.
See the Pen Long person name by Ahmad Shadeed (@shadeed) on CodePen.
Long links/words inside an article body

Sometimes there are long links or words in an article. This might not cause an issue with a very wide viewport. But for smaller sizes like mobile or tablet, this will cause a horizontal scrolling and it will be annoying.
We have two solutions for such an issue:
1) Use CSS word-break
.article-body p {
word-break: break-all;
}
Please test well when using word-break
since it has some differences between browsers. We recommend you to read this article on the topic.
2) Add overflow to the wrapper element and text-overflow to the links
.article-body p {
overflow: hidden;
text-overflow: ellipsis;
}
This solution is safer and better for links. But for words, I would use word-break
.

See the Pen Long links / words by Ahmad Shadeed (@shadeed) on CodePen.
Long article tags

When we place an article tag over a card, we should only add padding for the spacing. Determining width and height might make the UI break when the tag content is too long.
If you want to have a minimum width for the tag, that’s fine. We can use min-width
with padding around the tag content. That way, the width will be dynamic and the issue will be solved.
See the Pen Long Article Tags by CSS-Tricks (@css-tricks) on CodePen.
Section header with a link

In this example, we have a section title along with a “view more” link on the right. There are different ways to code this in CSS, one of them is using absolute positioning for the link.
This will cause issues in case the title was too long. A better solution could be to use flexbox. That way, it will automatically push the link to a new line when there is no enough space.

.header-2 {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
}
The above technique is called “Alignment Shifting Wrapping”. I learned about it two years ago from this article.
See the Pen Section header with a link by Ahmad Shadeed (@shadeed) on CodePen.
Conclusion
I learned the hard way that using dummy content or simply adding things randomly is not enough. We should add all types of content to our layouts until something breaks. I like to use Heydon Pickering’s forceFeed.js for adding content randomly to a specific component.
Very interesting post, thank you :)
For the floated image, you might also use CSS table layout, which is very robust and simple.
Another example I see too often is long lists with icons using multicolumns, when the content is too long: the text comes below the icon, which is not really beautiful.
Nice.
I am looking for a solution,
How to make pop up box float, when the content of web page is larger than the window size.
When background contents is scrollable and pop up window often goes out of sight from Windows preview area. In this case, unfortunately user have to scroll to pop up box position for interaction.
I’m not sure I understand it well, but can’t you use
position: fixed; top: 0;
?Give the popup box a position of fixed!
The Long Names section shows a “Media Object” without suggesting the “proper fix”:
Giving a
width
to both elements would create constraints (using percentage or knowing the parent box’s width) and usingflexbox
does not offer the best browser support. A more robust approach is to style the elements next to a float as block-formatting context (BFC). This has the advantage to work in all browsers, including IE6 (via “hasLayout”).This article seems to be missing the power of “floats and BFCs” as it mostly suggests bugs triggered by absolutely positioned elements which has always been a bad idea in the first place. I think suggesting that
flexbox
is an easy fix is a miss opportunity to learn good old layout techniques.Hi Thierry, Thanks a lot for your comment.
Totally agree with you on using floats. The reason that I showed bugs with
position: absolute
is that because I saw them in some websites and just wanted to show them.For example, If I want to build something like the media object, I would use float and enhance with Flexbox if needed.
You probably don’t want
word-break
; it was designed to integrate other alphabets into CJK pages, and it breaks lines according to Eastern typography rules, not Western ones.word-wrap
is almost always preferable.Thanks for your comment, Taylor.
Yes, I’ve seen that in a website I’m working on. But my concern is, how to handle long words in a paragraph without using
word-break
? Would love to hear your thoughts.Does
word-wrap: break-word;
not do what you need?Yes! Thank you :)
Nice article Ahmad, but in my experience this is not (referring to your opening) an “edge case”. This is a very real, frustrating, and frequent one.
What would you do if you added a Twitter Embedded Timeline and on phone, it is too wide because I had to set a length in the large view. It is in a wrapper to adjust the width. I can’t seem to effect the one or the other without breaking the one or the other. That’s on small or large queries. Is there a better way with javascript? A link to any reference would be great.
According to [url=https://dev.twitter.com/web/embedded-timelines]the Embedded Timelines code on dev.twitter[/url], the embed seems responsive. When I scale down my browser window, the embed adjusts accordingly.
Maybe you have old embed code?
For .card .author you can add overflow:hiddean instead float:left. Important is that author must to be a block element.
I guess you mean a block-formatting context. I agree, that’s the proper way to style a “media block”.
That really points to the pain point I am facing! It is good to see such solutions you have made.
I think that what the article conveys the most is the need to test, test, test.
Although having fixes to these kinds of problems is very useful, the developer / designer should be mindful of these fringe cases at the time as well.
If you don’t see anything breaking, you won’t see anything that needs fixing.