2012-09-12 35 views
3

我一直想用CSS定位替换我的网站中的表格,并一直试图通过教程等自学。我已经有一些早期的成功,但它都崩溃了我试图创建一个侧边栏。我希望问题有一些简单的解决方案。元素的相对/绝对定位不会接近我想要做的。我的目标是创建一个带有从上到下堆叠(浮动?)图像的侧栏,中间元素是无序列表的一部分。我有一次工作,但现在堆叠在一起。它必须是我设置浮动和绝对/相对定位的方式。在阅读了一些文章之后,我尝试添加一个div封装器,将它们放在里面,但我觉得自己变得更加困惑。是否有人可以推动我朝着正确的方向发展?下面是代码:简单的css定位(我认为)

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> 
+0

浮动侧边栏左边 –

+1

难道你把你的代码上[的jsfiddle(http://jsfiddle.net/)? – Wex

+0

这看起来很酷!我会尽力解决这个问题。谢谢 – Dana

回答

1

你真的不应该浮动你的div或列表。这些是默认的块元素,并且将垂直堆叠。

另外,正如Scrimothy提到的,您不需要绝对定位的元素,因为这会将元素从页面流中移出。换句话说,它们不再占用页面中的“真实”空间,而是在您定位它们的任何坐标处渲染。

同样,浮动也不占用空间,除了其他浮动元素。这就是为什么一些UI开发人员几乎可以浮动页面上的所有元素,并使用页脚或页面中的关键点“清除”它们。我个人不建议以这种方式来定位,而是要以他自己的方式。

看看这个快速教程,帮助您进行一些关键的概念定位:HERE

+0

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

2

通常(非敏感部位尤其是),你必须在你的.wrapper DIV整个内容(标题,内容,侧栏,页脚等)。然后设置您的.wrapper的宽度。您的.sidebar将有一组width,它可以是float: left;float: right;,具体取决于您想要的一面。设置您的.content div的width这将小于或等于您的.wrapper width - 您的.sidebar width。然后在下面加上你的.clearfix,这样.footer就落在了下面。在大多数情况下(至少对于大页面块),您可以避免position:absolute;,这有助于使事情更容易落实到位。

+0

有道理,谢谢。我也会尝试按照下面的建议让他们上课。我读了一些关于课程的文章与divs并且会更加混乱。这是令人沮丧的时刻! – Dana

+0

是的,@理查德也给出了很好的建议。尝试为样式化钩子使用类,除非您想要所有'div'元素的默认样式。 – Scrimothy

1

不要在定位既floatposition:absolute相同的元素。这没有多大意义。任何地方你都有float,你应该摆脱position:absolute

接下来,摆脱那些愚蠢的class="clear"元素。相反,目标.footerclear:both.pageoverflow-y:hidden;

+0

我在Lynda.com做了一个教程,当页面上的内容最初看起来不错时,我想我已经明白了。他在“清理浮法”这个明确要素上表现出色。清楚:因为您拥有下一个元素的风格,所以两者似乎都会更有意义。 – Dana

+0

问题不在于你使用'clear:left'还是'clear:right' - 为'clear'属性添加一个元素的想法是愚蠢的。只要理解CSS选择器并理解CSS属性,就不必诉诸于这种不寻常的黑客行为。 –