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

Solution to the root password login problem in MySQL 5.7

After I found that the previous article solved th...

A complete guide to some uncommon but useful CSS attribute operations

1. Custom text selection ::selection { background...

Disabled values ​​that cannot be entered cannot be passed to the action layer

If I want to make the form non-input-capable, I se...

Detailed explanation of HTML table inline format

Inline format <colgroup>...</colgroup>...

Example test MySQL enum type

When developing a project, you will often encount...

Web Design Principles of Hyperlinks

<br />Related articles: 9 practical tips for...

Analysis of the implementation process of three modes of VMWare network adapter

Three modes Bridged (bridge mode), NAT (network a...

Friendly Alternatives to Find Tool in Linux

The find command is used to search for files in a...

Summary of the advantages of Vue3 vs. Vue2

Table of contents 1. Why do we need vue3? 2. Adva...

Writing a shell script in Ubuntu to start automatically at boot (recommended)

The purpose of writing scripts is to avoid having...

How to enable MySQL remote connection in Linux server

Preface Learn MySQL to reorganize previous non-MK...

MySQL Series 13 MySQL Replication

Table of contents 1. MySQL replication related co...

MySQL max_allowed_packet setting

max_allowed_packet is a parameter in MySQL that i...