Html CSS中浮动/位置的困难

Html CSS中浮动/位置的困难,html,css,Html,Css,我正在制作一个布局模板。我希望它的工作方式是基本上有一个2列布局-第一列只是一个红色的酒吧(用于菜单),约50像素宽。第二列应该填充画布的其余部分(我限制为950px),并且应该分为3个部分:页眉、页脚和内容区域 在大多数情况下,我的布局工作,但我有几个问题。首先,红色竖条并没有完全填满整个画布(我将其包装在一个id为“pgcontainer”的节标记中)。它缺少大约10像素 第二个问题是,包含页眉、页脚和内容区域的第二个“列”没有浮动到红色垂直菜单栏的左侧,而是占据整个画布的宽度。我试着使用一

我正在制作一个布局模板。我希望它的工作方式是基本上有一个2列布局-第一列只是一个红色的酒吧(用于菜单),约50像素宽。第二列应该填充画布的其余部分(我限制为950px),并且应该分为3个部分:页眉、页脚和内容区域

在大多数情况下,我的布局工作,但我有几个问题。首先,红色竖条并没有完全填满整个画布(我将其包装在一个id为“pgcontainer”的节标记中)。它缺少大约10像素

第二个问题是,包含页眉、页脚和内容区域的第二个“列”没有浮动到红色垂直菜单栏的左侧,而是占据整个画布的宽度。我试着使用一个透明的div。我试着改变位置和浮动。似乎什么都不管用

我已经在这里发布了代码:。任何帮助都将不胜感激

HTML:


为了达到所需的布局,您所需的绝对最小值将低于css

#pgcontainer {
    ...

    position: relative;
}

#navbar-container {
    ...

    /*height: 100%;*/
    top: 25px;
    bottom: 25px;
}

#content {  
    ...

    /*width: 100%;*/
    position: relative;
    margin-left: 50px;
}
通常情况下,尽量不要使用ID

编辑:

不确定是否,因为它不是一个数字

您必须从
#pgcontainer

并将
$container.animate({marginLeft:'auto',marginRight:'auto'})
替换为
$container.animate({marginLeft:0,marginRight:0})


更新的fiddle:

您将导航栏定位为绝对位置,该位置不在文档流中。因此,您的主要内容跨越整个宽度。如果我不使用position:absolute,那么垂直条将浮动在其余内容的上方,并在顶部创建巨大的空白。我一直在这些不同的区域玩位置和浮动,运气不太好尽管瓦林德下面的答案非常接近!)酷-谢谢!所以这几乎奏效了。它肯定解决了第二个问题。技术上固定了第一个(垂直条一直延伸到底部)。但是,它会打断滑出菜单。从fiddle中很难看到,因为我无法添加所有资产,但有一个非画布菜单(即汉堡包菜单),它是红色竖条的一部分。当你点击它时,一个菜单会滑出显示出来。再次单击它会使其关闭。当您将“position:relative;”添加到“#pgContainer”时,这会中断,并且菜单不会滑出。(我的评论中已没有空间了)-实际上,它确实滑出,但滑出速度非常快,它们会立即关闭。有什么想法吗?看起来它是
pgContainer
$上的
margin:0 auto
。animate(margin:'auto')
更新了我的答案,并用快速而肮脏的方法解决了这个问题。再次感谢您的帮助-不幸的是它仍然不起作用。如果你点击红色竖条(汉堡图像应该在这里)顶部附近,你可以看到小提琴的动作。单击一次,什么也不会发生。单击两次,它将快速关闭和打开。我尝试删除边距:0“自动”并将“动画”更改为0(而不是“自动”),但没有解决此问题。我一加上“position:relative”,它就不起作用了。如果没有“位置:相对”,那么酒吧就会到处移动(这是另一个问题)。哦,是的,你说得对,肖恩。它发生在chrome中。我一直在检查firefox=(
@import url('http://fonts.googleapis.com/css?family=Montserrat:400,700');

    @import url('http://fonts.googleapis.com/css?family=Montserrat:400,700');

* {
    box-sizing: border-box;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    font-family: "Cabin";
}

html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
    outline: none;
    -webkit-font-smoothing: antialiased;
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

html {
    overflow-y: scroll;
}

body {
    width: 100%;
    font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
    background: #d6d1c7;
    font-size: 62.5%;
    line-height: 1;
}

br {
    display: block;
    line-height: 1.6em;
}

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

ol, ul {
    list-style: none;
}

input, textarea {
    -webkit-font-smoothing: antialiased;
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    outline: none;
}

blockquote, q {
    quotes: none;
}

blockquote:before, blockquote:after, q:before, q:after {
    content: '';
    content: none;
}

strong, b {
    font-weight: bold;
}

em, i {
    font-style: italic;
}

table {
    border-collapse: collapse;
    border-spacing: 0;
}

img {
    border: 0;
    max-width: 100%;
}

h1 {
    font-family: 'Montserrat', Arial, sans-serif;
    font-size: 3.6em;
    line-height: 1.3em;
    font-weight: 700;
    color: #484848;
    text-align: center;
    letter-spacing: -0.05em;
    margin-bottom: 15px;
}

p {
    font-size: 1.7em;
    line-height: 1.5em;
    margin-bottom: 20px;
    color: #616161;
}

#pgcontainer {
    max-width: 950px;
    padding: 25px 0;
    margin: 0 auto;
}

