Remove the triangle shape from the div and reveal the background

Is it possible to use CSS tricks to cut a triangle out of a div and show the background behind it? I didn't think this was achievable, but I know you pros can surprise me. Here's what I'm aiming for:

Please don't suggest a 3-column solution or anything like that. I want to keep it simple with as few graphics, or no graphics at all. Any cool CSS hacks for this?

Answer №1

To achieve a unique visual effect, one can employ clever techniques and creativity:

  • Utilize the `border-radius` property to create a div with a transparent corner;
  • Incorporate the `::before` (or `::after`) pseudo-element to design a large triangle matching the background color of the div;
  • Adjust the `z-index` to overlay the triangle above the div's background while keeping it below the content;
  • Implement a transparent container of the same size as the div to encapsulate it, using `overflow: hidden` to trim any excess parts of the triangle;

    et voilà!

Live Demonstration: http://jsfiddle.net/d9F3g/

Surprisingly, discovering this possibility was driven by my fondness for challenges and enigmatic queries :)

HTML

<div class="hiddenContent">
    obscured text visible partially;<br/>
    obscured text visible partially;<br/>
    obscured text visible partially;<br/>
    obscured text visible partially;<br/>
    obscured text visible partially;<br/>
</div>
<div class="coverContainer">    
    <div class="cutoutElement">Hello cut-out world</div>
</div>

CSS

.hiddenContent{    
    background: purple;
    position: absolute;
    z-index: 0;    
    top: 0;
}

.coverContainer{
    margin: 20px;
    width: 300px;
    height: 300px;
    background: rgba(0,0,0,0);    
    overflow: hidden;
}

.cutoutElement{    
    width: 230px;
    height: 230px;    
    background: teal;
    padding: 35px;    
    position: relative;
    z-index: 1;
    border-top-right-radius:30%;    
}
.cutoutElement:before{    
    content: '';
    position: absolute;
    top: -44px;
    left: -30px;
    border-width: 65px 65px 65px 65px;
    border-style: solid;
    border-color: rgba(0,0,0,0) teal teal rgba(0,0,0,0);
    z-index: -1;
}

Answer №2

If you want to separate your triangle from the rectangle, consider using this approach:

<div id="rectangle"><div id="mask"></div></div>
#rectangle{width:300px; height:120px; position:relative; margin-top:100px; background: rgb(30,87,153);
background: -moz-linear-gradient(top,  rgba(30,87,153,1) 0%, rgba(125,185,232,1) 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(30,87,153,1)), color-stop(100%,rgba(125,185,232,1)));
background: linear-gradient(to bottom,  rgba(30,87,153,1) 0%,rgba(125,185,232,1) 100%); 
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1e5799', endColorstr='#7db9e8',GradientType=0 );}

#rectangle:after{content:" "; position:absolute; width:0px; height:0px; top:0; left:100px;border:1px solid white; border-color:transparent white transparent white; border-width:0px 50px 50px 50px;}

#mask{position:absolute; left:0; width:100px; height:50px; background:white;}
#mask:after{position:absolute; content:" "; left:200px; width:100px; background:white; height:50px; }

Explore the fiddle here.

Answer №3

One solution that comes to mind for this scenario is utilizing an image mask. This typically involves overlaying one element on top of another in order to display content over the mask.

Currently, support for image masks may not be universal (primarily limited to webkit browsers at the moment), but this is likely to evolve and become more widely adopted in the future.

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

jQuery: Locate and eliminate any images in the text that appear after a specific group of tags if no images are present

Currently, I am in the process of developing a feed reader specifically designed to parse the Google News Feed. You can view and test the code on this Fiddle. My main challenge lies in removing titles and sources from the descriptions. Through my explorati ...

Issue with justify-content in Bootstrap 5 still unresolved

Currently working with Bootstrap 5 and have set up 4 cards. My goal is to position the second card on the left side. I've applied the class justify-content-xl-start to the card, however, it doesn't seem to be functioning as expected. I also attem ...

Creating personalized columns in a BootstrapVue table

I'm having an issue with the b-table component in bootstrap-vue. The array containing my items is structured like this: [{ id: 1, name: "Test", phone: 555-111-666, address: "Some Address", //etc... }] I have a couple of question ...

Triggering jQuery events can be customized by excluding certain elements using the

