-1

I am extremely new to programming and I am attempting to code a website currently. I've spent about 5 hours in the last 2 days just trying to get things to line up correctly on various browsers and I'm having a few issues. First, let me say that I'm sorry if this question has been answered somewhere before. I've looked at a lot of solutions to problems similar to mine and nothing has completely worked for me, so please be patient. First, I have the problem of things appearing in random places on different browsers. When I attempt to remedy this in the CSS, I spawn new problems in other browers. Secondly, as the browser window changes size, all the elements on the page start moving around and overlapping instead of just hiding outside of the window. My third issue I'm having (sort of) is that my code looks terrible. I'm not sure why some elements have to have -4.5% margins to line up with other elements that need -5% margins; and I'm not sure why I'm having to put negative margins on certain elements to begin with! Thank you in advance for all of your help and preventing me from going postal.

Josh

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <title>Horton Computer Solutions Home</title>
    <link href="css/styles.css" media="screen" rel="stylesheet" type="text/css" />
</head>

<body>
    <div id="page">
        <div id="site_title", style="float:left">

  <span class="titleleft">Horton</span>|<span class="titleright">Computer Solutions</span>
</div>
            <div id="title_bar", style="float:right">
                <ul>
                    <pre><a href="#">Home</a>    <a href="#">About Us</a>    <a href="#">Services</a>    <a href="#">Guarantee</a>    <a href="#">Blog</a>    <a href="#">Contact</a></pre>
                </ul>
                <br style="clear:both"/>
            </div>
<div id="mainContent">
    <div id="subContent1">
    </div>
    <div id="subContent2">
    </div>
    <div id="subContent3">
    </div>
</div>
</div>
</body>
</html>

And the CSS

/* language: css */
BODY
{
    font-family: Calibri, Arial, Helvetica, "Helvetica Neue", "Times New Roman";
    font-size: 16px;
    color: #FFF;
    text-align: left;
    background-color: #062134;
    margin: auto;
    padding: 20px 0 60px;
    width: 100%;
    height: 100%;
    position: absolute;
    z-index: 1;
}
A
{
    font-family: Helvetica;
}
DIV#title_bar UL A
{
    text-decoration: none;
    color: #FFFFFF;
}
DIV#title_bar UL A:hover
{
    text-decoration: underline;
    color: #FFFFFF;
}
DIV#title_bar UL A:visited
{
    text-decoration: none;
    color: #FFFFFF;
}
DIV#site_title
{
    font-family: Helvetica;
    font-weight: bold;
    font-size: 27px;
    color: #FFFFFF;
    margin: -4.5% 0 0 6.9%;
}
DIV#title_bar
{
    font-family: Helvetica;
    font-size: 16px;
    color: #FFFFFF;
    list-style: none;
    margin: -5% auto auto 40%;
    text-align: center;
    position: absolute;
}
DIV#contentBox
{
    background-color: #1468A8;
    border-style: none;
    width: 338px;
    height: 185px;
    padding: 19px;
}
#site_title .titleleft
{
    color: #FFFFFF;
}
#site_title .titleright
{
    color: #46C8FF;
}
DIV#mainContent
{
    border: 1px solid #FFFFFF;
    /*+box-sizing:border-box;*/
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    -ms-box-sizing: border-box;
    box-sizing: border-box;
    width: 950px;
    height: 1135px;
    background-color: #FFFFFF;
    margin: 10% 6%;
    padding: 15px;
}
DIV#subContent1
{
    color: #FFFFFF;
    width: 292px;
    height: 255px;
    background-color: #000000;
    margin: 440px 0 0;
}
DIV#subContent2
{
    color: #FFFFFF;
    width: 292px;
    height: 255px;
    background-color: #000000;
    margin: -255px 0 0 313px;
}
DIV#subContent3
{
    color: #FFFFFF;
    width: 292px;
    height: 255px;
    background-color: #000000;
    margin: -255px 0 0 626px;
}
#site_title .floatleft
{
    float: left;
    position: relative;
    left: 0px;
    top: 0px;
}
#title_bar .floatright
{
    float: right;
    position: relative;
    left: 0px;
    top: 0px;
}
4

5 回答 5

2

您的代码语义无处不在,遇到问题并不奇怪,在尝试解决任何跨浏览器问题之前,您必须确保您的代码经过验证 (http://validator.w3.org/)

首先,我会说放弃复杂的文档类型并使用 HTML5,它很严格并且适用于任何地方。如果您能提供帮助,请始终远离过渡文档类型。

所以

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

变成

<!DOCTYPE html>
<html>

在元素上设置多个属性时,您不需要逗号,所以

<div id="title_bar", style="float:right">

变成

<div id="title_bar" style="float:right">

避免样式属性,这里大多数元素都有 id,您可以从样式表中设置它们的样式(http://robertnyman.com/2008/11/20/why-inline-css-and-javascript-code-is-such-坏事/)

90% 的时间你不需要 clear:both,只要在容器元素上有 overflow:hidden 就足以清除浮动的子元素。

UL 元素必须包含 LI 元素

<ul>
    <pre><a href="#">Home</a>....</pre>
</ul>

变成

<ul>
<li><a href=""></a></li>
<li><a href=""></a></li>
</ul>

为 CSS 插入链接标签时,除非您为各种设备提供多个文件,否则您不需要 media="screen" 部分,如果未设置该属性,则这是默认值。

当在你的 CSS 文件中引用标签时不使用大写,这是相当老的时尚并且没有真正使用(也有些人可能会争辩说他们更喜欢它,你不会看到任何“著名”开发人员使用它们)

现在对于您的样式问题,您的 BR style="clear:both" 位于菜单元素内,应该在它之后以清除标题和菜单(也就像说只是在它们周围有一个包装器并溢出:隐藏在 CSS 中,它还会更好地将您的标题与您的内容分开)

float:left + position:relative 的冲突。此外,如果您设置 position:relative 则无需设置 top:0 和 left:0 这就是元素处于 position:relative 情况的位置。

我认为这至少应该让你朝着正确的方向前进,或者以某种方式帮助你:)

于 2012-04-12T19:04:48.060 回答
1

评论喜欢

<!-- language: css -->

不允许在 css 文件中使用。只使用

/* language: css */

评论。

于 2012-04-12T18:43:26.973 回答
1

如果您希望您的代码在所有浏览器上都一样工作。废弃您的样式并从重置样式表开始:(谷歌:重置样式表,第一个链接)。

格式很可能在所有浏览器上都是相同的(除了个别浏览器的限制,即:ie6、7 等上没有 css3)。我认为一旦消除了浏览器样式差异,您会发现格式化更容易。

于 2012-04-12T19:52:57.550 回答
0

你的错误在这里,删除 id end 后的逗号

    <div id="title_bar" style="float:right">

检查这个http://jsfiddle.net/srinivasan/aXaYg/2/

于 2012-04-12T20:27:17.400 回答
0

你的第二个问题的解决方案

  <body>
   <center>
     //do all here
  </center>
 </body>
于 2012-04-12T19:40:04.973 回答