【问题标题】:How do I automatically center a fixed element?如何自动居中固定元素?
【发布时间】:2015-08-18 01:37:58
【问题描述】:

我目前正在尝试将标题菜单固定在屏幕顶部,即使在滚动时也是如此。问题是,每当我将位置设置为固定时,它就会失去居中,并且边距对其没有影响。我希望能够使元素居中,同时在调整页面大小或以较小的分辨率查看时仍使其能够自动居中。

有问题的 HTML 和 CSS:

*{
    margin: 0;
    padding: 0;
}
h1{
    font: bold 20px Tahoma
}
h2{
    font: bold 20px Tahoma
}
header, section, footer, aside, nav, article {
    display: block;
}
html{
    height: 100%;
    background: rgb(143, 143, 143);
}
body{
    width: 100%;
    display: flex;
    justify-content: center;
}
#big_wrapper{
    margin: 0 0;
    display: flex;
    flex-direction: column;
    flex: 100%;

}
#top_header{
    position: fixed;
    text-align: center;
    height: 120px;
    background: rgb(45,45,45);
    /*border-bottom: 15px solid rgba(79, 79, 79, 0.4);*/
    border-radius: 8px;
    padding: 10px;
    /*box-shadow: rgb(30, 30, 30) 10px 10px 10px;*/
    font-family: Impact, sans-serif;
    font-size: 40px;
    color: white;
}
#centering_example{
    color: #a00d01;
    font-size: 45px;
    text-shadow: rgba(0,0,0,.5) -3px 3px 0;
}
#new_div{
    display: flex;
    flex-direction: row;
}
#main_section{
    position: relative;
    z-index: -1;
    top: 140px;
    max-width: 1200px;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    flex: 1;
    margin: auto;
    margin-top: 20px;
    padding: 20px;
    background: #3c3c3c;
    box-shadow: rgb(30, 30, 30) 10px 10px 10px;
}
#the_footer{
    position: relative;
    top: 140px;
    max-width: 1200px;
    margin: auto;
    text-align: center;
    padding: 20px;
    border-bottom-left-radius: 10px;
    border-bottom-right-radius: 10px;
    border-top: 1px solid black;
    background: #3c3c3c;
    box-shadow: rgb(30, 30, 30) 10px 10px 10px;
}
#SELECTED{
    display: inline-block;
    margin: 6px;
    margin-left: 5px;
    margin-right: 5px;
    border: 1px solid black;
    border-top: 1px solid #000000;
    border-top-color: rgba(0, 0, 0, 0.7);
    background: rgba(36,34,31, 0.6);
    box-shadow: rgba(0,0,0,1) 0 0 0;;
    padding: 9px 18px;
    border-radius: 8px;
    color: #f8f8f8;
    font-size: 16px;
    text-decoration: none;
    vertical-align: middle;
}
.media_button{
    display: inline-block;
    margin: 6px;
    margin-left: 5px;
    margin-right: 5px;
    border: 1px solid black;
    border-top: 1px solid #000000;
    background: rgba(69, 69, 69, 0.8);
    padding: 3px 8px;
    border-radius: 8px;
    box-shadow: rgba(0,0,0,1) 3px 3px 0;
    color: #787878;
    font-size: 13px;
    text-decoration: none;
    vertical-align: middle;
}
.media_button:hover{
    background: rgba(59,59,59, 0.6);
    box-shadow: rgba(0,0,0,1) 2px 2px 0;
    color: #f8f8f8;
}
.media_button:active {
    border-top-color: rgba(0, 0, 0, 0.7);
    background: rgba(36, 34, 31, 0.6);
    box-shadow: rgba(0, 0, 0, 1) 0 0 0;
}
.button{
    display: inline-block;
    margin: 6px;
    margin-left: 5px;
    margin-right: 5px;
    border: 1px solid black;
    border-top: 1px solid #000000;
    background: rgba(69, 69, 69, 0.8);
    padding: 9px 18px;
    border-radius: 8px;
    box-shadow: rgba(0,0,0,1) 3px 3px 0;
    color: #787878;
    font-size: 16px;
    text-decoration: none;
    vertical-align: middle;
}
.button:hover{
    background: rgba(59,59,59, 0.6);
    box-shadow: rgba(0,0,0,1) 2px 2px 0;
    color: #f8f8f8;
}
.button:active{
    border-top-color: rgba(0, 0, 0, 0.7);
    background: rgba(36,34,31, 0.6);
    box-shadow: rgba(0,0,0,1) 0 0 0;
}
<div id="big_wrapper">
    <header id="top_header">
        <p id="centering_example">centering help</p>
        <a id="SELECTED" class="button" href="index.html">Home</a>
        <a class="button">Games</a>
        <a class="button">About us</a>
        <a class="button">Blog</a>

    </header>

    <div id="new_div">

        <section id="main_section">
            <article>
                <p>This is text for the index page of the website.</p>
                <br>
            </article>

        </section>

    </div>

    <footer id="the_footer">
        <h6>Footer text</h6>
        <a class="media_button" href="https://www.twitter.com">Twitter</a>
        <a class="media_button" href="https://www.facebook.com">Facebook</a>
        <a class="media_button" href="http://steamcommunity.com">Steam Group</a>
    </footer>
