在 div 内部居中按钮,忽略其他元素

6

我有以下内容:

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边距:自动;
- user6360214
@csTroubled 我已经在下面添加了一个答案,请查看。 - Jishnu V S
6个回答

4
您应该在按钮上使用position:absolute;,在div上使用position:relative;。请看以下示例:

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>


3

您可以使用CSS3 Flexbox概念。

html,body{
  height:100%;
  width:100%;
  }
.wrapper {
           display:flex;
  justify-content:center;
           width:100%;
           height:100%;
           background-image: url("https://istack.dev59.com/wwy2w.webp");
           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>


1

有很多方法可以做到这一点,但是您可以简单地使用text-align:center;margin:0 auto;来实现。请参考下面的代码段

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>


1
你可以尝试这个:

  1. Outer div set as position: relative;

  2. For the div that you can to set middle, create a class with the following css:

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

1
您可以使用display: blockmargin: 0 auto,如下所示:
button {
  display: block;
  margin: 20px auto;
  font-size: 20px;
}

请看下面的代码片段:

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>

希望这有所帮助!

0
wrapper{
display: flex;
justify-content : center;
}

或者

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接