【问题标题】:How do I center my page while making my header take the full width of the browser window?如何在使标题占据浏览器窗口的整个宽度的同时使页面居中?
【发布时间】:2013-03-05 14:47:07
【问题描述】:

我在网络上看到了大量关于将内容集中在您的页面上是多么“简单”的演示。我想出了如何让我的标题占据浏览器窗口的整个宽度但是每当我尝试将我的内容居中时,我的所有浏览器都会扩展页面的宽度,所以我最终有一个水平滚动条。另一个问题是我的标题与浏览器的其余部分颜色不同,所以当我对正文进行更改时,有时它会更改标题的宽度,这样颜色就不会填满窗口。这个网站是我努力实现的一个很好的例子http://carlodiego.businesscatalyst.com/

我还希望我的标题有溢出:无;这样如果有人让浏览器窗口变小,标题就会开始消失。我已经为此工作了好几个小时,无论我做什么其他事情都会中断。下面是我的代码。抱歉,篇幅较长。

<body>
<header>
    <div id="wrapper">
        <div id="fixed">
            <div id="menu-container">
                <ul id="navigationMenu">
                    <li><a href="#" class="normalMenu">Home</a></li>
                    <li><a href="#" class="normalMenu">About</a></li>
                    <li><a href="#" class="normalMenu">Work</a></li>
                    <li><a href="#" class="normalMenu">Contact Me</a></li>
                </ul>
            </div>
            <div id="title">
            <h1>Alex</h1>
            <h1 id="last">Chaparro</h1>
            </div>
        </div>

</header>

        <div id="background">

        <div id="front-end">
           <h1>Front-End <br> Developer</h1>
        </div>
        <div id=content>

            <h1 class="heading">About</h1>
            <p>content</p>

            <h2>Currently Using</h2>
            <p>More Content...</p>

            <h2>Currently Following</h2>
            <p>Chris Mills | David Sawyer Mcfarland | Phil Thompson </p>

            <h1 class="heading">Work</h1>

        </div>
      </div>
</div>
</body>


body {
    margin: 0;
    padding:0;
    border: 0;
    font-size: 100%;
    line-height: 1.5;
    font-family: 'optimusprincepssemiboldRg';
    background: #fafafa;

    background-color: #eeeeee;
    }



article, aside, audio, canvas, datalist, details, figcaption, figure, footer, header, hgroup, menu, nav, section, video {
    display: block;
}

#title {
    font-family: 'optimusprincepssemiboldRg';
    color: black;
}

#last {
    font-size: 400%;
    line-height: 0;
    margin-top: 40px;
}

#menu-container {
    position: absolute;
    margin-left: 420px;
    white-space: nowrap; /* stops text from wrapping */
    overflow: hidden;
}

#background {
    background-color: #eeeeee;
    position: absolute;
    height: 100%;
    margin-top: 20px;
    margin-left: 20%;
    margin-right: 20%;
}

#front-end {
    margin-top: 100px;
    font-size: 200%;
    width: 100%;
    text-align: center;
}

.heading {
    font-size: 300%;
}

#fixed {
    position: relative;
    height: 200px;
}

header {
    width: 100%;
    overflow: hidden; /*This stops the color from going away*/
    background-color: #cee3ee;
}

#wrapper {
    margin-left: 20%;
    margin-right: 20%;
}

【问题讨论】:

  • “浏览器”我想你的意思是视口?浏览器是一个软件。不是您在网站上看到的东西。居中是指垂直居中还是水平居中?
  • 显示您的代码尝试,以便我们“修复”一些东西。我们不会从头开始编写页面。

标签: css header width center


【解决方案1】:

你想要一个 headermain div。并让他们absolute 定位。 并使用margin:0px auto; 使主体居中。

类似http://jsfiddle.net/WAR4r/

【讨论】:

  • 这让我朝着正确的方向前进。当您最小化页面时,顶部的颜色不会占据全角。如果我没有让我的问题更清楚,我很抱歉。我没有把我的代码放在那里,因为我认为它很长,下次会做。
  • 上面的颜色是什么意思?如果您的意思是标题,是的,无论页面大小如何,它都会占用整个宽度。
  • 实际上是 btevfik,如果您尝试编写的代码将 header css 更改为 header { text-align: right;并添加背景颜色。然后让窗口变小,出现一个水平滚动条,如果你向右移动它,你会看到颜色不再存在。我怎样才能避免这种情况?
  • 我没有看到任何问题。
  • 不,我去掉了边框,将 .main 设为 960px,并在页眉中添加了一些内容。如果您将浏览器窗口缩小一点,则会出现一个水平滚动条,如果您向右滚动,您会看到红色消失了,背景变成了白色。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-18
  • 2013-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-14
相关资源
最近更新 更多