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

Detailed explanation of the use of React list bar and shopping cart components

This article example shares the specific code of ...

js to achieve a simple carousel effect

This article shares the specific code of js to ac...

Which scenarios in JavaScript cannot use arrow functions

Table of contents 1. Define object methods 2. Def...

MySQL slow query method and example

1. Introduction By enabling the slow query log, M...

Solution to nacos not being able to connect to mysql

reason The mysql version that nacos's pom dep...

Using Docker+jenkins+python3 environment to build a super detailed tutorial

Preface: After the automation is written, it need...

Using text shadow and element shadow effects in CSS

Introduction to Text Shadows In CSS , use the tex...

Docker5 full-featured harbor warehouse construction process

Harbor is an enterprise-level registry server for...

Example code for implementing dotted border scrolling effect with CSS

We often see a cool effect where the mouse hovers...

Use Visual Studio Code to connect to the MySql database and query

Visual Studio Code is a powerful text editor prod...

Windows 10 is too difficult to use. How to customize your Ubuntu?

Author | Editor Awen | Produced by Tu Min | CSDN ...

Detailed process of installing various software in Docker under Windows

1. Install MySQL # Download mysql in docker docke...

Use of Linux xargs command

1. Function: xargs can convert the data separated...

Vue elementUI implements tree structure table and lazy loading

Table of contents 1. Achieve results 2. Backend i...

How to set and get the number of Mysql connections

Get the number of connections --- Get the maximum...