html自动图片尺寸,关于html:CSS背景图像适合宽度,高度应按比例自动缩放
我有
body {
background: url(images/background.svg);
}
期望的效果是該背景圖像的寬度等于頁面的寬度,高度變化以保持比例。 例如 如果原始圖像恰好是100 * 200(任何單位)且正文寬度為600px,則背景圖像最終應為1200px高。 如果調整窗口大小,高度應自動更改。 這可能嗎?
目前,Firefox看起來正在調整高度,然后調整寬度。 這可能是因為高度是最長的尺寸而且它試圖避免裁剪? 我想垂直裁剪,然后滾動:沒有水平重復。
此外,即使明確指定background-repeat:repeat,Chrome也會將圖像置于中心位置,不會重復,無論如何都是默認值。
html, body { height: 100%; }有幫助嗎? 還有,咳嗽。
這有一個CSS3屬性,即background-size(兼容性檢查)。雖然可以設置長度值,但它通常與特殊值contain和cover一起使用。在您的特定情況下,您應該使用cover:
body {
background-image: ? ?url(images/background.svg);
background-size: ? ? cover; ? ? ? ? ? ? ? ? ? ? ?/*
background-repeat: ? no-repeat;
background-position: center center; ? ? ? ? ? ? ?/* optional, center the image */
}
eggplanation為contain和cover
對不起的雙關語感到抱歉,但我將使用Biswarup Ganguly當天的照片進行演示。讓我們說這是你的屏幕,灰色區域在你的可見屏幕之外。為了演示,我將假設一個16x9的比例。
我們想用上面提到的圖片作為背景。但是,出于某種原因,我們將圖像裁剪為4x3。我們可以將background-size屬性設置為某個固定長度,但我們將關注contain和cover。請注意,我還假設我們沒有破壞body的寬度和/或高度。
contain
contain
Scale the image, while preserving its intrinsic aspect ratio (if any), to the largest size such that both its width and its height can fit inside the background positioning area.
這樣可以確保背景圖像始終完全包含在背景定位區域中,但是,在這種情況下,可能會有一些空白空間填充background-color:
cover
cover
Scale the image, while preserving its intrinsic aspect ratio (if any), to the smallest size such that both its width and its height can completely cover the background positioning area.
這可以確保背景圖像覆蓋所有內容。沒有可見的background-color,但是根據屏幕的比例,圖像的很大一部分可能會被切斷:
用實際代碼演示
div > div {
background-image: url(http://i.stack.imgur.com/r5CAq.jpg);
background-repeat: no-repeat;
background-position: center center;
background-color: #ccc;
border: 1px solid;
width: 20em;
height: 10em;
}
div.contain {
background-size: contain;
}
div.cover {
background-size: cover;
}
/********************************************
Additional styles for the explanation boxes
*********************************************/
div > div {
margin: 0 1ex 1ex 0;
float: left;
}
div + div {
clear: both;
border-top: 1px dashed silver;
padding-top:1ex;
}
div > div::after {
background-color: #000;
color: #fefefe;
margin: 1ex;
padding: 1ex;
opacity: 0.8;
display: block;
width: 10ex;
font-size: 0.7em;
content: attr(class);
}
Note the grey background. The image does not cover the whole region, but it's fully contained.
Note the ducks/geese at the bottom of the image. Most of the water is cut, as well as a part of the sky. You don't see the complete image anymore, but neither do you see any background color; the image covers all of the .
這正是我想要的,謝謝。它似乎不適用于Chrome(或至少Chromium) - 有任何好主意嗎?
根據quirksmode.org/css/contents.html#t44,它應該在Chrome中工作,沒有任何前綴。但是,您嘗試過-webkit-前綴嗎?
Yauhen的答案(在本頁底部)實際上是我在尋找的:background-size: contain
@DannyBeckett:雖然這是可能的,但它不是OP想要的:"我想垂直裁剪,然后滾動:沒有水平重復。"
@DannyBeckett:新版本現在應涵蓋所有相關問題。畢竟,我之前的回答是相當微小的。
contain就像縮放適合。 cover就像縮放填充。 contain調整圖像大小以匹配寬度和高度之間的較大尺寸。 cover調整圖像大小以匹配寬度和高度之間的較小尺寸。
@Zeta抱歉,我的錯誤,我混淆了兩個邊框:藍色和紅色邊框。
基于來自https://developer.mozilla.org/en-US/docs/CSS/background-size的提示,我最終得到了以下對我有用的配方
body {
overflow-y: hidden ! important;
overflow-x: hidden ! important;
background-color: #f8f8f8;
background-image: url('index.png');
/*background-size: cover;*/
background-size: contain;
background-repeat: no-repeat;
background-position: right;
}
雖然我使用background-position: center;,但這對我來說比backgound-size: cover;更好。
我可以知道為什么我們需要overflow-y: hidden部分嗎?
我不確定你到底在尋找什么,但是你真的應該看看Chris Coyier寫的CSS-Tricks的這些優秀博客文章:
閱讀每篇文章的描述,看看它們是否符合您的要求。
第一個回答了以下問題:
Is there a way to make a background image resizeable? As in, fill the background of a web page edge-to-edge with an image, no matter the size of the browser window. Also, have it resize larger or smaller as the browser window changes. Also, make sure it retains its ratio (doesn't stretch weird). Also, doesn't cause scrollbars, just cuts off vertically if it needs to. Also, comes in on the page as an inline tag.
第二篇文章的目標是獲得以下內容,"網站上的背景圖像始終覆蓋整個瀏覽器窗口。"
希望這可以幫助。
試試這個,
element.style {
background: rgba(0, 0, 0, 0) url("img/shopping_bgImg.jpg") no-repeat scroll center center / cover;
}
背景圖像不是設置完美然后他的CSS是問題創建所以他的CSS文件更改為下面的代碼
html {
background-image: url("example.png");
background-repeat: no-repeat;
background-position: 0% 0%;
background-size: 100% 100%;
}
%; background-size:100%100%;"
只需添加以下一行:
.your-class {
height: 100vh;
}
vh是視口高度。
這將自動縮放以適合設備的瀏覽器窗口。
在這里查看更多:使div為瀏覽器窗口的100%高度
body{
background-image: url(../url/imageName.jpg);
background-attachment: fixed;
background-size: auto 100%;
background-position: center;
}
描述問題究竟是什么以及如何解決它,請不要在沒有解釋的情況下粘貼一堆代碼。
我遇到了同樣的問題,在調整瀏覽器尺寸時無法調整圖像大小。
壞代碼:
html {
background-color: white;
background-image: url("example.png");
background-repeat: no-repeat;
background-attachment: scroll;
background-position: 0% 0%;
}
好代碼:
html {
background-color: white;
background-image: url("example.png");
background-repeat: no-repeat;
background-attachment: scroll;
background-position: 0% 0%;
background-size: contain;
}
這里的關鍵是添加這個元素 - > background-size:contains;
這對我有用:
background-size: auto 100%;
設置背景大小沒有幫助,以下解決方案對我有用:
.class {
background-image: url(blablabla.jpg);
/* Add this */
height: auto;
}
它基本上會裁剪圖像并使其適合,background-size: contain/cover仍然不適合。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的html自动图片尺寸,关于html:CSS背景图像适合宽度,高度应按比例自动缩放的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab数字图像处理函数,MATLA
- 下一篇: 判断单链表是否带环?若带环,求环的长度?