Reputation: 91
I am learning HTML and CSS and I am having difficulty centering a button within a <div>
. Here is the code I currently have:
.box-information {
border: 2px solid #000000;
margin: 0 auto 15px auto;
padding: 0 10px 60px 10px;
width: 80%;
background-color: #ffffff;
position: relative;
}
.button-blue:link,
.button-blue:visited {
width: 7em;
display: block;
padding: 10px 15px;
background-color: rgba(66, 85, 123, 1);
font-size: 1.0em;
text-indent: 0;
text-align: center;
color: #ffffff;
position: absolute;
bottom: 10px;
left: auto;
}
<div class="box-information your-business">
<p class="title-information">
Your Business
</p>
<p class="text-information">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</p>
<a class="button-blue learn-more" href="#">Learn More</a>
</div>
I am able to offset the button from the bottom, and I can offset the button horizontally if I use anything but AUTO.
Please help me understand what I am doing wrong.
Upvotes: 1
Views: 15913
Reputation: 1449
Set margin-left:50%;
. This set my button to the middle of its parent control.
Upvotes: 0
Reputation: 35493
Update
.box-information {
border: 2px solid #000000;
margin: 0 auto 15px auto;
padding: 0 10px 60px 10px;
width: 80%;
background-color: #ffffff;
position: relative;
}
.button-blue:link,
.button-blue:visited {
width: 7em;
display: block;
padding: 10px 15px;
background-color: rgba(66, 85, 123, 1);
font-size: 1.0em;
text-indent: 0;
text-align: center;
color: #ffffff;
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
}
<div class="box-information your-business">
<p class="title-information">
Your Business
</p>
<p class="text-information">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</p>
<a class="button-blue learn-more" href="#">Learn More</a>
</div>
Upvotes: 1
Reputation: 2708
To center a block element in it's parent, all you need to do is add:
margin-left:auto;
margin-right:auto;
to the button's CSS properties.
(You will need to remove the position: absolute
and the absolute positioning properties first.)
Take a look at this codepen
: https://codepen.io/anon/pen/qXYEpd
Upvotes: 4