如何考虑用户不同分辨率使两个div居中

发布于 2024-12-01 18:11:58 字数 1442 浏览 0 评论 0原文

我是 css 新手,我在将两个 div 居中时遇到困难。我的屏幕分辨率为 1920 x 1080,如果我网站的访问者分辨率较低,则使用边距不会使两个 div 居中。我知道它会移动,对吗?

详细信息:

请在这里看看我的问题---> http://i1204.photobucket.com/albums/bb409/bendaggers/help.png

  • Div id = Topwrapper
  • Div id = MainWrapper:保存容器和侧边栏(灰色)
  • Div id = Container (黄绿色)
  • Div id = Sidebar (蓝色)

我对 Topwrapper 没有任何问题,正如你所看到的,它位于中心位置,但我最大的问题是容器和侧边栏。我无法将它与 Topwrapper 对齐。你能帮我编码吗?另一件事,你能考虑一下屏幕分辨率吗,据我所知,我的Mainwrapper代码会调整它的宽度,因为min-width=1000px;

如果您认为我的代码有更好的想法,请随时修改它。

    <div id="TopWrapper"></div>
    <div id="MainWrapper">

        <div id="Content"></div>
        <div id="Sidebar"></div>

    </div>

#MainWrapper {
    height:3000px;
    min-width:1000px;
    background-color:#CCC;

}

#TopWrapper {
    background-image:url(images/topwrapper.png);
    background-repeat:no-repeat;
    background-position: center bottom;
    min-width:100%;
    height:77px;
    margin: 0;
}

#Content {
    height:3000px;
    min-width:630px;
    background-color: #0F0;
    float:left;
    margin-left:150px;
    display:inline;


}

#Sidebar {
    height:3000px;
    min-width:350px;
    background-color: #00F;
    margin-left:20px;
    float:left;
    display:inline;
}

提前非常感谢您!

Im a newbie in css and Im having trouble centering two divs. I have a 1920 x 1080 screen resolution and using margin will not center the two divs if the visitor of my website have lower resolution. I know it will moved, right?

Details:

Please take a look at my problem here ---> http://i1204.photobucket.com/albums/bb409/bendaggers/help.png

  • Div id = Topwrapper
  • Div id = MainWrapper: holds the container and sidebar (gray)
  • Div id = Container (yellow green)
  • Div id = Sidebar (blue)

I have no problem with the Topwrapper, as you can see, it is in the center position but my very big problem is the Container and Sidebar. I cant align it with the Topwrapper. can you help me code? Another thing, can you please consider the screen resolution, as far as i know, my Mainwrapper code will adjust its width because of the min-width=1000px;

If you think you have a better idea with my codes, please feel free to revise it.

    <div id="TopWrapper"></div>
    <div id="MainWrapper">

        <div id="Content"></div>
        <div id="Sidebar"></div>

    </div>

#MainWrapper {
    height:3000px;
    min-width:1000px;
    background-color:#CCC;

}

#TopWrapper {
    background-image:url(images/topwrapper.png);
    background-repeat:no-repeat;
    background-position: center bottom;
    min-width:100%;
    height:77px;
    margin: 0;
}

#Content {
    height:3000px;
    min-width:630px;
    background-color: #0F0;
    float:left;
    margin-left:150px;
    display:inline;


}

#Sidebar {
    height:3000px;
    min-width:350px;
    background-color: #00F;
    margin-left:20px;
    float:left;
    display:inline;
}

Thank you very much in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

完美的未来在梦里 2024-12-08 18:11:58

您可以设置现有 div“#MainWrapper”的宽度,以补充两个 div 中已有的内容,确保它们之间有空间,并为两侧的边距留出空间(可能比两个 div 的总和宽 30px)两个 div),然后给出“margin-left:auto;”和“右边距:自动;”到 div #MainWrapper 以便它居中。然后,您需要设置“body”标签的样式,为其提供您想要的颜色。

