简单的 CSS 定位(我认为)

3

我一直想用CSS定位来替换我的网站中的表格,并一直在通过教程等方式自学。我早些时候取得了一些成功,但当我尝试创建侧边栏时,一切都崩溃了。我希望这个问题有一些简单的解决方法。元素的相对/绝对定位与我想做的事情完全不符。我的目标是有一个侧边栏,其中的图像会从顶部到底部堆叠(漂浮?),中间的元素将成为无序列表的一部分。我曾经让它工作过一次,但现在它们堆叠在一起。这肯定是我设置漂浮和绝对/相对定位的方式有问题。在阅读了这里的一些文章后,我尝试添加一个div包装器来将它们放在里面,但我认为我更加困惑了。有可能有人能引导我走向正确的方向吗?以下是代码:

CSS

body   
{
    background: #b6b7bc;
    font-size: .80em;
    font-family: "Helvetica Neue", "Lucida Grande", "Segoe UI", Arial, Helvetica, Verdana, sans-serif;
    margin: 50px;
    padding: 0px;
    color: #696969;
    height: 160px;
}

a:link, a:visited
{
    color: #034af3;
}

a:hover
{
    color: #1d60ff;
    text-decoration: none;
}

a:active
{
    color: #034af3;
}

p
{
    margin-bottom: 10px;
    line-height: 1.6em;
}


/* HEADINGS   ----------------------------------------------------------*/

h1, h2, h3, h4, h5, h6
{
    font-size: 1.5em;
    color: #666666;
    font-variant: small-caps;
    text-transform: none;
    font-weight: 200;
    margin-bottom: 0px;
}

h1
{
    font-size: 1.6em;
    padding-bottom: 0px;
    margin-bottom: 0px;
}

h2
{
    font-size: 1.5em;
    font-weight: 600;
}

h3
{
    font-size: 1.2em;
}

h4
{
    font-size: 1.1em;
}

h5, h6
{
    font-size: 1em;
}



/* PRIMARY LAYOUT ELEMENTS  ---------------------------------------------------------*/

.page
{
    width: 960px;
    background-color: #fff;
    margin: 20px auto 0px auto;
    border: 1px solid #496077;
}

.header
{
    position: relative;
    margin: 0px;
    padding: 0px;
    background: #4b6c9e;
    width: 100%;
    top: 0px;
    left: 0px;
}

.header h1
{
    font-weight: 700;
    margin: 0px;
    padding: 0px 0px 0px 20px;
    color: #f9f9f9;
    border: none;
    line-height: 2em;
    font-size: 2em;
}

.main
{
    padding: 0px 12px;
    margin: 0px 4px 4px 4px;
    min-height: 420px;
    width: 500px;
    float: left;
}

.leftCol
{
    padding: 6px 0px;
    margin: 12px 8px 8px 8px;
    width: 200px;
    min-height: 200px;
}

.footer
{
    color: #4e5766;
    padding: 8px 0px 0px 0px;
    margin: 0px auto;
    text-align: center;
    line-height: normal;
}




/* MISC ----------------------------------------------------------*/

.clear
{
    clear: both;
    width: 936px;
    height: 35px;
}

.title
{
    display: block;
    float: left;
    text-align: justify;
}


.bold
{
    font-weight: bold;
}


p.clear
{
    clear: both;
    height: 0;
    margin: 0;
    padding: 0; 
}

#wrapper
{
    position:relative;
    height: 500px;
    width: 900px;
}

#insidemain
{
    position:absolute;
    float: left;    
    width: 500px;
    height 180px;
}


/* ---------------- Sidebar Items ---------------------*/


#sidebar   /* Sidebar container */
{
    position:absolute;
    border-top: 1px solid #99CC33;
    border-left: 1px solid #99CC33;
    height: 300px;
    width: 180px;
    margin-right: 5px;
    padding: 5px 0 0 5px;
}

#sidebarHeader
{
    position:absolute;
    height: 37px;
    width: 172px;
    float: left;
    background-image: url(../img/TopMenu.jpg);
    background-repeat:no-repeat;
}

