What is the best way to create a triangle shape that is responsive to elements?

I'm trying to make a background shape with a triangle inclusion using CSS. I want it to be responsive to all screen sizes, but I'm having some trouble. Any tips on how to achieve this?

header {
width: 100%;
height:150px;
background: #eee;
}

main {
width: 100%;
height:150px;
background: #000;
position:relative;
z-index: 1;
}

main::before {
background: inherit;
    top: 0;
    content: '';
    display: block;
    height: 90%;
    left: 0;
    position: absolute;
    right: 0;
    transform: skewY(-3deg);
    transform-origin: 0;
    z-index: -1;
}

main::after {
    border-top: 16vh solid blue;
    border-left: 50vw solid transparent;
    width: 0;
    height: 0;
    transform: skewY(-3deg);
    position: absolute;
    content: "";
    transform-origin: 0;
    right: 0;
    top: -10%;
    }
<header></header>
<main>
<main>

How can I ensure the expected result is achieved for every screen size?

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

Answer №1

Utilize the clip-path - polygon properties to achieve this effect.
Unfortunately, this feature is not supported on Internet Explorer 🤷‍♂️

For more details:

Mozilla: https://developer.mozilla.org/en-US/docs/Web/CSS/clip-path
W3Schools: https://www.w3schools.com/cssref/css3_pr_clip-path.asp
CSS Tricks: https://css-tricks.com/almanac/properties/c/clip-path/

main {
  position: relative;
  width: 100%;
  height: 150px;
  background: #000;
  z-index: 1;
  overflow: hidden;
}
main::before,
main::after {
  content: '';
  position: absolute;
  display: block;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 0;
}
main::before {
  background-color: #00f;
  -webkit-clip-path: polygon(0% 0%, 100% 0%, 100% 100%, 0% 0%);
  clip-path: polygon(0% 0%, 100% 0%, 100% 100%, 0% 0%);
}
main::after {
  background-color: #fff;
  -webkit-clip-path: polygon(0 0, 100% 0, 100% 75%, 0 100%);
  clip-path: polygon(0 0, 100% 0, 100% 75%, 0 100%);
}
<main><main>

Answer №2

If you want to give it a shot, here's a possible approach:

