【问题标题】:How Do You Center Float? [duplicate]你如何居中浮动? [复制]
【发布时间】:2014-02-20 09:53:53
【问题描述】:

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

这是 HTML5 代码

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

以及对应的CSS3

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

【问题讨论】:

  • 我不明白与我有关的答案,我是个新手。虽然它可能会回答我的问题,但我似乎无法用它来解决我的问题。

标签: html css alignment center


【解决方案1】:

看起来您将 h3.company 和 h3.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% 并破坏布局
  • 那么你只需要处理每个标题的文本对齐方式

希望这会有所帮助。

【讨论】:

  • 您的答案是最好的,感谢您的详细解释。
【解决方案2】:

请看代码

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 建议原始解决方案没有响应。我已更新以使其具有响应性。

【讨论】:

  • 这通常是个坏主意,有几个原因
  • 当在宽度小于 554 像素的设备或浏览器窗口上查看时,此建议的解决方案开始失效。
  • @JasonAller 我同意,但问题是如何安排组件,而不是响应式设计。如果需要,我会更改为响应式设计。干杯。
  • @ZachSaucier 您能否详细说明您的评论?
  • @ToanNguyen 1. 它们没有响应 2. 它们破坏了其他东西,例如堆叠顺序 3. 不使用 calc 或负边距,它们的位置是基于它们的尺寸,不允许调整它们的大小并保持完美的定位 4. 难以管理和添加(例如添加第四个元素)
【解决方案3】:

我会在容器上使用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 中的浮动之前

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    • 2010-09-18
    • 2011-06-13
    • 2013-06-20
    • 2014-02-16
    相关资源
    最近更新 更多