【问题标题】:Center button inside div, ignoring other elementsdiv内的中心按钮,忽略其他元素
【发布时间】:2017-05-09 16:40:45
【问题描述】:

我有以下内容:

div.div
  p.paragraph
    | about 3 lines of text... (dynamic)
  button.button-i-want-to-center
    | click_me

我希望它看起来像这样:

___________________________
| Text.....................|
| Text.....................|
| Text.....................|
|          ______          |
|         |BUTTON|         | <--- Center of div: 
|                          |      I want the button to ignore 
|                          |      the text.
|                          |
|                          |
___________________________

请记住,div 的大小是动态的。

我怎样才能做到这一点?我尝试了 flex,但没有成功。固定边距也不好。

【问题讨论】:

  • 1. display:inline-block; 2. margin:auto
  • @csTroubled 我在下面添加了一个答案,请查看

标签: html css pug


【解决方案1】:

您应该在 div 上的按钮 position:relative; 上使用 position:absolute;。看这个例子:

div{
    position:relative;
  height:180px;
  background-color:green;
    }
    
button{
    position:absolute;
    top:calc(50% - 15px);
    left:calc(50% - 50px);
    height:30px;
    width:100px;
}
<div>
<p>
  lines of textlines of textlines of textlines of textlines of textlines of textlines of textlines of textlines of textlines of textlines of textlines of textlines of textlines of text
</p>
<button>Click me</button>
</div>

【讨论】:

    【解决方案2】:

    有很多方法可以做到这一点,但你可以简单地通过text-align:center;margin:0 auto; 做到这一点 看下面的sn-p

    html,body{
      height:100%;
      width:100%;
      background-image:url("https://th3rdculture.files.wordpress.com/2011/06/dark-forest-35836-240944.jpg");
      background-size:cover;
      margin:0;
    }
    .wrapper {
      float:left;
      width:100%;
      text-align:center;
    }
    p{
      color:#fff;
    }
    button {
      display:block;
      margin:0 auto;
    }
    <body>
    <div class="wrapper">
        <p> some texts here </p>
        <p> other texts here other texts here other texts here  </p>
        <button class="button">Button</button>
    </div>
    </body>

    或者您希望文本左侧和按钮仅居中使用text-align:left

    html,body{
      height:100%;
      width:100%;
      background-image:url("https://th3rdculture.files.wordpress.com/2011/06/dark-forest-35836-240944.jpg");
      background-size:cover;
      margin:0;
    }
    .wrapper {
      float:left;
      width:100%;
      text-align:left;
    }
    p{
      color:#fff;
    }
    button {
      display:block;
      margin:0 auto;
    }
    <body>
    <div class="wrapper">
        <p> some texts here </p>
        <p> other texts here other texts here other texts here  </p>
        <button class="button">Button</button>
    </div>
    </body>

    【讨论】:

      【解决方案3】:

      你可以试试这个:

      1. 外部div设置为position: relative;

      2. 对于可以设置中间的div,创建一个类,css如下:

        .center {
           position: absolute;
           margin: auto;
           top: 0; bottom: 0;
           left: 0; right: 0;
         }
        

      【讨论】:

        【解决方案4】:

        您可以使用 css3 flexbox 概念

        html,body{
          height:100%;
          width:100%;
          }
        .wrapper {
                   display:flex;
          justify-content:center;
                   width:100%;
                   height:100%;
                   background-image: url("https://i.stack.imgur.com/wwy2w.jpg");
                   background-repeat:no-repeat;
                   background-size:cover;
                   
             }
        
            .button {
             position:absolute;
              align-self:center;
            }
        p{
          color:white;
          }
        <div class="wrapper">
           <p> other texts</p>
                <button class="button">Button</button>
            </div>

        【讨论】:

          【解决方案5】:

          你可以使用display: block & margin: 0 auto,比如:

          button {
            display: block;
            margin: 20px auto;
            font-size: 20px;
          }
          

          看看下面的sn-p:

          button {
            display: block;
            margin: 20px auto;
            font-size: 20px;
          }
          <div class="content-holder">
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laudantium, consectetur, saepe. Praesentium doloribus dolorum qui nisi rem veniam iure. Ducimus, harum, molestiae. Harum consequatur cum accusantium fugiat, reiciendis repudiandae iste!
            <button>Button</button>
          </div>

          希望这会有所帮助!

          【讨论】:

            【解决方案6】:
            wrapper{
            display: flex;
            justify-content : center;
            }
            

            wrapper{
            position : relative;
            }
            .button{
            position: absolute;
            top : 50%;
            left : 50%;
            }
            

            【讨论】:

              猜你喜欢
              • 2020-06-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-04-12
              • 2017-07-12
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多