【问题标题】:Centering Div box in Div Box在 Div Box 中居中 Div 框
【发布时间】:2012-11-15 23:36:13
【问题描述】:

我已将页脚添加到EasyCareBath.com,我可以让 div 正常工作,除了“.footer_box”,它不会居中。我添加了“左:15%”,当你把窗户炸开时,它不再对齐。是我遗漏了什么还是我对 css 做了太多?

.footer_spacer {
    height: 50px;   
}
.footer_wrapper {
    height: 375px;
    background-color: #e6dccb;
}
.footer {
    border-top: 10px solid #789E65;
    box-shadow: 0 5px 20px rgba(0, 0, 0, 0.6);
    margin-top: 0px;
    /*min-width: 1007px;*/
    height: 235px;
    background-attachment: scroll;
    background-image: url(../images_footer/footer_bg.png);
    background-repeat: repeat-x;
    background-position: center top;
    position: relative;
}
.footer_box {
    height: 236px;
    width: 1007px;
    /*position: relative;*/
}
.footer_copyright {
    width: 100%;
    /* [disabled]height: 100px; */
    /*float: left;*/
    padding-top: 15px;
    position: relative;
    text-align: center;
    word-spacing: normal;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 14px;
    font-weight: 100;
    font-variant: normal;
    text-transform: none;
    color: #354245;
}
#footer_col_01 {
    position:absolute;
    left:11px;
    top:3px;
    width:164px;
    /*height:230px;*/
    z-index:1;
}
#footer_col_02 {
    position:absolute;
    left:186px;
    top:3px;
    width:164px;
    /*height:230px;*/
    z-index:1;
}
#footer_col_03 {
    position:absolute;
    left:361px;
    top:3px;
    width:164px;
    /*height:230px;*/
    z-index:1;
}
#footer_col_04 {
    position:absolute;
    left:535px;
    top:3px;
    width:183px;
    /*height:230px;*/
    z-index:1;
}
#footer_col_05 {
    position:absolute;
    left:728px;
    top:3px;
    width:265px;
    /*height:230px;*/
    z-index:1;
    font-size: 14px;
}
footer {
    font-family: Arial, Helvetica, sans-serif;
    color: #fff;
    font-size: 11px;
    float: left;
    width: 100%;
}
footer a {
    color: #FFF;
    text-decoration: none;
}
footer a:hover {
    color: #FFF;
    text-decoration: underline;
}
footer h6 {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 16px;
    font-weight: 500;
    color: #00A1CC;
    line-height: 46px;
    /*margin-bottom: -2px;*/
}
footer li {
    line-height: 18px;
    list-style-type: none;
    margin-left: 12px;
}
.footer_img_center {
    text-align: center;
}

【问题讨论】:

  • 将来,如果您将 CSS 放入 jsfiddle 中的标记中 jsfiddle.net,通常可以获得更快的帮助。
  • 我不确定为什么这被否决了 - 似乎是一个合理的问题。也许是因为您包含了即将过时的实际链接?无论哪种方式,似乎都是一个有效的问题,所以我会投票反对大规模下降。 ://

标签: css html alignment center


【解决方案1】:

将您的 .footer_box 规则更改为:

.footer_box {
    height: 236px;
    width: 1007px;
    margin: 0 auto;
    position: relative;
    }

margin: 0 auto; 将 footer_box 居中。

position: relative; 使您的绝对定位列基于footer_box 而不是正文定位。

旁注/我一直喜欢的类比: 将“auto”想象成将橡皮筋连接到连接到页面边缘的项目的那一侧......在这种情况下,“auto”在右侧和左侧,所以 - 如果有橡皮筋在拉无论屏幕大小如何,它都将始终居中。

【讨论】:

  • 你太棒了!它工作得很好!我已经看到了类似的答案,但他们没有在“保证金”中包含“位置:相对”,再次感谢!
  • 这个答案是正确的 - 只是为了添加它,从技术上讲,0 之后不需要“px” - 因为 0 是 0。不需要使用任何单位并且看起来更干净。跨度>
  • @Andy - 是的 - 'px' 不是必需的 - 只是我从未改过的一个习惯 - 我仍然喜欢 'px' 的外观,但我可能应该改掉。 :)(我会从答案中删除它)谢谢。
  • @dgPehrson - 可能position:relative; 从未出现的原因是通常人们不会绝对定位列 - 这通常不是好的做法,应该使用float:left; 来代替。
  • @Andy - 我个人认为 0px 看起来更干净,并且在您调整样式时可以防止错误。每次更改尺寸时,它还可以避免添加/删除“px”。
【解决方案2】:

修复你的css如下:

.footer_box {
    height: 236px;
    margin: 0 auto;
    position: relative;
    width: 1007px;
}

【讨论】:

    【解决方案3】:

    你是在正确的轨道上:

    .footer_box {
        height: 236px;
        width: 1007px;
        position: relative;
        margin: 0px auto;
    }
    

    左右的自动边距将确保页脚始终居中,无论屏幕大小如何。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-21
      • 2015-04-18
      • 2016-02-29
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多