简体   繁体   English

将div元素彼此相邻并居中

[英]Put div elements next to each other and center them

I am trying to put 5 div elements next to each other and also center them. 我正在尝试将5个div元素彼此相邻并居中放置。 However, I cant make it work. 但是,我无法使其工作。 Here is my code 这是我的代码

<div style="width:200px;margin-right:20px;">
  <div class="panel panel-default text-center" style="border: 1px solid #fab05d;border-radius:0px;">
     <div class="panel-heading" style="background-color:#fab05d;color:#ffffff !important;border-radius:0px;">
        <h1>2017</h1>
        <p style="line-height: 14pt; margin-top:3px;margin-right:25px;">EARLY BIRD*</p>
        <p style="clear:both;line-height: 16pt;">GOLD PACKAGE <br>REGISTRATION**</p>
     </div>
     <div class="panel-body" style="padding-top:5px;">
        <p style="color:#929393;font-size:15px;"><strong>$x.00 TICKET</strong></p>

        <div style="display:inline-block;margin:auto;padding-top:1px;margin-top:1px;" class="text-center">
        <input type="image" src="http://localhost/wordpress/wp-content/uploads/2017/03/gold_remove.png" id="gold_r" style="">
        <input type="text"  class="text-center" id='goldval' name="quantity" size="5" onchange="gold_change()" style="vertical-align:top;font-weight:bold;border-radius:5px;border:1px solid #929393;">
        <input type="image" src="http://localhost" id="gold_a" style="">

        </div>
     </div>
  </div>

I have 5 of them. 我有五个 They are basically the same, only the content is different. 它们基本相同,只是内容不同。 All 5 divs are inside container-fluid (I am using bootsrap). 所有5个div都位于容器流体内部(我正在使用bootsrap)。 I have tried to add display:inline-block to my container but it doesn't work. 我试图将display:inline-block添加到我的容器中,但是它不起作用。 Also, I used float:left but in that case, I can't properly align my divs when the screen size changes. 另外,我使用了float:left,但是在那种情况下,当屏幕尺寸更改时,我无法正确对齐div。 I tried to use the extended version of Bootstrap grid when you can create equal 5 columns but in that case, my div's content gets messed up. 当您可以创建相等的5列时,我尝试使用Bootstrap网格的扩展版本,但在那种情况下,我的div内容混乱了。 Can anyone please give me some solution that will work for all screen sizes? 任何人都可以给我一些适用于所有屏幕尺寸的解决方案吗? Thanks 谢谢

Try this in a larger screen, I think this will work. 在更大的屏幕上尝试一下,我认为这会起作用。 The snippet's screen is very small that's why it does not align together. 代码段的屏幕很小,这就是为什么它不能对齐。 Use Media Queries to adjust the div 's size so that it can accommodate smaller screen size 使用媒体查询来调整div的大小,使其可以容纳较小的屏幕尺寸

 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <div class="container"> <div class="row"> <div style="width:200px;height:250px;margin-right:20px; border: 1px solid red;" class="col-xs-2"> </div> <div style="width:200px;height:250px;margin-right:20px; border: 1px solid red;" class="col-xs-2"> </div> <div style="width:200px;height:250px;margin-right:20px; border: 1px solid red;" class="col-xs-2"> </div> <div style="width:200px;height:250px;margin-right:20px; border: 1px solid red;" class="col-xs-2"> </div> <div style="width:200px;height:250px;margin-right:20px; border: 1px solid red;" class="col-xs-2"> </div> </div> </div> 

This may help you! 这可能对您有帮助!

 .col-xs-2{ background:green; color:#FFF; } .col-half-offset{ margin-left:4.166666667% } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/> <div class="container"> <div class="row" style="border: 1px solid black"> <div class="col-xs-2" id="p1">One</div> <div class="col-xs-2 col-half-offset" id="p2">Two</div> <div class="col-xs-2 col-half-offset" id="p3">Three</div> <div class="col-xs-2 col-half-offset" id="p4">Four</div> <div class="col-xs-2 col-half-offset" id="p5">Five</div> <div>lorem</div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM