What is the best way to align the image within a skewed div to appear "normal"?

I've been trying to create a diamond shape with an image inside, but I'm facing issues with the distortion of the image. My goal is for the image to appear in its regular horizontal orientation and completely fill the diamond shape.

If you want to take a look at the code I've created.

.diamond {
  width: 300px;
  height: 300px;
  border: 3px solid white;
  outline: 2px solid black;
  outline-offset: 20px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) rotate(67.5deg) skewX(45deg) scaleY(0.70711);
  overflow: hidden;
}

.diamond-image {
  background-image: url("https://images7.alphacoders.com/108/1082408.png");
  background-size: cover;
  position: absolute;
  height: 100%;
  width: 140%;
  top: 0px;
  left: 0px;
  transform-origin: top bottom;
}

html{
    height: 100%;
}

body{
    background: rgb(224,215,215);
    background: radial-gradient(circle, rgba(224,215,215,1) 0%, rgba(144,144,144,1) 100%);
}
<div class="diamond">
  <div class="diamond-image"></div>
</div>

Answer №1

It may prove challenging to reverse the transformation. I suggest constructing this in a different manner without using transformation.

You have the ability to manipulate the shape by adjusting the width/height and utilizing CSS variables

html{
  min-height: 100%;
  background: radial-gradient(circle, rgba(224,215,215,1) 0%, rgba(144,144,144,1) 100%);
}

