Is there a way to crop a square 500x500 pixels image with transparent background on all sides of approximately 125 pixels using CSS?

I have a PNG image that is 500x500 pixels with a transparent background. Unfortunately, the image contains a lot of empty space around it, causing it to appear as if it has excessive margins or padding when used in my HTML.

What I actually need from the image is just the central portion which is 250x250 pixels, leaving 125px of transparent empty space on all sides. Is there a way to use CSS to achieve the appearance of cropping the sides of the image?

Since the images are fetched from an API, cropping the sides using software like Photoshop is not an option.

Check out this example image I'm working with -

Answer №1

I successfully cropped it by using the image as a background.

#image-box{
  background-color: #fafafa;
  background-image: url("http://raw.communitydragon.org/pbe/game/assets/loadouts/regalia/crests/prestige/prestige_crest_lvl_200.png");
  height: 300px;
  width: 300px;
  background-repeat: no-repeat;
  background-size: 135%;
  background-position: center center;
} 

img{
  border: 1px solid black;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>My Document</title>
</head>
<body>
  <div id="image-box">
  </div> 
</body>
</html>

You can view the result here: https://codepen.io/bhanusinghR/pen/jONBOJa

Answer №2

If you're looking to enhance your image, consider using a background or containing the image within a clip:

.clip {
  overflow: hidden;
  width: 160px;
  height: 160px;
}
.clip img {
  margin-top: -48px;
  margin-left: -48px;
}
<div class="clip">
  <img src="http://raw.communitydragon.org/pbe/game/assets/loadouts/regalia/crests/prestige/prestige_crest_lvl_200.png">
</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

Seeking guidance on selecting text using XPath

My HTML document contains the following HTML code: <!DOCTYPE html> <html> <head> <title>page XYZ</title> </head> <body> <p><b>bold text</b> some more text </p> <p>< ...

tips on splitting data retrieved from a database into multiple lines

I am looking to create a break line between the command and result: outputs.append(output) outputs.append(request.POST.get("cmds")) device.config = outputs device.save() I have added the value 'outputs' to device.confi ...

Customizing hover color with tailwind CSS

How can I change the color of an icon on mouseover using Tailwind CSS? I have tried to go from this https://i.sstatic.net/ObW6C.png to this https://i.sstatic.net/Tagp3.png, but it's not working. .btn { @apply agt-h-10 agt-w-10 agt-bg-zinc-100 agt- ...

Issue with Bootstrap column functionality on iOS devices detected

I created a card using bootstrap that displays perfectly on all devices except for iOS devices. The layout completely changes on iOS devices as shown in the screenshot attached. Here is the link to view my card on the test server: Here are the screenshot ...

How to Send Javascript DOM Value to Inner HTML

I am trying to extract a value from a hidden element ID and set it as the inner HTML for another element ID. Below is my code: <script> function replaceText(){ var x=document.getElementById("mainTitle1").textContent; document.getElementById("mainTi ...

Emphasize the checkbox

In my table, I have radio buttons that should turn the neighboring cell green when clicked. Using JQuery, I added a "highlight" class on $.change event to achieve this effect. However, I encountered an issue where some radio buttons load with the "checked ...

Clickable links and compliant W3C coding

Recently, I have encountered errors in the W3C validator due to the presence of "name" attributes in some <a> tags in my document. The validator flagged these attributes as obsolete. I am curious to know when and why this happened? Additionally, I n ...

Django music app failing to show playlists

I am currently developing a music streaming platform, and I am facing an issue with displaying the user's playlists in the <div id="divId">. Even though there are playlists stored in the database for the user, the system always shows t ...

Adjusting Flexslider to perfectly accommodate the height and width of the window dimensions

Currently, I am using Flexslider version 1.8 and seeking to set up a fullscreen image slider on my homepage. My goal is to adjust the slider to match the browser window size. While experimenting with width:100% and height:auto properties, I have managed t ...

Address the underlying issues with the background

I am working on an app and I want to include a fixed background image. I am using HTML5, CSS3, and JavaScript for this project. I have applied the CSS code to set the background image as fixed: body{ background:#1F1F1F url(../assets/bg.png) fixed;} Ev ...

display the hidden box contents when clicking the button within a PHP loop

I am attempting to create a simple e-commerce site for learning purposes, but I encountered an issue when trying to display information upon clicking a button. The button does not seem to trigger any action as expected. It is meant to reveal text from the ...

Loads a PHP file automatically using the URL from a jQuery Ajax method

Using the jquery ajax method, I am trying to populate a dropdown menu with a set of option values from my title.php file. However, upon loading the page, in the Chrome Android browser, it redirects to the title.php. Does anyone have any insights into what ...

Absolute positioning causes an element's height to increase

As I delve into the realm of typographical animations and seek to calculate the baseline of a font at various sizes, I've stumbled upon a peculiar discovery: It appears that the height values of elements tend to increase in an erratic manner when thei ...

Unable to show PHP echo values retrieved from MySQL database

I am facing an issue with displaying images in my tables. Currently, I have two tables set up as follows: TABLE users (id, name, image, user_group_id) TABLE user_groups (id, name, menu) The menu column in the user_groups table contains HTML code like thi ...

Traversing through Object consisting of dual arrays within VueJS

Currently, I am working on a chat application built in VueJS and encountering an issue when attempting to display messages along with their respective timestamps. The challenge arises from the need to iterate through an object that contains two arrays: one ...

Easily center list item numbers vertically within fixed height containers

I am facing an issue with the alignment of ordered list item numbers in a list with fixed height and vertically centered content. The numbers seem to be positioned inaccurately. li { height: 80px; border: 1px solid black; } li > span { heigh ...

What is the best way to define ngOptionValue for my ng-option selection?

After updating my select/option code to include a search feature, it caused an issue with my function create. Here is the HTML code: <div class="input-group"> <label htmlFor="categoria" class="sr-only"> ...

Pass information to a PHP file using JavaScript when the form is submitted

Essentially, I am looking to extract values from various inputs and spans using JavaScript when the submit input is clicked. These values will then be sent to PHP using $post in order to ultimately send them via email. Previously, I tested replacing all of ...

Dropdown with multiple selections organized in a hierarchical structure

I am in need of the following : Implement a drop-down menu that reflects hierarchical parent-child relationships. Include a checkbox for each node to allow for selection. If all child nodes are selected, their parent node should be automatically ch ...

Using gridstack-angular.js with the Hot Towel Template: A Comprehensive Guide

I recently started using the Hot Towel Template and decided to incorporate the gridstack-angular package. I am attempting to implement the demo functionality into my-page.controller.js, my-page.route.js, and my-page.html. After loading the dependencies wi ...