Is there a way to hide the div "popu" when clicking on the img "tri"? I've tried using .not() since the img is a child of the div popu, but it didn't work. Also, I need to make sure that clicking on the div "textb" does not trigger the hide actio ...

Show a specific item when selecting an option from a dropdown menu

Hey there, I have a question regarding creating a theme for the Genesis Framework. Right now, I'm facing a challenge with one particular element. Here's the situation: I've got two drop-down lists, but I only want the second one to be visib ...

Navigation through Image Filters

Everything seems fine with my code, but when I click on the navigation links, they direct me to index.html or the landing project instead of filtering the images. How can I modify the links to properly filter the images? This is the Javascript and html co ...

Listening for a click event on a newly added element using JQuery

I am relatively new to using JQuery and I have encountered an issue with adding a click event listener for a dynamically created button. When the user clicks on the first button, my function successfully adds a second button to a div. However, I am facing ...

Exploring the power of Google Maps Radius in conjunction with AngularJS

I am currently developing an AngularJS app that utilizes a user's location from their mobile device to create a dynamic radius that expands over time. You can view the progress of the app so far by visiting this link: . The app is functional, but I a ...

The dropdown navigation bar fails to close upon being clicked

I'm currently facing an issue with the navbar in my project. The bootstrap navbar doesn't close automatically after clicking on a link. I have to move my mouse away from the navbar for it to close, which is not ideal. Additionally, I'm worki ...

Is it possible to modify the value on the src img tag prior to scraping the data

Hey everyone, check out this piece of code I have: foreach ($image as $snimka){ $url = $snimka->src; $ch = curl_init($snimka->src); $fp = fopen('images/' . basename($url), 'wb'); curl_setopt($ch, CURLOPT_FILE, $fp ...

Setting up CSS module class names in a Vue project using Vite js configuration

Within my vite.config.ts file, I have specified a configuration for CSS modules. return defineConfig({ ... css: { ... modules: { localsConvention: "camelCase", generateScopedName: "[name]__[local]__[hash:base64:2]" ...

Troubleshooting: Unable to fetch CSS file in Node.js Express framework

I'm trying to access my .html file with localhost using node.js, but I'm facing an issue with loading the CSS that is included in the file. I am using the express framework for this purpose. Code: var express = require('express'); var ...

Positioning of buttons within a div

I am currently designing an inspiration post style: https://i.stack.imgur.com/I5J0H.png However, I am encountering some challenges while creating it: The buttons are not rounded when the window size is changed Social icons are not being displayed I am ...

verify access with mysql database

Need urgent assistance, sorry if this is a repeated query due to my username! I am facing an issue with my login page. I want it to redirect to my home page when authentication fails by cross-checking input with a MySQL database which should output succes ...

Toggle class with jquery if the checkbox is checked

I am trying to achieve a simple task using jQuery. I have a checkbox inside a box, and I want to toggle a class on the box with a transition when the checkbox is checked. However, my code is not working as expected. Here is what I have: $(document).ready( ...

Using absolute positioning for child elements within a table cell division in CSS

Similar Question: Is it possible to position items relatively or absolutely within a display: table-cell? I am working with a display: table layout example. You can view the demo here. In the second panel, there is a #pos div that has a position: abs ...

What could be causing my absolutely positioned div to be hidden from view?

I'm working on designing a slideout slideshow with three images and content that looks like papers hidden in folders, complete with a handle at the top or bottom of each folder displaying its name. Here's an example of what I'm envisioning: ...

Can Spring Boot be set up to return JSON instead of HTML for InvalidTokenException when configuring OAuth2?

When testing my Spring Boot application, I realized that querying one of my REST endpoints with an invalid token using Postman resulted in a response content in HTML format instead of JSON. The endpoint correctly responded with 401 InvalidTokenException, b ...

Automated line wrapping within a div

My goal is to showcase images within a unique div structure. Currently, the arrangement appears as follows: It seems that the green divs are extending beyond their parent div (the black one). This is how it's structured: <div class="photoView c ...

The same size background effect

I am looking to create a list of names that are all the same size. How can I achieve this using Bootstrap? <ul class="list-unstyled"> <li> <a class="btn btn-xs btn-warning"> <span >Loren ipsum</span> </a> ...