Richard Rodgers
Richard Rodgers

Reputation: 625

How do I center align a div within another div

I'm trying to center align a div that is located within another div. I want to vertically center the "options" div that is located inside the "plan-container"

Thanks in advance.

.plan-container {
    width: 960px;
    height: auto;
    margin-top: 62px;
    overflow: hidden;
    background-color: red;
}

.options {
    float: left;
    width: 151px;
    height: 100px;
    background-color: green;
}

.plan {
    float: left;
    width: 220px;
    height: 600px;
    margin-left: 23px;
    background-color: purple;
}

.plan:last-child {
    float: right;
}

.plan-featured{
    width: 300px;
    height: 600px;
    background-color: purple;
}
<div class="plan-container">
  <div class="options">Options</div>
  <div class="plan">Box one</div>
  <div class="plan plan-featured">Box two</div>
  <div class="plan">Box three</div>
</div>

Upvotes: 0

Views: 627

Answers (3)

Awol
Awol

Reputation: 353

Vucko's answer is correct. I wanted to add a comment, but since I don't have enough reputation yet, I'll just post it as an answer.

You can use the vertical-align property on the inner div that needs centering. This property only works on elements that have display:inline-block or display:table. Refer to the actual spec here.

Repeating Vucko's answer:

.options {
    display: inline-block;
    vertical-align: middle;
}

Upvotes: 0

Vucko
Vucko

Reputation: 20844

You can use inline-block instead of float, and than you can use the vertical-align property:

.plan-container>div{
  display: inline-block;
  vertical-align: middle;
}

JSFiddle

However, beware of the whitespace issue.

Upvotes: 0

Neeraj Prajapati
Neeraj Prajapati

Reputation: 541

Try it-

.plan-container {
    display: flex;
    flex-wrap: wrap; /* optional. only if you want the items to wrap */
    justify-content: center; /* for horizontal alignment */
    align-items: center; /* for vertical alignment */
}

Upvotes: 0

Related Questions