搜尋
首頁web前端css教學如何在響應式容器內垂直對齊影像?

How Can I Vertically Align an Image Within a Responsive Container?

在響應式容器內垂直對齊圖像

挑戰

您有一個HTML 結構,其中一個容器保持方形縱橫比作為瀏覽器視窗已調整大小。在此容器內,您想要新增映像,但需要確保它保持垂直對齊。挑戰的出現是因為影像的高度是可變的,容器的高度無法固定。

使用CSS 內嵌元素

  1. 建立一個內聯塊偽元素作為容器元素的第一個(或容器元素的第一個(或容器元素最後一個)子元素,並將其高度設為100%以佔據整個容器的高度。
  2. 為偽元素和影像設定vertical-align: middle,使元素垂直居中。
  3. 透過設定 font-size 刪除元素之間的任何空白: 0;在容器元素上刪除字元(空格)所佔用的空間。

HTML:

<div class="container">
    <div>
<p>CSS:</p>
<pre class="brush:php;toolbar:false">.container {
    height: 300px;
    text-align: center;  /* align the inline(-block) elements horizontally */
    font: 0/0 a;         /* remove the gap between inline(-block) elements */
}

.container:before {    /* create a full-height inline block pseudo=element */
    content: ' ';
    display: inline-block;
    vertical-align: middle;  /* vertical alignment of the inline element */
    height: 100%;
}

#element {
    display: inline-block;
    vertical-align: middle;  /* vertical alignment of the inline element */
    font: 16px/1 Arial sans-serif;        /* <h3 id="製作容器響應式">製作容器響應式</h3><p>要建立高度相對於寬度調整大小的響應式容器,您可以使用百分比值對於頂部/底部填充屬性:</p><pre class="brush:php;toolbar:false">.responsive-container {
  width: 60%;

  padding-top: 60%;    /* 1:1 Height is the same as the width */
  padding-top: 100%;   /* width:height = 60:100 or 3:5        */
  padding-top: 45%;    /* = 60% * 3/4 , width:height =  4:3   */
  padding-top: 33.75%; /* = 60% * 9/16, width:height = 16:9   */
}

包裝圖像內容

為了避免容器頂部或底部出現過多空間,請將圖像包裝在包裝元素中並將其定位絕對在容器內以填充其整個空間:

.responsive-container {
  width: 60%;
  position: relative;
}

.responsive-container .wrapper {
  position: absolute;
  top: 0; right: 0; bottom: 0; left: 0;
}

包括影像和CSS對齊

HTML:

<div class="responsive-container">
  <div class="dummy"></div>

  <div class="img-container">
    <img src="/static/imghwm/default1.png" data-src="http://placehold.it/150x150" class="lazy" alt="">
  </div>
</div>

用於圖片對齊的CSS:

.img-container {
  text-align: center; /* Align center inline elements */
  font: 0/0 a;       /* Hide the characters like spaces */
}

.img-container:before {
  content: ' ';
  display: inline-block;
  vertical-align: middle;
  height: 100%;
}

.img-container img {
  vertical-align: middle;
  display: inline-block;
}

瀏覽器相容性的替代方案

為了更喜歡好地跨瀏覽器相容性,您可以使用div元素作為影像容器的第一個子元素而不是偽元素:

HTML:

<div class="img-container">
    <div class="centerer"></div>
    <img src="/static/imghwm/default1.png" data-src="http://placehold.it/150x150" class="lazy" alt="">
</div>

CSS:

.img-container .centerer {
  display: inline-block;
  vertical-align: middle;
  height: 100%;
}

使用max-* 屬性進行影像控制

保留影像當容器內寬度較小時,可以使用max-height 和max-width屬性圖:

.img-container img {
    max-height: 100%;  /* Set maximum height to 100% of its parent */
    max-width: 100%;   /* Set maximum width to 100% of its parent */
}

以上是如何在響應式容器內垂直對齊影像?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
@rules具有多少特異性,例如@keyframes和@media?@rules具有多少特異性,例如@keyframes和@media?Apr 18, 2025 am 11:34 AM

前幾天我得到了這個問題。我的第一個想法是:奇怪的問題!特異性是關於選擇者的,而在符號不是選擇器,那麼...無關緊要?

您可以嵌套@Media和@support查詢嗎?您可以嵌套@Media和@support查詢嗎?Apr 18, 2025 am 11:32 AM

是的,您可以,而且它並不重要。不需要CSS預處理器。它在常規CSS中起作用。

快速吞噬緩存破壞快速吞噬緩存破壞Apr 18, 2025 am 11:23 AM

您應該肯定會在CSS和JavaScript(以及圖像和字體以及其他內容)等資產上設置遙遠的高速緩存標頭。告訴瀏覽器

尋找可以監視CSS質量和復雜性的堆棧尋找可以監視CSS質量和復雜性的堆棧Apr 18, 2025 am 11:22 AM

許多開發人員寫瞭如何維護CSS代碼庫的文章,但並沒有很多關於如何測量該代碼庫質量的文章。當然,我們有

數據學家用於建議不執行值的值數據學家用於建議不執行值的值Apr 18, 2025 am 11:08 AM

您是否曾經有一種需要接受簡短而任意的文本的表格?喜歡名字或其他。那完全是用的。有很多

蘇黎世的最初會議蘇黎世的最初會議Apr 18, 2025 am 11:03 AM

我很高興能前往瑞士蘇黎世參加前界(Love the Name and URL!)。我以前從未去過瑞士,所以我很興奮

使用CloudFlare工人建立全棧無服務器應用程序使用CloudFlare工人建立全棧無服務器應用程序Apr 18, 2025 am 10:58 AM

我在軟件開發方面最喜歡的發展之一是無服務器的出現。作為一個傾向於陷入細節的開發人員

在NUXT應用程序中創建動態路由在NUXT應用程序中創建動態路由Apr 18, 2025 am 10:53 AM

在這篇文章中,我們將使用我構建和部署的電子商務商店演示來進行Netlify,以展示如何為傳入數據製作動態路線。這是一個公平的

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前By尊渡假赌尊渡假赌尊渡假赌
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器