要在 css 中設置圖片大小,可以使用以下屬性:1. width:設置圖片的寬度。2. height:設置圖片的高度。3. max-width 和 max-height:保持圖片的寬高比。
如何在 CSS 中設置圖片大小
為了在 CSS 中設置圖片的大小,您可以使用以下屬性:
width: 設置圖片的寬度。
height: 設置圖片的高度。
設置固定大小
要設置固定大小的圖片,可以分別使用 width 和 height 屬性:
<code class="<a style='color:#f60; text-decoration:underline;' href=" https: target="_blank">css">img {
width: 200px;
height: 150px;
}</code>
登錄后復制
設置相對大小
您還可以使用百分比值來設置圖片的相對大小,使其適應容器的大小:
<code class="css">img {
width: 100%;
height: auto;
}</code>
登錄后復制
保持圖片寬高比
要保持圖片的寬高比,可以使用 max-width 和 max-height 屬性:
<code class="css">img {
max-width: 100%;
max-height: 100%;
}</code>
登錄后復制
示例
以下示例展示了如何使用這些屬性來設置不同大小的圖片:
<code class="css">/* 設置固定大小的圖片 */
img.fixed {
width: 200px;
height: 150px;
}
/* 設置相對大小的圖片 */
img.relative {
width: 100%;
height: auto;
}
/* 保持圖片寬高比 */
img.aspect-ratio {
max-width: 100%;
max-height: 100%;
}</code>
登錄后復制






