I have a single column layout where the column is a centered div with a fixed width. I want to place a wider div within the column which overflows it's parents, but center it within the parent. Conceptually something like the following:
<div style="width: 100px; margin: 0 auto; overflow:visible;" id="parent">
<div style="width: 400px; margin 0 auto;" id="child"></div>
</div>
The centering works as long as the child div is thinner than its parent, but once it gets larger, it always aligns left with the parent for some reason.
#wrapper {
margin: 0 auto;
width: 200px;
background-color: #eee;
position: relative;
height: 200px;
}
#child {
position: absolute;
top: 0;
left: 50%;
margin: 0 0 0 -200px;
width: 400px;
background-color: #ddd;
}
<div id="wrapper">
<div id="child">Child div</div>
</div>
jsFiddle
When an element overflows his parent, it is normal behaviour that it only overflows to the right. When you, for example, have a site that is wider then the viewport, you never have to scroll left, but only to the right. This solution is based on a absolute centered div, with a negative left margin (that value is the half of his own width). So if you know the width of this element, this solution should be fine.
Tested in FF 3.6, IE7 and IE8
I made a variation of Justus' solution. Instead of relative positioning, I used a negative margin of 50% in the inner element.
#wrapper {
margin: 0 auto;
padding: 10px 0 10px;
width: 200px;
background-color: #eee;
}
#child {
margin: 0 -50%;
width: 400px;
background-color: #ddd;
}
This way you don't need to know the element sizes ahead of time.
I am not 100% sure but try giving the parent element a position set to relative and child absolute and then set top, left and width/height properties for the child div accordingly.
This is how I solve it:
http://jsfiddle.net/WPuhU/1/
Also take care of the scrollbars(they do not appear if your window view is smaller then the overflowing div). Auto centers the overflowing div.
css:
#center-3 {height:40px;background-color: #999;}
#center-1 {height:20px;top:10px;background-color: #aaa;}
/* the necesary code */
body {width:100%;margin:0;}
#center-4 {
width: 100%;
overflow:hidden;
/* remove the next 2 line for a normal flow */
position: absolute;
z-index: -1;
}
#center-3 {
position: relative;
margin: 0 auto;
width: 200px;
}
#center-2, #center-1 {
position: relative;
width: 400px;
}
#center-2 {
left: 50%;
}
#center-1 {
left: -50%;
}
html:
<div id="center-4">
<div id="center-3">
<div id="center-2">
<div id="center-1"></div>
</div>
</div>
</div>
<div id="other-stuff">Here comes the other stuff above.</div>
#parent {
position: relative;
width: 100px;
overflow: visible;
}
#child {
width: 400px;
position: absolute;
left: 50%;
transform: translateX(-50%);
}
Related
Playing around with some absoloute divs I have in my header. logo, search bar etc and trying to get them centered.
Will margin: 0 auto; ever work with an element set as absolute?
I know of some options to center divs within a 100% width such as calc, transform, flex. Are there any more centering 100% options?
Yes, it will work if the element is positioned correctly and has a width specified.
In the example below, left: 0/right: 0 is added so that the element is centered relative to the parent. For instance:
.container {
position: relative;
}
.absolute {
position: absolute;
width: 80%;
height: 40px;
background-color: #000;
margin: 0 auto;
left: 0;
right: 0;
}
<div class="container">
<div class="absolute"></div>
</div>
Alternatively, you can also use a combination of left: 50%/transform: translateX(-50%) in order to center the element horizontally.
.container {
position: relative;
}
.absolute {
position: absolute;
width: 80%;
height: 40px;
background-color: #000;
left: 50%;
transform: translateX(-50%);
}
<div class="container">
<div class="absolute"></div>
</div>
I have two divs with float:left inside a container with a fixed width - something like that
<div style="width:1100px">
<div id="A" style="float:left; width: 400px"></div>
<div id="B" style="float:left; min-width: 600px"></div>
</div>
Here is the problem. Both internal divs A and B are generated dynamically and div B can exceed 700px. In that case, it goes below div A. I cannot easily change the width of the container, because it is also generated automatically by bootstrap.
I've tried to play with the overflow options, but it didn't work. I could recalculate the width of the container dynamically with jquery based on the total width of divs A and B, but it will be overdo.
What is the easiest way to force div B stay next to div A regardless of its width?
Thanks
like my comment and using akinuri modified fiddle, using percentage and without scroll bar;
#container {
width: 1100px;
position: relative;
overflow: show; /* or hidden if you dont want the scrool bar */
}
#A {
background: yellow;
width: 60%;
}
#B {
position: absolute;
top: 0;
left: 60%; /* width of #A */
width: 80%;
background: blue;
}
you get
http://jsfiddle.net/TRFmL/1/
EDIT
I think now I have whatyou want:
#container {
width: 100%;
position: relative;
overflow: show; /* or hidden if you dont want the scrool bar */
}
#A {
float:left;
display:inline-block;
background: yellow;
width: 60%;
}
#B {
float: right;
margin-right: -40%; // this is the result of 100 - (A+B)
display:inline-block;
width: 80%;
background: blue;
}
One way I can think of is using absolute positioning.
<div id="container">
<div id="A">a</div>
<div id="B">b</div>
</div>
#container {
width: 1100px;
position: relative;
overflow: auto; /* or hidden if you dont want the scrool bar */
}
#A {
background: yellow;
width: 400px;
}
#B {
position: absolute;
top: 0;
left: 400px; /* width of #A */
width: 900px;
background: blue;
}
FIDDLE
You can use inline-block display for the div, then set white-space to nowrap and remove any white space between the two div.
Live example: http://jsfiddle.net/VMVAb/
I have a div inside a div, the child both being centered in its parent and bigger than the parent making it overflow equally on both sides of it. The child has another div inside it with some text.
<div class="outer">
<div class="inner">
<div class="text">
testing testing
</div>
</div>
</div>
css:
.outer
{
width: 400px;
height: 400px;
background: beige;
margin: 0 auto;
}
.inner
{
width: 600px;
height: 200px;
background: pink;
position: absolute;
left:0;right:0;
margin: auto;
}
.text
{
margin-left: auto;
margin-right: auto;
width: 400px;
}
http://jsfiddle.net/msVVD/4/
Now, if the document width is narrowed by resizing the browser window, or in the jsfiddle case, resized by dragging the handle between "JavaScript" and "Result", the text will not stay on the same horizontal position, but "travel" to the right.
Why?
You need to set a min-width to the body (or parent container in which the absolutely positioned element is aligned according to) like so
body
{
min-width: 600px;
}
This will prevent the absolutely positioned from traveling
FIDDLE
You are not positioning the .inner element relatively to the .outer one. Add position: relative to .outer.
Changes in your CSS:
.outer
{
width: 400px;
height: 400px;
background: beige;
margin: 0 auto;
position: relative;
}
.inner
{
width: 600px;
height: 200px;
background: pink;
position: absolute;
left:0;right:0;
margin: auto;
margin-left: -100px;
padding-left: 100px;
}
Fiddle: http://jsfiddle.net/msVVD/7/
I have what is a fairly common page layout where the content div is centralised on the page using margin:auto 0. The width of the div itself varies depending on available page width.
I want another div featuring a logo to 'stick' to the outside left hand side of this div (ie no gap or overlap between the two) at a fixed height. What CSS should I use for this?
something like
html:
<html>
<div id='content'>
<div id='stickything'>a</div>
</div>
</html>
css:
html {
width: 100%;
}
#content {
position: relative;
width: 100px;
height: 600px;
margin: auto;
background-color: green;
}
#stickything {
position: fixed;
width: 25px;
height: 30px;
top: 0px;
margin-left: -25px;
background-color: red;
}
http://jsfiddle.net/Kkcnn/
Use position:absolute. It must help:
.container-div{
position: relative
}
.outer-div{
position:absolute;
top: 0 (your choice)
left: -/outer div's width/
}
I am trying to align vertically a modal box. But the margin-top:50% did not work as i expect. Basically i want a square centered on another square.
<style type="text/css">
#modal {
width: 300px;
height: 300px;
z-index: 100;
background-color: red;
margin: 0 auto;
position:relative;
margin-top:50%; //problem here
}
#content {
position: absolute;
width:950px;
height: 950px;
margin: 0 auto;
background-color: green;
}
#replace {
width:950px;
height:500px;
}
</style>
<div id="replace">
<div id = "content"></div>
<div id="modal"></div>
</div>
thanks
demo
Instead of margin you need to use top:325px; for #modal
If it's not too much trouble to have the modal inside the content div, you might want to try this HTML:
<div id = "content">
<div id="modal"></div>
</div>
...with this CSS:
#modal {
width: 300px;
height: 300px;
background-color: red;
position: absolute;
top: 50%;
left: 50%;
margin-left: -150px;
margin-top: -150px;
}
#content {
position: absolute;
width: 550px;
height: 550px;
background-color: green;
}
Demo
Basically you're absolutely-positioning the modal div inside the content div, and telling it to go start at 50% from the top and 50% from the left. This will center the top-left corner of the modal, but not the div as a whole. To center the div as a whole, you then have to add a negative margin to move it back up and to the left. The amount of the negative margin is half the height/width of the modal div.
If you want to keep the same HTML, you can still accomplish the same thing using this technique, just make sure to do position: relative on your replace div so that any absolutely-position children are positioned relative to it.
Add position: relative; to your container if you want absolutely positioned elements inside it to be positioned relative to it:
#replace {
position: relative;
width: 950px;
height: 500px;
}
Then set to top value of the item you want to position. One thing to note here, your #replace div is the container here, but it's smaller than the #content div, so when you position #modal, you're going to have to give it specific pixel values to get it centered over #content.