Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center button inside div, ignoring other elements

Tags:

html

css

pug

I've got 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 flex, but didn't do the trick. Fixed margins are no good either.

like image 458
csTroubled Avatar asked Dec 24 '16 05:12

csTroubled


3 Answers

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>
like image 50
ADH - THE TECHIE GUY Avatar answered Nov 20 '22 10:11

ADH - THE TECHIE GUY


You should use position:absolute; on the button position:relative; on the div. 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>
like image 22
ab29007 Avatar answered Nov 20 '22 08:11

ab29007


You can use display: block & margin: 0 auto, like:

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

Have a look at the snippet below:

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>

Hope this helps!

like image 1
Saurav Rastogi Avatar answered Nov 20 '22 08:11

Saurav Rastogi