bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

css實現div水平和垂直居中

這篇文章主要介紹“css實現div水平和垂直居中”,在日常操作中,相信很多人在css實現div水平和垂直居中問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”css實現div水平和垂直居中”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

鹽山網站制作公司哪家好,找創新互聯公司!從網頁設計、網站建設、微信開發、APP開發、響應式網站開發等網站項目制作,到程序開發,運營維護。創新互聯公司從2013年成立到現在10年的時間,我們擁有了豐富的建站經驗和運維經驗,來保證我們的工作的順利進行。專注于網站建設就選創新互聯公司

示例1 chrome33、ie8測試通過:

代碼如下:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
/* 固定寬高div,在瀏覽器中保持水平、垂直居中 */
#div1 {
width:400px; height:300px;
position:absolute;
left:50%; top:50%;
margin-left:-200px; margin-top:-150px;
background:#f90;
}
</style>
</head>
<body>
<div id="div1"></div>
</body>
</html>


示例2 chrome33、ie8測試通過:

代碼如下:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
/* 固定寬高div在固定寬高div中保持水平、垂直居中 */
#div1 {
width: 400px;height: 200px;
background-color: #f00;
position: relative;
/* 為div賦予單元格屬性,使其可以應用align屬性 */
display: table-cell;
vertical-align: middle;
}
#div2 {
width: 200px;height: 100px;
background-color: #0f0;
/* 使當前標簽在父標簽中水平居中,即0 auto 0 auto */
margin: 0 auto;
display: block;
}
</style>
<body>
<div id="div1">
<div id="div2"></div>
</div>
</body>
</html>


示例3 chrome33、ie8測試通過:

代碼如下:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
#div1 {
width: 400px;height: 200px;
background-color: #f00;
/* 使當前標簽在父標簽位置固定 */
position: absolute;
top: 50%;left: 50%;
margin: -100px 0 0 -200px;
}
#div2 {
width: 200px;height: 100px;
background-color: #0f0;
display: block;
/* 使當前標簽在父標簽中保持水平、垂直居中 */
top: 50%;left: 50%;
margin: 50px 0 0 100px;
}
</style>
</head>
<body>
<div id="div1">
<div id="div2"></div>
</div>
</body>
</html>


示例4 chrome33測試通過,ie8測試不通過,參考http://www.w3school.com.cn/tiy/t.asp?f=css3_box-pack:

代碼如下:


<!DOCTYPE html>
<html>
<head>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* 使用box-pack樣式實現div中子元素居中 */
#div1 {
width:350px;
height:200px;
border:1px solid black;
/* Firefox */
display:-moz-box;
-moz-box-pack:center;
-moz-box-align:center;
/* Safari, Chrome, and Opera */
display:-webkit-box;
-webkit-box-pack:center;
-webkit-box-align:center;
/* W3C */
display:box;
box-pack:center;
box-align:center;
}
#div2 {
width:100px;
height:50px;
background-color: #ff0;
border:1px solid black;
}
</style>
</head>
<body>
<div id="div1">
<div id="div2"></div>
</div>
</body>
</html>


注:對頁面中所有元素應用box-sizing:border-box樣式是為了將padding、margin值都計入width、height中,即為元素指定的任何內邊距和邊框都將在已設定的寬度和高度內進行繪制(http://www.w3school.com.cn/cssref/pr_box-sizing.asp)。在上述代碼中應用該樣式無意義,但是在企業級應用中應用該樣式可以減少很多界面缺陷,如標簽未對齊。

到此,關于“css實現div水平和垂直居中”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注創新互聯網站,小編會繼續努力為大家帶來更多實用的文章!

文章名稱:css實現div水平和垂直居中
分享路徑:http://vcdvsql.cn/article40/pepcho.html

成都網站建設公司_創新互聯,為您提供網站導航網站營銷品牌網站設計關鍵詞優化自適應網站服務器托管

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

外貿網站建設