2015-09-28 3 views
0

Я центрировал div вертикально с помощью CSS, и он отлично работает. Но у меня есть фон, и когда я изменяю размер браузера, текст выходит из фона.Вертикально центрированный div выходит из фона

enter image description here

.wrapper{ 
    height: 100%; 
    width: 100%;  
    background: rgba(51,51,51,.5); 
} 


.outer { 
    display: table; 
    position: absolute; 
    height: 100%; 
    width: 100%; 
} 

.middle { 
    display: table-cell; 
    vertical-align: middle; 
} 

.inner { 

    margin-left: auto; 
    margin-right: auto; 
    width: 50%; 
} 

jsfiddle

ответ

1

.wrapper является div, поэтому он уже имеет width: 100% (потому что ДИВ имеет display: block, и мы знаем, что все блоки полная ширина элементов). .wrapper { height: 100%; } означает, что он наследует всю высоту своего родителя (в этом случае это body, body наследует высоту своего родителя - html - так что вы получаете высоту видового экрана); Таким образом, мы можем удалить все стили .wrapper. И просто добавить background: rgba(51,51,51,.5); к .outer

html, 
 
body { 
 
    width: 100%; 
 
    height: 100%; 
 
    padding: 0; 
 
    margin: 0; 
 
} 
 

 
.wrapper { 
 

 
} 
 

 
.outer { 
 
    display: table; 
 
    position: absolute; 
 
    height: 100%; 
 
    width: 100%; 
 
    background: rgba(51,51,51,.5); 
 
} 
 

 
.middle { 
 
    display: table-cell; 
 
    vertical-align: middle; 
 
} 
 

 
.inner { 
 
    margin: 0 auto; 
 
    width: 50%; 
 
}
<div class="wrapper"> 
 
    <div class="outer"> 
 
     <div class="middle"> 
 
      <div class="inner"> 
 
      
 
       <h1>The Content</h1> 
 
       
 
       <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p> 
 
      
 
      </div> 
 
     </div> 
 
    </div> 
 
</div>

0

попробовать это https://jsfiddle.net/serGlazkov/5t9asdc1/3/ вы должны использовать min-height и удалять здесь position: absolute;

.outer { 
    display: table; 
    /*position: absolute;*/ 
    height: 100%; 
    width: 100%; 
} 
+0

'//', вероятно, не следует использовать для демонстрации, так как это не 'копируемыми' Используйте '/ ** /' вместо этого. – jbutler483

0

Jsfiddle Here

Не нужно использовать position:absolute в .outer и укажите высоту .wrapper.

.wrapper{ 
/* height:100%; remove this-**/ 
width: 100%;  
background: rgba(51,51,51,.5); 
} 
.outer { 
display: table; 
/* position: absolute; remove this-**/ 
min-height: 100%; 
width: 100%; 
} 
Смежные вопросы