Center button inside div, ignoring other elements

I have the following:

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

I want it to look like this:

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

Keeping in mind that the size of the div is dynamic.

How can i achieve this? I tried to bend, but did not. Corrected fields are not good either.

+4
source share
6 answers

You must use position:absolute;the button position:relative;on the button. See this example:

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>
Run code
+2
source

you can use css3 flexbox concept

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>
Run code
+2
source

:

  • div position: relative;

  • div, , css:

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

display: block margin: 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>

, !

+1

There are many ways to do this, but you can just do it with text-align:center;or margin:0 auto; see the snippet below.

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

or do you want the text to remain on the left, and the button only centerd use 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>
Run code
+1
source
wrapper{
display: flex;
justify-content : center;
}

or

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

Source: https://habr.com/ru/post/1664741/


All Articles