当窗口增加或减小宽度时,将窗口的div调整为相对于窗口的顶部中心

时间:2017-01-23 08:27:20

标签: javascript html css

我在修复CSS时面临问题,我有一个div,我需要在window width增加或减少时调整div,div必须在top center处。页。我试过像

<div style="display:table;margin:0 auto"></div> 

但是当窗口宽度增加时,这不适合居中。

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

您需要设置CSS,如下所示,

div {
width:70%; // set width in '%' to adjust with respect to window.
margin:0 auto; // to centerlize div.
}

答案 1 :(得分:0)

这是你需要的吗?

 <div style="text-align:center;">
     <div style="display:inline-block;margin:0 auto;background:red;">mydiv</div>
    </div>