CSS3原生支持div與瀏覽器等高等寬方法_html/css_WEB-ITnose
Jun 21, 2016 am 09:00 AM
一般我們需要設(shè)置一個div與瀏覽器等高并等寬充滿全屏,然后設(shè)置背景圖片來達(dá)到一個高大上的排版效果。具體的例子看下面的圖片演示:
隨著鼠標(biāo)的滾動,整個圖片也滾上去了。
以前為了達(dá)到這樣的等高效果,一般通過js來獲取當(dāng)前瀏覽器高度然后動態(tài)設(shè)置div的height,有時候還需要不斷地檢測瀏覽器resize事件來不斷調(diào)整div的height。
其實(shí)CSS自帶的兩個單位vw與vh就能支持與瀏覽器等高等寬的效果,完全可以拋棄js了!
演示效果
html代碼如下:
<!DOCTYPE html><html><head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>全屏圖片</title> <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css"> <style> .fullbg { position: relative; width: 100vw; height: 100vh; background-position: center center; background-size: cover; background-repeat: no-repeat; } .inner-content { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); color: #fff; } </style></head><body> <div class="fullbg" style="background-image: url('http://s.dgtle.com/portal/201601/08/180115ol7n5o75zy7hm002.jpg?szhdl=imageview/2/w/1900');"> <div class="inner-content"> <h1>我就是這么屌</h1> </div> </div> <div class="entry-content"> <p> 相信方今不再會有人質(zhì)疑手機(jī)僅僅只是一個純粹的通訊工具,隨著移動社交應(yīng)用日漸成為生活中不可缺少的工具,手機(jī)的照相功能表現(xiàn)甚至成為了衡量一部手機(jī)好壞的關(guān)鍵指標(biāo)。在絕大部分的旗艦級手機(jī)新品發(fā)布會之中,我們經(jīng)??梢钥吹礁鲝S商們在介紹自家旗艦級機(jī)型拍照能力之時自吹自捧得無可匹敵,但我們深知在這些頂級水平的旗艦級手機(jī)當(dāng)中,仍然需要使用真正的實(shí)力去決一勝負(fù),而這正是本文的主要任務(wù)。 </p> <p> 本文我們選擇了四款旗艦機(jī)型,當(dāng)中分別有索尼 Xperia Z5 Premium 、蘋果 iPhone 6s Plus、 三星 Galaxy S6 Edge+、谷歌 Nexus 6P。 </p> </div></body></html>
css核心代碼如下
.fullbg { position: relative; width: 100vw; height: 100vh; background-position: center center; background-size: cover; background-repeat: no-repeat;}

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The key to keep up with HTML standards and best practices is to do it intentionally rather than follow it blindly. First, follow the summary or update logs of official sources such as WHATWG and W3C, understand new tags (such as) and attributes, and use them as references to solve difficult problems; second, subscribe to trusted web development newsletters and blogs, spend 10-15 minutes a week to browse updates, focus on actual use cases rather than just collecting articles; second, use developer tools and linters such as HTMLHint to optimize the code structure through instant feedback; finally, interact with the developer community, share experiences and learn other people's practical skills, so as to continuously improve HTML skills.

To reduce the size of HTML files, you need to clean up redundant code, compress content, and optimize structure. 1. Delete unused tags, comments and extra blanks to reduce volume; 2. Move inline CSS and JavaScript to external files and merge multiple scripts or style blocks; 3. Simplify label syntax without affecting parsing, such as omitting optional closed tags or using short attributes; 4. After cleaning, enable server-side compression technologies such as Gzip or Brotli to further reduce the transmission volume. These steps can significantly improve page loading performance without sacrificing functionality.

HTMLhasevolvedsignificantlysinceitscreationtomeetthegrowingdemandsofwebdevelopersandusers.Initiallyasimplemarkuplanguageforsharingdocuments,ithasundergonemajorupdates,includingHTML2.0,whichintroducedforms;HTML3.x,whichaddedvisualenhancementsandlayout

It is a semantic tag used in HTML5 to define the bottom of the page or content block, usually including copyright information, contact information or navigation links; it can be placed at the bottom of the page or nested in, etc. tags as the end of the block; when using it, you should pay attention to avoid repeated abuse and irrelevant content.

ThetabindexattributecontrolshowelementsreceivefocusviatheTabkey,withthreemainvalues:tabindex="0"addsanelementtothenaturaltaborder,tabindex="-1"allowsprogrammaticfocusonly,andtabindex="n"(positivenumber)setsacustomtabbing

To embed videos in HTML, use tags and specify the video source and attributes. 1. Use src attributes or elements to define the video path and format; 2. Add basic attributes such as controls, width, height; 3. To be compatible with different browsers, you can list MP4, WebM, Ogg and other formats; 4. Use controls, autoplay, muted, loop, preload and other attributes to control the playback behavior; 5. Use CSS to realize responsive layout to ensure that it is adapted to different screens. Correct combination of structure and attributes can ensure good display and functional support of the video.

Adeclarationisaformalstatementthatsomethingistrue,official,orrequired,usedtoclearlydefineorannounceanintent,fact,orrule.Itplaysakeyroleinprogrammingbydefiningvariablesandfunctions,inlegalcontextsbyreportingfactsunderoath,andindailylifebymakingintenti

To create HTML text areas, use elements, and customize them through attributes and CSS. 1. Use basic syntax to define the text area and set properties such as rows, cols, name, placeholder, etc.; 2. You can accurately control the size and style through CSS, such as width, height, padding, border, etc.; 3. When submitting the form, you can identify the data through the name attribute, and you can also obtain the value for front-end processing.
