How to contain position absolute div? - html

I have this fiddle here and this is the illustration below
what I need to achieve is to make the black container dynamically expand base on the item inside (the items are A, B, C) the output must be
without setting the height statically
my html is
<div class="container">
<div class="itemA">A</div>
<div class="itemB">B</div>
<div class="itemC">C</div>
<div>
my css is
.container{
position:relative;
width:200px;
min-height:300px;
background-color:black
}
.itemA{
position:absolute;
top:260px;
background-color:red;
width:30px;
height:30px;
}
.itemB{
position:absolute;
top:50px;
right:90px;
background-color:green;
width:30px;
height:30px;
}
.itemC{
position:absolute;
top:220px;
right:50px;
background-color:blue;
width:30px;
height:30px;
}

You can use this script. First compute the max height then set the height of the container
$(function(){
var y = 0;
$('.container .item').each(function(){
y = Math.max(y, $(this).height() + $(this).position().top);
});
$('.container').css('height', y);
});
.container{
position:relative;
width:200px;
min-height:200px;
background-color:black
}
.itemA{
position:absolute;
top:260px;
background-color:red;
width:30px;
height:30px;
}
.itemB{
position:absolute;
top:50px;
right:90px;
background-color:green;
width:30px;
height:30px;
}
.itemC{
position:absolute;
top:220px;
right:50px;
background-color:blue;
width:30px;
height:30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
<div class="itemA item">A</div>
<div class="itemB item">B</div>
<div class="itemC item">C</div>
<div>

I prefer to using jQuery so here it is, "borrowed" this answer's code as its pretty much what we need. Just made some small changes.
So we look at the parents children div and get the farthest child position, add that child's height and then set the parents height to that. Boom, done.
var t = 0;
$(".container div").each(function() {
var position = $(this).position();
if (position.top > t) {
// Get the position and the height so we can set the parent height
t = position.top + $(this).height();
$('.container').height(t);
}
});
.container {
position: relative;
width: 200px;
min-height: 200px;
background-color: black
}
.itemA {
position: absolute;
top: 260px;
background-color: red;
width: 30px;
height: 30px;
}
.itemB {
position: absolute;
top: 50px;
right: 90px;
background-color: green;
width: 30px;
height: 30px;
}
.itemC {
position: absolute;
top: 220px;
right: 50px;
background-color: blue;
width: 30px;
height: 30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="itemA">A</div>
<div class="itemB">B</div>
<div class="itemC">C</div>
</div>

In js you can have the position of each absolute-positionned elements. I wrote you a script that find the biggest value horizontaly and verticaly and apply this width and height to the container : http://jsfiddle.net/45atnh0u/7/
Its basically use $(this).offset().left and $(this).offset().top
But be carrefull with the right and bottom values.

The closest is to give the container overflow: hidden , then move your elements left right top bottom in the dimensions of your container . If the container has width 200px you move element left 200px - element width and it shall remain in your container.
Updated fiddle:
.container{
display: block;
position:relative;
width:200px;
min-height:200px;
background-color:black;
overflow: hidden;
}
.itemA{
position:absolute;
top: 0;
left: 170px;
background-color:red;
width:30px;
height:30px;
}
.itemB{
display: block;
position:absolute;
top:50px;
right:90px;
background-color:green;
width:30px;
height:30px;
}
.itemC{
display: block;
position:absolute;
bottom: 0;
right:30px;
background-color:blue;
width:30px;
height:30px;
}
http://jsfiddle.net/45atnh0u/8/

As mentioned above JS might be a better fit, but I thought a quicker way with just CSS might also appeal.
The idea is for the container to take the height of the body.
Add
body {
height: 100%;
position: absolute;
}
and change your container height to
min-height:100%;
Here's an updated fiddle.
Hope it helps you.

Related

Place <div>-Container on the right side of a margin:auto centered <div>

I want to position a div on the right side of a other div which is centered via margin:auto. (Like a sidebar)
Simple HTML:
#c1{
background-color:#CCCCCC;
width:300px;
margin:auto;
}
#c2{
background-color:#BBBBBB;
width:100px;
}
<div id="c1">con1</div>
<div id="c2">con2</div>
I have tried the solution from this question but it does not work in my fiddle. The second div is in the next line. Link:
positioning elements left and right of a div with margin:auto
How to solve this?
You can use positioning to create this effect:
#c1{
background-color:#CCCCCC;
width:300px;
margin:auto;
position:relative;
}
#c2{
background-color:#BBBBBB;
width:100px;
position:absolute;
right:-100px;
top:0;
}
And then you need to rearrange the HTML to look like this:
<div id="c1">
con1
<div id="c2">con2</div>
</div>
HTML:
<div id="c1">
con1
<div id="c2">con2</div>
</div>
CSS:
#c1 {
background-color:#CCCCCC;
width:300px;
margin:auto;
position: relative;
}
#c2 {
background-color:#BBBBBB;
width:100px;
position: absolute;
right: -100px;
top: 0px;
}
Here is JSFiddle
Using position property and a little moderation in your DOM you can easily achieve this.
Here's the Code:
.wrapper {
position: relative;
width: 100%;
padding:0;
margin:0;
}
#c1 {
background-color:#CCCCCC;
width:300px;
margin:auto;
height:100px;
position: absolute;
left:0;
right:0;
top:0;
}
#c2 {
background-color:#294596;
width:100px;
height:100px;
position: absolute;
left:0;
top:0;
}
<div class="wrapper">
<div id="c1">con1</div>
<div id="c2">con2</div>
</div>
It will do the trick for you.

