简体   繁体   中英

Filling sides of a centered div?

I want to fill the sides of a centered div with another div or span on each side.

I'm using margining to center the div as shown in this fiddle.

HTML

<div id='A>
  <div id='Ad'>
  </div>
</div>

CSS

#A{
  z-index: 3000;
  position: fixed;
  width: 100%;
  height:  40px;
  background: rgba(0,0,0,0.05);
}

/*
div or span to the left
*/

/*
centered div
*/
#Ad{
  z-index: 3000;
  width:  400px;
  height:  40px;
  margin-left:  auto;
  margin-right:  auto;
  border-left: solid 1px #ff0000;
  border-right: solid 1px #ff0000;
}

/*
div or span to the right
*/

How can I have a div that always takes up the remaining space on the left and another div that takes up the remaining space on the right.

Clarification:

Center column needs to be constant width. Left and Right Columns vary with the window size.

This would achieve what you want - it allows you to have a fixed width central div with left and right columns that fill up the remaining space:

HTML:

<div id="A">
    <div id="Ad">Centre</div>
    <div id="left">Left</div>
    <div id="right">Right</div>
</div>

CSS:

#A {
    z-index: 3000;
    position: fixed;
    width: 100%;
    height: 400px;
    background: rgba(0, 0, 0, 0.05);
}
/*
centered div
*/
 #Ad {
    z-index: 3000;
    width: 400px;
    height: 400px;
    margin-left: auto;
    margin-right: auto;
    border-left: solid 1px #ff0000;
    border-right: solid 1px #ff0000;
}
#left, #right {
    position:absolute;
    left:0;
    top:0;
    right:50%;
    margin-right:200px;
    background:#F00;
    height: 400px;
}
#right {
    left:50%;
    right:0;
    margin-left:200px;
    margin-right:0;
}

The key is that the margin on the left/right is half of the central column's total width, so adjust it to take into account any borders or padding.

Working example: http://jsfiddle.net/2AztF/

I would just use 3 <div>s floated within the main container

HTML:

<div id='A'>
  <div id='AdLeft'></div>
  <div id='Ad'></div>
  <div id='AdRight'></div>
</div>

CSS:

#A { overflow:auto }
#AdLeft { float:left; width:25%; }
#Ad { float:left; width:50%; }
#AdRight { float:left; width:25%; }

Here is a modified jsfiddle .

To fill space on the right and left side of your div code use and make sure you have no margin or padding on those sides.

float:right; 
float:left;

HTML:

<div class='container'> 
<div class='left'></div> 
<div class='center'></div>
<div class='right'></div> 
</div>

CSS:

.container { overflow: hidden; margin:0; padding:0; }
.right { float: right; width: 150px; }
.center{ float: right; width:50px; margin-right: 50px; }
.left{ float: left; width: 150px; }

The margin-right of .center will fill the space accordingly.

Make 3 divs :

<div id="A"></div>
<div id="B"></div>
<div id="C"></div>
<div style="clear:both"></div>

CSS:

 #A,#B,#C{
    float:left;
    width:10%;
  } 
  #B{
    width:80%;
  }

Here, B is you main div. It is good practice to clear when you use float property.

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