What are some ways to create a tilted slanting div box?

Hi there, I'm just starting out with CSS and HTML. I really want to design a div similar to this one for my school assignment.

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

I experimented with using transform:skewed(25deg), but unfortunately it's not creating the balanced box div that I envisioned.

Answer №1

To achieve the desired slant effect, utilizing clip-path is recommended, as mentioned in the comments. It is crucial to ensure that the path matches on both the inner and outer elements for the border to align properly.

.outside {
  position: relative;
  width: 70vmin;
  height: 70vmin;
  background: red;
  -webkit-clip-path: polygon(0 0, 100% 10%, 100% 100%, 0% 100%);
  clip-path: polygon(0 0, 100% 10%, 100% 100%, 0% 100%);
}

.inside {
  position: absolute;
  top: 5px;
  left: 5px;
  right: 5px;
  bottom: 5px;
  background: black;
  -webkit-clip-path: polygon(0 0, 100% 10%, 100% 100%, 0% 100%);
  clip-path: polygon(0 0, 100% 10%, 100% 100%, 0% 100%);
}
<div class="outside">
  <div class="inside"></div>
</div>

Answer №2

Implementing a skew effect on a pseudo element:

.box {
  width: 250px;
  height: 250px;
  border-bottom: 5px solid red;
  position: relative;
  overflow: hidden;
}

.box:before {
  content: "";
  position: absolute;
  inset: 0;
  border: 5px solid red;
  border-bottom: none;
  transform-origin: left;
  transform: skewY(10deg); /* tweak the angle here */
}
<div class="box"></div>

Answer №3

For a transparent background similar to the image you have, you can achieve it by using two elements for the top and bottom sections and cutting it away as shown below:

:root {
  --thickness: 10px;
  --color: red;
  --width: 50vw;
  --total-height: 80vh;
  --offset-height: 20vh;
}

.clip-path-top {
  width: var(--width);
  height: var(--offset-height);
  background-color: var(--color);
  clip-path: polygon(0 0, var(--thickness) 0, 100% calc(100% - var(--thickness)), 100% 100%, calc(100% - var(--thickness)) 100%, var(--thickness) var(--thickness), var(--thickness) 100%, 0 100%);
}

.clip-path-bottom {
  width: var(--width);
  height: calc(var(--total-height) - var(--offset-height));
  background-color: var(--color);
  clip-path: polygon(0 0, var(--thickness) 0, var(--thickness) calc(100% - var(--thickness)), calc(100% - var(--thickness)) calc(100% - var(--thickness)), calc(100% - var(--thickness)) 0, 100% 0, 100% 100%, 0 100%);
}


/* for demonstration purposes only */
body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: gray;
}
<div class="clip-path-top"></div>
<div class="clip-path-bottom"></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

Utilizing Bootstrap 4 to create fixed and fluid width side-by-side divs with scrollbars

I want to create a basic dashboard page. I decided to arrange two divs next to each other to serve as the side menu and content. Here is what the layout looks like: https://i.sstatic.net/EATK6.png I encountered an issue where the vertical scrollbar is n ...

Tips for disabling the source of an external iframe

Is there a way to close an iframe src with a click event from within the iframe itself? Let's suppose I have this parent HTML: <body> <script> document.write("<iframe src='default.html' id="myFrame" width=' ...

Ensure that children elements are aligned to the bottom by setting the axis of the HTML

Elements positioned within a parent DIV typically flow from top to bottom. Even when using Javascript to add elements to the parent DIV, they maintain this top-to-bottom positioning. I am interested in achieving a bottom-to-top axis alignment within the pa ...

Is there a way to replicate the data from one canvas onto another canvas using getContext('webgl')?

I am currently working on a project that involves displaying medical images on one canvas and annotating those images by drawing shapes or lines on another canvas. My issue arises when I try to copy the drawn annotations from canvas#2 to canvas#1. Here is ...

Is it suitable to use Twitter Bootstrap span classes for determining the widths of table columns?

