Make an Element overflow to the Left - html

I was wondering how to make the rectangle in this fiddle: http://jsfiddle.net/gztdG/2/ 'overflow' to the left, so that the rectangle aligns to the right edge of the square. A simple question, but I can't figure out how to achieve this with position: absolute. Thanks in advance. Here is the html:
<div id='square'>
<div id='rectangle' />
</div>
And here is the css:
#square {
height:50px;
width:50px;
background:blue;
margin:auto;
}
#rectangle {
width:200px;
height:20px;
background:red;
position:absolute;
}

I think, the correct answer is to set position: relative; on the parent container, then have the rectangle align to the right (see this http://jsfiddle.net/gztdG/5/):
#square {
position: relative; <---
height:50px;
width:50px;
background:blue;
margin:auto;
}
#rectangle {
position:absolute;
right: 0; <---
width:200px;
height:20px;
background:red;
}

Use negative margin, like that:
margin-left:-150px;
http://jsfiddle.net/4qr7z/1/

You could also move the div tags so that the rectangle is not wrapped in the square (unless your design requires it)
<div id='square'></div>
<div id='rectangle'></div>
#square {
height:50px;
width:50px;
background:blue;
margin-left:150px;
overflow:auto;
}
#rectangle {
width:200px;
height:20px;
background:red;
position:relative;
}
This would move the square instead, and use margin-left:150px;
The reason I could imagine this being useful is that pages are loaded from left to right, so if the window is moved, you might experience some displacement, since margin-left:-150px will push it right.

Related

Absolute Positioning not great with width and left values

I have a div with an absolute positioning which is again a child of absolute positioned element. setting width:100%;left:1px;right:1px to the child not working. Problem i face is, its getting beyond the parent the element.
<div class="outer">
<div class="inner">
</div>
</div>
.outer{
position:absolute;
width:80px;height:80px;
border:1px solid #d3d3d3;
}
.inner{
position:absolute;
width:100%;
height:100%;
background:red;
left:1px;right:1px;bottom:1px;top:1px
}
Refer here
Just take away the 100% on the child element and the inner div will fit the parent.
.outer{
position:absolute;
width:80px;height:80px;
border:1px solid #d3d3d3;
}
.inner{
position:absolute;
background:red;
left:1px;right:1px;bottom:1px;top:1px
}
This is because you have the width and height to be 100%, meaning it'll be also 80px PLUS the top left right and bottom properties so the box lays over the other. Now if you want it to go inside the box and be perfectly proportioned remove height and width:
.inner{
position:absolute;
background:red;
left:1px;right:1px;bottom:1px;top:1px
}
You can also make this:
.outer{
margin-top: 10px;
position:absolute;
width:80px;height:80px;
border:1px solid #d3d3d3;
padding: 1px;
}
.inner{
position:relative;
width:100%;
height:100%;
background:red;
}

Divs with children not stacking properly

Please take a look at this fiddle https://jsfiddle.net/t2w4yd8j/1/
I have a couple of questions about this:
1) There seems to be a padding between the .top div(red) and the browser if I use the relative position. However if I change the position of .top div(red) to absolute the padding goes off. Why is that?
2) The .next div(pink) should stack after the .main div(grey). But the main div seems to be taking a bit more extra space even though the height is set to auto and there is no children in the extra space. Why is that?
Thanks
CSS
.main{
height:auto;
width:100%;
text-align:center;
background-color:#CCC;
}
.top{
position:relative;
top:0px;
left:0px;
width:100%;
height:50px;
background-color:#F00;
}
.middle{
position:relative;
top:-25px;
width:100%;
height:auto;
text-align:center;
z-index:3;
}
.midfill{
width:200px;
height:50px;
display: inline-block;
background-color:#0F0;
}
.bottom{
position:relative;
top:-50px;
left:0px;
width:100%;
height:50px;
background-color:#00F;
}
.next{
width:100%;
height:100px;
background-color:#F0F;
}
HTML
<div class="main">
<div class="top"></div>
<div class="middle">
<div class="midfill"></div>
</div>
<div class="bottom"></div>
</div>
<div class="next"></div>
1) By placing it relative, it relates to it's parent, the body tag. Remove the padding and margin from the body and HTML tag, and it fits. When you place the div absolute, it's taking out of the document flow, making it relate to the viewport. That explains the difference.
html, body { margin: 0; padding: 0; }
2) you position the div's relative, and then move them around. But the place stays reserved in the parent div. I moved the divs a bit around.
html, body {
margin: 0;
padding: 0;
}
.main{
height:auto;
width:100%;
text-align:center;
background-color:#CCC;
}
.top{
width:100%;
height:50px;
background-color:#F00;
}
.middle{
position: absolute;
margin-top: -25px;
width:100%;
height:auto;
text-align:center;
z-index:3;
}
.midfill{
display: inline-block;
width:200px;
height:50px;
background-color:#0F0;
}
.bottom{
width:100%;
height:50px;
background-color:#00F;
}
.next{
width:100%;
height:100px;
background-color:#F0F;
}
Updated Fiddle
Solution for your both problem is following. By Default it takes extra margin by removing it from body solved your issue:
body{
padding:0;
margin:0;
}
Check Fiddle Here.

