2016-08-15 4 views
1

Если вы измените размер панели результатов, чтобы сделать ее более узкой, вы увидите, что область изображения будет изменяться в зависимости от размера.Центр div вертикально и горизонтально внутри другого div

Я надеюсь, что текст будет всегда центрироваться по вертикали и горизонтали, когда произойдет такое изменение размера.

Я не могу найти нужное css для этого.

.holders { 
 
    position: absolute; 
 
    bottom: 0; 
 
    left: 50%; 
 
    margin-left: -150px; /*half of image width*/ 
 
} 
 
.holder { 
 
    float: left; /*this cannot be changed because we have a row of blocks like the example one in the bottom*/ 
 
    position: relative; 
 
} 
 
.text { 
 
    position: absolute; 
 
    top: 150px; 
 
    /*this is not right at this moment*/ 
 
} 
 
.img { 
 
    width: 100%; 
 
    height: auto; 
 
}
<div style="position: fixed;top:0;left:0;width:100%;height:100%"> 
 
    <div class="holders"> 
 
    <div class="holder"> 
 
     <div class="text"> 
 
     This is text that should be in the center of the block vertically and horizontally 
 
     </div> 
 
     <img class="img" src="http://www.fnordware.com/superpng/pnggrad8rgb.png" /> 
 
    </div> 
 
    </div> 
 
</div>

Вот jsfiddle пример: https://jsfiddle.net/mddc/4tx5h1tq/34/

Спасибо за любую помощь!

ответ

2

.holders { 
 
    position: absolute; 
 
    bottom: 0; 
 
    left: 50%; 
 
    /* margin-left: -150px; */ 
 
    transform: translateX(-50%);  /* see comments below */ 
 
} 
 
.holder { 
 
    float: left; 
 
    position: relative; 
 
} 
 
.text { 
 
    width: 100%; 
 
    text-align: center; 
 
    position: absolute; 
 
    top: 50%;       /* center text vertically */ 
 
    left: 50%;       /* center text horizontally */ 
 
    transform: translate(-50%, -50%); /* horizontal & vertical centering fine-tuning; 
 
             http://stackoverflow.com/a/36817384/3597276 */ 
 
} 
 
.img { 
 
    width: 100%; 
 
    height: auto; 
 
}
<div style="position: fixed;top:0;left:0;width:100%;height:100%"> 
 
    <div class="holders"> 
 
    <div class="holder"> 
 
     <div class="text"> 
 
     This is text that should be in the center of the block vertically and horizontally 
 
     </div> 
 
     <img class="img" src="http://www.fnordware.com/superpng/pnggrad8rgb.png" /> 
 
    </div> 
 
    </div> 
 
</div>

Revised Fiddle

+1

Пересмотренный jsfiddle работает отлично! Спасибо!!!! – curious1

1

Вы считаете using flexbox? Он будет делать то, что вы хотите, с минимальными изменениями.

.holders { 
    position: absolute; 
    bottom: 0; 
    left: 50%; 
    margin-left: -150px; /*half of image width*/ 

} 
.holder { 
    float: left; /*this cannot be changed because we have a row of blocks like the example one in the bottom*/ 
    position: relative; 
} 

.text { 
    position: absolute; 
    top: 0; 
    right: 0; 
    bottom: 0; 
    left: 0; 
    display: flex; 
    flex-direction: column; 
    justify-content: space-around; 
} 

.text p { 
    flex: 0 0 auto; 
    margin: 0; 
    text-align: center; 
} 
.img { 
    width: 100%; 
    height: auto; 
} 
+0

Благодаря Ф.О. r chiming in! Я протестировал ваше решение. Он правильно позиционирует текст по вертикали. Однако, горизонтально, это не когда область изображения сужается. Вы можете сократить текст до «Это текст» и увидеть позицию текста, когда изображение реагирует сжимаемо. – curious1

+0

Это только потому, что у вас есть «margin-left: -150px' на' .holders'. Если вы уберете это, [это больше не работает] (https://jsfiddle.net/4tx5h1tq/37/) –

+0

Вся эта структура html отражает фактическую страницу. Я должен сосредоточить отзывчивый образ плюс текст. Спасибо за помощь! – curious1

1

Вы можете сделать то же самое с меньшим количеством кода.

HTML

<div class="center"> 
    <p class="text">This is text that should be in the center of the block vertically and horizontally</p> 
</div> 

CSS

.center { 
    -webkit-align-items: center; 
    -webkit-justify-content: center; 
    align-items: center; 
    background: url('http://www.fnordware.com/superpng/pnggrad8rgb.png'); 
    display: -webkit-flex; 
    display: flex; 
    height: 300px; 
    justify-content: center; 
    width: 300px; 
} 

LINK

https://jsfiddle.net/4tx5h1tq/42/

+0

Хенрик, спасибо за вшивание! – curious1

Смежные вопросы