html - 简单的 css 定位(我认为)

标签 html css

我一直想用 css 定位替换我网站中的表格,并一直试图通过教程等自学。我取得了一些早期的成功,但当我尝试创建侧边栏时,一切都崩溃了。我希望这个问题有某种简单的解决方案。元素的相对/绝对定位并没有接近我想要做的。我的目标是让侧边栏的图像从上到下堆叠( float ?),中间元素是无序列表的一部分。我让它工作了一次,但现在它们堆叠在一起。它必须是我设置 float 和绝对/相对定位的方式。在这里阅读了一些文章后,我尝试添加一个 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>

最佳答案

通常(尤其是对于非响应式网站),您的 .wrapper div 将包含整个内容(页眉、内容、侧边栏、页脚等)。然后设置您的 .wrapper 的宽度。您的 .sidebar 将具有一组 width 并且它会 float: left;float: right; 取决于在你想要的那一边。设置您的 .content div 的 width 小于或等于您的 .wrapper 宽度 - 您的 .sidebar 宽度。然后在下面添加您的 .clearfix,以便 .footer 位于所有内容之下。在大多数情况下(至少对于大页面 block )您可以避免 position:absolute; 这有助于使事情更容易落实到位。

关于html - 简单的 css 定位(我认为),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12394793/

相关文章:

css - 使用 CSS 在鼠标悬停在子菜单上时保持顶级导航悬停效果

html - 导航确实在画廊 : How can i fix this? 下消失

html - 在调整窗口大小时调整 div 大小

c# - 2 "Dynamic"div 之间的 "Static"div [CSS][ASP.NET]

javascript - 无法更改 jquery 对象 css

html - 菜单未显示所有元素

css - 如何让 flex 元素扩展以适应居中的 flex 盒,直到达到一定的最大宽度?

html - 如何在html中显示Json数据到对话框

html - Safari/Firefox 中不同的 H1 css 高度

html - html 旁边不需要的白色区域