简体   繁体   English

在CSS中对齐三个框

[英]Align three boxes in css

I need to align the three boxes in a horizontal position. 我需要将三个方框水平对齐。 Also I need to add toggle color of the border of the second box. 另外,我需要添加第二个框的边框的切换颜色。 I am pasting my codes here. 我在这里粘贴代码。 My HTML code is 我的HTML代码是

Unordered List 无序列表

This is a simple list 这是一个简单的清单

    First Item (bold) 第一项(粗体)
  • Second Item 第二项
  • Third Item 第三项
  • Last Item (underlined) 最后一项(带下划线)
            <div class="box2">
                <h2>Button Jquery Demo</h2>
                <p>The background of this box should be set to white on document.ready()</p>
                <p>Clicking the button below will alternate the color of the border of this box to red and back to default on on each click.</p>
                <a href="javascript:void(0);" onclick="alternateColor();">Toggle Color</a>
            </div>
            <div class="box3">
                <h2>Table Example</h2>
                <p>Table rows should include a hover state</p>
                <table>
                  <thead>
                  <div id="col3">
                      <table summary="" border="1" cellspacing="0" cellpadding="3">


                    <tr>
                      <th>Driver</th>
                      <th>Hometown</th>
                      <th>Car</th>
                    </tr>
                  </thead>

                  <tbody>
                    <tr>
                      <td>John S.</td>
                      <td>Lincoln, NE</td>
                      <td>55</td>
                    </tr>
                    <tr>
                      <td>Jane D.</td>
                      <td>Omaha, NE</td>
                      <td>24</td>
                    </tr>
                  </tbody>

                  <tfoot>
                    <tr>
                      <td>Mike J.</td>
                      <td>Albany, NY</td>
                      <td>1</td>
                    </tr>
                  </tfoot>
                </table>
</div>

My CSS code is 我的CSS代码是

.box1 {
    background-color: #4b4244;
    width: 300px;
    padding: 25px;
    margin: 25px;
    height: 240px ;
    border-radius: 25px;
    display:inline-block;

}
.box2 {
    margin: auto;
    background-color:white;
    width: 300px;
    padding: 25px;
    height: 240px;
    border-radius: 25px;
    display: inline-block;

}
.box3 {
    position: absolute;
    right: 0px;
    background-color: #4b4244;
    width: 300px;
    padding: 25px;
    margin: 25px;
    height: 240px ;
    border-radius: 25px;
    display: inline-block;

}

Use float:left to align your boxes: 使用float:left对齐框:

jsFiddle jsFiddle

EDIT: 编辑:

I suspect the floating boxes are throwing your footer off. 我怀疑浮动盒子会让您的页脚丢掉。 In that case, wrap your boxes in a containing tag (eg main) and give it this :after content: 在这种情况下,将您的盒子包装在一个包含标签(例如main)中,并:after内容:after加上:after内容:

main:after{
    content:"";
    display:block;
    clear:both;
}

updated fiddle 更新的小提琴

try with this below code it may help you and look it into full screen 尝试使用以下代码,它可能会对您有帮助,并使其全屏显示

 $(document).ready(function(){ $("a.toggleColor").click(function(){ $(".box2").toggleClass("border"); }); }); 
 div.box1,.box2,.box3{ float : left; height: 240px ; width: 300px; padding: 25px; background-color: #4b4244; margin : 25px; border-radius: 25px; } .box2 { background-color:white !important; border : 1px #4b4244 solid; } .border{ border : 1px red solid; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="box1"> <h2>Unordered List</h2> <p>This is a simple list</p> <ul> <li>First Item (bold)</li> <li>Second Item</li> <li>Third Item</li> <li>Last Item (underlined)</li> </ul> </div> <div class="box2"> <h2>Button Jquery Demo</h2> <p>The background of this box should be set to white on document.ready()</p> <p>Clicking the button below will alternate the color of the border of this box to red and back to default on on each click.</p> <a href="javascript:void(0);" class="toggleColor">Toggle Color</a> </div> <div class="box3"> <h2>Table Example</h2> <p>Table rows should include a hover state</p> <div id="col3" > <table summary="" border="1" cellspacing="0" cellpadding="3"> <thead> <tr> <th>Driver</th> <th>Hometown</th> <th>Car</th> </tr> </thead> <tbody> <tr> <td>John S.</td> <td>Lincoln, NE</td> <td>55</td> </tr> <tr> <td>Jane D.</td> <td>Omaha, NE</td> <td>24</td> </tr> </tbody> <tfoot> <tr> <td>Mike J.</td> <td>Albany, NY</td> <td>1</td> </tr> </tfoot> </table> </div> 

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

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