Make an overlay div fill entire of a container having overflow

I want to display a loader inside the container. I am trying to display the overlay div inside the container.
if I use absolute position, the overlay also going top.
Here is Fddle : http://jsfiddle.net/vaykmry4/5/
Code :
<style>
.container
{
margin: 25%;
position:relative;
width:200px;
height:200px;
border:3px solid #ddd;
overflow:auto;
}
.overlay {
width:100%;
height:100%;
margin:auto;
left:0;
top:0;
position:absolute;
background:#fff;
opacity:.8;
text-align:center;
}
.loader {
display:inline-block;
}
</style>
<div class="container">
<div class="overlay">
<span class="loader">
loading...
</span>
</div>
<div class="content">Here is content ... <div>
</div>
Thanks.
First of all I should note that a fixed element is positioned relative to the initial containing block which is established for the html element.
Hence you should use absolute positioning to position the overlay relative to its nearest containing block which is established by the container.
.container {
position: relative;
overflow: auto;
}
.overlay { position: absolute; }
Second, It will work until the content start growing. When the content height gets bigger than the overlay, the overlay will not fill the entire space of the container anymore.
Since you may use JavaScript in order to to display the overlay (including loading, etc.) one solution is to add overflow: hidden; to the container to prevent from scrolling.
Finally, you should set top property of the .overlay element according to the position of the vertical scroll-bar.
Here is the jQuery version of the above approach:
var $container = $(".container");
$(".overlay").fadeIn().css("top", $container.scrollTop() + "px");
$container.css("overflow", "hidden");
EXAMPLE HERE
You are using margin: 25% on container which is causing the gap of 50% top-bottom value for overlay, so use height: 150% instead of 100%
.container
{
margin: 25%;
position:relative;
width:200px;
height:200px;
border:3px solid #ddd;
overflow:auto;
}
.overlay {
width:100%;
height: 150%;
margin:auto;
left:0;
top:0;
bottom: 0;
right: 0;
position:absolute;
background:#000;
opacity:.5;
}
.content {
height:300px;
}
working fiddle
position: absolute will let you place any page element exactly where you want it with the help of top right bottom left attributes. These values will be relative to the next parent element.
position: fixed is a special case of absolute positioning. A fixed position element is positioned relative to the viewport.
In your case you should use position: absolute for your .overlay
Use this:
HTML:
<div class="container overlay">
<div class="content"><div>
</div>
CSS:
.container
{
margin: 25%;
position:relative;
width:200px;
height:200px;
border:3px solid #ddd;
overflow:auto;
}
.overlay {
margin:auto;
left:0;
top:0;
position:relative;
background:#000;
opacity:.5;
}
.content {
height:300px;
}
Here is the working fiddle

Issue with having a div centered in the middle of the page

