【问题标题】:CSS centering multiple images/elementsCSS居中多个图像/元素
【发布时间】:2012-09-15 20:35:28
【问题描述】:

我想将以下 2 个按钮并排居中,而不是彼此下方。这里是an example of how it is looking at the moment on JS Fiddle

你能告诉我处理这个问题的最佳方法吗?非常感谢任何帮助。

【问题讨论】:

    标签: css image center


    【解决方案1】:

    display:inline-block 定义为 anchor tags 并将 text-align:center 定义为 parent。像这样写:

    a.button {
         display:inline-block;
         *display:inline;/* For IE7 */
         *zoom:1;/*For IE7*/
         padding: 3px 10px 3px 10px;
         width:50px;
         margin-left:auto;
         margin-right:auto;
         text-align: center;
         color: #636363;
         text-decoration: none;
         border-top: solid 1px #ccc;
         border-left: solid 1px #ccc;
         border-bottom: solid 1px #ccc;
         border-right: solid 1px #ccc;
    }
    
    .parent{
        text-align:center;
    }
    

    HTML

    <div class="parent">
        <a class="button">Test</a>
        <a class="button">Test</a>
    </div>
    

    查看http://jsfiddle.net/2ws9r/11/

    【讨论】:

      【解决方案2】:

      添加一个固定宽度和边距为0的容器;

      http://jsfiddle.net/2ws9r/13/

      希望对你有帮助

      【讨论】:

        【解决方案3】:

        JSFiddle

        <div>
          <a class="button">Test</a>
          <a class="button">Test</a>
        </div>
        
        div{ text-align: center; }
        a.button {
             display: inline-block;
             padding: 3px 10px 3px 10px;
             width:50px;
             margin-left:auto;
             margin-right:auto;
             text-align: center;
             color: #636363;
             text-decoration: none;
             border-top: solid 1px #ccc;
             border-left: solid 1px #ccc;
             border-bottom: solid 1px #ccc;
             border-right: solid 1px #ccc;
        }
        
        a.button:hover  {
             color: #179FD9;
        }
        

        【讨论】:

        • 移除显示:块;将它们向左移动并且不居中。例如jsfiddle.net/2ws9r/5
        • 或者让它显示:inlineblock;
        • 谢谢迪帕克斯。刚试过这个,它工作正常。愚蠢地没有想到将它包装在一个 div 中并居中!
        猜你喜欢
        • 2013-08-29
        • 1970-01-01
        • 2011-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-06
        • 2013-08-28
        相关资源
        最近更新 更多