#sidebarItems ul
{
    position:absolute;
    height: 27px;
    width: 172px;
    float:left;
    background-image: url(../img/MenuItems.jpg);
    background-repeat:no-repeat;
    /*left: 6px;
    top: 45px;*/
    background-position: 0px -27px;
}

#sidebarFooter
{
    position:absolute;
    height: 46px;
    width: 172px;
    float:left;
    background-image: url(../img/BottomMenu.jpg);
    background-repeat:no-repeat;
}

同时,还有 HTML

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <title></title>
        <link href="Styles/Simple.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <div class="page">
            <div class="header">header
                <div class="title">
                    <h1>
                        Test Page
                    </h1>
                </div>
            </div>
            <p class = "clear">clear</p>
            <div id="wrapper">
                <div id="sidebar">
                    <div id="sidebarHeader">
                    </div>
                    <div id="sidebarItems">
                        <ul>
                            <li>test item</li>
                        </ul>
                    </div>
                    <div id="sidebarFooter">
                    </div>
                </div>
            </div>
            <div id="insidemain">
                main
            </div>
        </div>
        <div class="clear">clear</div>
        <div class="footer">
            <a href="http://www.google.com/">
                Blah blah test to see how far this will go across the page blah blha lorem ipsum and various other stuff that is meaningless etc
            </a>
        </div>
    </body>
</html>

1
你能把你的代码放在jsfiddle上吗? - Wex
看起来很酷!我会试着玩一下。谢谢。 - Dana
3个回答

2
通常情况下(特别是对于非响应式网站),您需要将.wrapper div包围整个内容(标题、内容、侧边栏、页脚等)。然后设置.wrapper的宽度。您的.sidebar将具有设置的width,并且它将根据您想要的侧面float: left;float: right;。设置.content div的width,它应小于或等于.wrapper width - 您的.sidebar width。然后在下面添加您的.clearfix,使.footer落在所有内容下方。在大多数情况下(至少对于大页面块),您可以避免使用position:absolute;,这有助于使事物更容易地落入位。

明白了,谢谢。我也会尝试按照下面的建议将它们制作成类。我已经稍微阅读了一些关于类和div的文章,并会进一步尝试。目前有点令人沮丧! - Dana
是的,@Richard 也给出了很好的建议。除非您想要所有div元素的默认样式,否则请尝试使用类作为您的样式钩子。 - Scrimothy

1

你真的不需要浮动你的div或列表。它们默认是块级元素,无论如何都会垂直堆叠。

此外,正如Scrimothy所提到的,您不希望使用绝对定位元素,因为这将使元素脱离页面流。换句话说,它们不再在页面中占用“真实”的空间,而是在您定位它们的任意坐标处呈现。

同样,浮动也不占用空间,除了与其他浮动元素一起使用时。这就是为什么一些UI开发人员会在页面上浮动几乎每个元素并使用页脚或在页面的关键断点处“清除”它们。我个人不建议以这种方式进行定位,但各有所好。

看看这个快速教程是否能帮助您理解一些关键的定位概念:这里


那个快速教程帮了我很多。谢谢你。这些概念开始更加深入。我意识到的另一件事是,Dreamweaver中的预览窗格很方便,但显然不像Firefox这样的典型浏览器。所以现在回到沙盒,用一些简单的布局!再次感谢! - Dana

1
不要同时使用floatposition:absolute来定位同一个元素,这没有多大意义。任何地方都使用了float,你应该摆脱position:absolute
接下来,摆脱那些愚蠢的class="clear"元素。相反,使用clear:both来定位.footer,并使用overflow-y:hidden;来定位.page

我在Lynda.com上做了一个教程,当页面上的东西初始看起来很好时,我以为自己已经解决了所有问题。他非常注重使用p:clear元素来“清除浮动”。而clear:both似乎更合理,因为您将其放在下一个元素的样式中。 - Dana
问题并不在于您使用 clear:left 还是 clear:right - 仅为了 clear 属性而添加元素的想法很愚蠢。只要理解 CSS 选择器和属性,就不必采用像那样不寻常的技巧。 - Richard JP Le Guen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接