如何将两个div移动/堆叠在一起时居中?

问题描述:

请注意,是的,这里有类似的问题,但我尝试了他们提到的解决方案,但仍然无法让我的物品居中。我卡住了文本对齐:center,display:inline-block;保证金:0自动和宽度:100%在多个地方,我仍然无法让项目显示居中。如何将两个div移动/堆叠在一起时居中?

O.k.有了这样的方式,这里是代码,然后解释如下:

<h4 style="margin-bottom:-15px"><strong>The Item Item Below:</strong></h4> 
    <img style="float:left;width:100%; max-width: 408px; height:100%; max-height: 134px; background-color:red; margin-top:40px; margin-right:20px" 

     src="http://s25.postimg.org/kbrcsousf/408x134.png"> <span style="float:left; margin:0 auto; max-width:100%"> 
     <p style="text-align:center; text-decoration: underline"><strong> Works 
      with these Things:</strong></p> 
     <div style="float:left;max-width:100%; width:160px;text-align:center;margin-right:20px;margin-bottom:20px"> 
     <div style="background-color: rgb(255, 0, 255);"><span style="text-decoration: underline;"><strong>Flowers 
       A<br> 
      </strong></span></div> 
     <div style="background-color: rgb(0, 255, 255);"><img src="http://s25.postimg.org/i8gxl0uzf/140x124.png"></div> 
     </div> 
     <div style="float:left;max-width:100%; width:230px;text-align:center;"> 
     <div style="background-color: rgb(255, 0, 255);"><span style="text-decoration: underline;"><strong>Flowers 
       B<br> 
      </strong></span></div> 
     <div style="background-color: rgb(255, 255, 0);"><img src="http://s25.postimg.org/a48tga8kf/209x112.png"></div> 
     </div> 
    </span> 

现在的解释:
所以,我第一次对自己一行的第一个图像开始时便有了底下面有两对div。我想我浪费了太多的水平空间,所以我通过在该图像中放置float:left并将两对div封闭在“408x134”图像的右边,将两对div放在/右边标记和放置浮动:留在它。

我正在设计这个响应性/移动的想法,并且正试图避免现在使用@media。

因此,当他们在浏览器上水平缩小屏幕尺寸时,右边的两个项目在“408x134”图像下方下降一对,这正是我想要的。随着屏幕变得更小,下面的两个项目分开,“209 x 112”图像在“140 x 124”图像下。 (嗯,它们和每个人之上的div标签)。

这就是我想要的。 然而,我不能得到的最后一部分是我想要这两对div中心。我怎样才能做到这一点?如果你也可以解释你的解决方案,那就太好了,这样我终于可以明白如何解决这个问题,或者避免将它放在第一位。现在我意识到第一对div-Flowers A和“140 x 124”不会完全居中,因为我已经放置了一个右边距,这样当它们相邻时可以有一些空间另一个是水平的,当它们堆放在一个较小的屏幕上时,它们也在彼此之下。为了这一点,我想我将不得不使用@media标签去除/调整边距并添加换行符或其他div或其他东西。

+0

有你不想使用媒体查询的原因吗?其他明智的你将不得不使用javascript – lucadem1313

我的袖口:在我有限的经验divs击败css的点。其次,有脚本来检测屏幕大小,并允许你提供单独的CSS基于此。多数民众赞成我是如何玩它的。

尝试下面的代码:

<h4 style="margin-bottom:-15px"><strong>The Item Item Below:</strong></h4> 
    <img style="float:left;width:100%; max-width: 408px; height:100%; max-height: 134px; background-color:red; margin-top:40px; margin-right:20px" 

     src="http://s25.postimg.org/kbrcsousf/408x134.png"> <span style="float:left; margin:0 auto; max-width:100%"> 
     <p style="text-align:center; text-decoration: underline"><strong> Works 
      with these Things:</strong></p> 
      <center> 
     <div style="margin: 0 auto;display:inline-block;max-width:100%; width:160px;text-align:center;margin-right:20px;"> 
     <div style="background-color: rgb(255, 0, 255);"><span style="text-decoration: underline;"><strong>Flowers 
       A<br> 
      </strong></span></div> 
     <div style="background-color: rgb(0, 255, 255);"><img src="http://s25.postimg.org/i8gxl0uzf/140x124.png"></div> 
     </div> 
     <div style="margin: 0 auto; display:inline-block;max-width:100%; width:230px;text-align:center;"> 
     <div style="background-color: rgb(255, 0, 255);"><span style="text-decoration: underline;"><strong>Flowers 
       B<br> 
      </strong></span></div> 
     <div style="background-color: rgb(255, 255, 0);"><img src="http://s25.postimg.org/a48tga8kf/209x112.png"></div> 
     </div> 
    </span> 
    </center> 
+0

非常感谢。如果您将div-20px添加到围绕鲜花A和“140 x 124”图片的div并向鲜花B和“209 x 112”图片周围的div添加垂直对齐:顶部,您将获得完全符合我的加上你能够做到的中心位置。我尝试添加中心标签,但没有得到它的工作,但那是因为我没有删除span标签,也没有删除所做的其他更改。由于中心标签在HTML 5中不被支持,我将其更改为'

',它工作正常。我也想要一个解释,但我是 – Rolo
+0

哦,好吧,很高兴它为你工作。我知道上面的代码需要一些调整。感谢评论Rolo!很高兴你明白了! :) – hiew1

+0

非常高兴的工作代码。因此,添加一些解释:1.您在'img'标签和'span'标签中留下了'float:left',我已经包围了我拥有的两对div,2.您将'float:left'替换为我的2个主div用'margin:0 auto; display:inline-block'。 'vertical-align:top'不得不添加到围绕Flowers B&“209 x 112”图片的div上,因为'float:left'已被移除,默认情况下它包含顶部垂直对齐。希望这些额外的笔记可以帮助那些对CSS不熟悉的人,并试图理解如何安排/布置项目。 – Rolo

我试着看你的HTML,我想你就错过了几个标签。另外,最佳做法是不要将CSS样式与HTML内联;您通常希望将CSS放在单独的样式表中。越过这一切,我认为你需要了解一些关于你正在使用的元素和属性的东西。

A div默认情况下是块级元素,除非您为其添加一些其他覆盖样式,否则它将占用其容器(父级)的100%宽度。一个选项(你选择的)是float:left;,它会让这个元素的行为类似于内联元素(如spanimg),除非它总是尝试将自身与容器的左边对齐;无论您设置为容器的对齐方式如何。

我会在你的情况建议是设置图像容器div元素display:inline-block;而不是float:left;,这样它会通过父母的text-align:center;性能受到影响,当没有余地所有div元素是在一个排,他们会掉下来,但在中间对齐。您目前不似乎有一个容器元素,但你的设置应该大概是这个样子:

#container{ 
 
    text-align:center; 
 
} 
 

 
#container .imgwrap{ 
 
    display:inline-block; 
 
}
<div id="container"> 
 
    <div class="imgwrap"><img src="#"/></div> 
 
    <div class="imgwrap"><img src="#"/></div> 
 
    <div class="imgwrap"><img src="#"/></div> 
 
</div>

+0

感谢您的解释。我也在寻找,并将不得不阅读更多。至于你的代码,我希望右边的两个主div尽可能地保持完整,因为我为它们都有一个头,因此,我从来不想让第一个图像留下。他们永远在一起,当他们不得不分手时,他们仍然与第一张图像分开。我相信我之前可以做你已经做好的事情,因为我可以并排两个div。不过,如果我需要参考它,我会记住这一点。 – Rolo