Home Web Front-end CSS Tutorial How to set the size of img image in css

How to set the size of img image in css

Apr 26, 2024 am 11:48 AM
css

To set the image size in CSS, you can use the following attributes: 1. width: Set the width of the image. 2. height: Set the height of the picture. 3. max-width and max-height: Maintain the aspect ratio of the image.

How to set the size of img image in css

How to set the size of an image in CSS

In order to set the size of an image in CSS, you can use the following Properties:

  • width: Set the width of the image.
  • height: Set the height of the image.

Set fixed size

To set a fixed size image, you can use the width and height attributes respectively :

img {
  width: 200px;
  height: 150px;
}
Copy after login

Set relative size

You can also use percentage values ​​to set the relative size of the image so that it fits the size of the container:

img {
  width: 100%;
  height: auto;
}
Copy after login

Maintain the aspect ratio of the image

To maintain the aspect ratio of the image, you can use the max-width and max-height attributes:

img {
  max-width: 100%;
  max-height: 100%;
}
Copy after login

Example

The following example shows how to use these properties to set images of different sizes:

/* 设置固定大小的图片 */
img.fixed {
  width: 200px;
  height: 150px;
}

/* 设置相对大小的图片 */
img.relative {
  width: 100%;
  height: auto;
}

/* 保持图片宽高比 */
img.aspect-ratio {
  max-width: 100%;
  max-height: 100%;
}
Copy after login

The above is the detailed content of How to set the size of img image in css. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does placeholder mean in vue What does placeholder mean in vue May 07, 2024 am 09:57 AM

What does placeholder mean in vue

How to write spaces in vue How to write spaces in vue Apr 30, 2024 am 05:42 AM

How to write spaces in vue

How to get dom in vue How to get dom in vue Apr 30, 2024 am 05:36 AM

How to get dom in vue

What does span mean in js What does span mean in js May 06, 2024 am 11:42 AM

What does span mean in js

What does rem mean in js What does rem mean in js May 06, 2024 am 11:30 AM

What does rem mean in js

How to introduce images into vue How to introduce images into vue May 02, 2024 pm 10:48 PM

How to introduce images into vue

What is the function of span tag What is the function of span tag Apr 30, 2024 pm 01:54 PM

What is the function of span tag

How to wrap prompt in js How to wrap prompt in js May 01, 2024 am 06:24 AM

How to wrap prompt in js

See all articles