Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Only Triangle Have Hover Effect using CSS [duplicate]

I am in quite the quandary! I would like to add cursor: pointer to my CSS, but the problem is it is a triangle. If I used the following:

#triangleholder {
  width: 100px;
  height: 100px;
  border: 1px solid red;
}

#triangle {
  width: 0;
  height: 0;
  border-right: 50px solid transparent;
  border-left: 50px solid transparent;
  border-bottom: 50px solid blue;
  cursor: pointer;
}
<div id="triangleholder">
  <div id="triangle">
  </div>
</div>

The whole triangle and everything around it has the "Cursor" affect, how can I make only the triangle have the hover affect?

like image 955
Michael Jones Avatar asked Apr 20 '15 21:04

Michael Jones


2 Answers

This can be done with pure CSS if we construct the triangle using transforms and overflow:hidden

FIDDLE

#triangleholder {
  width: 100px;
  height: 100px;
  border: 1px solid red;
}
#triangle {
  position: relative;
  height: 50px;
  overflow: hidden;
}
#triangle:before {
  content: '';
  position: absolute;
  width: 71px; /*using pythagorus: sqrt( (100^2) /2 ) */
  height: 71px;
  background: blue;
  transform: rotate(45deg)translateX(29%);
  cursor: pointer;
}
<div id="triangleholder">
  <div id="triangle">
  </div>
</div>

NB: The code: translateX(29%) is used to place the rotated blue square back into the center of the container after it is rotated. This value seems to be constant even if we change the dimensions of the container (FIDDLE)

like image 189
Danield Avatar answered Sep 20 '22 13:09

Danield


Use SVG or CSS3 to draw the arrow. Give that element cursor: pointer give the div wrapper non-cursor

Relevant article to implement this: http://www.smashingmagazine.com/2014/11/03/styling-and-animating-svgs-with-css/

like image 22
fred randall Avatar answered Sep 17 '22 13:09

fred randall