</div>

【问题讨论】:

    标签: html css centering


    【解决方案1】:

    尝试给它一个left:50%; 和一个减半宽度的左边距。

    left: 50%;
    margin-left: -200px;
    

    更新的小提琴:

    http://jsfiddle.net/x3vh99vg/1/

    不使用硬编码边距的更好方法(zgood 在下面的评论中建议)是将 x 轴上的元素平移 50%;

    transform:translateX(-50%);
    

    小提琴:

    http://jsfiddle.net/x3vh99vg/2/

    【讨论】:

    • 最好使用transformX(-50%) 而不是硬编码的负边距。
    • @zgood 也许是个愚蠢的问题,但我该如何实现呢?
    • @HaukurHaf 这里是更新后的fiddle。该属性是transform:translateX(-50%); 而不是transformX(-50%)
    • @CloudTwoNJ 看我上面的评论
    • 哇,我以为我自己试过了。一定是那里有错字。这很好用:-)
    【解决方案2】:

    你需要添加:

    #top_header {
        top: 0;
        left: 0;
        right: 0;
        width: YOUR WIDTH;
        margin: 0 auto;
    }
    

    http://jsfiddle.net/5gLz8zur/

    【讨论】:

    • 固定宽度可能是个坏主意
    • 如何在原始问题之外添加 html?他没有说任何关于纯 CSS 解决方案的内容......这个问题被标记为 html 和 css
    • 对不起,我不是故意的。您只是在做出假设(只有 css 可以更改,并且必须 100% 向后兼容),从而给出低于标准的答案。这是一个帮助人们不传播不良建议的网站
    • 仅供参考:display: flex; 是 css3,在他的示例中使用。
    • @SeanStopnik 我不打算评论只是为了增加这种近乎毫无意义的火焰,但我觉得我应该为上述声明辩护。上面提到的所有原因是因为这是我从我的主项目文件中编辑的,而不是项目本身。中断标记用于测试固定位置,缺少 href 标记是由于我删除了文件引用,ID 的数量是由于我使用了许多不同的样式,并非所有样式都存在于此。这是一个帮助他人编写代码的地方,而不是发起一场激烈的战争。
    【解决方案3】:

    您可以将 header 内容包装在一个 div 中,然后为您的 header 赋予 display:flexposition:fixed 的样式。

    看到这个fiddle

    HTML:

    <header>
        <div id="top_header">
            <p id="centering_example">centering help</p>
            <a id="SELECTED" class="button" href="index.html">Home</a>
            <a class="button">Games</a>
            <a class="button">About us</a>
            <a class="button">Blog</a>
        </div>
    </header>
    

    CSS:

    header{
        position: fixed;
        width:100%;
        display: flex;
        justify-content: center;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-12
      • 2011-01-01
      • 1970-01-01
      • 2021-11-12
      • 2021-03-14
      • 2013-03-21
      • 2021-12-25
      相关资源
      最近更新 更多