You can style the existing div "#MainWrapper" with a width that complements the two divs with content already in them, making sure there is space between both of them and leave room for margins on both sides (maybe 30px wider than the sum of the two divs), and then give "margin-left:auto;" and "margin-right:auto;" to the div #MainWrapper so it centers itself. You would then need to style the "body" tag to give it the color you want.

遥远的绿洲 2024-12-08 18:11:58

试试这个:

#MainWrapper {
    height:3000px;
    width:1000px;
    background-color:#CCC;
    margin:0 auto;

}

#TopWrapper {
    background-color:#000000;
    min-width:100%;
    height:77px;
    margin: 0;
}

#Content {
    height:3000px;
    width:630px;
    background-color: #0F0;
    float:left;


}

#Sidebar {
    height:3000px;
    width:350px;
    background-color: #00F;
    margin-left:20px;
    float:right;
}

如果这不是您要找的,请告诉我。我很高兴与您合作。

哦,如果你想要背景是某种颜色,只需添加:

    body {
    background-color:#CCC; /* or whatever colour you like */
}

Try this:

#MainWrapper {
    height:3000px;
    width:1000px;
    background-color:#CCC;
    margin:0 auto;

}

#TopWrapper {
    background-color:#000000;
    min-width:100%;
    height:77px;
    margin: 0;
}

#Content {
    height:3000px;
    width:630px;
    background-color: #0F0;
    float:left;


}

#Sidebar {
    height:3000px;
    width:350px;
    background-color: #00F;
    margin-left:20px;
    float:right;
}

If this isn't what you're looking for, let me know. I'll be happy to work with you.

Oh, and if you want the background to be a certain colour, just add:

    body {
    background-color:#CCC; /* or whatever colour you like */
}
翻了热茶 2024-12-08 18:11:58

这是另一种解决方案,将您的内容包装到另一个宽度为 1000px 的 div 中,然后设置 margin:0 auto;这意味着它在页面上居中,并且可以适应任何屏幕分辨率

这是具有新样式 ID 内容包装器的 CSS 和 HTML

#MainWrapper {
    height:3000px;
    min-width:1000px;
    background-color:#CCC;
}
#TopWrapper {
    background-image:url(images/topwrapper.png);
    background-repeat:no-repeat;
    background-position: center bottom;
    min-width:100%;
    height:77px;
    margin: 0;
}
#Content {
    height:3000px;
    width:630px;
    background-color: #0F0;
    float:left;
    display:inline;
}
#Sidebar {
    height:3000px;
    width:350px;
    background-color: #00F;
    margin-left:20px;
    float:left;
    display:inline;
}
#content-wrapper {
    margin:0 auto;
    width:1000px;
}

<div id="TopWrapper"></div>
<div id="MainWrapper">
  <div id="content-wrapper">
    <div id="Content"></div>
    <div id="Sidebar"></div>
  </div>
</div>

This is another solution, wrap your content into another div with a width of 1000px, then set margin:0 auto; which means center it on the page and it'll be fluid to any screen resolution

Here is the CSS with new style ID content-wrapper and the HTML

#MainWrapper {
    height:3000px;
    min-width:1000px;
    background-color:#CCC;
}
#TopWrapper {
    background-image:url(images/topwrapper.png);
    background-repeat:no-repeat;
    background-position: center bottom;
    min-width:100%;
    height:77px;
    margin: 0;
}
#Content {
    height:3000px;
    width:630px;
    background-color: #0F0;
    float:left;
    display:inline;
}
#Sidebar {
    height:3000px;
    width:350px;
    background-color: #00F;
    margin-left:20px;
    float:left;
    display:inline;
}
#content-wrapper {
    margin:0 auto;
    width:1000px;
}

<div id="TopWrapper"></div>
<div id="MainWrapper">
  <div id="content-wrapper">
    <div id="Content"></div>
    <div id="Sidebar"></div>
  </div>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文