Creating triangular shapes that can be interacted with by hovering

I'm trying to create a diagonal triangle on the screen that changes color when hovered over. However, my current method only creates a 'hack triangle' which is actually just a rectangle and doesn't respond to the transparent section.

Is there a way to make this hover effect work only when hovering directly on the triangle?

body {
  padding: 0;
  margin: 0;
}

.triangle {
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 100vh 0 0 100vw;
  border-color: transparent transparent transparent #007bff;
}

.triangle:hover {
  border-color: transparent transparent transparent red;
}
<div class="triangle"></div>

The responses in other threads are aimed at simple triangles, unlike mine which covers the entire screen with different side lengths and shapes.

Answer №1

One way to achieve this effect is by using the clip-path property.

body {
  margin: 0;
}

.triangle {
  width: 100vw;
  height: 100vh;
  background:#007bff;
  -webkit-clip-path:polygon(0 0,0 100%, 100% 100%);
  clip-path:polygon(0 0,0 100%, 100% 100%);
}

.triangle:hover {
  background:red;
}
<div class="triangle"></div>

If you want better support and responsiveness, consider using skew transformation with some JavaScript to adjust the angle based on window resize:

var w = window.innerWidth;
var h = window.innerHeight;
document.querySelector('.triangle').style.transform="skewY("+(Math.atan(h/w)*180/Math.PI )+"deg)";

window.onresize = function(event) {
    w = window.innerWidth;
    h = window.innerHeight;
    document.querySelector('.triangle').style.transform="skewY("+(Math.atan(h/w)*180/Math.PI )+"deg)";
};
body {
  margin: 0;
  overflow:hidden;
}

.triangle {
  width: 100vw;
  height: 100vh;
  background:#007bff;
  transform-origin:top left;
  transform:skewY(20deg);
}

.triangle:hover {
  background:red;
}
<div class="triangle"></div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Echoing data from an input form is not possible with an associative array

I'm having trouble printing the associative array after inserting data from an input box. Currently, I can only display the latest data entered in the input box. What I really want is to continuously add data to the array and display it every time new ...

Utilizing Google Maps to generate outcomes for an online platform

My approach with Google Maps is a bit unconventional. Instead of rendering images, I aim to execute JavaScript code that processes data and returns a text response. However, I soon realized that running JavaScript as a remote web service might not be pos ...

The div element on my website spans the entire width, yet it appears slightly narrower than the body

My website features a scrolling div element that spans 100% of the screen, but I am encountering an issue with slight horizontal scrolling and the background image extending beyond the visible area. I am striving to adjust the width of the div so that it ...

Adjusting nearby parts when hovering in CSS

I'm curious as to why the size of the second div does not change when hovering over the third div, unlike when hovering over the first div. #block { width: 50%; white-space: nowrap; } .div { display: inline-block; text-align: center; } di ...

Use JavaScript to change the CSS pseudo-class from :hover to :active for touch devices

Looking to eliminate hover effects on touch devices? While it's recommended to use a hover class for hover effects, instead of the hover pseudo-class, for easier removal later on, it can be a challenge if your site is already coded. However, there&apo ...

What is the solution for resolving the delay between a variable in HTML and the controller in Angular 4?

I am encountering a common issue in my Angular 4 or 2 project. Whenever I initialize a variable inside a subscribe function, there is a delay before it gets assigned a value. The problem arises when I try to use this variable in the HTML template using *ng ...

Issue with implementing autocomplete using AJAX and PHP for multiple input fields

HTML code: <table> <tr> <td align="center"> <label for="InputBox1"><b>~</b><font color="red">*</font>: &nbsp;</label> </td> </tr> <tr> <td align="center"> <div id="1"> ...

Dependency on the selection of items in the Bootstrap dropdown menu

I am currently struggling with a few tasks regarding Bootstrap Dropdown Selection and despite looking for information, I couldn't find anything helpful. Check out my dropdown menu Here are the functions I would like to implement: 1) I want the subm ...

Unable to scroll when utilizing ng-html-bind functionality

My device specifications: $ ionic info Your system details: Cordova CLI: 6.2.0 Gulp version: CLI version 3.9.1 Gulp local: Ionic Framework Version: 1.2.4 Ionic CLI Version: 2.0.0 Ionic App Lib Version: 2.0.0-beta.20 OS: Distributor ID: LinuxMint Desc ...

automatically changing radio button selection based on dropdown choice

Let's address the issue at hand: I have a dropdown list with over three options, and alongside it, a radio group for yes or no responses. What I am aiming to achieve is setting the radio button to "no" when option 1 is selected, and to "yes" when any ...

Chrome reports a Javascript error: indicating that it is not recognizing the function

When working with a js file and html, I encountered an issue where one function works fine but another prompts an error in Chrome: Uncaught TypeError: specification_existing is not a function I'm puzzled as to why one function works while the othe ...

Error: Syntax mishap detected - Oh dear

Recently, I've been developing a small slideshow / public display for a client using the HTML5 Rock's Slideshow code. Everything was going smoothly until I encountered a DOM Exception 12 - an error related to CSS selectors. Strangely, I couldn&ap ...

Issue with URL routing causing page to display incorrectly

When using the normal Href, the page is rendered properly, but the URL is not hidden: <asp:Repeater ID="rCompany" runat="server" DataSourceID="sdsCompany" > <ItemTemplate> <div class="col4" id="trip_third"> <a href="discover_detail.as ...

Vue Filtering and Pagination Implementation

In Vue pagination, how can you control the number of array objects to be displayed based on a user-selected amount like 10, 15, or 25? I successfully implemented rendering 10 items per page and it's functioning smoothly. ...

Tips on modifying the maxlength attributes for all "field answer" class elements

Looking for some help with adjusting the "maxlength" attribute in a class called "field answer." The current maxlength is set to 250, but I need it changed to 9999. Can someone guide me through this process? <div class="field answer"> &l ...

Populate a table dynamically in JavaScript using a variable

I have a table with IDs such as r0c1 = row 0 column 1. I attempted to populate it using the following script, but it doesn't seem to be functioning correctly: var data = new Array(); data[0] = new Array("999", "220", "440", "840", "1 300", "1 580", " ...

The alignment of the list is off when using Flexbox on smaller screens

When my flexbox shifts into a column layout at 600 pixels, the list in the second box loses its center alignment on smaller screens. Despite trying various margin and padding adjustments within and outside of media queries, the content remains too far to t ...

pop-up window that shows chosen choices using javascript or ajax

I have a specific HTML code that allows users to select multiple options. I would like these selected options to be displayed in a popup or a div in real-time as the user makes their selections. I have attempted using a selectbox with checkboxes, but extra ...

What is the process for capturing a screenshot of a specific DOM element using Intern JS?

I'm currently utilizing intern.js for testing a web application. It allows me to run tests and generate screenshots in case of failures. My goal is to capture a screenshot of a specific element for conducting CSS regression testing with tools like res ...

What is the reason for background images not loading when using `display: none`?

I'm really struggling to understand this puzzle: When the page loads, will mypic.jpg be downloaded by the browser? #test1 { display: none; } #test2 { background-image: url('http://www.dumpaday.com/wp-content/uploads/2017/01/random-pic ...