Krupal Patel
Krupal Patel

Reputation: 1497

How to centering a div horizontally without width & margin

How to centering a div horizontally without width in old version browsers which are not supported display:inline-block property.

How to centering my .container div horizontally without width

<div class="main_section">
  <div class="content_wrapper">
    <div class="container">My content</div>
  </div>
</div>

Upvotes: 0

Views: 1191

Answers (3)

user3260487
user3260487

Reputation: 75

You can try setting the outer div margin: 0 auto

It will work for responsive too.

Upvotes: 0

Duncan Lukkenaer
Duncan Lukkenaer

Reputation: 13914

Make the content you want to center (.container) inline-block. This way you can center it with text-align: center on the parent div.

.container {
  display: inline-block;
  background-color: red;
}

.content_wrapper {
  background-color: blue;  
  text-align: center;
}
<div class="main_section">
  <div class="content_wrapper">
    <div class="container">My content</div>
  </div>
</div>

Upvotes: 4

Krupal Patel
Krupal Patel

Reputation: 1497

body {
  padding: 200px 0 0 0;
  margin: 0;
}
.main_section {
  float: right;
  position: relative;
  right: 50%;
  z-index: 999;
}
.content_wrapper {
  float: right;
  position: relative;
  right: -50%;
}
.container {
  color: #333;
  background: #f5f5f5;
  border: 5px #ccc solid;
  padding: 10px;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 20px;
  letter-spacing: 1px;
  max-width: 1000px;
  box-shadow: -1px -1px 5px 1px #666666;
}
<div class="main_section">
  <div class="content_wrapper">
    <div class="container">Div align center without fix width</div>
  </div>
</div>

Upvotes: 0

Related Questions