/*===Menu====*/

#navbar-container {
    display: block;
    max-width: 50px;
    height: 100%;
    background: #7a2126;
    padding: 5px 10px;
    margin-right: 15px;
    -webkit-border-radius: 8px;
    -moz-border-radius: 8px;
    border-radius: 8px;
    position: absolute;
}

#navbar {
    padding-top: 10px;
    background: #7a2126;
    padding-left: 10px;
    margin-left: -10px;
    width: 50px;
    float: left;
}

#navbar-icons {
    margin-top: 100px;   
}

#navbar-icons a img {
    max-width: 50px;
    max-height: 50px;
    margin: 10px 0px 10px -10px;
}

.menubtn {
    /* needs positioning for z-index http://stackoverflow.com/a/10600930/477958 */
    position: relative;
    z-index: 101;
    color: #274a61;
    text-decoration: none;
    font-size: 0em;
    line-height: 0em;
    top: -2px;
    padding: 15px;
    background-image: url('../../images/white-menu-button.png');
    background-position: 50% 50%;
    background-size: 25px 25px;
    background-repeat: no-repeat;
    float: left;
}

.menubtn:hover, .openmenu .menubtn {
    color: #bdd43e;
    background-image: url('../../images/white-menu-button.png');
}

    /** toggle menu **/
body.openmenu {
    position: fixed;
    overflow: hidden;
}



.overlay {
    position: fixed;
    z-index: 99;
    background-color: rgba(0,0,0,0.5);
    bottom: 0;
    right: 0;
    left: 0;
}

.openmenu .overlay {
    top: 0;
}

#hamburgermenu {
    height: 100%;
    width: 0;
    background: #7a2126;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 101;
    overflow: hidden;
    -webkit-box-shadow: 3px 0 7px rgba(0,0,0,0.55);
    -moz-box-shadow: 3px 0 7px rgba(0,0,0,0.55);
    box-shadow: 3px 0 7px rgba(0,0,0,0.55);
}

#hamburgermenu ul {
    margin-top: 45px;
    z-index: 101;
    overflow-y: auto;
    overflow-x: hidden;
}

#hamburgermenu ul li {
    display: block;
}

#hamburgermenu ul li a,
#hamburgermenu ul li h1{
    display: block;
    min-width: 130px;
    padding: 18px 15px;
    color: #ffffff;
    font-size: 1.75em;
    font-weight: bold;
    text-decoration: none;
    cursor: pointer;
}

.sub-menu li a:hover,
#hamburgermenu li a:hover{
    color: #000000;
    background: #DEBFC1;
}

