float div中的垂直对齐和高度问题

时间:2014-01-05 18:47:53

标签: html css css3 height vertical-alignment

我们已经在Stackoverflow中回顾了有关垂直对齐和高度100%问题的问题,如:

https://stackoverflow.com/questions/6743208/css-why-vertical-align-middle-does-not-work

Fill height of a floating nested div

css div 100% height issue

更多类似的链接,但它们都没有帮助我们。


JSfiddle代码:http://jsfiddle.net/cD6nd/1/

HTML code:

<div id="header">
<div id="logo_container">
    <a href="main-catalog"><img src="img/logo-finder.png" alt="Logo Example"></a>
</div><!--Logo container-->
    <a href="main-catalog" id="aplication_lookup_button" class="button_link">Search by application</a>
    <a href="search-product-catalog" id="search_product_button" class="button_link">Search by part number</a>
</div><!-- Main header where logo and buttons are showed it -->

CSS代码:

#header
{
  width:100%;
  background: #000;
  float:left;
}

#logo_container
{
  width: 14.0056%;
  text-align: center;
  float: left;
}

#logo_container img
{
  max-width: 50%;
  height: auto;
}

.button_link{
  text-align: center;  
  color: white;
  text-decoration:none;
  text-transform: uppercase;
  width: 25%;
  height: 4.1em;
  float: left;
}

#aplication_lookup_button
{
  background: #424242;
  border-bottom: 4px solid #2eaeb8
}

#search_product_button:hover
{
  color: #2eaeb8;
  font-weight: bold;
}

以下是这一时刻的概述:

enter image description here


我们希望a-href文本位于容器的中间,我们希望使用height:100%而不是使用我们当前使用的em

2 个答案:

答案 0 :(得分:2)

你可以使用伪元素和内联块属性:

.button_link:before {
        content:'';
    display:inline-block;
    height:100%;
    vertical-align:middle;
    }

http://jsfiddle.net/cD6nd/5/

如果要在inline-block元素中包装文本并显示多行http://jsfiddle.net/cD6nd/6/

,此技术非常有用

答案 1 :(得分:1)

http://jsfiddle.net/cD6nd/2/

.button_link {
  line-height: 70px;
  width: 40%;
}

编辑:

添加宽度40%http://jsfiddle.net/cD6nd/4/