本文主要介紹了淺談css網頁的幾種版面的相關資料,小編覺得蠻不錯的,現在分享給大家,也給大家做個參考。一起跟著小編過來看看吧,希望能幫助大家。
2018年已經過了一周,總結一下2017年在公司wiki上寫的一篇關於css佈局的知識,當時也藉鑒了幾個大神寫的css佈局知識,和自己在專案中遇到的坑。廢話不多說。請看以下的乾貨。
1、左邊固定,右邊自適應佈局的兩種實作方式
#效果圖如下:
##大屏展示:
<style type="text/css"> .left{ float: left; width: 100%; height: 200px; background-color: red; } .left-content{ margin-left: 30%; } .right{ float: left; width: 30%; margin-left: -100%; height: 200px; background-color: green; } .layout0{ clear: both; width: 100px; height: 100px; background-color: yellow; } </style> <body> <p id="body"> <p class="left"> <p class="left-content"> 设置子元素的margin,然后父元素必须浮动。 用父元素包裹,主要是因为right会覆盖left,从而导致left内容不可以看到,如果直接在left上设置margin或者padding会导致布局变化,因此只能再用一个p包裹内容,并且去除right覆盖的宽度。 </p> </p> <p class="right">-margin必须大于或等于自身的宽度才会上移</p> <p class="layout0"></p> </p> </body>實作過程中需要注意的是:1.自適應的容器需要容器包住,否則容器內的內容會被覆蓋。 2.right容器的負邊距必須大於或等於自身的寬度才會上移。 3.如果right容器負邊距等於自身的寬度它會靠右對齊,如果負邊距等於-100%,則會靠左對齊。 第二種透過浮動佈局來實現左邊固定,右邊自適應的佈局主要的程式碼如下:
<style type="text/css"> .left{ float: left; width: 200px; height: 200px; background-color: yellow; } .right{ padding-left: 200px; height: 200px; background-color: red; } @media (min-width: 650px) and (max-width: 1000px){ .left{ width: 150px; } .right{ margin-left: 150px; } } @media (max-width: 640px){ .left{ width: 100px; } .right{ margin-left: 100px; } } </style> <body> <p id="main"> <p class="left">左边固定宽度,右边自适应</p> <p class="right"></p> </p> </body>實現過程中需要注意的是:
1. left需要脫離文件流,而right只需要正常顯示就可以。
2.left只是覆蓋在right上邊,因此想要讓right內容完整顯示需要給right padding-left或margin-left。
大螢幕展示:
<style type="text/css"> #head{ height: 200px; background-color: yellow; } #body{ width: 100%; float: left; } .main{ background-color: green; min-height: 200px; margin: 0 210px; } .left{ float: left; background-color: red; width: 200px; height: 200px; margin-left: -100%; } .right{ float: right; background-color: blue; width: 200px; height: 200px; margin-left: -200px; } #footer{ clear: both; height: 200px; background-color: orange; } </style> <body> <p id="head">即左右固定,中间自适应,它可以利用margin-left为负数来实现,它的实现原理就是margin为负值可以改变float元素的排列位置</p> <p id="body"> <p class="main">当多个元素同时从标准流中脱离开来时,如果前一个元素的宽度为100%宽度,后面的元素通过负边距可以实现上移。当负的边距超过自身的宽度将上移,只要没有超过自身宽度就不会上移</p> </p> <p class="left"></p> <p class="right"></p> <p id="footer"></p> </body>實作過程中需要注意:#1.中間自適應的p需要放在left和right容器前面並且內容p需要用父容器包裹2.left和right容器向同一個方向浮動。 主要程式碼如下:
<style type="text/css"> #head{ height: 200px; background-color: yellow; } #body{ overflow: hidden; } .left{ float: left; background-color: red; width: 200px; height: 200px; } .right{ float: right; background-color: blue; width: 200px; height: 200px; } .main{ background-color: green; height: 200px; margin: 0 210px; } #footer{ clear: both; height: 200px; background-color: orange; } </style> <body> <p id="head">左右固定宽度并且向两边浮动,中间的p设置两边的margin</p> <p id="body"> <p class="left"></p> <p class="right"></p> <p class="main">该方案有一个缺陷,在小屏幕情况下回导致right被挤下去,main没有了</p> </p> <p id="footer"></p> </body>實作過程中需要注意:1.該方式只需要注意中間自適應的p需要放在left和right容器的後面。 2.left和right容器向兩邊浮動。 主要程式碼如下:
<!DOCTYPE html> <html> <meta charset="utf-8"> <head> <title>使用flex 实现“双飞翼布局”</title> </head> <style type="text/css"> #main{ display: flex; display: -webkit-flex;//谷歌浏览器加前缀 flex-flow: row nowrap; justify-content: flex-start; align-items: center; } .left{ flex: 0 0 auto; width:100px; height: 200px; background-color: red; word-wrap: break-word; overflow: hidden; } .main{ flex: 1 1 auto; height: 200px; background-color: green; } .right{ flex: 0 0 auto; width: 100px; height: 200px; background-color: yellow; } </style> <body> <p id="main"> <p class="left">flex 语法我参照了阮一峰关于flex语法介绍 http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html</p> <p class="main"></p> <p class="right"></p> </p> </body> </html>如果未了解flex佈局請移至文末點擊連結查看阮一峰大神寫的關於flex語法
3、定位佈局
這邊就不絮絮叨叨的講一些基礎的css定位知識了(ps:不會的請自行到w3c官網查閱),我主要來講解工作中遇到的坑。以免其他人跟我一樣掉進坑裡。 第一:使用多個fixed時,注意自己需要基於什麼定位,因為如果父級有用transform屬性時,可能會導致子元素的fixed基於父元素容器定位,而不是基於body定位。效果如下:<!DOCTYPE html> <html> <head> <title>关于position的定位的坑</title> </head> <style type="text/css"> body{ margin: 0; padding: 0; } i{ font-style: normal; cursor: pointer; } #delete-button{ position: absolute; left: 45%; top: 45%; text-align: center; vertical-align: middle; height: 50px; margin: auto; cursor: pointer; } #delete-button > i{ display: inline-block; width: 32px; height: 32px; border-radius: 16px; background-color: orange; color: red; font-size: 32px; vertical-align: middle; line-height: 28px; } /*第一个模态框的样式*/ #layout{ display: none; width: 100%; height: 100%; } /*使用flex布局水平竖直居中*/ /*#layout-box{ position: fixed; width: 100%; height: 100%; left: 0; top: 0; display: flex; display: -webkit-flex; flex-flow: column nowrap; justify-content: center; align-items: center; background-color: rgba(0,0,0,0.3); }*/ /*使用postion 和 transform 水平垂直居中*/ #layout-box{ position: fixed; width: 100%; height: 100%; background-color: rgba(0,0,0,0.3); } .modal-dialog{ position: absolute; left: 50%; top: 50%; width: 500px; height: 200px; border-radius: 10px; transform: translate(-50%, -50%); -webkit-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); -o-transform: translate(-50%, -50%); background-color: #fff; } .dialog-title{ text-align: center; color: #333; font-size: 28px; margin-bottom: 10px; } .dialog-content{ text-align: center; color: #666; font-size: 18px; } .dialog-button{ margin-top: 20px; width: 100%; color: #333; } .dialog-button >.button-box{ display: inline-block; width: 48%; text-align: center; } .button-box span{ display: inline-block; padding: 10px; color: #fff; border-radius: 6px; cursor: pointer; } #confirm{ background-color: #27ad9a; } #cancel{ background-color: red; } /*添加按钮的样式*/ #add-button > i{ display: inline-block; width: 32px; height: 32px; border-radius: 16px; background-color: #27ad9a; color: #fff; font-size: 32px; vertical-align: middle; line-height: 28px; text-align: center; } #add-button{ display: inline-block; cursor: pointer; } /*第二个模态框的样式*/ .layout2{ display: none; position: fixed; width: 100%; height: 100%; left: 0; top: 0; background-color: rgba(0,0,0,0.2); } .modal-dialog2{ position: fixed; left: 50%; top: 50%; width: 50%; height: 50%; border-radius: 10px; transform: translate(-50%, -50%); -webkit-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); -o-transform: translate(-50%, -50%); background-color: rgba(0,0,0,0.2); } .modal-dialog2 > span{ display: block; } .modal-text{ float: left; } #close{ color: red; font-size: 24px; float: right; cursor: pointer; } </style> <body> <p id="delete-button"><i>-</i>删除</p> <p id="layout"> <p id="layout-box"> <p class="modal-dialog"> <p class="dialog-title">提示</p> <p class="dialog-content">是否删除该项,点击确定</p> <p class="dialog-button"> <p class="button-box"> <span id="confirm">确定</span> </p> <p class="button-box"> <span id="cancel">取消</span> </p> </p> <p id="add-button"><i>+</i>添加</p> <p class="layout2"> <p class="modal-dialog2"> <span class="modal-text">你是我的小可爱</span> <span id="close">关闭</span> </p> </p> </p> </p> </p> </body> <script type="text/javascript"> document.getElementById("delete-button").onclick= function(){ var layout = document.getElementById("layout") layout.style.display = "block" } document.getElementById("confirm").onclick=function(){ var layout = document.getElementById("layout") layout.style.display = "none" } document.getElementById("cancel").onclick=function(){ var layout = document.getElementById("layout") layout.style.display = "none" } document.getElementById("add-button").onclick=function(){ var layout = document.getElementsByClassName("layout2") layout[0].style.display = "block" } document.getElementById("close").onclick=function(){ var layout = document.getElementsByClassName("layout2") layout[0].style.display = "none" } </script> </html>如果我們試著把父容器上的transform屬性移除,我們可以看到子容器沒有基於父容器定位,而是基於body定位的,寬度也是基於body給的50%寬度。效果圖如下:
#
<!DOCTYPE html> <html> <head> <title>关于position的定位的坑</title> </head> <style type="text/css"> body{ margin: 0; padding: 0; } i{ font-style: normal; cursor: pointer; } #delete-button{ position: absolute; left: 45%; top: 45%; text-align: center; vertical-align: middle; height: 50px; margin: auto; cursor: pointer; } #delete-button > i{ display: inline-block; width: 32px; height: 32px; border-radius: 16px; background-color: orange; color: red; font-size: 32px; vertical-align: middle; line-height: 28px; } /*第一个模态框的样式*/ #layout{ display: none; width: 100%; height: 100%; } /*使用flex布局水平竖直居中*/ #layout-box{ position: fixed; width: 100%; height: 100%; left: 0; top: 0; display: flex; display: -webkit-flex; flex-flow: column nowrap; justify-content: center; align-items: center; background-color: rgba(0,0,0,0.3); } /*使用postion 和 transform 水平垂直居中*/ .modal-dialog{ width: 500px; height: 200px; border-radius: 10px; background-color: #fff; } .dialog-title{ text-align: center; color: #333; font-size: 28px; margin-bottom: 10px; } .dialog-content{ text-align: center; color: #666; font-size: 18px; } .dialog-button{ margin-top: 20px; width: 100%; color: #333; } .dialog-button >.button-box{ display: inline-block; width: 48%; text-align: center; } .button-box span{ display: inline-block; padding: 10px; color: #fff; border-radius: 6px; cursor: pointer; } #confirm{ background-color: #27ad9a; } #cancel{ background-color: red; } /*添加按钮的样式*/ #add-button > i{ display: inline-block; width: 32px; height: 32px; border-radius: 16px; background-color: #27ad9a; color: #fff; font-size: 32px; vertical-align: middle; line-height: 28px; text-align: center; } #add-button{ display: inline-block; cursor: pointer; } /*第二个模态框的样式*/ .layout2{ display: none; position: fixed; width: 100%; height: 100%; left: 0; top: 0; background-color: rgba(0,0,0,0.2); } .modal-dialog2{ position: fixed; left: 50%; top: 50%; width: 50%; height: 50%; border-radius: 10px; transform: translate(-50%, -50%); -webkit-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); -o-transform: translate(-50%, -50%); background-color: rgba(0,0,0,0.2); } .modal-dialog2 > span{ display: block; } .modal-text{ float: left; } #close{ color: red; font-size: 24px; float: right; cursor: pointer; } </style> <body> <p id="delete-button"><i>-</i>删除</p> <p id="layout"> <p id="layout-box"> <p class="modal-dialog"> <p class="dialog-title">提示</p> <p class="dialog-content">是否删除该项,点击确定</p> <p class="dialog-button"> <p class="button-box"> <span id="confirm">确定</span> </p> <p class="button-box"> <span id="cancel">取消</span> </p> </p> <p id="add-button"><i>+</i>添加</p> <p class="layout2"> <p class="modal-dialog2"> <span class="modal-text">你是我的小可爱</span> <span id="close">关闭</span> </p> </p> </p> </p> </p> </body> <script type="text/javascript"> document.getElementById("delete-button").onclick= function(){ var layout = document.getElementById("layout") layout.style.display = "block" } document.getElementById("confirm").onclick=function(){ var layout = document.getElementById("layout") layout.style.display = "none" } document.getElementById("cancel").onclick=function(){ var layout = document.getElementById("layout") layout.style.display = "none" } document.getElementById("add-button").onclick=function(){ var layout = document.getElementsByClassName("layout2") layout[0].style.display = "block" } document.getElementById("close").onclick=function(){ var layout = document.getElementsByClassName("layout2") layout[0].style.display = "none" } </script> </html>第二:解決在手機上的抖動問題(ps:這個問題我參考了網上大神寫的一篇博客請移至文末查看)**一、**在webkit內核瀏覽器中給fixed加上防抖樣式- webkit - transform : translateZ(0);**二、**設定html 和body 的css {height:100%;overflow:auto;margin:0;} 這個影響全域樣式不建議使用。 三、在fiexd內設定position:absolute,如下:
#
<p style="position:fiexd;bottom:0px;"> <p style="position:absolute;"> </p> </p>
@media screen and (max-width:600px){ 写入当屏幕小于或等于600px时的样式 } @media screen and (min-width:900px){ 写入当屏幕大于或等于900px时的样式 } @media screen and (min-width:600px) and (max-width:900px){ 写入当屏幕在600px-900px之间的样式 } @media screen and (max-device-width: 480px){ 写入最大设备宽度为480px,比如说iPhone上的显示,这里的max-device-width所指的是设备的实际分辨率,也就是指可视面积分辨率 } @media only screen and (-webkit-min-device-pixel-ratio: 2){ 写入专门针对iPhone4的移动设备样式 } @media all and (orientation:portrait){ 写入设备在纵向时的样式 } @media all and (orientation:landscape){ 写入设备在横向时的样式 } @media not print and (max-width: 1200px){ not是用来排除某种制定的媒体类型 写入在除打印设备和设备宽度小于1200px下的所有设备的样式 } @media only screen and (max-device-width:240px){ only用来定某种特定的媒体类型,可以用来排除不支持媒体查询的浏览器。 写入只能在最大设备宽度为240px的屏幕下使用的样式 }
相关推荐:
以上是css網頁的幾種版面實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

