Home > Software design >  How to create triangle and use it as a arrow in tooltip
How to create triangle and use it as a arrow in tooltip

Time:06-10

I wanted to make some custom tooltip using html and CSS. The main problem, what I have is with arrow styles.

The result is more or less as I wanted, but I can't believe that I need to set this values, like: right, top etc. pixel by pixel. There is maybe more elegant way to do this?

body {
  background: #de302f;
}

.container {
  position: relative;
  max-width: 600px;
  height: auto;
  border: 2px solid #ffffff;
  margin: 10px auto;
  padding: 30px;
  box-sizing: border-box;
}

.item {
  position: absolute;
  width: 50px;
  height: 50px;
  border-bottom: 2px solid #ffffff;
  top: 100%;
  right: -26.9px;
  transform: rotate(45deg);
  margin-top: -25px;
  background: #de302f;
}

.container:after {
  content: '';
  position: absolute;
  width: 50px;
  height: 73px;
  border-left: 2px solid #ffffff;
  top: 24px;
  right: -52px;
}
<div >
  <div >
  </div>
</div>

CodePudding user response:

You can simplify a little like below:

body {
  background: #de302f;
}

.container {
  --t: 2px; /* thickness */
  --b: var(--t) solid #fff; /* border here */
  
  position: relative;
  max-width: 600px;
  height: 50px;
  border: var(--b);
  border-bottom: none;
  margin: 10px auto;
  padding: 30px;
  box-sizing: border-box;
  clip-path: inset(0 0 -100vmax);
}
.container:before,
.container:after {
  content: '';
  position: absolute;
  top: 100%;
  height: 40px; /* control the height here */
  right: calc(-1*var(--t));
  border-right: var(--b);
}
.container:after {
  width: 100%;
  border-top: var(--b);
  transform: skewX(30deg); /* control the angle here */
  transform-origin: 0 calc(100% - var(--t));
}
<div >

</div>

  • Related