gpt4 book ai didi

html - css居中动态div

转载 作者:太空宇宙 更新时间:2023-11-04 12:13:28 25 4
gpt4 key购买 nike

我有一个 div #content,我在运行时添加左浮动 div #block 并且在 div (a) 、#lcontent 和 #rcontent 的两侧都有固定宽度的 div当侧 div 具有动态高度并且#content 随时间变化时,我如何让#content 保持在中心位置

无论里面有 2、3、4 还是 100 #block,内容都应该保持在中间。但是#block 应该总是在#content 的左边

只有#lcontent、#rcontent 和#block 是固定宽度的 What i want to show up on web

html代码

<div id="contentcontainer">
<div id="lcontent"></div>
<div id="mcontent">
<div id="content">
<div id="block"></div>
<div id="block"></div>
<div id="block"></div>
<div id="Block"></div>
<div id="Block"></div>
</div>
</div>
<div id="rcontent"></div>
</div>

CSS代码

#lcontent
{
float: left;
width: 100px;
min-width: 100px;
min-height: 100px;
background: lightblue;
}
#rcontent
{
float: right;
width: 100px;
background: lightblue;
}
#mcontent
{
background: lightgrey;
float: right;
right: 50%;
position: relative;
}
#content
{
float: right;
right: -50%;
position: relative;
background: green;
min-height: 200px;
text-align: center;
}
#block
{
margin: 5px;
width: 300px;
border: 1px solid red;
display: inline-block;½
height: 150px;
}

最佳答案

使用纯 css 会非常困难,需要您了解容器将拥有的盒子数量(并根据它向父级添加一个类) - 如果这是动态内容,那么您应该为此使用服务器端语言。您还需要大量的媒体查询:

.container {
text-align:center;
}
.centered {
text-align:left;
margin:auto;
}
.block {
margin:3px;
width:100px;
height:100px;
float:left;
vertical-align:top;
border:1px solid red;
}

.seven {max-width:770px;} /* this is the number of block * their outer width (margin + border + padding + width) */

/*one block*/
@media (max-width:219px) { /* this is the width of 2 blocks outer width -1px (you may need to add the width of the left and right column to this plus any left and right padding of the center column. This is really the max width of the container div */
.centered {
width:110px; /* the outer width of a block */
}
}

/*two blocks*/
/* keep adding media queries to either your max number of blocks or the largest screen size you want to support */
/*min width is 2 * 1 block outer width, max width is 3 * 1 block outer width - 1px*/
@media (min-width:220px) and (max-width:329px) {
.centered {
width:220px; /* the outer width of 2 blocks */
}
}
@media (min-width:330px) and (max-width:439px) {
.centered {
width:330px;
}
}
@media (min-width:440px) and (max-width:549px) {
.centered {
width:440px;
}
}
@media (min-width:550px) and (max-width:659px) {
.centered {
width:550px;
}
}
@media (min-width:660px) and (max-width:769px) {
.centered {
width:660px;
}
}
@media (min-width:770px) and (max-width:879px) {
.centered {
width:770px;
}
}
@media (min-width:880px) and (max-width:989px) {
.centered {
width:880px;
}
}
@media (min-width:990px) and (max-width:1100px) {
.centered {
width:990px;
}
}
<div class="container">
<div class="centered seven"><!--the seven class is the number of children blocks-->
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
</div>
</div>

Fiddle Example

Otherwise you can try a js solution

关于html - css居中动态div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28876653/

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