triangle in css in javascript

Here's an example of how to create a triangle shape using pure CSS and JavaScript:

<!DOCTYPE html>
<html>
<head>
	<title>Triangle Example</title>
	<style>
		.triangle {
		    height: 0;
		    width: 0;
		    border-top: 50px solid transparent;
		    border-bottom: 50px solid transparent;
		    border-left: 100px solid red;
		}
	</style>
</head>
<body>
	<div id="triangle"></div>
	<script>
		var triangle = document.getElementById("triangle");
		triangle.classList.add("triangle");
	</script>
</body>
</html>
427 chars
23 lines

In this example, we first create a div element with the ID "triangle". We then select this element using JavaScript and add a CSS class called "triangle" to it.

The CSS class defines a triangle shape by setting the height and width to 0, and using border properties to create the triangle shape with a 100px base and 50px height. Note that we set the border-top and border-bottom to transparent to create the angled sides of the triangle.

By adding the "triangle" class to our div element, we apply this CSS style to the element, creating a red triangle shape.

This is just one example of how to create a triangle in CSS and JavaScript; there are many other methods and variations you could use depending on your specific requirements.

gistlibby LogSnag