Issue with linear gradient not functioning in :before pseudo element

Does anyone know how to create a triangle cut effect on a div without the background being rendered as a rectangle?

body {
  background: #333;
}

.parent {
  height: 200px;
  background: yellow;
  position: relative;
  overflow: hidden;
  display: block;
}

.parent:before {
  content: '';
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 20%;
  background: linear-gradient(190deg, transparent 20%, #FFFFFF 20.2%), linear-gradient(90deg, #FFFFFF 0%, #FFFFFF 100%);
  z-index: 2;
}
<div class="parent"></div>

I am aiming for a design like this:

https://i.sstatic.net/jJe8Q.png

The goal is not to 'cut' the .parent div itself but to have the cut effect on another div that sits on top of it. The triangle cut will be white with transparency around it to reveal what's inside the .parent div.

Based on the image provided, here is the breakdown:

  • The yellow div represents .parent.
  • The white triangle corresponds to .slant.

Answer №1

Looks like your background is functioning correctly. Try adjusting the color scheme and see how it looks.

Here's a similar example to help guide you:

background: linear-gradient(120deg, transparent 10%, #333333 10.2%), linear-gradient(45deg, #ff9900 0%, #00cc66 100%);

Answer №2

If you want to modify your code without using pseudo elements, you can try the following approach:

body {
  background: #333;
}

.parent {
  height: 200px;
  background: 
    linear-gradient(yellow,yellow)                               top   /100% 30%,
    linear-gradient(to bottom left,yellow 50%,transparent 50.5%) bottom/100% 70%;
  background-repeat:no-repeat;
}
<div class="parent"></div>

Updated Version

body {
  background: #333;
}

.parent {
  height: 200px;
  background: yellow;
  position:relative;
}

.parent:before {
  content:"";
  position:absolute;
  top:30%;
  left:0;
  right:0;
  bottom:0;
  background:linear-gradient(to bottom left,transparent 50%,#fff 50.5%);
}
<div class="parent"></div>

Answer №3

To create interesting effects, you can utilize the clip-path property in your div elements.

clip-path: polygon(0 0, 100% 0, 100% 100%, 0 50%);

By using the clip path property, you can clip your elements into various shapes. The polygon method offers precise control to define linear paths along the element. By specifying the vertices of the polygon, you can create intricate shapes with clockwise precision.

For assistance in creating shapes through code, visit

In this example, the coordinates provided correspond to x = 0, y = 0 (top left corner), the rightmost corner, the bottom right corner, and the midpoint of the left edge at 50%.

The clip path will eliminate any background outside of this defined shape.

Keep in mind that while the div itself remains a rectangular block, its content may overflow if height and padding are not managed correctly.

body {
  background: #333;
}

.parent {
  height: 200px;
  background: yellow;
  position: relative;
  overflow: hidden;
  display: block;
  z-index: 0;
  clip-path: polygon(0 0, 100% 0, 100% 100%, 0 50%);
}

.parent-white {
  height: 200px;
  background: white;
}
<div class="parent-white">
  <div class="parent">
  
  </div>
</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

Ways to expand a navbar background without compromising the central alignment of its elements

I need the blue color of the navigation bar to cover the entire screen, while keeping the About, Updates, and Who am I? links centered. The background should adjust accordingly if there are any resizing changes. If there's a better method for centerin ...

Displaying content generated by Html.Raw in MVC4 using JavaScript and AJAX is causing displacement of content within the <a> tags in the HTML

My project includes a feature where emails sent to a specific address are parsed, with attachments saved to the network and metadata stored in a database. If the serial number of a finished good matches the one included in the email, a note is generated an ...

Firefox is unable to display SWF files

My code snippet: <div id="sw" style="cursor:pointer" > <object id="swfobj" type="application/x-shockwave-flash"> </object> The JavaScript part: function openfile(filePath) { $("#swfobj").attr("data", filePath); $("#sw ...

Error encountered when using Symfony 2 command line interface

Just started learning Symfony2 and encountered a problem. I made changes to my CSS and when I try to re-install them in Eclipse shell, I get this error message: 'C:\wamp\www\Symfony' is not recognized as an internal or external ...

Ways to adjust iframe height as it loads

$('#song-link').change(function () { var link = $('#song-link').val(); SC.oEmbed(link, { element: document.getElementById('putTheWidgetHere') }); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1 ...

The percentage height setting for a div is not functioning properly, but setting the height in pixels or viewport

Within a dialog box body, I am attempting to display a table and have applied a CSS class to the wrapping div. When specifying the height in pixels or viewport height units, it works as expected. However, when using a percentage like 50%, the height of the ...

Creating an interactive button using RichFaces and Bootstrap for seamless Ajax functionality

I have a challenge with making a fully clickable span or button that includes a Bootstrap refresh glyph-icon. The current code I have inherited only allows the icon itself to be clicked, leaving the area between the icon and button border unclickable. This ...

Show different text or letter when hovering

Is there a way to hide one letter and display another in a link, with a different style on hover? For example: This is a... ...regular link. And this is a... ...hovered link. Any ideas on how to make this happen? Thank you. Edit: Just to clarify — ...

Parsing HTML with Qt library isn't able to detect any elements

I am currently working on a web crawling application. Within the code, I have the following snippet: // Normally, the HTML content is retrieved from the web using QNetworkAccessManager & QNetworkReply: // QString htmlCode = this->reply->readAll( ...

Stop the MatSort feature from activating when the space key is pressed in Angular Material

I am currently using angular material tables and have implemented filters for each column. The filter inputs are located in the row header of each column, which is working fine. However, I am facing an issue where whenever I type and press the space key, ...

Troubleshooting Problems with CSS Span Placement

Challenges with Span Positioning in CSS Currently, I am encountering difficulties while working on the index.html.erb file for an example Rails website. Below is a snippet of the HTML code I am struggling with (please note that this is simply a fabricated ...

The functionality of jQuery click events seems to be disabled on the webpage, however, it is working perfectly fine on jsFiddle

After thoroughly checking the js tags and ensuring everything is properly closed, it's frustrating when the JavaScript suddenly stops working. The code functions perfectly in JSFiddle, leading me to believe that the issue may lie with something I&apos ...

Incorporating Ace Editor into a jQuery UI Resizable Element

I am attempting to create a resizable Ace editor by placing it inside a resizable component. Despite my efforts with the jQuery UI Resizable component, I am unable to get the ace editor to display within the resizable component. Code: <!doctype html> ...

Switch up the placement of articles, going back and forth between left

On the page, I want to display articles with alternating positions - one on the left, the next on the right. In this example, all the articles are currently being displayed twice, both on the left and the right side. How can I ensure that only one instanc ...

Unable to choose the specific div element within the container that has an inset shadow applied by utilizing Pseudo

It seems that I am encountering an issue when trying to select the div elements within a container that has a defined shadow using pseudo elements ::before OR ::after. Once the shadow is applied, the div elements become unselectable. Please refer to the c ...

Columns with adjustable widths

I am looking to create a layout with 3 columns (which may change, could be up to 4) that are all flexible width and fill the screen. Does anyone know if this is possible using CSS? Basically, I want three blocks lined up horizontally that take up the enti ...

"Troubleshooting a dropdown navigation bar problem in CSS

I am currently working on creating a CSS navbar dropdown, but I am encountering some issues. I am not very experienced with CSS, so any assistance would be greatly appreciated. The text within the "Products" box is not changing color like it should, and yo ...

Insufficient column height when using CSS flex-grow property

I am facing an issue with my 3 flex columns that have varying text lengths, resulting in uneven heights. I have tried using flex-grow: 1; to make them match in height, but it does not seem to be working as intended. Can someone please help me identify what ...

How to make background color transparent in CSS for Safari browser

Does anyone know how to make the background color transparent in Safari? Right now, my PNG file is showing with a white background instead of being transparent. I have attempted: background-color: transparent; background-color: rgba(255,255,255,0); appe ...

Incorporating an HTML image into a div or table using jQuery

I am a beginner in using JQuery within Visual Studio 2013. My question is how to insert an img tag into a table or div using JQuery? For example, I have a div and I would like to generate an image dynamically using JQuery. Or, I have a dynamically create ...