#hamburgermenu ul.sub-menu li a {
    display: block;
    min-width: 130px;
    padding: 5px 15px;
    color: #ffffff;
    font-size: 1.35em;
    font-weight: normal;
    text-decoration: none;
    margin-top: -5px;
    z-index: 102;
}

.header-logo {
    height: 52px;
    margin-top: -15px;
    float: left;
    display: block;
}

#header-text {
    color: #ffffff; 
    margin-top: -10px;
    margin-left: auto;
    margin-right: auto;
    display: block;
}

/*===Search====*/
.search-container {
    margin-top: 5px;
    display: block;
    width: 23em;
}

.searchbox-container {
    overflow: hidden;
    float: right;
    height: 4em;
    width: 4em;
    border-radius: 2em;
    box-shadow: 0 0 5px #ffffff;
    -moz-transition: all 0.35s;
    -webkit-transition: all 0.35s;
}

.searchbox-container:hover {
    width: 22em;
    border-radius: 5px 2em 2em 5px;
}

.searchbox-container:hover input {
    display: inline-block;
    width: 19em;
    padding: 10px;
}

input {
    -moz-appearance: none;
    -webkit-appearance: none;
    appearance: none;
    float: left;
    width: 0em;
    height: 2em;
    margin: 1em;
    margin-right: -4.5em;
    background: white;
    color: #6a5d4f;
    font-size: 1em;
    font-weight: 600;
    padding: 0px;
    border: 0;
    border-radius: 5px;
    box-shadow: 0 1px 5px rgba(0, 0, 0, 0.2) inset;
    text-shadow: 0 1px 1px rgba(0, 0, 0, 0.15);
    -moz-transition: all 0.25s;
    -webkit-transition: all 0.25s;
}

input:focus {
    outline: none;
    box-shadow: 0 -1px 1px rgba(255, 255, 255, 0.25), 0 1px 5px rgba(0, 0, 0, 0.15);
}

.button {
    float: right;
    width: 1.75em;
    height: 1.75em;
    margin: 0.125em;
    background: #7a2126;
    text-align: center;
    font-size: 2em;
    color: #fdf6e3;
    border-radius: 50%;
    box-shadow: 0 -1px 1px rgba(255, 255, 255, 0.25), 0 1px 1px rgba(0, 0, 0, 0.25);
    text-shadow: 0 -2px 1px rgba(0, 0, 0, 0.3);
}

.button i {
    margin-top: 0.3em;
}

.button img {
    margin: 0.3em;
    width: 25px;
    height: 20px;
}

.button:active {
    border: 0 !important;
    text-shadow: 0 0 0;
}
/*===Content Form====*/

.clear {
    clear: both;   
}

#content {
    width: 100%;
    background: #fff;
    -webkit-border-radius: 8px;
    -moz-border-radius: 8px;
    border-radius: 8px;
}

#form-content {
    width: 100%;
    float: left;
    background-color: #ffffff;
    min-height: 550px;
}

#logo {
    width: 100%;
}

#logo img {
    -webkit-border-radius: 8px 8px 0px 0px;
    -moz-border-radius: 8px 8px 0px 0px;
    border-radius: 8px 8px 0px 0px;
}

/*===Footer====*/
#footer {
    background-color: #998b77;
    color: #ffffff;
    clear: both;
    -webkit-border-radius: 0px 0px 8px 8px;
    -moz-border-radius: 0px 0px 8px 8px;
    border-radius: 0px 0px 8px 8px;
    min-height: 100px;
}

#sponsor {
    display: block;
    margin: 0px auto;
    padding-top: 10px;
    align-content: center;
}

#sponsor img {
    display: inline !important;
    max-width: 17%;
    padding: 5px 15px 5px 15px;
}

#footer-text {
    text-align: center;
    margin-top: 20px;
    padding-bottom: 20px;
}
#pgcontainer {
    ...

    position: relative;
}

#navbar-container {
    ...

    /*height: 100%;*/
    top: 25px;
    bottom: 25px;
}

#content {  
    ...

    /*width: 100%;*/
    position: relative;
    margin-left: 50px;
}