简体   繁体   中英

Centered layout with the sidebar extension to the right of the screen

I'm trying to create a fixed layout, with the sidebar's background extend to the far right. I drew a sketch to illustrate the image:

在此处输入图片说明

how would I go about extending the sidebar background to extend till the end of the right screen, on any window size? I tried with:

#sidebar {
    z-index: 1000;
    overflow: hidden;
    position: absolute;
    width: 100%;
    background: url(../img/sidebar-base.png) no-repeat 0 -8px;
    min-height: 200px;
    &::after {
        content: '';
        z-index: 10;
        display: block;
        height: 100px;
        overflow: hidden;
        width: 100%;
        background: url(../img/sidebar-rx.png) repeat-x 0 -9px;
        position: absolute;
        top: 0;
    }
}

but a scroll would appear horizontally, and if I apply overflow:hidden on the body I wouldn't be able to scroll to the bottom. Thank you!

EDIT : I did try to find my luck with javascript but there's still a little scroll:

$(function(){
    $sidebar = $('#sidebar');
    $sidebar.css({width: window.innerWidth - ($sidebar.offset().left)})
});

If your problem lies only in the scrolling, you can easily fix this with this line

overflow-x: hidden;

and applying it to the background's parent or the body element altogether.

Is there anyone following here or not? anyway, I think you should static position and hidden overflow like below:

#sidebar {
        z-index: 1000;
        overflow: hidden;
        position: static;
        width: 100%;
        height:100%;
        right:0;
        top:0;
        margin:0;}

Also to hide the scrolls, you should hide your body overflow too.

Hope to be right and helpful...

Set body to 100%

body {
height: 100%;
}

Then set the sidebar height to "height: auto;". That will make it extend to the height of the viewport. From there, add fixed positioning like you said.

You could do:

overflow-y:hidden

That should get rid of the scroll bar across the bottom.

I would also then use a lot of right hand padding in the sidebar to extend it out.

尝试将侧边栏宽度设置为30%,将内容设置为70%。

What you should do is create a wrapper div.

<div class="sidebar-parent">
  <div class="sidebar"><!-- Stuff Here --></div>
</div>

Your document should look like this when finished:

<html>
<head>
  <title>Experiment</title>
  <style type="text/css">
    .content {float: left; width: 49%; height: 500px; border: 1px solid #000;}
    .sidebar-parent {float: left; width: 50%; background-color: green;}
    .sidebar {width: 500px; height: 500px; border: 1px solid #000;}
  </style>
</head>
<body>
  <div class="content">blah blah blah</div>
  <div class="sidebar-parent">
    <div class="sidebar"><!-- Stuff Here -->blah blah blah</div>
  </div>
</body>
</html>

The main thing to remember is the container div "sidebar-parent" is what's getting the width and containing the background.

To center them you'll need width: 50%; parent containers for both content and sidebar. You make those float:left; to fill the screen and then the content child container float: right; and the sidebar child container float: left; within their parent containers.

Summary: 2 50% width containers each containing 1 child container. Stack the parents together with a left float and then position the fixed width child containers within their parents.

That will center them and now you'll have the ability to have extended backgrounds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM