简体   繁体   English

DIV中的垂直和水平对齐

[英]Vertical & Horizontal align in a DIV

I've got a DIV with a BG image, and I want to center just 2 paragraphs both vertically and horizontally within that DIV. 我有一个带有BG图像的DIV,我想在该DIV中将垂直和水平两个段落居中。 Here's the code: 这是代码:

Code: 码:

 #dark-table-carol { background-color: #2e2e2e; width: 100%; padding-top: 15px; padding-bottom: 15px; padding-left: 40px; padding-right: 40px; text-align: left; margin-top: 30px; margin-bottom: 30px; background-image: url(http://scott.ewarena.com/blog/wp-content/themes/bootstrapstarter/carol-candy-carts-quote.jpg); height: 600px; background-repeat: no-repeat; background-position: center bottom; margin: auto; /* position: absolute; background-size:contain; top: 0; left: 0; bottom: 0; right: 0; */ } 
 <div id="dark-table-carol"> <h3 id="dark-table-head-style" align="center">"It was like Scott reached into my head and saw exactly what I wanted to achieve. He brought my ideas to life very quickly!</h3> <p id="dark-table-paragraph" align="center"> Carol Davies - Carol's Candy Carts </p> </div> 

I've tried a few things, obviously vertical-align:middle; 我已经尝试了几件事,显然是vertical-align:middle; , setting padding to 50% for top & bottom, but no luck. ,将顶部和底部的填充设置为50%,但没有运气。

Any advice would be great :) Thanks! 任何建议将是巨大的:)谢谢! Scott 斯科特

You can add the following two styles to your image container. 您可以将以下两种样式添加到图像容器。

display: table-cell;
vertical-align: middle;

https://jsfiddle.net/h3qh9pgu/ https://jsfiddle.net/h3qh9pgu/

I've been looking the whole day for a better solution as the one I gave you will not work for me. 我整天都在寻找更好的解决方案,因为我给您的解决方案对我来说不起作用。 Alas, I can't find answers. las,我找不到答案。 Hope this works for you. 希望这对您有用。 It worked in the fiddle. 它在小提琴中起作用。

I prefer to use top bottom left and right properties to center elements. 我更喜欢使用top bottom leftright属性中心元素。 Change these values to check out how it behaves. 更改这些值以检查其行为。 And remember to add margin and position properties as shown below. 并记住要添加marginposition属性,如下所示。

 #container{ position:relative; width:300px; height:300px; border:dotted 1px #33aaff; } #child{ width:50px; height:50px; background-color:#ff55aa; position: absolute; margin:auto; top:0; left:0; bottom:0; right:0; } 
 <div id="container"> <div id="child"></div> </div> 

 #dark-table-carol { display: table; background-color: #2e2e2e; width: 100%; padding-top: 15px; padding-bottom: 15px; padding-left: 40px; padding-right: 40px; text-align: left; margin-top: 30px; margin-bottom: 30px; background-image: url(http://scott.ewarena.com/blog/wp-content/themes/bootstrapstarter/carol-candy-carts-quote.jpg); /*background-size:contain; */ height: 600px; background-repeat: no-repeat; background-position: center bottom; margin: auto; /*position: absolute; top: 0; left: 0; bottom: 0; right: 0;*/ } .inner { display: table-cell; vertical-align: middle; margin-left: auto; margin-right: auto; } 
 <div id="dark-table-carol"> <div class="inner"> <h3 id="dark-table-head-style" align="center">"It was like Scott reached into my head and saw exactly what I wanted to achieve. He brought my ideas to life very quickly!</h3> <p id="dark-table-paragraph" align="center"> Carol Davies - Carol's Candy Carts </p> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM