Design a triangular shape using CSS only

I am working on a responsive web application and I need to have two distinct content areas set up in the following manner: https://i.sstatic.net/5Q6L7.jpg

Up to now, I have attempted:

border-right: 30px solid transparent;
border-bottom: 30px solid #4c4c4c; 
height: 100%;
width: 100%;
position: fixed;

Unfortunately, I was unable to create a triangle shape.

Is there an alternative method in CSS to create a triangle while still allowing the content to be fully contained within the div, other than using the border property?

Any assistance would be greatly appreciated.

Answer №1

It appears that you are in search of triangles with distinctive borders and a transparent division in between, which none of the current responses seem to cover accurately. Allow me to provide an example. Achieving this effect is indeed possible but requires a significant amount of manipulation.

The CSS Transforms Method:

The following code snippet utilizes pseudo-elements and transforms to generate the triangular effect. While the output is responsive, the utilization of skew transforms means that adjustments to the skew angles would be necessary if the container's shape changes to a rectangle. Additionally, further tweaking of positioning attributes may be required.

.container {
  position: relative;
  overflow: hidden;
  height: 200px;
  width: 200px;
}
.div-1,
.div-2 {
  position: absolute;
  top: 0px;
  left: 0px;
  height: 100%;
  width: 100%;
  overflow: hidden;
}
/* CSS Code continues... */


Answer №2

Creating a triangle without specifying width and height.

.triangle1 {
    border-right: 30px solid transparent;
   border-bottom: 30px solid #4c4c4c; 
   position: fixed;
}

View Working Example

For the image provided, I have created two inverted triangles.

.triangle1 {
    border-right: 100px solid transparent;
    border-bottom: 100px solid #4c4c4c; 
    position: fixed;
}
.triangle2 {
    border-left: 100px solid transparent;
    border-top: 100px solid #4c4c4c; 
    position: fixed;
    margin-left: 3px;
}
<div class="triangle1">
</div>

<div class="triangle2">
</div>

Edit:

Adding text inside the triangle can be achieved by inserting another div with text and positioning it accordingly.

See Updated Fiddle

Answer №3

.triangle2 {
    width: 0; 
    height: 0; 
    border-left: 0px solid transparent;
    border-right: 200px solid transparent;
    border-bottom: 150px solid black;
}

When it comes to DIV Element 1, create a triangle shape using CSS properties for the top of a square element positioned below it.

Answer №4

Sample HTML code :

<div id="Triangle-1"></div>
<div id="Triangle-2"></div>

Custom CSS :

#Triangle-1 { width: 0; height: 0; border-bottom: 100px solid black; border-right: 100px solid transparent; }
#Triangle-2 { width: 0; height: 0; margin-left: 15px;border-top: 100px solid black; border-left: 100px solid transparent; }

You can modify the size and shape of your triangles by adjusting the border properties.

Feel free to experiment with this code!

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

Tips for customizing the back button in Ionic 3

<ion-header> <ion-navbar> <ion-buttons left> <button ion-button navPop icon-only> <ion-icon ios="ios-arrow-back" md="nbsons-arrow-back"></ion-icon> </button> </ion-buttons> <ion-title> ...

Tips for stopping an image from stretching the cell in flexbox using CSS

When using Flexbox to style elements, the width of the parent should be determined by the text inside child1. The image inside child2 should grow proportionately as the parent grows. However, it is important to ensure that the parent div does not exceed it ...

Gatsby utilized the background-image CSS style within the Emotion library for CSS-in-JS implementation

The background image is not visible with the current setup. To troubleshoot, I attempted to set the background to pink within const background and it worked. This confirms that Emotion is functioning correctly. Upon inspecting with the React Dev Tools ext ...

The Android webview fails to load the page, but the app successfully loads it when accessed through the

I'm experiencing an issue with my HTML5 mobile web app loading fine in a browser but getting stuck on an Android webview. We have implemented a splash screen before loading the web app, but the webview seems to be stuck on the splash screen and does n ...

When a user right-clicks on certain words, the menu opens using JavaScript