在這篇文章中,布萊克·莫里(Blackle Mori)向您展示了一些駭客,同時試圖推動同位HTML支持的極限。如果您敢於使用這些,以免您也被標記為CSS罪犯。

具有CSS的自定義光標很棒,但是我們可以將JavaScript提升到一個新的水平。使用JavaScript,我們可以在光標狀態之間過渡,將動態文本放置在光標中,應用複雜的動畫並應用過濾器。

互動CSS動畫和元素相互啟動的元素在2025年似乎更合理。雖然不需要在CSS中實施乒乓球,但CSS的靈活性和力量的增加,可以懷疑Lee&Aver Lee有一天會成為一種

有關利用CSS背景濾波器屬性來樣式用戶界面的提示和技巧。您將學習如何在多個元素之間進行背景過濾器,並將它們與其他CSS圖形效果集成在一起以創建精心設計的設計。

好吧,事實證明,SVG的內置動畫功能從未按計劃進行棄用。當然,CSS和JavaScript具有承載負載的能力,但是很高興知道Smil並沒有像以前那樣死在水中

是的,讓#039;跳上文字包裝:Safari Technology Preview In Pretty Landing!但是請注意,它與在鉻瀏覽器中的工作方式不同。

此CSS-tricks更新了,重點介紹了年鑑,最近的播客出現,新的CSS計數器指南以及增加了幾位新作者,這些新作者貢獻了有價值的內容。

在大多數情況下,人們展示了@Apply的@Apply功能,其中包括Tailwind的單個property實用程序之一(會改變單個CSS聲明)。當以這種方式展示時,@Apply聽起來似乎很有希望。如此明顯


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SublimeText3漢化版
中文版,非常好用