【问题标题】:How to position buttons next to each other [duplicate]如何将按钮彼此相邻放置[重复]
【发布时间】:2015-05-31 13:55:07
【问题描述】:

我刚刚开始关注网页设计,我正在尝试实现一些简单的东西,但我不确定实现这一目标的最佳方法是什么。

我只是想做的是将按钮放置在彼此旁边,而不管它前面的文本的长度如何。 IE 如下面的屏幕截图所示,我有 4 列带有段落和按钮。

【问题讨论】:

  • 请在问题中包含您的 HTML 和 CSS,而不是共享屏幕截图。否则,您的问题可能会被关闭。有关更多信息,请参阅有关提问的帮助中心。
  • 如果您添加代码,它会帮助您更轻松
  • 请更具体一点:您想让所有按钮都具有相同的固定宽度还是可调整大小?另外,@TylerH 是对的:请包含您的代码示例并突出显示有问题的部分。最好的问候,
  • 另一个可能的重复:stackoverflow.com/questions/20481612/…

标签: html css


【解决方案1】:

使用flexbox

div {
    display: -webkit-flex;
    -webkit-flex-flow: column wrap;
    -webkit-align-items: center;
}
ul {
    display: -webkit-flex;
}

li {
    -webkit-flex: 1;
}

http://jsfiddle.net/pTsW4/85/

browser support

【讨论】:

    【解决方案2】:

    不确定 flexbox 支持的范围有多大。我会选择单独的 div 或 jQuery 在所有(相对)列上应用 equal height 的解决方案,然后将按钮放在底部(绝对)。

    【讨论】:

      【解决方案3】:

      在父容器上使用display: flex; 以确保所有块具有相同的高度。然后从bottomleft 中定位<button> 绝对值。通过向左平移按钮宽度的 -50% 来居中。

      http://codepen.io/anon/pen/jPyLGK

      ul {
        font-family: verdana, sans-serif;
        display: flex;
        list-style: none;
        justify-content: space-between;
        width: 600px;
      }
      
      li{
        background-color: #eeeef0;
        border-radius: 10px;
        box-shadow: 0 0 5px #666;
        flex-grow: 3;
        flex-shrink: 1;
        margin: 10px;
        padding: 0 10px 50px;
        position: relative;
        text-align: center;
      }
      
      li p {
        text-align: left;
      }
      
      li button {
        position: absolute;
        bottom: 10px;
        left: 50%;
        transform: translateX(-50%);
      }
      <ul>
        <li>
          <h2>Test 1</h2>
          <p>Lorem ipsum dolor sit amet.</p>
          <img src="http://lorempixel.com/120/150" />
          <button>Go now!</button>
        </li>
        <li>
          <h2>Test 2</h2>
          <p>Lorem ipsum dolor sit amet.</p>
          <img src="http://lorempixel.com/90/160" />
          <button>Go now!</button>
        </li>
        <li>
          <h2>Test 3</h2>
          <p>Lorem ipsum dolor sit amet.<br /> Lorem ipsum dolor sit amet.</p>
          <img src="http://lorempixel.com/100/260" />
          <button>Go now!</button>
        </li>
        <li>
          <h2>Test 4</h2>
          <p>Lorem ipsum dolor sit amet.</p>
          <button>Go now!</button>
        </li>
      </ul>

      【讨论】:

      • 这可能会使用 IE10 和更早版本无法与 display: flex; 一起使用的注释
      • IE10 确实支持 flex box,但实际上它使用的是 2012 版本的语法。如果有人感兴趣,就在这里:w3.org/TR/2012/WD-css3-flexbox-20120322
      • 没错;它也在 -ms- 前缀后面。
      【解决方案4】:

      http://jsfiddle.net/pTsW4/87/

      * { 
          margin: 0; 
          padding: 0; 
          list-style: none; 
          box-sizing: border-box;  
       }
      
      
      ul {
          margin: 0;
          display:table;
      }
      
      li {
          display:table-cell;
          width:30%;
          padding: 20px 20px 40px 20px;
          position: relative;
          border: 1px solid #000;
      }
      
      button {
          position: absolute;
          bottom: 10px;
          left: 50%;
          transform: translateX(-50%);
      }
      

      将显示表添加到“ul”,将表格单元格添加到子“li”是为了确保高度相等。添加了 40px 的填充底部以为按钮留出空间,然后按钮绝对定位到 'li' 的底部。

      【讨论】:

        【解决方案5】:

        尝试在单独的 div 中添加按钮:

        <div><p>Text 1</p></div>
        <div><p>Text 2</p></div>
        <div><p>Text 3</p></div>
        <div><p>Text 4</p></div>
        <div>...buttons...</div>
        

        然后用css定位div。

        【讨论】:

          【解决方案6】:

          JSFiddle demo

              <tr>
                  <td> Funny Hello Images</td>
                  <td> Funny Hello Images</td>
                  <td> Funny Hello Images</td>
                  <td> Funny Hello Images</td>
              </tr>
              <tr>
                  <td>Find the best Hello images, greetings and pictures here. Browse our great collection of hello pictures and choose your favourite to send to a friend.</td>
                  <td> Share an image on Facebook by clicking the button below the graphic, or copying and pasting the link.</td>
                  <td>Share an image on Facebook by clicking the button below the graphic, or copying and pasting the link.</td>
                  <td>Hello</td>
              </tr>
              <tr>
                  <td> <button>button1</button></td>
                  <td> <button>button2</button></td>
                  <td> <button>button3</button></td>
                  <td> <button>button4</button></td>
              </tr>
          </table>
          

          CSS:

          table td { 
              table-layout:fixed;
            width:20%;
            overflow:hidden;
              word-wrap:break-word;
          }
          

          更新了链接

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2019-11-05
            • 2015-10-29
            • 2020-07-22
            • 2015-07-27
            • 2017-05-08
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多