Looking to implement a custom context menu that opens when specific words are right-clicked by the user in JavaScript. I have been searching for a solution for quite some time now. The word should be recognized based on an array of specific words. Can some ...

Optimizing CSS usage within Django: top recommendations

Throughout a 6-month-long project, I have continuously added new URLs. However, I am now encountering an issue when using the extend 'base.html' function on other pages where the CSS overlaps and creates confusion. My question is: What are the b ...

Retrieve data from an HTML table using PHP

This snippet of code is encapsulated within table tags and retrieves data from a database to populate the table. Within the database, there is an auto-increment column named 'id'. The objective is to obtain the 'id' value associated wit ...

Streaming live video on the website

Hi there! I'm looking to integrate live video capturing into an HTML/JavaScript site for a presentation. However, I'm not sure where to start my search. Can anyone point me in the right direction? :) The live video will be captured by a camera o ...

Utilizing DataTables to dynamically populate a table with data fetched from an AJAX request

I'm currently in the process of developing a program that showcases various topics within a specific subject. My main query revolves around whether or not the DataTable functionality will be compatible with this particular setup. The following is th ...

Previewing a PDF file with multiple headers displayed above the text content

Creating multiple headers for each page of a PDF document using dompdf can be tricky. The headers you generated are fixed at the top with a width of 100%. However, you are facing an issue where on the second and subsequent pages, the header overlaps with t ...

The transparent background causes the vertical menu to malfunction on IE8

This is the first website I am working on: www.olgoya.com The issue I am facing is that all major browsers, except IE8, display the submenu even when the mouse hovers over the 'portfolio' item. Upon investigation, I found that the problem lies w ...

Attempting to achieve the effect where the entire row of a table changes color when the mouse hovers

I'm attempting to create a gradient effect for an entire row in a table when the mouse hovers over any cell within that row. Despite using what I believe is the correct CSS code, nothing changes upon mouseover (even though the original gradient appear ...

Discover the steps to convert an image to base64 while circumventing the restrictions of the same-origin policy

I've been struggling to convert an image link to base64 in order to store it on the client-side browser (IndexedDB). Despite searching for a solution for days, I have not been able to find one that addresses my issue. While I can successfully convert ...

Issues with jQuery Sliders

Hello there! I am currently in the process of creating a basic jQuery slideshow. The specific requirement for this slideshow is quite simple - I want the images to continuously slide to the left. I have come across numerous tutorials online, but most of th ...

Tips for showing data from Ajax responses on an HTML page

In my code, there are two JavaScript functions: The function fetch_items is responsible for returning data in the form of stringvalue. On the other hand, the function extract_items($arr) passes values to the fetch_items function. function fetch_items ...

Is it feasible to incorporate one iOS app within another iOS app through in-app purchasing or subscription?

Simply put, we are creating interactive content in Flash that we want to distribute through an iOS app either by in-app purchase or subscription. In our testing, we have found that Flash can produce standalone iOS apps that work well for our needs. Instea ...

Use the $.get() method in JavaScript to send a parameter to my controller function

My objective is to showcase the event details in a modal. To achieve this, I am running a JavaScript script that calls the "GetEventsDetails" method in my "Event" controller with the event ID. While debugging in Chrome, I can see the ID being passed corre ...

AngularJS bidirectional binding with numerous select choices

I am faced with a unique challenge - I have a list of non-exclusive fields that users want to see presented in a select box allowing multiple selections, rather than individual checkboxes bound to Boolean values in the model. While I know how to create t ...

In order to design a v-btn-toggle with vertically aligned buttons, rather than horizontally

I'm currently in the process of developing a quiz using VueJS and Vuetify. My challenge lies in organizing the answer options vertically for the user to select. I attempted to utilize the v-btn-toggle component, but encountered an issue where the butt ...

Activate divs with Bootstrap5 modal toggle functionality

What adjustments must be made to the Bootstrap 5 example below in order to achieve the following two objectives: The "afterAcceptingTerms" division should remain hidden until the user clicks on the Accept Terms modal button, ensuring that only the "before ...