3

.headers
{
    background-color: #ff0000;
    width: 100%;
    overflow: hidden;
    display: table;
}
.logo
{
    height: 35px;
    border: 1px solid #00ff00;
    float: left;
    width: 30px;
}
.slogan
{
    float: right;
    display: table-cell;
    vertical-align: middle;
}
<div class="headers">
    <div class="logo"></div>
    <div class="slogan">IIN</div>
</div>

How do i center my div[slogan] without using negative margin/top 50%?

Gibbs
  • 21,904
  • 13
  • 74
  • 138

4 Answers4

4

you don't need floats when using display: table/table-cell ... this should center the .slogan div using the table-cell layout.

.headers
{
    background-color: #ff0000;
    width: 100%;
    overflow: hidden;
    display: table;
}
.logo
{
    height: 35px;
    border: 1px solid #00ff00;
    /*float: left; NOT THIS */
    width: 30px;
    display: table-cell; /* THIS */
}
.slogan
{
    /*float: right; NOT THIS */
    display: table-cell;
    vertical-align: middle;
    text-align: right; /* THIS */
}
<div class="headers">
    <div class="logo"></div>
    <div class="slogan">IIN</div>
</div>
MikeM
  • 27,227
  • 4
  • 64
  • 80
  • I want it aligned vertically at right. Not horizontally – Gibbs Apr 28 '15 at 15:36
  • Yes thanks. Should i set width to restrict it to only content's width? – Gibbs Apr 28 '15 at 15:39
  • @GopsAB shouldn't need to...you can set the width of the `.headers` div and `.slogan` will auto size (`.headers` width minus `.logo` width) – MikeM Apr 28 '15 at 15:43
1

If you are looking to middle slogan vertically then please check the fiddle https://jsfiddle.net/nileshmahaja/e6byt6zt/

CSS

.headers
{
    background-color: #ff0000;
    width: 100%;
    overflow: hidden;
    display: table;
    vertical-align:middle;
}
.logo
{
    height: 35px;
    border: 1px solid #00ff00;
    float: left;
    width: 30px;
}
.slogan
{
    display: table-cell;
    vertical-align: middle;
    text-align:right;
}
Nilesh Mahajan
  • 3,506
  • 21
  • 34
1

change your slogan class with this

.slogan {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
    width: 100%;
}
LTasty
  • 2,008
  • 14
  • 22
0

You should use 'margin: 0 auto ;' on the . If you want to align text inside the div you can just use 'text-align: center ;'.

Check this post click

Community
  • 1
  • 1
Atanas44
  • 3
  • 3