-1

I have a div inside of a div and I want to position the div in the centre.

<div class=\"dialogs-icon\"><img src=\"images/space.png\" width=\"25\" height=\"25\" class=\"d-icon\"><div class=\"messages\">hi</div></div>

<style>
.dialogs-icon {
    display: inline;
    position: relative;
}

.d-icon {
    width: 25px;
    height: 25px;
    margin: 5px 15px 5px 5px;
}

.messages {
    width: 250px;
    position: absolute;
    top: 50px;
}
</style>

How do I make .messages centred to .dialogs-icon?

I want to make horizontally center positioning

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156

3 Answers3

0

Use margin: auto in the div you want to center.

<style>
.dialogs-icon {
    display: inline;
    position: relative;
}

.d-icon {
    width: 25px;
    height: 25px;
    margin: 5px 15px 5px 5px;
}

.messages {
    width: 250px;
    margin: auto;
    top: 50px;
}
</style>
0

Try this

.messages{
    width:250px
    margin:0px auto; //This will center your content but it needs width to be defined
}

There is another way to do it by using center tag. Note that center tag is removed from htm5 but it still works in the browsers hi

Skyyy
  • 1,539
  • 2
  • 23
  • 60
  • its not working with absolute position but working with relative position property. And you can see center tag is working too http://jsfiddle.net/4ntV4/1/ there will be some way to make it work with absolute position you need to play with display property – Skyyy Jun 30 '14 at 16:53
0

To horizontally center and have absolute positionning mixed together, you could use the .messages box as a container sprayed ont the whole width of the page, and center its content:

DEMO

HTML

<div class="messages">
        <p>hi</p>
</div>

CSS

.dialogs-icon {
}
.d-icon {
    width: 25px;
    height: 25px;
    margin: 5px 15px 5px 5px;
}
.messages {
    width:100%;
    position:absolute;
   left:0;
}
.messages p {
    width:50%; /* or whatever */
    border:solid;
    background:white;
    margin:auto;
}
G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129