gpt4 book ai didi

javascript - 将
置于无侧边栏区域的中心

转载 作者:行者123 更新时间:2023-11-28 06:59:55 26 4
gpt4 key购买 nike

我需要将其居中 #wrapper <div>在页面上的无侧边栏区域。通过这些选项,它以整体 <body> 为中心。并位于侧边栏后面。

#wrapper /* Centers #posts <div>, but at the same time it makes #posts <div> go behind the sidebar, which I don't want to. */ {
width: 800px;
}
#posts {
width: 800px;
float: left; /* Is needed for the Masonry script. */
}
.entry /* Sets options for each post in two columns in #posts <div>. */ {
width: 400px; /* Musthave option too. */
}
#sidebar {
position: fixed;
top: 0px;
right: 0px;
width: 250px;
height: 100%;
display: table; /* Is needed for centering stuff inside of the sidebar. */
}
<body>
<div id='wrapper'> <!-- This one is supposed to be centered in the sidebar-free area on the page. -->
<div id='posts'>
<!-- Here are all of the posts. -->
<div class='entry'>
</div>
</div>
</div>
<div id='sidebar'>
</div>
</body>

现在的样子:

enter image description here

它看起来像什么:

enter image description here

最佳答案

您可以向正文添加一些内边距以补偿侧边栏宽度。如果添加与侧边栏宽度相同数量的填充,则整个主体将被推向左侧。请查看代码片段。

您还可以使用flex-box如果你不需要支持就可以实现这一点

/* New =================================================================== */

body {
/* Push content of the body to the left. Use same amount as the sidebar width */
padding-right: 250px;
/* Remove default margin */
margin: 0;
}

#wrapper {
/* Centers the wrapper */
margin: 0 auto;
}

/* Old =================================================================== */

#wrapper /* Centers #posts <div>, but at the same time it makes #posts <div> go behind the sidebar, which I don't want to. */ {
width: 800px;
}
#posts {
width: 800px;
float: left; /* Is needed for the Masonry script. */
}
.entry /* Sets options for each post in two columns in #posts <div>. */ {
width: 400px; /* Musthave option too. */
}
#sidebar {
position: fixed;
top: 0px;
right: 0px;
width: 250px;
height: 100%;
display: table; /* Is needed for centering stuff inside of the sidebar. */
}

/* Demo ============================================================= */

#wrapper {
/* So you can see it in the demo */
height: 80px;
background: red;
}

#sidebar {
/* So you can see it in the demo */
height: 80px;
background: blue;
}
<body>
<div id='wrapper'> <!-- This one is supposed to be centered in the sidebar-free area on the page. -->
<div id='posts'>
<!-- Here are all of the posts. -->
<div class='entry'>
</div>
</div>
</div>
<div id='sidebar'>
</div>
</body>

关于javascript - 将 <div> 置于无侧边栏区域的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32259084/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com