你如何居中浮动?

时间:2014-02-19 23:33:05

标签: html5 css3 alignment center

我在h3标记中有三个div个标记。我正在尝试将所有三个h3标记放在同一水平线上,第一个h3标记左对齐,第二个h3标记居中,第三个{{1}标签正确对齐。我在这里寻找答案并发现很多,但未能正确实现它们。感谢

这是HTML5代码

h3

和相应的CSS3

<div class="education">                         
    <h3 class="date"> April 2013 – Present</h3> 
    <h3 class="place"> Sutton ON, Canada </h3> 
    <h3 class="company" > All Reasons Party Rentals </h3>
</div>

3 个答案:

答案 0 :(得分:2)

看起来你的h3.companyh3.place选择器的属性是错误的,因此布局与HTML的顺序相反。

但是,我认为这会奏效:

.education h3 {float:left;padding:0;margin:0;width:33.333%;}

h3.date {text-align:left;}
h3.place {text-align:center;}
h3.company {text-align:right;}

在这里工作,标题上有彩色背景,这样你就可以准确看到宽度是多少: http://jsfiddle.net/QfAeA/

了解其运作方式:

  • 所有3个标题都是浮动的,并且宽度相同,占可用空间的三分之一,因此它们可以平等分享
  • 填充和边距已设置为0,因此不会使它们超过33.333%并破坏布局
  • 然后您只需处理每个标题的文本对齐

希望这有帮助。

答案 1 :(得分:0)

请参阅代码

<强> HTML

 <div class="container">
    <div class="tabletContainer">


        <div class="left">
            <h3>April 2013 – Present</h3>
        </div>
        <div class="middle">
            <h3>All Reasons Party Rentals</h3> 
        </div>

    </div>
    <div class="right">
        <h3>Sutton ON, Canada </h3>
    </div>
</div>

<强> CSS

   /* reset browser styles */
    html, body, div, span, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend,
    table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed,
    figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary,
    time, mark, audio, video {
        margin: 0;
        padding: 0;
        border: 0;
        font-size: 100%;
        vertical-align: baseline;
    }

    article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section {
        display: block;
    }

    body {
        line-height: 1.2;
    }

    ol {
        padding-left: 1.4em;
        list-style: decimal;
    }

    ul {
        padding-left: 1.4em;
        list-style: square;
    }

    table {
        border-collapse: collapse;
        border-spacing: 0;
    }
    /* end reset browser styles */

    /*Fill all available spaces*/

    * {
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
        box-sizing: border-box;

    }


    .container {
        width: auto;
    }

        .container:after {
            content: " ";
            clear: both;
            display: table;
        }


    .tabletContainer {
        /*The total width for the first two column*/
        width: 67%;
        float: left;
        display: block;
    }



    .left {

        float: left;
        /*Each column takes have of the container size, so their width =67/2 = 33.5%*/
        width: 50%;
    }

    .middle {

        float: right;
        /*Each column takes have of the container size, so their width =67/2 = 33.5%*/
        width: 50%;
    }

    .right {
        float: right;
        width: 33%;

    }

    .right h3 {
        float: right;
    }

    /*For tablet devices, show only the two columns in a row and a column underneath*/

    @media (min-width: 481px) and (max-width: 768px) {
        .tabletContainer, .right {
            float: none;
            width: auto;
        }

      .right
      {
          clear: both;
          width: 50%;


      }

        .right h3 {
            float: left;
        }
    }


    /*For mobile phones, show only one column*/
    @media (max-width: 480px) {
       .tabletContainer, .left, .right, .middle {
            float: none;
            width: auto;
           display: block;
        }
        .right h3 {
            float: left;
        }



    }

的jsfiddle:

http://jsfiddle.net/KxryX/

编辑:好的,因为Jason和Zach建议原始解决方案没有响应。我已更新,以使其响应。

答案 2 :(得分:0)

我会在容器上使用text-align:center,在这种情况下使用body,并将两者浮动在外面。这允许任何元素的任何(或变化的)宽度,同时保持完全对齐。在小屏幕上,它变成垂直布局以适合

body { text-align:center; }
h3.date {
    text-align:left; 
    float:left;
}   
h3.company {
    margin:20px auto;
    display:inline-block;
}   
h3.place {
    text-align:right; 
    float:right;
}

Demo

如果您希望公司名称在小屏幕上保持在其他公司名称之上,请将其置于HTML中的浮点数之前