Upon receiving a project, I noticed that Bootstrap "span" classes were used in tables extensively. I am questioning the validity of the markup below. Can you explain if it is valid or not, and why? <table class="table"> <tr> <td clas ...

The ion-datetime-button within an ion-item does not have full height

I am experiencing an issue where the ion-items containing ion-select and ion-datetime-button elements do not have consistent heights. Even though they are all nested within ion-cols with identical properties. The ion-items with ion-datetime-buttons appear ...

Steps to avoid HTML encoding the ' character in Next.js _document.js file

Currently, I am working on integrating VWO into my website by following the steps mentioned here. I have included the VWO script tag within a NextJs Head tag as shown below: <Head> <script type='text/javascript' id='vwoC ...

"Enhance Your Website with a Stunning Background Image Using Twitter Boot

With a fixed navigation bar at the top and a container spanning span12 with full-width background, it is important for the content of the background image to be displayed fully regardless of window size. What would be the most effective way to create or c ...

HTML code now launches in the existing electron window rather than opening a new window

<html> <head> <title></title> </head> <input type="button" name="new" id="newmon" value="new"> <button onclick="open1()">monitor 1</button&g ...

Error encountered in ASP.NET Core MVC due to dependency inversion with CRUD operations

Today I delved into dependency inversion. Prior to implementing this concept, my CRUD system was functioning well, but now I am encountering some errors. The error message says "Cannot implicitly convert type int to ContractLayer.UsersDto." In the image pr ...

Tips for preloading a small placeholder image before the main content is loaded

After browsing , I noticed an interesting image loading style. The website initially shows a patterned image before revealing the actual content. This method creates visually appealing content while waiting for the entire webpage to load. Upon inspecting ...

Excessive display of Typescript Alert causing unnecessary interruptions

Why is my button creating multiple alerts when clicked? When I click the Login Button for the first time, the alert shows once. But on clicking it again, the alert shows 2 times. Subsequent clicks increase the alert count accordingly. Furthermore, if I c ...

What is the best method for rendering or retrieving "field[]" in Django forms?

Are you using an array field in Django to dynamically add fields with JavaScript? For example, when a user clicks on a button, a new field appears. However, you're having trouble getting Django to render the field as "field[]" name and retrieving tha ...

I am struggling to set a required input in Angular2

I am new to Angular and currently working on a project that requires input validation to ensure that no field is left blank. The project consists of an HTML file along with a .ts file for functionality. Below is a snippet from the HTML: <div class="f ...

When the user clicks, the page will reload before the POST function is executed

Upon clicking the confirm button, the button click function logs the first two entries in the console. However, when executing the post call function, the page reloads causing an undefined index k error and prevents the post function from completing. If I ...

What is the method used to incorporate the radial gradient in this design?

As I was browsing the internet, I came across several websites with beautiful natural gradients on their backgrounds. However, I'm puzzled as to how they achieved this effect. It doesn't seem like a simple image file because the gradient adjusts ...

Identify the specific code that will be executed upon pressing a button - checkbox restriction

Check out my code example on jsfiddle: https://jsfiddle.net/elenderg/wzarrg06/63/ In the first div, there are 10 buttons. See image below: https://i.sstatic.net/BDdtG.png <button class='btn btn-info custom' onclick="myFunction()" ...

The dropdown feature in AngularJS experiencing sporadic malfunctions on Google Chrome

HTML: <select id="bId" ng-options="b as b.name for b in books" ng-model="selectedBook" ng-change='onBookChange()'> </select> The issue: The options are not always displaying and the selected option is not visible. List ...

Unable to get Bootstrap IMG-RESPONSIVE to function properly in Firefox and IE within the navbar

I recently encountered an issue with the Bootstrap IMG-RESPONSIVE class not functioning properly in the navigation bar on Firefox and IE. Oddly enough, it was working perfectly just a few days ago until I made some unknown changes, causing it to only work ...

Is there a way to organize radio buttons that are located within separate div tags together?

How can I ensure that only one radio button across different divs is selectable? <div><input type="radio" /></div> <div><input type="radio" /></div> <div><input type="radio" /></div> <div><i ...