How to display only the center of the image in the img tag in HTML (three methods)

How to display only the center of the image in the img tag in HTML (three methods)

There are currently three ways to display the center of an image in the img tag in html. I will record them here.

The first method: using CSS clip:rect(top right bottom left); usage, need to be used with position: absolute: as follows

<img src="upload/2022/web/77b8b5ca-11d3-4307-9a93-c12df5eb1a35.jpg"
     style="position: absolute;clip: rect(0px,250px,200px,50px);width: 300px;height: 200px">

Setting the width and height of the image is equivalent to scaling the image proportionally to its actual width and height, and then using the rect method to set the cropping range of the image.

- The second method: using the background attribute of img:

<style type="text/css">
        img {
            background-image: url(upload/2022/web/77b8b5ca-11d3-4307-9a93-c12df5eb1a35.jpg);//Set the background image background-repeat: no-repeat;//The background image will only be displayed once.
            background-attachment: scroll;//
            background-position: -50px 0px;//Set the offset of the background image. This -50 is equivalent to the background being offset 50 to the left as a whole, so that the center of the image can be displayedbackground-size: 300px 200px;////Set the size of the background image, which is equivalent to the actual width and height of the image being proportionally scaledbackground-color: transparent;//
            width: 200px;//
            height: 200px;//
        }
    </style>

Use the background to control the center position of the image display. You need to set the background to scale proportionally according to the actual width and height of the image, and then offset the background's movement to control the image's width and height. It should be noted that the src of the image cannot be set. When the img tag does not set the src, a gray border will appear on the displayed image, and there is no way to remove it. border:0px has no effect. My previous solution was to put a default fully transparent image in the src, which solved the problem.

The third method: include img in div and use div's overflow: hidden; to control it. It is more flexible to use.

<div style="width: 100px;height: 100px;overflow: hidden">
<img src="upload/2022/web/77b8b5ca-11d3-4307-9a93-c12df5eb1a35.jpg" style="position: relative" id="img_id">
</div>
<script>
    var img = document.getElementById("img_id");
    var image = new Image();
    var realWidth = 0; //Store the actual width of the image var realHeight = 0; //Store the actual height of the image //Get the width and height of the image image.src = "upload/2022/web/77b8b5ca-11d3-4307-9a93-c12df5eb1a35.jpg";
    //Processing of successful loading image.onload = function () {
        realWidth = image.width; //Get the actual width of the image realHeight = image.height; //Get the actual height of the image //Let the width and height of img be proportional to the actual width and height of the image, and then offset if (realWidth > realHeight){
            img.width = (100/realHeight)*realWidth; //Proportional scaling width img.height = 100; //Consistent with div height img.style.left = '-' + ((100/realHeight)*realWidth-100)/2 + 'px'; //Set the image's relative position offset to half the width-height of the img tag }else if (realWidth < realHeight){
            img.width = 100 ; // same height as div img.height = (100/realWidth)*realHeight; //proportional scaling height img.style.top = '-' + ((100/realWidth)*realHeight-100)/2 + 'px'; //set the image's relative position offset to half the height-width of the img tag }else {
            img.width = 100;
            img.height = 100;
        }
    };
    //Processing of image loading failure img.onerror = function () {
        img.src = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1492076382452&di=04ebd6c4688b2ffbd8ae18e685234704&imgtype=0&src=http%3A%2F%2Fd.hiphotos.baidu.com%2Fzhidao%2Fwh%253D450%252C600%2Fsign%3D0c96dc86da33c895a62b907fe4235fc6%2F0823dd54564e9258d2bb2dff9f82d158ccbf4e17.jpg";
        img.width = 100;
        img.height = 100;
    }
</script>

The comments above are very clear. The main thing is that div controls the size, and the img tag adjusts its own size according to the size of div. The offset is performed to display the middle part of the picture. Personally, I think the third method is better.

The above are the methods (three methods) that I introduced to you in HTML to only display the center of the image in the img tag. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

<<:  Implementation principle and configuration of MySql master-slave replication

>>:  How to communicate between WIN10 system and Docker internal container IP

Recommend

Specific example of MySQL multi-table query

1. Use the SELECT clause to query multiple tables...

JavaScript to implement click to switch verification code and verification

This article shares the specific code of JavaScri...

Docker deploys Mysql, .Net6, Sqlserver and other containers

Table of contents Install Docker on CentOS 8 1. U...

Detailed explanation of web page loading progress bar (recommended)

(When a web page is loading, sometimes there is t...

Using HTML web page examples to explain the meaning of the head area code

Use examples to familiarize yourself with the mean...

Timeline implementation method based on ccs3

In web projects we often use the timeline control...

PostgreSQL materialized view process analysis

This article mainly introduces the process analys...

Methods and steps for deploying GitLab environment based on Docker

Note: It is recommended that the virtual machine ...

How to run multiple MySQL instances in Windows

Preface In Windows, you can start multiple MySQL ...

vite2.x implements on-demand loading of ant-design-vue@next components

1. Use version vite:2.0 ant-design-vue: 2.0.0-rc....

WeChat applet implements a simple handwritten signature component

Table of contents background: need: Effect 1. Ide...

Detailed explanation of Vue custom instructions and their use

Table of contents 1. What is a directive? Some co...

Interactive experience trends that will become mainstream in 2015-2016

The most important interactive design article in ...