Skip to content
Nubgrammer
Menu
  • Home
  • Contact Me
Menu

CSS Triangles without Borders!

Posted on November 1, 2018November 1, 2018 by Tyler Sells

Hey Guys, it’s been a little while…

I won’t bore you with what I’ve been up to, but long story short life has been a little crazy over the last year.  I’ve wrote some apps at work and have learned a TON.  I’m also dropping the TL;DR format because I just don’t like it anymore.  Now for the main event….

The traditional way of making a CSS triangle:

This method uses what I would call a CSS hack to make the borders of a pseudo-element look like a triangle.  You can find examples of how that works all over the web.  In fact, this page is a really good choice if you have to do it this way.

div:after{
  content:"";
  position:absolute;
  border-top:10px solid transparent;
  border-bottom:10px solid transparent;
  border-right:10px solid #f00;
  top:0;
  right:0;
}

However, dynamically styling a pseudo-element can be very problematic.  This is why I spent 3 days trying to come up with a solution to a problem I was having.  Here it is:

Looks pretty much the same, right?  Good, that’s what I was going for.  However, there are a couple of distinct advantages of the second method. I’m not going to say I came up with it because let’s face it, someone somewhere has probably already figured this out.  That’s fine, but I couldn’t find it when I needed it.  I did find some pretty cool options here that involve SVGs and gradients, but they weren’t really suitable for my needs and browser support is iffy at best for some of them.

This is what this method looks like:

div::after{
  content:"";
  position:absolute;
  width:15px;
  height:15px;
  right:0px;
  top:50%;
  transform: translate(50%, -50%) rotate(45deg);
  background:#F00;
  
}

Why is this better?

Well, I guess “better” is a subjective term.  There are a few problems with this idea.  You may have to play around with some z-indexes and slightly unorthodox positioning methods to get it to work exactly right, but at least in my case, that was more than worth it.

The way it works is pretty simple.  If you cut a square diagonally in half, you get two perfect triangles.

By rotating the square, you get a diamond shape.

Lastly, using a parent element to hide half of the diamond with overflow:hidden, you achieve a triangle.

So why did I waste 3 days looking for this?

I’m working on a JS and CSS framework that led me to the need to create a custom tooltip. I know it’s been done a thousand times before, but I just wanted to make something.  The problem was that I couldn’t dynamically change the color of the triangle to match whatever color I wanted the tooltip to be.

This was my result:

CSS Triangle fail

As you can see, that doesn’t look good.  The triangle needs to be the same color as the tooltip itself.  My first thought was to just copy the background color from the tooltip element and then insert it into the border-color property using JS.  This led down a long rabbit-hole of custom DOM extensions and some dark depths of stackoverflow.

This solution works because I can use the inherit property on the pseudo-element to grab the background property from the tooltip element.  From there, it becomes trivial to style the tooltip element dynamically with JS or even just by changing the CSS class of the parent element.

In Conclusion…

Like I said before, this may not work for EVERY situation and there are some drawbacks, but if you find yourself in my shoes and need this or come up with some other ways to use this, please let me know in the comments!

Author: Tyler Sells

Github

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • More
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to email a link to a friend (Opens in new window) Email
  • Click to share on Reddit (Opens in new window) Reddit

Like this:

Like Loading...

2 thoughts on “CSS Triangles without Borders!”

  1. Matt says:
    July 16, 2021 at 2:13 pm

    Thanks for this post, it was exactly what I was looking for (couldn’t figure out how to style the triangles with normal before/after pseudo elements)

    Reply
  2. K-Sato says:
    March 31, 2022 at 8:25 pm

    Thanks for this!! Saved me a lot of time!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Follow me on Twitter

My Tweets

Github Repos

vtsells (Tyler Sells)

Tyler Sells

vtsells
http://www.nubgrammer.com
Joined on Jun 21, 2017
9 Public Repositories
100DaysOfCode
embers
MultiSelect
MVC-Project-Start
nubgrammer.com
PermIT
Spray
vtsells.github.io
Wizard
0 Public Gists

Categories

  • #100DaysOfCode (4)
  • ASP.NET (7)
  • ASP.NET Core (1)
  • ASP.NET MVC (3)
  • CSS (4)
  • General (13)
  • JS (3)
  • LESS (2)
  • Snippets (4)
  • Tools (4)
  • Tutorials (9)

Recent Posts

  • Creating a Knockout.js project on Codepen
  • 100DaysOfCode Day 3 – A State of Mind
  • 100DaysOfCode Day 2 – The Building Blocks
  • 100DaysOfCode Day 1 (Sort of cheated already)
  • Committing to #100DaysOfCode
© 2025 Nubgrammer | Powered by Superbs Personal Blog theme
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT
%d

    Privacy Policy