【问题标题】:How to Vertically center images and text in CSS如何在 CSS 中垂直居中图像和文本
【发布时间】:2014-03-19 17:34:35
【问题描述】:

我在这里有这个小提琴,我想垂直居中标题中的文本、页脚中的图片和贴在标题上的中间图片...我不知道如何实现这一点并且我已经尝试了一些东西,但无济于事。我希望在这里得到您的帮助(=

jsfiddle 链接:http://jsfiddle.net/xF6xV/2/

HTML:

    <body>      
    <div id="header">        
            <h1>Test Text but it's not inside the actual div...</h1>
    </div>
    <div id="content">
        <div id="topcontent">
            <img src="http://www.curiousfix.com/wp-content/uploads/2013/06/only_god_forgives_xxlg-2000x2964.jpg" id="left" >                    
            <img src="http://www.curiousfix.com/wp-content/uploads/2013/06/only_god_forgives_xxlg-2000x2964.jpg" id="right"/>   
            <img src="http://typographica.org/wp-content/uploads/2009/04/Lakside-styles-315x165.png" id="mid" />
        </div>
        </br>
        <div id="botcontent">
            <div class="slidercontainer">                      
                <div id="area">
                    <div id="knob"></div>

                </div>
            </div>
        </div>
    </div>
    <div id="footer">
        <img src="http://www.duv-ev.de/images/Leiste_Impressum_511x127_rgb-srgb_72.jpg"/>
    </div>

CSS:

body,
html {
    height: 98%;
}
#header {
    height: 15.69%;
    background-color: gray;
    line-height:  15.69%;
}

#header h1{
    text-align: center;
    color: white;    
    vertical-align: middle;
}

#content{
    height: 71.16%;
    left:0;
    margin-left: 15%;
    margin-right: 15%;
    right:0;       
}
#topcontent{
    height:56.245%; 
}

.slidercontainer{
    width: 100%;
}
#botcontent{
    height: 43.755%;
}

img {
    max-height:100%;
}

#footer{
    text-align: center;
    background-color: gray;
    height: 13.15%;
}


#left{
    float:left;
    margin-left: 10%;
    max-width: 30%;
}

#right{
    float: right;
    max-width: 30%;
    margin-right:10%;
}
#mid{
    display: block;
    margin-left: auto;
    margin-right: auto;
    max-width: 10%;
}

#area {
    background: gray;
    height:6em;
    width: 100%;  
    border-radius: 1em;
}

#knob {      
    height: 6em;
    width: 6em;
    background: orange;
    border-radius: 1em;
}

【问题讨论】:

标签: css responsive-design vertical-alignment


【解决方案1】:

给你。

WORKING DEMO

CSS 变化:

#header h1 {
    color: #FFFFFF;
    display: inline-block;
    text-align: center;
    vertical-align: middle;
    width: 100%;
}

希望这会有所帮助。

【讨论】:

  • 好的,这会将文本放在 div 中,但如何垂直居中?
【解决方案2】:

我个人根据用途使用3种方法。

FIDDLE

1) 翻译()

2) 行高

3) 垂直对齐

<div id="div1">
    <img src="http://www.curiousfix.com/wp-content/uploads/2013/06/only_god_forgives_xxlg-2000x2964.jpg"/>
</div>
<div id="div2">
    <h1>Hello</h1>
</div>
<div id="div3">
          <img src="http://www.curiousfix.com/wp-content/uploads/2013/06/only_god_forgives_xxlg-2000x2964.jpg"/>
</div>

#div1, #div2, #div3 {
    display:block;
    margin:20px auto;
    width:400px;
    height:200px;
    background-color:#ccc;
    text-align:center;
    position:relative;
}
#div1 img {
    height:100px;
    position:absolute;
    top:50%;
    left:50%;
    -webkit-transform:translateY(-50%) translateX(-50%);
}
#div2 h1 {
    line-height:200px;
}
#div3 {
    display:table-cell;
    vertical-align:middle;
}
#div3 img {
    height:100px;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-25
    • 2010-11-01
    • 2014-10-22
    • 1970-01-01
    • 2018-05-26
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多