there is an odd css issue, the text inside a div did not move with the div

Guys
I have a simple page with a strange issue. Even two senior front-end developers could not find out why.
Here is the code, very simple and easy to understand
<html>
<head>
<style type="text/css">
body
{
padding-left:250px;
}
#box1
{
width:150px;
height:150px;
background-color:#063;
margin-bottom:10px;
text-align:center;
line-height:150px;
float:left;
}
#box2
{
width:150px;
height:150px;
background-color:#00F;
margin-bottom: 15px;
text-align:center;
line-height:150px;
}
#box3
{
width:150px;
height:150px;
background-color:#FC3;
text-align:center;
line-height:150px;
}
</style>
</head>
<body>
<div id="box1">box1 green</div>
<div id="box2">box2 blue</div>
<div id="box3">box3 yellow</div>
</body>
</html>
Since #box1 is float:left, so it will be on the top of the page, and the #box2 should be invisible (covered by #box1).
However, when i view it from firefox/chrome, i can see the text of #box2 still visible and the text's position is not right.
I want to know why the text is visible and at the wrong place and how to fix.
I really appreciate your help.
Thank you
Luke
DEMO
Try the above link . Just set position for your box1.
position:absolute
It works fine.
REASON:
Since you don't use any position it will be considered as static position rather than default.
STATIC POSITIONING: Part of page flow. Scrolls normally. No position change.
ABSOLUTE POSITIONING: It is out of page flow. It is usually moved from original position[0,0].
You can use position absolute, instead of float. float will not work that way. Below code will help
<style type="text/css">
body
{
padding-left:250px;
}
#box1
{
position:absolute;
z-index:10;
width:150px;
height:150px;
background-color:#063;
margin-bottom:10px;
text-align:center;
line-height:150px;
}
#box2
{
position:relative;
z-index:8;
width:150px;
height:150px;
background-color:#00F;
margin-bottom: 15px;
text-align:center;
line-height:150px;
}
#box3
{
width:150px;
height:150px;
background-color:#FC3;
text-align:center;
line-height:150px;
}
</style>
You can use z-index value to determine which div is on top. Remove the background color of box1 and see, then you will see the box 2 is underneath box1

Div not being centre aligned inside another div

I can't seem to centre align my div(title) which sits inside another div.
HTML
<div class="wrapper">
<div id="header">
<div class="title">Home</div>
</div>
</div>
CSS
#header {
position:relative;
width:1200px;
height:400px;
margin:auto;
border:1px solid red;
}
.title {
position:absolute;
width:1000px;
height:140px;
background-color:red;
margin:auto;
}
Remove position: absolute and it works perfectly.
Position: absolute is only necessary when you need very specific placement outside of the normal document flow. In this case, nothing special is needed apart from automatic left and right margins, which you already have.
you are mixing stuff.
Remove position absolute.
or if you want it to be absolute you can do this
.title {
position:absolute;
width:1000px;
height:140px;
left: 50%;
background-color:red;
margin-left:-500px;
}

Overflow-y not working when overflow-x is hidden?

I have a DIV that contains several other divs. I need divs to be able to peek out of the parent vertically, but not horizontally.
I thought using overflow-x and overflow-y would solve this little problem, but I can only get either x and y to show, or get them both to hide.
My CSS and HTML:
.game {
position:absolute;
width:400px; height:300px;
top:100px; left:100px;
background-color:#cccccc;
overflow-x:hidden;
overflow-y:visible;
}
.block1 {
position:absolute;
width:100px; height:100px;
top:-50px; left:150px;
background-color:#ffcccc;
}
.block2 {
position:absolute;
width:100px; height:100px;
top:150px; left:-50px;
background-color:#ccffcc;
}
<div class="game">
<div class="block1"></div>
<div class="block2"></div>
</div>
See this JSFiddle: both child divs are cut off, even though overflow-y is set to visible.
Structural Change Needed
This gets what you want if it works otherwise (I don't know if the html/css changes affect other aspects of your game). It solves it by layering the "game" so that its vertical direction fills the entire screen, and then your "window" (grey area) is set by a child div. This allows the overflow: hidden horizontally, but not have it vertically.
See fiddle.
HTML
<div class="game">
<div>
<div class="block1"></div>
<div class="block2"></div>
</div>
</div>
CSS
html, body { height: 100%; margin: 0;}
.game {
position:absolute;
width:400px;
height:100%;
top: 0;
left:100px;
overflow:hidden;
}
.game > div {
position: absolute;
top: 100px;
height: 300px;
width: 100%;
background-color:#cccccc;
}
.block1 {
position:absolute;
width:100px; height:100px;
top:-50px; left:150px;
background-color:#ffcccc;
}
.block2 {
position:absolute;
width:100px; height:100px;
top:150px; left:-50px;
background-color:#ccffcc;
}
try Changing your game class to
.game {
width:400px; height:300px;
top:100px; left:100px;
background-color:#cccccc;
overflow-x:hidden;
overflow-y:auto;
}
Thanks,
Dhiraj