Before someone labels this as a duplicate, I have searched on here and not finding a solution that quite fits what I need to do.
Here is my fiddle
Here is the code for the html..
<div class="top-background"></div>
<div class="center-content"></div>
<div class="bottom-background"></div>
and here is the CSS stylesheet...
body {
margin: 0;
padding:0;
}
.top-background {
background-color:black;
width:100%;
height:50%;
position:absolute;
top:0;
left:0;
}
.bottom-background {
background-color:white;
width:100%;
height:50%;
position:absolute;
bottom:0;
}
.center-content {
background-color:yellow;
width:250px;
height:120px;
margin: auto;
position:relative;
}
I can move the center-content div towards center by using top:300px. But that won't be any good because of depending on screen size.
The center-content div will have a graphic in it (it will not be a yellow background as shown), the graphic is reversed in the colors I have. I could have probably done this in the body along with text-align center, but then everything I put in will align center (and that's not going to be pretty) and used a table.
For doing this assing some css properties to the class center-content is enough..
.center-content {
background-color:yellow;
width:250px;
height:120px;
margin: auto;
position:relative;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -125px;
}
See this fiddle http://jsfiddle.net/Cwm76/sscuL/
Apply this:
.center-content {
background-color:yellow;
width:250px;
height:120px;
margin: auto;
position:absolute;
top: 50%;
left: 50%;
margin-top: -60px; /* half of the height */
margin-left: -125px; /* half of the width */
}

Can i make a trespass div area like this?

I'm new to front end design.
made a code like this.
<div id="header">
header
</div>
<div id="content">
content
</div>
<div id="sidebar">
in here will be login module
</div>
how can the sidebar will be trespassing area like the pic?
any good solution?
If you simple need to position sidebar like this, you can use position: absolute. As a very basic test add following classes to your HTML:
body {
margin:0
}
div {
border: 1px solid black
}
#header {
height: 50px;
}
#content {
height:200px
}
#sidebar {
position: absolute;
top: 0;
left: 100px;
width: 100px;
height: 250px;
}
And here is a demo: http://jsfiddle.net/Mxqh4/
Again this is just a demo, and in a real project would have to be dynamically adjusted, but that's should give you a start.
Of course it's possible. Anything is possible with CSS ;)
You have to learn to use the position property and float.
DEMO HERE
#wrap{
width:100%;
margin:0 auto;
}
#header{
width:100%;
height:50px;
background:orange;
}
#content{
width:100%;
height:500px;
background:green;
float:;
}
#sidebar{
width:25%;
height:550px;
background:red;
float:left;
position:relative;
bottom:550px;
left:20px;
}

w images in header float issue

I have a header that is larger than most screen widths. I centered that and I have the overflow hidden so when you expand your browser on a bigger screen more of it is visible. I also have 2 images on top of that, one floating right and one floating left. my problem is that the left image is in place floating left but the right image won't go all the way right. both if I put both images on the same z-index they just stack instead of floating right and left. Any suggestions? here is my css and html:
#triangleleft{
width:100%;
height:531px;
z-index:58;
position:absolute;
top:+53px;
}
#triangleright{
width:100%;
height:531px;
z-index:59;
position:absolute;
top:+53px;
}
.triangleleft{
background:url(Layer-58.png)no-repeat;
float:left;
margin-left:0px;
height:531px;
width:100%;
}
.triangleright{
background:url(Layer-59.png)no-repeat;
float:right;
margin-right:0px;
height:531px;
width:100%;
}
<div id="triangleleft">
<div class="triangleleft"></div>
</div>
<div id="triangleright">
<div class="triangleright"></div>
</div>
also here is the code for my header image that I think is screwing this up
#wrapper {
height:100%;
position: relative;
}
#Layer-57 {
position: relative;
height:529px;
background:#3b96a9 url(layer-57.jpg) top center no-repeat;
top:-529px;
overflow-x: hidden;
z-index: 57;
}
<div id="wrapper"> <div id="Layer-57"></div> </div>
replace your style with this
<style>
#triangleleft {
width:90%;
height: 531px;
z-index: 58;
position: absolute;
top: +53px;
}
#triangleright {
width:90%;
height: 531px;
z-index: 59;
position: absolute;
top: +53px;
}
.triangleleft {
background: url(Layer-58.png)no-repeat;
float: left;
margin-left: 0px;
height: 531px;
width: 100%;
}
.triangleright {
background: url(Layer-59.png)no-repeat;
float: right;
margin-right: 0px;
height: 531px;
width: 100%;
}
</style>
Revised Answer (previous answer removed for clarity's sake):
Looking closer at the leaderbe.com page you referenced in your comment below, I noticed that the HTML structure of the divs was quite different than what you had. You need to put the triangleright div inside the triangleleft div and use styles like follows:
See this jsfiddle: http://jsfiddle.net/uKrNT/2/
<div id="wrapper"> <div id="Layer-57">layer 57</div> </div>
<div id="triangleleft">
<div id="triangleright">
</div>
</div>
#triangleleft{
width:100%;
height:531px;
z-index:58;
position:absolute;
top:+53px;
float:left;
background:red url(http://www.leaderbe.com/images/diamond-left.png)no-repeat;
margin-left:0px;
overflow:visible;
opacity:.5;
}
#triangleright{
width:100%;
height:531px;
z-index:59;
float:right;
background:blue url(http://www.leaderbe.com/images/diamond-right.png)no-repeat;
margin-right:0px;
opacity: .5;
overflow:visible;
}
#wrapper { height:100%; position: relative; }
#Layer-57 { position: relative; height:529px; background:#3b96a9 url(layer-57.jpg) top center no-repeat; top:-529px; overflow-x: hidden; z-index: 57; }