再问div居中的问题

li_guang_hua 2009-12-04 09:07:38
我在body中放置一个div(id="MyContent"),此div的背景用了一幅背景图片,图片宽度1200px;我的设想是不管用什么浏览器,不任屏幕的宽窄,打开网站,此div的中轴线均位于浏览器中间,不知CSS该如何设置。

这样设置:
#MyContent{
margin:0 auto;
text-align:center;
}
试一下,19寸屏幕可以,可15寸不行啊。
还是请高手指教一下,谢谢!!!

...全文
103 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
cstester 2009-12-04
  • 打赏
  • 举报
回复

<div id="div1" style="float:center"></div>



<div id="div1" style="float:middle"></div>


这两个总有个是对的 。没编辑器。只好这两条都发上来。不敢确认。
爪哇鹅 2009-12-04
  • 打赏
  • 举报
回复

div {
position:absolute;
top:50%;
left:50%;
right:50%;
bottom:50%;
margin:-150px 0 0 -200px;
width:400px;
height:300px;
}

这是一个测试,中间DIV宽高变了之后margin还得调。
CPPACE 2009-12-04
  • 打赏
  • 举报
回复
js可以,50%可能也可以
chenjing1104 2009-12-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hanyise520 的回复:]
body{
text-align:center;


}
不就完了
[/Quote]

应该是这样的吧!
我只相信汗水 2009-12-04
  • 打赏
  • 举报
回复
你想要背景图居中?
css里好象有个background-position:center属性,应该是这样,你试下。
kevin_xu4365 2009-12-04
  • 打赏
  • 举报
回复
body
{
margin:0px;
margin-top:1px;
background:#fff;
font-family: 微软雅黑, Verdana,宋体, Arial, Helvetica, sans-serif;
font-size:11pt;
color:#000;
overflow:auto;
padding:0px;
height:auto;
text-align:center;
}
#main
{
height:800px;
width:960px;
/*text-align:left;
margin:0 auto;*/
position:absolute;
left:50%;
margin-left:-480px;
}
zzxap 2009-12-04
  • 打赏
  • 举报
回复
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var popupHeight = $("#popupContact").height();
var popupWidth = $("#popupContact").width();
//centering
$("#popupContact").css({
"position": "absolute",
"top": windowHeight/2-popupHeight/2,
"left": windowWidth/2-popupWidth/2
});

本文来自CSDN
hanyise520 2009-12-04
  • 打赏
  • 举报
回复
body{
text-align:center;


}
不就完了
jenny0810 2009-12-04
  • 打赏
  • 举报
回复
[Quote=引用楼主 li_guang_hua 的回复:]
我在body中放置一个div(id="MyContent"),此div的背景用了一幅背景图片,图片宽度1200px;我的设想是不管用什么浏览器,不任屏幕的宽窄,打开网站,此div的中轴线均位于浏览器中间,不知CSS该如何设置。

这样设置:
#MyContent{
margin:0 auto;
text-align:center;
width:1000px;
}
试一下,19寸屏幕可以,可15寸不行啊。
还是请高手指教一下,谢谢!!!


加个宽度试试


[/Quote]
zhujiazhi 2009-12-04
  • 打赏
  • 举报
回复
如果CSS搞不定,就用JS来搞的
li_guang_hua 2009-12-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 cstester 的回复:]
HTML code<divid="div1" style="float:center"></div>

HTML code<divid="div1" style="float:middle"></div>

这两个总有个是对的 。没编辑器。只好这两条都发上来。不敢确认。
[/Quote]
float只能选left或right,没有center选项啊

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