【问题标题】:Is it possible to have a div bottom center aligned with the width?是否可以让 div 底部中心与宽度对齐?
【发布时间】:2015-06-19 09:13:19
【问题描述】:

是否有可能让 div 居中对齐(即)我总是需要将 div 放在底部中心,并在指定宽度内固定位置。我知道是左还是右?有什么办法吗?

【问题讨论】:

  • 试着看看这个非常相似的问题的答案:stackoverflow.com/questions/1042209/…
  • 另外,定义“底部” - 可视窗格的底部?紧接着所有内容?在可视窗格的底部,除非内容延伸到其下方?这个问题的中心部分很容易回答,但如果没有一些澄清,整个问题可能无法令人满意地回答。
  • 如果您的答案符合您的需求并且是正确的,请考虑通过单击帖子旁边的大勾号将其标记为已接受。
  • 这看起来与 stackoverflow.com/questions/971123 完全相同

标签: css


【解决方案1】:

我想这对你有帮助

css:

 #parent{
    position:fixed;
    bottom:0px;
    width:100%;   //width should be 100%
 } 
 #child{
    width:100px; //min width should give to center the div.
    margin:0px auto; //here it will make center 
 }    

HTML

<div id='parent'>
    <div id='child'>
      centered div
    </div>
</div>

【讨论】:

    【解决方案2】:
    .myDiv
    {
       position: fixed;
       bottom: 0px;
       width: 200px;
       margin-left: auto;
       margin-right: auto;
    }
    

    【讨论】:

    • Ivan,可能需要解释一下宽度元素是margin-left:auto; margin-right:auto; 位工作所必需的。另外,我认为你不需要position:fixed
    • 是的,宽度是必需的,否则 div 将有 100% 的宽度,并且左右边距没有区别 :) 位置:固定,因为 Webdevelopertut 希望 div 位于底部中心.我不确定底部:0px;无需任何定位即可工作。
    • margin-left:auto 和 margin-right:auto 不适用于绝对位置和固定位置。
    【解决方案3】:

    其他答案对我不起作用,但这是对 Shiva 答案的轻微修改。有一个父子 div 组合,其中一个抓住页面底部,另一个抓住中心 像这样

    .bottomOfThePage{
        position:fixed;
        bottom:0px;
        width:100%;
    }
    .centerOfThePage{
        text-align: center;
        //other text stuff like color
     }
    

    html

    <div class="bottomOfThePage">
        <div class="centerOfThePage">
            <p>footerStuffHere</p>
        </div>
    </div>
    

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 2014-10-22
      • 2012-07-07
      • 2018-12-17
      • 2017-02-16
      • 2014-11-18
      • 2014-08-04
      • 2022-01-15
      • 2019-04-09
      相关资源
      最近更新 更多