Home  >  Article  >  Web Front-end  >  How to set css image size

How to set css image size

coldplay.xixi
coldplay.xixiOriginal
2021-04-28 15:58:3145185browse

How to set the css image size: 1. Image size setting introduced through the img tag, the syntax is [img{width:150px;height:60px}]; 2. Use the [background-size] attribute to specify the background The size of the image.

How to set css image size

The operating environment of this tutorial: windows7 system, css3 version, DELL G3 computer.

How to set the css image size:

1. Image size settings introduced through the img tag

We can directly Set the width and height in the image tag. It should be noted here that directly setting the width and height values ​​​​in the HTML img tag does not require html units. The default is PX pixels.

The advantage of setting the height and width in the img tag is intuitive. For inserting pictures into articles, you can use this method to control the height and width of the picture. The disadvantage is that if the picture list is formatted like this, a lot of HTML code will be added, making it inconvenient to modify it uniformly.

css Set img image size syntax:

img{width:150px;height:60px}

Here, set the CSS width to 150px, and the css height to 60px. Note that the values ​​​​of css width and css height in the CSS style have units and must be remembered. Be sure to bring your unit with you when you live.

If we set the style directly on img, this will control the width and height of the image in the entire web page. In order to control the image width and height style within the specified object, we usually add the object CSS name before img.

2. Set the background image size with css:

The background-size attribute specifies the size of the background image.

Grammar:

background-size: length|percentage|cover|contain;

Example:

<!DOCTYPE html>
<html>
<head>
<style> 
body
{
background:url(/i/bg_flower.gif);
background-size:63px 100px;
-moz-background-size:63px 100px; /* 老版本的 Firefox */
background-repeat:no-repeat;
padding-top:80px;
}
</style>
</head>
<body>
<p>上面是缩小的背景图片。</p>
<p>原始图片:<img src="/i/bg_flower.gif" alt="Flowers"></p>
</body>
</html>

Rendering:

How to set css image size

Related tutorial recommendations:CSS video tutorial

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

Statement:
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