简体   繁体   English

使用zurb基础时垂直对齐CSS 5

[英]Vertical align CSS when using zurb foundation 5

I'm trying to align a line of text in the middle of a header panel (a full width approx 250px high container), using CSS and the Zurb Foundation 5 framework, but can't get it to work for some reason. 我正在尝试使用CSS和Zurb Foundation 5框架在标题面板(一个宽约250px高的容器)中间对齐一行文本,但由于某些原因无法使其工作。

I've already tried each of the 6 vertical alignment methods summarised at this link http://www.vanseodesign.com/css/vertical-centering/ (line height method, css table method, absolute position and negative margin, absolute position and stretching, padding top and bottom, floating div). 我已经尝试过这个链接中总结的6种垂直对齐方法中的每一种http://www.vanseodesign.com/css/vertical-centering/ (线高法,css表法,绝对位置和负边距,绝对位置和拉伸,填充顶部和底部,浮动div)。 The best I've managed has either floated the text outside of the header panel and in the middle of the whole page or moved the text up or down a short distance (seemingly random) while distorting the height of the header panel. 我已经成功的最好已经或者漂浮的文字标题面板之外 ,整个页面的中间或移动文本向上或向下短距离(看似随机的),而扭曲梁板的高度。

It leads me to think there's something in the CSS of my header panel that is conflicting (or some conflict in the stock foundation framework) - but I can't work it out. 这让我觉得我的标题面板的CSS中存在一些冲突(或者股票基础框架中的一些冲突) - 但是我无法解决它。 Grateful for any pointers. 感谢任何指针。

Thanks! 谢谢!

The CSS for my header panel which comes immediately after the standard ZURB title bar (45px high - see padding), is as follows: 我的标题面板的CSS紧跟在标准ZURB标题栏之后(45px高 - 请参见填充),如下所示:

#headerpanel {
  background: linear-gradient(rgba(0, 0, 0, 0.3) , rgba(0,0,0,0.3)), url( ../../images/footerbg1.jpg);  
  background-size: cover;
  padding: 45px 0 250px;
  text-align: center;
  position: relative;
}

And my HTML layout is: 我的HTML布局是:

<section id="headerpanel">       

 <div class="row">
  <div class="large-12 columns">

    <div class="spanMiddle">
   <?php print @$this->heading; ?>
    </div>

  </div>
 </div>
</section>

I've then tried setting the params for the spanMiddle div in a number of ways - eg 然后我尝试以多种方式设置spanMiddle div的参数 - 例如

#spanMiddle {
  position: absolute;
    top: 50%;
    left: 50%;
    height: 30%;
    width: 50%;
    margin: -15% 0 0 -25%;
}

I've also tried it without the below part, and with dedicated p or span tags (set with .p {} or .spam {} immediately after the headerpanel css): 我也尝试过没有下面的部分,并使用专用的p或span标签(在headerpanel css之后立即设置.p {}或.spam {}):

 <div class="row">
  <div class="large-12 columns">
  </div>
 </div>

I've also tried: 我也尝试过:

.spanMiddle {
position: relative;
top: 50%;
transform: translateY(-50%);
} 

and

    margin-top:auto;
    margin-bottom:auto;

Where am I going wrong? 我哪里错了?

Try this CSS : 试试这个CSS:

#headerpanel {
    background: linear-gradient(rgba(0, 0, 0, 0.3) , rgba(0,0,0,0.3)), url( ../../images/footerbg1.jpg);  
    background-size: cover;
    text-align: center;
    position: relative;
    display: table;
    width: 100%;
    height : 250px; /* Determine the height of the box */
}

#headerpanel .row,
#headerpanel .columns{
    display: table;
    width: 100%;
    height: 100%;                       
}
#headerpanel .spanMiddle {
    display: table-cell;
    text-align: center;
    vertical-align: middle;                     
}

Here is the fiddle 这是小提琴

Hope it helps :) 希望能帮助到你 :)

If you can set the header height explicitly you can try out the vertical centering method # 3 from here http://css-tricks.com/centering-in-the-unknown/ 如果您可以明确设置标题高度,可以从这里尝试垂直居中方法#3 http://css-tricks.com/centering-in-the-unknown/

 #headerpanel {
 background: linear-gradient(rgba(0, 0, 0, 0.3) , rgba(0,0,0,0.3));
 background-size: cover;
 height: 250px;
 text-align: center;}

.row, .columns  {
height: 100%;}

.columns:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em;}

.spanMiddle {
display: inline-block;
vertical-align: middle;}

Here's a fiddle http://jsfiddle.net/j2e4sosx/1/ 这是一个小提琴http://jsfiddle.net/j2e4sosx/1/

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

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