Why are positioned divs overlapping? - css

I have a main wrapper div with 2 content divs inside. The position attribute of both content divs is set to relative, but for some reason they're overlapping as shown here:
I want the div outlined in red to be underneath the blue one and am having trouble figuring out how to do so.
#wrap {
height: 500px;
width: 350px;
border: 3px solid black;
}
#upper {
position: relative;
width: 40%;
height: 70%;
top: 5%;
left: 2%;
border: 1px solid blue;
text-align: center;
}
#lower {
position: relative;
width: 40%;
height: 20%;
left: 2%;
border: 1px solid red;
}
<div id="wrap">
<div id="upper"></div>
<div id="lower"></div>
</div>
Can someone please help me figure out how to align them correctly?

The styling of the div#upperDiv has top:5% which causing this to happen. Although relative but div#upperDiv is taking the 5% top to overlap on div#lowerDiv.
Solution: EITHER take that top:5% styling off from upperDiv OR add the same top style to lowerDiv.

For lower div can you try adding clear:both;
#lowerDiv {
position: relative;
clear:both;
width: 40%;
left: 2%;
border: 1px solid red;
text-align: center;
}

It is happening because u r using height in percentage. As you've taken Height of upperDiv is 70%. it is taking 70% of ur main div. and ur lower div is having more data than it can adjust in the same outer div. so ur main div should big enough so that ur lowerDiv can adjust in remaining 30% of space u r providing to it. or u can adjust ur upperDiv's percentage value of height so that both can adjust in that space.

Related

Positioning div right to a already centered content without absolute position

I have two child divs (inline-block) inside a wrapper div. I want the left Div to be centered and the right one simply on the right of the left div.
<div id="Wrapper1"><div id="leftElement1">LEFT ELEMENT</div><div id="rightElement1">RIGHT</div></div>
The Problem is, if I use margin-left to reposition the whole wrapper, the Left Element is not centered on small screen sizes.
If I center leftElement1 and use position: absolute to position rightElement1 the Warpper Div does not adjust its width and height according to its children.
For a better understanding check http://jsfiddle.net/aaq810gs/6/
Any help is appreciated!
If i understand right you want something like this:
#rightElement1 {
background-color: blue;
position: relative;
width: 100px;
display: inline-block;
height: 50px;
float: right;
top: -100px;
}
Applied in your first example.
fiddle
Or something like this:
#rightElement1 {
background-color: blue;
position: fixed;
width: 100px;
display: inline-block;
}
fiddle
I am not really sure if I get exactly what you mean, but I think something like this could work for you.
- You better switch to %, because than it will work better on mobile devices.
- Second thing is adding margin:0 auto; for #leftElement1 so it stays in the middle. #rightElement2 will just stick to it on the right, because it is inline-block.
Now you can add whatever margin to the wrapper and it stays the same.
Fiddle http://jsfiddle.net/stassel/vzx6fm55/
HTML:
<div id="Wrapper1">
<div id="leftElement1">LEFT ELEMENT</div>
<div id="rightElement1">RIGHT</div>
</div>
CSS:
#Wrapper1 {
width: 90%;
background-color: red;
margin: 0 auto;
white-space: nowrap;
padding: 10px;
margin-left:10%;}
#rightElement1 {
background-color: blue;
width: 10%;
display: inline-block;
height: 50px;}
#leftElement1 {
background-color: green;
width: 60%;
margin:0 auto;
display: inline-block;}
div {
height: 100px;
text-align: center;
color: white;}
SOLVED
Thank you for all your answers! Unfortunately I wasn't able to describe my Question properly, so none of the solutions worked.
Finally I was able to solve the problem myself. The Key to the solution was another centered outer wrapper, with a fixed size of the to-be-centered Element and overflow: visible. The inner content overlaps now the outer wrapper.
#outerWrapper {
width: 700px;
overflow: visible;
margin: 0 auto;
}
#Wrapper {
width: 810px;
background-color: red;
}
http://jsfiddle.net/aaq810gs/9/

Moving Text Freely Inside Its parent

I'm New to CSS.
Is there a way to move text within the following span like a non-repeated background-image?
for example: 50px from left, 40 px from left? without causing extra height and width!
 
 
div{
width: 300px;
height: 300px;
border: 1px solid black;
}
<body>
<div>
<span>Hello World</span>
</div>
</body>
Can I ask more question? how many space does a 16px character occupies? I mean what does 16px mean? 16pixel wide? 16 pixel height? when we select a character with mouse, there is a blue box around the selected character, which is bigger than that character. is this relevant to this question?
Firstly I think font-size always relates to the height of the letters and the reason the "blue box" is slightly larger is because it is highlighting the line specified by line-height.
To position the text inside your box you have a couple of options:
1) You can absolutely position the span inside the div like so:
div {
width: 300px;
height: 300px;
border: 1px solid black;
position: relative;
}
span {
position: absolute;
top: 50px;
left: 50px;
}
http://jsfiddle.net/5xukp/
2) You can set the span's display to block or inline-block and then apply margin or padding to position the span like so:
span {
display: inline-block;
margin-top: 50px;
margin-left: 50px;
}
http://jsfiddle.net/m79as/
EDIT - In response to your comment, there is no property like background-position where you can set it to be center center or center left however you can use vertical-align and text-align to position the text. In order to vertically align the span correctly you will need to set the display to table-cell
div {
width: 300px;
height: 300px;
border: 1px solid black;
display: table-cell;
text-align: center; /* left|right|center|justify */
vertical-align: middle; /* top|text-top|middle|bottom|text-bottom */
}
http://jsfiddle.net/6ExB2/