.box {
  --d:15px; /* gap between border and outline*/
  --b1:3px; /* width of outline (black)*/
  --b2:3px; /* width of border  (white) */

  --g1:transparent calc(48% - var(--d) - var(--b1) - var(--b2)) ,
       #fff        calc(49% - var(--d) - var(--b1) - var(--b2)) calc(49% - var(--b1)),
       #000        calc(50% - var(--b1));
  
  --g2:#fff       calc(48.5% - var(--d) - var(--b1) - var(--b2)) calc(48.5% - var(--d) - var(--b1)),
      transparent calc(49% - var(--d) - var(--b1)) calc(49% - var(--b1)),
      #fff        calc(49.5% - var(--b1)) 49.5%,
      transparent 50%;
  width:200px;
  height:400px;
  display:inline-block;
  vertical-align:top;
  background: 
    linear-gradient(to top    right,var(--g1)) top right    / 50% 50%,
    linear-gradient(to top    left ,var(--g1)) top left     / 50% 50%,
    linear-gradient(to bottom right,var(--g1)) bottom right / 50% 50%,
    linear-gradient(to bottom left ,var(--g1)) bottom left  / 50% 50%,
    url(https://picsum.photos/id/1074/800/800) center/cover;
  background-repeat:no-repeat;
  
  -webkit-mask: 
    linear-gradient(to top    right,var(--g2)) top right,
    linear-gradient(to top    left ,var(--g2)) top left,
    linear-gradient(to bottom right,var(--g2)) bottom right,
    linear-gradient(to bottom left ,var(--g2)) bottom left;
  -webkit-mask-size:50% 50%;
  -webkit-mask-repeat:no-repeat;
  mask: 
    linear-gradient(to top    right,var(--g2)) top right,
    linear-gradient(to top    left ,var(--g2)) top left,
    linear-gradient(to bottom right,var(--g2)) bottom right,
    linear-gradient(to bottom left ,var(--g2)) bottom left;
  mask-size:50% 50%;
  mask-repeat:no-repeat;
}
<div class="box"></div>
<div class="box" style="width:300px;--b1:5px;--d:20px"></div>
<div class="box" style="height:200px;--b2:5px;--d:5px"></div>

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

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

Challenge involving CSS and Javascript for solving puzzles

In my attempt to create a puzzle with 2 rows and 3 columns using CSS and JavaScript, I envision the pieces of the puzzle being black and cut into various shapes. The objective is for users to drag these pieces onto the board in order to complete it. I have ...

CSS gradient problem (line break)

Is there a way to apply a gradient from the top to the bottom of a webpage without it restarting halfway down where a text div ends? The issue can be seen on my codepen.io project: https://codepen.io/pprunesquallor/pen/zwapGQ?editors=1100 I'm using B ...

Experience choppy scrolling in Internet Explorer

Check out my click and drag scrolling Image Viewer here. While it functions perfectly in Firefox and Chrome, Internet Explorer is giving me some trouble. The movement seems jerky, especially when scrolling diagonally. It's like the scroll is sluggish ...

Creating extensive pianoroll visualization using HTML and JavaScript

Currently, I am focusing on developing an HTML5 audio application utilizing the latest Web Audio API, and I require a "pianoroll" feature. This is essentially a keyboard grid where users can draw notes, similar to what is seen in many music production soft ...

Searching for the identification of the Object name or id that has been clicked within a Canvas Element

Within a <canvas> element, I have incorporated two images. var ctx = document.getElementById('canvas').getContext('2d'); var img1 = new Image(); img1.src = 'cloud.jpg'; img1.name = "Image 1"; img1.onload = function() { ...

The functionality of Jquery datatables seems to be faulty when navigating to the second page

While utilizing the jQuery datatables plugin, I encountered an issue where the event click function only worked on the first page and not on subsequent pages. To address this problem, I discovered a helpful resource at https://datatables.net/faqs/ Q. My ...

VueJS3 and Vuetify are in need of some additional CSS classes

We are currently in the process of upgrading our application from old VueJS+Vuetify to VueJS3. However, we have encountered some classes defined in the template that are not available in the new version. These classes include xs12 (which is intended to co ...

A guide to displaying the date retrieved from a JSON data file API request using JavaScript

How can I display the date from a JSON data file API call using JavaScript? I am facing difficulty in showing the date on the dashboard display page. I am utilizing JavaScript with async-await for calling the API, and Bootstrap 4 for design. The function ...

How can I increase the element by $100 using a dropdown selection in JavaScript?

Hey there! Looking to create a dropdown list with two shipping options: Special Shipping Normal Shipping <select> <option>Special Shipping</option> <option>Normal Shipping</option> </select> If the user selects Speci ...

I seem to be encountering an issue with the image tag while working with HTML code

I have been studying through freecodecamp.org and this is my first tribute project. Everything has been going well, but I am encountering an issue with the image tag. I need to submit my project online, so I have been trying to input image links from Googl ...

Generate the stored image data onto an HTML 5 canvas

I have been diving into the world of HTML5, exploring how to create and draw shapes in canvas and save them in a MySQL database for later restoration within the canvas itself. During my research, I came across this helpful tutorial. After successfully sa ...

How can I stop full-page auto-scroll screenshot extensions from automatically scrolling?

As the owner of a membership website, I'm faced with the challenge of preventing users from easily taking full page screenshots of the valuable text content in my members area. Many browser extensions, such as Fireshot and GoFullPage, enable auto-scro ...

Attempting to extract content from a specific span element

I've been struggling to extract the text within a <span> element from a webpage. Every attempt I've made that doesn't result in an error has come back empty, with no output whatsoever. Here is the code I'm using: import time imp ...

JQuery: Implementing automatic selection in a selectbox

Seeking assistance on how to automatically preselect a value in a select box. I am trying to have the select box automatically choose admin aid VI, but my script is not functioning correctly. Can anyone provide guidance on this issue? Here is the HTML co ...

What is the origin of the character?

Currently running a string test. When I obtain the innerHTML, there are unexpected u'\n and \n', AssertionError: u'\n<p><strong>The user/password code entered is incorrect!</strong></p>\n' != ...

Unexpected behavior is being encountered with the else statement, and there are compatibility issues with IE and Mozilla Browser in the overall script

Script is functioning as expected in Google Chrome, but is not responsive in IE and Mozilla browsers JavaScript code: <script src="jquery.min.js"></script> <script> function Run() { if(jQuery('#inputtext').val() == '0 ...

Having trouble retrieving the default text from an input element using Selenium

Seeking the date 11/30/2022 from the SOA Handled Date/Time field on a private site shown here. The text is within an input field that is pre-filled when the page loads, and the corresponding HTML looks like this. <td> <input type="tex ...

Change the opacity of a DIV or any other element when hovering over it

I have successfully applied opacity: 1; to a paragraph when hovering over itself. However, I want the opacity to also change when hovering over the header above it. Here is my CSS code: .testH { font-family: impact; text-align: center; font-s ...

The CSS form appears to be too large for the page

On every single one of my pages, the content fits perfectly within the body: However, when I have forms on the page, they extend beyond the footer and the body doesn't resize accordingly: Where could the issue be coming from? ...

The navbar expands in height when resized

I've been working on creating a responsive navbar, but I'm facing an issue where the height of the bar doubles between phone screen size and full window size. Despite trying various Bootstrap classes, I haven't had any success in fixing this ...