html::before {
  content: "";
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 60vh;
  transform-origin: right;
  transform: skewY(-5deg);
  background: linear-gradient(-160deg, blue 15vw, #000 15.2vw);
}

Answer №3

After extensive research over the past couple of days, I have discovered a solution that is incredibly effective. I am excited to share it here.

By making a slight adjustment to the main::after property, I was able to achieve responsiveness.

Changing transform-origin: 0; to transform-origin: -99%;

And adjusting top: -10% to top: 0

This simple tweak has made all the difference...

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

Original code

I have a database containing HTML code for a table. I would like to display this code in its original form within a textarea so that I can edit it. However, when I try to echo the code, it appears in compiled form as a table rather than the original HTML ...

I'm currently in the process of incorporating horizontal scrolling into various sections of an image gallery. The images will stack vertically only when the window width

I am currently developing an image gallery with multiple sections that contain various images. My goal is to arrange each section of images in a single row, allowing horizontal scrolling similar to Netflix's layout. However, I'm facing challenges ...

Is it possible to continuously increase the value of a CSS property with each click?

I am trying to implement a feature where the value of an element decreases each time a different element is clicked. Currently, I have the following code: $("#trigger_heritage").click(function () { $(".heritage_content ul").css("margin-left", + -880); ...

Looking to extract the hidden value from an HTML input field using Selenium

Unable to retrieve the value of a hidden element in HTML, even though it is displaying. Seeking assistance in accessing the value despite the type being hidden. <input type="HIDDEN" label_value="Sub Reference" title="Sub Reference" id="ACC_NO" dbt="BK_ ...

Numerous HTML forms for submission

How can I distinguish between different forms and submit buttons that are directed to a specific php function? On my current page, I have two forms but each submit button triggers different actions. I attempted to use ISSET to manage the submit actions, ...

Having trouble with Vue router functionality

I've attempted troubleshooting my router countless times, but it still won't work properly. I'm unable to navigate or switch to different pages in my Vue project. Despite following all the steps outlined in this link , nothing seems to chang ...

Tips for showcasing two pieces of content next to each other across the entire webpage using a combination of Bootstrap, CSS, and Laravel

I am facing a challenge with displaying two contents side by side on a full page and stacking them vertically when the screen size is small (mobile). I have managed to achieve the side by side display on a full page, but they stack on top of each other on ...

Displaying a dropdown selection that showcases two object properties lined up side by side

I need the select option dropdown values to display employee names along with their titles in a lined up format. For instance, if my values are: Bob Smith Director Mike Kawazki HR Jane Doe Manager I want them to be shown as: Bob Smith Director Mi ...

In Mobile View, I want to swap out my current image for text to keep it hidden

I am attempting to include text that is only visible in Mobile View as a replacement for my logo. Below is the code I am using: CSS: @media only screen and (max-width:1024px) { header .logo { display: none; } header .logo a { display: non ...

Enter the website address into the designated text field, then simply click on the GO button to be redirected to the destination website within

I'm looking for a basic HTML code that allows users to input a website URL in a text field on my website, hit "GO," and be redirected to the entered site. It seems like this feature is hard to come by as I haven't found any websites with similar ...

How to retrieve Checkbox label text using Selenium and C#

Trying to extract the text value of a checkbox label. Check out this HTML code snippet <div id="content" class="large-12 columns"> <div class="example"> <h3>Checkboxes</h3> <form id='checkboxes'> &l ...

What could be causing this text to not center properly on mobile devices?

Here is the main content. There are no alignment commands in the CSS or HTML. Check out the jsfiddle: https://jsfiddle.net/9t4afmx3/ Any idea why it's left-aligned on mobile devices? <body> <p align="center"><img src="Screen Shot 20 ...

What is the process for triggering data-dismiss after the href has been activated?

Could someone help me with this issue? <a class='btn btn-danger' href='page.html'>Delete</a> I am trying to activate the "data-dismiss" attribute after the "href" is activated. My initial attempt was using this code: < ...

Creating a page turn effect during the loading of a page within an iframe

Is there a way to create a page turn effect similar to reading a book for an iframe that is loading dynamic content? Any suggestions would be greatly appreciated. Link: ...

Incompatibility Issue with Ajax Request on iPad 1 (iOS 5.1.1)

Having an issue with reloading content on my iPad web app. The get request works fine in the browser, but I'm experiencing trouble in Safari on the iPad 1 (iOS 5.1.1). Any suggestions on how to fix this? <script type="text/javascript"> ...

Locate a DOM element by its attribute identifier

Is there a way to use jQuery to find a DOM element based on the name of its attribute, rather than the attribute value? For instance: <div id="div1"> <div id="div2" myattr="myvalue"> </div> </div> I want to locate all element ...

How can I dynamically generate multiple Reactive Forms from an array of names using ngFor in Angular?

I am in the process of developing an ID lookup form using Angular. My goal is to generate multiple formGroups within the same HTML file based on an array of values I have, all while keeping my code DRY (Don't Repeat Yourself). Each formGroup will be l ...

Exploring the Impact of 2 HostBindings on Class Generation from Inputs in Angular 4

I'm struggling with the code in my component, which looks like this: @Component({ selector: "home", templateUrl: "./home.html" }) export class Home { constructor() {} @HostBinding("class") @Input() public type: string = "alert" @HostBindi ...

bootstrap style list-group without overlapping

In the image below, you can see that I have a list of American states within a list-group class. My issue is that I am attempting to eliminate the whitespace and essentially move Arkansas below Alaska and so forth. Here is my current HTML and CSS: CodePen ...

Conceal a component when the succeeding one appears on a separate line

I'm looking for a solution to display an address on a webpage in two different formats based on the length. The first format should be: Street Number, PostalCode City If the address is too long to fit on one line, it should be displayed as: Street ...