problems with a negative top margin of a relative positioned element (vertical alignment)

I'm trying to align vertically a div inside a container with a height defined. I'm following the guide of http://www.vertical-align.com/, but I'm facing some issues.
According to the website, if I use this css with for this code:
#containingBlock {
height: 200px;
position: relative;
overflow: hidden;
border: 1px solid red;
}
#containingBlock > div {
position: absolute;
top: 50%;
border: 1px solid green;
}
#containingBlock > div > div {
position: relative;
top: -50%;
border: 1px solid orange;
}
<div id="containingBlock">
<div>
<div>
This should be placed in the middle
</div>
</div>
</div>
Fiddle available here
I should obtain a text perfectly in the middle. But this doesn't happen because the top: -50% doesn't work. According to Mozilla dev the top property + % value should be based on the parent's height, which has the same height of its child automatically in this case. But the "automatic wrap height" does not seem to be take into consideration. If I specify a explicit height for the parent div (I mean, the first one nested), everything seems to be ok, but I would like it to take the height of its child automatically! What's wrong with this?
If the height of the block to be positioned is known you can affect the correct positioning with negative margin (i.e 50% of the known height).
If it is not known you can affect it with a CSS transform as follows
-webkit-transform:translate(0%, -50%);
This moves the object vertically half it's own height...and so on
HTML
<div class="containingBlock one">
<div>
This should be placed in the middle
</div>
</div>
CSS
.containingBlock {
height: 200px;
position: relative;
border: 1px solid red;
}
.containingBlock > div {
position: absolute;
top: 50%;
border: 1px solid green;
-webkit-transform:translate(0%, -50%);
}
JSfiddle
here's a fiddle: http://jsfiddle.net/dC22r/4/
you have to set an height to the div that has to be centered then give it top:50% and subtract half his height with a negative margin.

Fluid Image 80% width and height relative to its parent vertically and horizontally aligned

Height was not respected on this fiddle
I want the image to have a height and width of 80% relative to its parent, vertically and horizontally aligned. For some reason, it does not work.
HTML:
<div id="menu_header_new_orig">
<img id="menu_logo_orig" src="https://imagizer.imageshack.us/v2/849x565q90/833/uua2.jpg" />
</div>
CSS:
#menu_header_new_orig {
margin-top: 2.5%;
height:40%;
width:100%;
overflow: hidden;
position: relative;
border: 1px solid green;
text-align: center;
}
#menu_logo_orig {
width: 80%;
height: 80%;
position: relative;
}
I have figured it out here, but just in case somebody have better solution.
If I understand you correct :
The parent #menu_header_new_orig own parent must also have a height (obviously 100%)
html, body {
height: 100%;
}
set display to inline, 10% top to get vertical alignment
#menu_logo_orig {
top: 10%;
display: inline;
position: relative;
width: 80%;
height: 80%;
}
Is that what you were heading for? [not really sure] - try to set #menu_header_new_orig height to other things than 40% to get it in another perspective.
forked fiddle -> http://jsfiddle.net/Dmc7j/

Creating a border gap

I'm trying to get a gap created within a div's border to fit an image, similar to this:
Is there a way to do this in pure CSS? All I can see is:
.box {
background: url(img.png) bottom left;
border-top: 1px solid #eee;
border-left: 1px solid #eee;
}
But my problem is border-right: 1px solid #eee; creates a line on top of my image, which is of course not desired.
It needs to be responsive. This image is an example, but you get the general idea.
Something like this?
http://jsfiddle.net/6Ufb5/
div {
border: 1px solid #ccc;
position: relative;
}
img {
position: absolute;
top: 10px;
left: 10px;
}
Give the container position relative and the img absolute, shift it to left 10px and shift it down 10px from the top and you have what you desire.
For the responsive part, that's just giving the container and/or img a % width.
Like so:
http://jsfiddle.net/6Ufb5/2/
You can achieve this by using absolute positioning of the image element - and it has to be in a <img> element, not as the background image because it will never overlap the parent border (or even if it does by adjusting the background-position property, the border will lie on top of the background image... a behavior that is expected, by the way.
<div class="box">
Content goes here
<img src="http://placehold.it/300x200" />
</div>
And the CSS:
.box {
position: relative;
border: 1px solid #333;
}
.box img {
position: absolute;
bottom: -1px;
right: -1px;
}
If you want a dynamic and/or responsive solution, you might have to resort to JS to doing so - such as resizing the image depending on the box dimensions, and assigning a height to the box to take into account of the image height (since image is absolutely positioned, it is taken out of the document flow).
See fiddle here: http://jsfiddle.net/teddyrised/xH6UV/
This might work if you can alter your markup. For accessibility I think the image should be an image and not a background, and this method is responsive (though you may want to alter margins at small sizes with media queries).
http://jsfiddle.net/isherwood/79Js5
.box {
border: 1px solid #ccc;
display: inline-block;
padding: 10px 0 10px 10px;
width: 40%;
}
.box img {
margin-right: -10%;
margin-bottom: -10%;
width: 105%;
}
<div class="box">
<img src="http://placehold.it/200x100/f3f3f3" />
</div>

Resources