简体   繁体   中英

How to have centered content with a left menu using css only?

I have two divs:

<div id="left_menu" >&nbsp;menu&nbsp;</div>
<div id="content" >&nbsp;centered&nbsp;</div>

Currently they have a css of

#content {
    margin-left:auto;
    margin-right:auto;
    display:table;
}

So this would create a div with menu and a line below that a centered div with centered. What I want is a centered div#content with div#left_menu to the left of it. I DON'T want to center BOTH the divs together, only the div#content. This should be done with only divs and css and should work on all browsers.

So this could possibly look like

---> menu centered <--------

Just to clarify things:

I'm not centering/positioning the text, it's the divs that matter (text is there for marking the position in the example). I want both divs on the same line (like a span, but i want to use divs), the centered div should be centered in the middle of the page. The menu div should be right next to it, touching the left border of the centered div.

This solution should work for every screen size (eg if the screen is very large the two side gaps to the left and right of the menu and content should be very large, eg if the screen is too small for both the menu and content, there should be no gaps and the result should look like (the >< represent the cutoff) Notice how if the screen is too small, the menu div is fully displayed first with the centered div cutoff (as if it were just two divs floated left).

>menu  cent<

Due to the number of incorrect answers being submitted:

1) Please verify your answers by creating your own .html file with your code

2) Refresh once on full screen and refresh once with browser resized to a smaller size such that the browser cannot hold both divs (eg the centered div is semi-cutoff)

3) Use inspect element tool(chrome) or equivalent tools to be sure that the two divs are touching, the centered div is indeed centered, etc

To further clarify what i want i've included a better example(NOT a solution though):

This does not work for every screen size: http://jsfiddle.net/prt38/2/

Updated per requests in comments.

I really like using the vertical-align property when vertically-aligning elements.

HTML:

<div id="container">
    <span id="alignment"></span><div id="wrapper">
        <div id="sidebar">

        </div><div id="main">

        </div>
    </div>
</div>

Notice how the closing and the succeeding are touching. For inline and inline-block elements to touch, there cannot be space between them in the markup.

CSS:

html, body { 
    height: 100%;
    margin: 0;
    padding: 0;
    text-align: center; }
#container { white-space: nowrap; }
#wrapper { 
    white-space: nowrap;
    text-align: left;
    margin: 0 75px 0 0;
    vertical-align: middle;
    display: inline-block; }
#alignment { 
    height: 100%;
    vertical-align: middle;
    display: inline-block; }
#sidebar {
    background: red;
    width: 75px;
    height: 200px;
    vertical-align: middle;
    display: inline-block; }
#main { 
    background: blue;
    width: 300px;
    height: 400px;
    vertical-align: middle;
    display: inline-block; }

Preview: http://jsfiddle.net/Wexcode/2Xrcm/8/

Your do it with simple overflow:hidden like this:

#left_menu{
    float:left;
    width:200px;
    height:100px;
    background:green;
}
#content {
    margin-left:auto;
    margin-right:auto;
    display:table;
    height:100px;
    background:red;
    overflow:hidden;
}

http://jsfiddle.net/hnXqg/

<div align="center">
<span id="left_menu">&nbsp;menu&nbsp;</span>
<span id="content">&nbsp;centered&nbsp;</span>
</div>

The solution for this is you have to create a wrapper class or id for a div like..

<div id="wrapper">
    <div id="left_menu" >&nbsp;menu&nbsp;</div>
    <div id="right">
        <div id="content" >&nbsp;centered&nbsp;</div>
    </div>
</div>

then the css is..

#wrapper{
    margin:0px auto;
    display:table;
    width:90%;
}

#menu{
    float:left;
    width:300px;
    margin:5px;
}

#right{
   float:right;
   display:block;
}

#content{
    displat:table;
    margin:0px auto;
}

I think this is what you are looking for. Adjust the sizes to suit your needs, obviously.

<style type="text/css">
    .container {
        margin: auto;
        width: 500px;
    }

    .menu {
        margin: 10px;
        width: 180px;
    }

    .content {
        margin: 10px;
        width: 280px;
        text-align: center;
    }

    .floatLeft {
        float: left;
    }

    .clear {
        clear: both;
    }
</style>

<div class="container">
    <div class="menu floatLeft">
        Menu
    </div>

    <div class="content floatLeft">
        Content
    </div>

    <div class="clear"></div>
</div>

Edited:

<style type="text/css">
    .container {
        margin: auto;
        width: 500px;;
        background: red;
    }

    .menu {
        width: 50px;
        margin-left: 50px;
        background: green;
    }

    .content {
        width: 300px;
        margin: auto;
        background: blue;
    }

    .floatLeft {
        float: left;
    }

    .clear {
        clear: both;
    }
</style>

<div class="container">
    <div class="menu floatLeft">
        Menu
    </div>

    <div class="content">
        Content
    </div>

    <div class="clear"></div>
</div>

I think this css should do the job, if I understood your question:

#left_menu{background:red;
width:100px;
height:100px;
float:left;
margin: auto 0;
z-index:2}
#content {
  background:white;
  width:100px;
  height:100px;
margin: auto 0;
float:left;
position:absolute;
left:20%;
z-index:200;
padding-left:4%
}

And Html is below:

<div id="left_menu" >RED DIV</div>
<div id="content" >WHITE DIV</div>

html { text-align: center; } div#content { display: inline; margin: 0 auto; text-align: left;width: 980px; }

something like this should work.

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