【问题标题】:css centering dynamic divcss居中动态div
【发布时间】:2015-05-06 18:02:18
【问题描述】:

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

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

只有#lcontent、#rcontent 和#block 具有固定宽度

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;   
}

【问题讨论】:

标签: html css dynamic center


【解决方案1】:

使用纯 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

【讨论】:

  • 如果我这样做会发生以下情况。第一行有3个#block居中,第二行有2个#block也居中。并且第 2 行的块应该位于第 1 行的前 2 个块的下方。此外,当父级包含的块多于一行时,整个父级(#content)被推到 #rcontent 和 #lcontent 下方
  • 我不喜欢@media 解决方案。但我确实喜欢小提琴解决方案。我实际上已经尝试过实施它,但首先当页面加载时我的内容是空的。它是通过 DOM 填充的,因此在加载时我无法获取变量。然后我尝试在每次将对象添加到 CONTENT 时设置变量,但我只是得到 width = 0 和 newWidth = NaN
  • 它是否被添加到具有 display:none; 的 div 中?或可见性:隐藏?如果是这样,宽度将始终为 0
  • 不,不幸的是,我只是尝试了以下代码,它应该与小提琴示例相同,但它不起作用。也许有人可以告诉我我做错了什么。我在 heboit.dk/test/test.html 上有代码,您可以看到这些块在容器中居中
  • 谢谢。它有助于将小提琴 js 代码移动到 html 文件的底部。它有效。
【解决方案2】:

您可以将不可见的占位符添加到内联块的末尾。这将使最后一行左对齐。

http://jsfiddle.net/aakt65x4/

但是,如果您不填写第一行,则整个内容将显示为左对齐。但我想这就是你想要的,对吧?

HTML:

<!--
  Centers a group of boxes that wrap to the width of its container.
  Also left-aligns them inside the container.
  Issue: Does not center group if there aren't enough boxes to fill
  the first row.
  -->
<div class="container">
    <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 class="block"></div>
    <div class="block"></div>
    <div class="block"></div>

    <!--
      How many placeholders do you need?
      At least the number of blocks minus two.
      -->
    <div class="placeholder"></div>
    <div class="placeholder"></div>
    <div class="placeholder"></div>
    <div class="placeholder"></div>
    <div class="placeholder"></div>
    <div class="placeholder"></div>
    <div class="placeholder"></div>
    <div class="placeholder"></div>
</div>

CSS:

body {
    text-align: center;     /* center a max-width container */
    font-size: 0;           /* remove spaces between blocks */
}
.container {                /* you don't need this */
    background-color: #eee; /* so you can see what's happening */
    max-width: 960px;       /* to demonstrate the centering of a max-width container */
    display: inline-block;  /* center the max-width container */
    text-align: center;     /* center the group of blocks */
}
.block {
    display: inline-block;  /* left-align within the group */
    background-color: red;  /* so you can see what's happening */
    height: 100px;
    width: 100px;
    margin: 10px;
}
.placeholder {
    display: inline-block;  /* add to the line of blocks */
    width: 120px;           /* width + margin of a block */
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-08
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    • 2011-12-14
    • 1970-01-01
    相关资源
    最近更新 更多