『壹』 DIV+CSS body頁面居中代碼
html { text-align:center }
body { width:960px; margin:0 auto; text-align:left }
使用方法:
不需要加任何多餘容器,放置到CSS文件的第一行即可。可以兼容IE5.5以上的IE 及 其他瀏覽器。
代碼解釋:
width:960px; 這是目前主流網頁的寬度,當然你也可以改成你自己感覺合適的,但是此寬度必須有。 text-align:left; 是為了還原html 中居中的文字。
該代碼為最簡單實用的代碼了,用了包你成功! 呵呵~ 其他都是浮雲~
『貳』 問一個簡單的DIV+CSS代碼要讓一塊DIV在頁面中居中怎麼寫
margin:0 auto;padding:0; 給body加這個屬性就行
『叄』 html中如何讓一整個網頁居中
<html>
抄<head>
<metacharset="UTF-8">
<title>我的第一個HTML頁面</title>
</head>
<body>
<divstyle="display:block;margin:0auto;width:50%;background:#ccc;">
<p>body元素的內容會顯示在瀏覽器中。</p>
<p>title元素的內容會顯示在瀏覽器的標題欄中。</p>
<p>居中顯示</p>
</div>
</body>
</html>
先給所有的元素外面 加個div 然後給這個div定義 寬度,左右margin為auto,就行了
『肆』 如何設置網頁div塊居中
讓1和2居中不好控制,讓他的父級居中,1和2占滿並排就可以了。
<div
style="width:404px;margin:0px
auto">
<div
style="float:left;width:200px;border:1px
solid
#f60">1111111</div>
<div
style="float:left;width:200px;border:1px
solid
#ff0">2222222</div>
</div>
注意:1和2的寬度(width+border寬度=父級的寬度)
如果不符合你的要求,就另外這樣,設置1和2的央style="float:left;
margin-left:50px"和style="float:right;margin-right:50px">
具體語法可網上更近一步學習。
『伍』 網頁居中代碼
<palign=center>
<scripttype="text/javascript">
document.write(unescape("%3Cspanid='_ideConac'%3E%3C/span%3E%3Cscriptsrc='
http://dcs.conac.cn/js/01/000/0000/40434131/CA010000000404341310001.js'type='text/javascript'%3E%3C/script%3E"));
</script>
</p>
『陸』 css樣式 div居中 怎樣寫
水平居中可以使用margin:0px auto;實現(盒子需要定義寬度);
垂直居中有一下四種方式:
方法1:
.parent {
width:800px;
height:500px;
border:2px solid #000;
position:relative;
}
.child {
width:200px;
height:200px;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
background-color: red;
}
方法2:
.parent {
width:800px;
height:500px;
border:2px solid #000;
display:table-cell;
vertical-align:middle;
text-align: center;
}
.child {
width:200px;
height:200px;
display:inline-block;
background-color: red;
}
方法3:
.parent {
width:800px;
height:500px;
border:2px solid #000;
display:flex;
justify-content:center;
align-items:center;
}
.child {
width:200px;
height:200px;
background-color: red;
}
方法4:
.parent {
width:800px;
height:500px;
border:2px solid #000;
position:relative;
}
.child {
width:300px;
height:200px;
margin:auto;
position:absolute;/*設定水平和垂直偏移父元素的50%,再根據實際長度將子元素上左挪回一半大小*/
left:50%;
top:50%;
margin-left: -150px;
margin-top:-100px;
background-color: red;
}
『柒』 怎麼寫CSS樣式讓DIV在網頁中居中顯示
一般來說只可以讓他左右居中,如果來上下居中就比較麻煩了。左右居中需要先定義寬度,然後讓外邊距(主要是左右外邊距)自動。width:100px;margin:auto;
『捌』 CSS怎樣讓一個div居中
主要css代碼有兩個:
1,text-align:center
2,margin:0
auto;
其兩個樣式需要配合使用才能實現div盒子的居中顯示排版。
首先我們對body設置text-align:center,再對需要居中的div盒子設置css樣式margin:0
auto,這樣即可讓對應div水平居中。
舉個例子,居中代碼為:
<!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=gb2312"
/>
<title>CSS使div居中製作</title>
<style>
body{
text-align:center}
#nav{
margin:0
auto;
width:200px;
height:150px;
border:5px
double
#FF0066;}
/*
css注釋:為了觀察效果設置寬度
高度
邊框等樣式
*/
</style>
</head>
<body>
<div
id="nav">
DIV居中
</div>
</body>
</html>
為了觀察div居中效果,我們對div設置一個div命名為「#div」在html中div標簽內使用id=「nav」,設置其寬度為200px;高度為150px,邊框也設置了顏色。
效果如圖:
以上就是CSS讓DIV居中的方法。
『玖』 div居中代碼怎麼寫
如果只是水平居中的話,只需要先定義DIV的寬度,再設置外邊距margin屬性的左右邊距為auto即可。此時DIV便會相對於它的父容器水平居中。
比如 <div class=div_a><div class="div_b"><div></div>
設置div_b的樣式
.div_b{width:100px; margin:0 auto;}
這樣div_b便在div_a中水平居中了。如果外面沒有父DIV,則div_b便相對於整個頁面居中。注意如果div_b定義了浮動屬性float則無效。
『拾』 網頁如何居中顯示,div樣式該怎麼寫
代碼寫法如下
<div style="text-align:center"></div> 即可