Div size with relative content - html

I have a div with some images inside:
<div>
<img><img><img><img>
<div>
The images would normally display in a row, however I have used some CSS to change their relative positions from left (-7px for the second one, -14px for the third, etc.) so they are stacked partially over each other. Is there a way to make the div to be only the width of the partially stacked images, rather than the width of where the images would be if they weren't moved?

If the content isn't dynamically generated then you can just manually set the div to the width you want using css.
<div class="overlap">
<img><img><img>
</div>
.overlap {
width: 200px;
}

If you set the css as
div img {position: absolute;}
they will all overlap without needing any repositioning.
It will set the size of the img, and div to 0 so it will only work if there is nothing below it on the page.

To center the images try wrapping them in another div. Not sure what you have in all of your css but you can get the idea anyway. Something Like:
.overlap{
width:200px;
outline:1px solid red;
position:relative;
}
.overlap img{
outline:1px solid blue;
display:block;
height:50px;
width:25px;
position:relative;
float:right;
}
.overlap img{
left:-7px;
}
.overlap img + img{
left:-14px;
}
.overlap img + img + img{
left:-21px;
}
#center{
width:100px;
margin:0px auto;
}
<div class="overlap">
<div id="center">
<img><img><img>
</div>
</div>

Related

Arranging 3 div's in a container and one with auto height

I am trying to arrange 3 divs side by using float:left, there is fixed height for two div's child1 and child3, but there is no height for child2, i need the child2 div height as the same height of the container div.
<div id="container">
<div id="child1">Child1</div>
<div id="child2">Child2</div>
<div id="child3">Child3</div>
<div>
#container
{
margin-left: 3px;
padding: 10px 0px;
position: relative;
display: block;
width: 500px;
background:yellow;
overflow:hidden;
}
#child1
{
float:left;
width:100px;
height:300px;
background:green;
}
#child2
{
float:left;
width:100px;
height:auto;
background:cyan;
}
#child3
{
float:left;
width:100px;
height:400px;
background:red;
}
here is the fiddle: http://jsfiddle.net/2ksxL/2/
You can change the #container {display: flex;}, but that does not have awesome support in IE (http://caniuse.com/flexbox). If you need more support you will have to come up with a jQuery solution that can find the height of the container and give it to #child2.
Since you haven't define any height for container, the container height is going to depend on the max height that's been defined to the #childX. In this case, #child3. So what you can do is compare the height of both #chidl1 and #child3 and set the height of #child2 to the max one via this little jQuery.
var highestCol = Math.max($('#child1').height(),$('#child3').height());
$('#child2').height(highestCol);
FIDDLE

Center a Variable Width/Height Image Inside Fixed Height/Variable Width Container

Goal:
A wrapper div that is fixed height and variable width. This wrapper contains a picture, which will have variable height and width (Could be portrait or landscape).
The picture should resize automatically to be either 90% of the wrapper's width or 90% of the wrapper's height, whichever is the smallest. It should also be constantly centered, both horizontally and vertically.
It's somewhat hard to describe via text, so see the manipulated screenshot below for an example of three possible images, when the wrapper is three different possible widths:
What I've Got Now:
I'm using a structure of: div->span->div->img.
I've got the sizing working completely. I've got the horizontal positioning working. I've got the vertical positioning working when the image is constrained by height.
When the image starts to resize smaller (constrained by width), the vertical positioning doesn't work. This is because the inner div isn't resizing.
Code
Working Fiddle
HTML:
<div class="selected-thumb">
<span>
<div>
<img src="http://www.andymercer.net/wp-content/uploads/2014/01/tree26-300x225.jpg"></img>
</div>
</span>
</div>
<div class="selected-thumb">
<span>
<div>
<img src="http://www.andymercer.net/wp-content/uploads/2014/03/ada_complete-300x171.png"></img>
</div>
</span>
</div>
<div class="selected-thumb">
<span>
<div>
<img src="http://www.andymercer.net/wp-content/uploads/2013/12/employee_randy_fake-209x300.jpg"></img>
</div>
</span>
</div>
CSS:
.selected-thumb {
text-align: center;
background: #c0c0c0;
border: #a0a0a0 solid 1px;
margin: 20px;
height:200px;
}
.selected-thumb span {
display:block;
height:200px;
}
.selected-thumb span div {
position:relative;
max-height:90%;
max-width:90%;
height:200px;
top:50%;
margin:0 auto;
}
.selected-thumb span div img {
width:auto;
height:auto;
max-height:100%;
max-width:100%;
position:relative;
top:-50%;
}
What I Need:
I need to fix the vertical placement, and I am running into a wall. I've tried using display:table, I've tried creating a pseudo-element and using display:inline-block. I can't find a way to accomplish what I need. All useful info will be upvoted regardless of which I select as correct.
Check this:
html,body {height:100%;}
.selected-thumb {
text-align: center;
background: #c0c0c0;
border: #a0a0a0 solid 1px;
margin: 20px;
height:200px;
line-height: 200px;
}
.selected-thumb img {
display: inline-block;
vertical-align: middle;
width:auto;
height:auto;
max-height:90%;
max-width:90%;
}
fiddle: http://jsfiddle.net/Ts4W9/2/

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;
}

How to split page into 4 equal parts using two container divs?

I am designing a webpage that needs to be split into 4 equal DIVs. This would be easy if I didn't also need to overlap text onto two of these DIVs. So, I have decided the best route would be to stack two container DIVs on top of each other, each with a width of 100% and height of 50%. Then, I would split these into two DIV classes, each with a height of 100% width of 50%, thus giving me 2 DIVs per container DIV, which are 2 in number.
My current CSS:
#collectionsTop {
width: 100%;
height: 50%;
overflow:hidden;
margin: 0 auto;
}
.topRight {
background-color:red;
width:50%;
height:100%;
float:left;
clear:both;
display:inline-block;
overflow:hidden;
}
.topLeft {
background-color:blue;
width:50%;
height:100%;
float:left;
clear:both;
display:inline-block;
overflow:hidden;
}
#collectionsBottom {
width: 100%;
height: 50%;
overflow:hidden;
margin: 0 auto;
}
.bottomRight {
background-color:yellow;
width:50%;
height:100%;
float:left;
clear:both;
display:inline-block;
overflow:hidden;
}
.bottomLeft {
background-color:green;
width:50%;
height:100%;
float:left;
clear:both;
display:inline-block;
overflow:hidden;
}
And my HTML:
<div id="collectionsTop">
<div class="topRight"><img src="http://www.solomovies.ch/uploads/blog/lorem-ipsum-1440x900-text-on.jpg"></div>
<div class="topLeft"><img src="http://www.solomovies.ch/uploads/blog/lorem-ipsum-1440x900-text-on.jpg"></div>
</div>
<div id="collectionsBottom">
<div class="bottomRight"><img src="http://www.solomovies.ch/uploads/blog/lorem-ipsum-1440x900-text-on.jpg"></div>
<div class="bottomLeft"><img src="http://www.solomovies.ch/uploads/blog/lorem-ipsum-1440x900-text-on.jpg"></div>
</div>
Apparently, none of the above works in any capacity at all, displaying the images in their full resolution, not floated, and in no way limited by their parent DIVs. I have no idea why. Please help.
You have placed - clear:both in the css of topLeft , topRight elements
idea => clear:both; - No floating elements allowed on the left or the right side of a specified element ,
hence in your case also similar thing is happening,
check this fiddle : http://jsfiddle.net/4q4Jz/
update:
now check the fiddle.. demo
remove all the `clear:both;
and try it.
`

How can I make div with overflow: hidden overlap floating div?

I am working on a site where a 3rd party in-line HTML editor is being used (CKEditor). I have the editor control wrapped in a DIV that is relatively positioned and has a z-index that places is at the top of the visible stack. The problem is that on some pages there are images that are floating (float: right) on the right side. Some of the CKEditor styles are setting elements overflow property to hidden (overflow: hidden).
So although my containing DIV has a larger z-index than the floating image the CKEditor elements are not overflowing on top of the image. This creates the a result that looks as if the top right corner of the editor has been cut out.
Is there a way I can work around this without trying to edit CKEditor styles? Check out this example sinario:
http://jsfiddle.net/nmartin867/StHJA/
HTML
<body>
<div class="floating">
I'm floating!
</div>
<div class="container">
<div class="inner">
Why am I not overlapping?
</div>
</div>
CSS:
div{
border: solid 1px red;
}
.container{
height:300px;
position: relative;
overflow: visible;
z-index: 1;
background-color:black;
color: blue;
}
.inner{
background-color:yellow;
overflow:hidden;
/*overflow:visible;*/ <--This would work
text-align: right;
}
.floating{
color:black;
width:100px;
height:100px;
background-color:green;
float:right;
}
You could do this but I am not sure if it applies to your situation.
.inner{
background-color:yellow;
position: absolute;
width:100%;
text-align: right;
}
Alternatively when you want to override third party styles but do not wish to edit them in the third party application you can recreate the same css class in your own stylesheet and force it to overwrite the third parties by using important! eg:
float: none !important;
Have you tried absolute positioning instead? Because you are floating a DIV that is not in the same container you want to overlap, it will position outside in the body itself. Also, you did not set the z-index for the floated DIV, so it will be layered behind because it is ahead of the other container in sequential order.
div{
border: solid 1px red;
}
.container{
height:300px;
position: relative;
overflow: visible;
z-index: 1;
background-color:black;
color: blue;
}
.inner{
background-color:yellow;
overflow:hidden;
/*overflow:hidden;*/
text-align: right;
}
.floating{
color:black;
width:100px;
height:100px;
background-color:green;
/* float:right;*/
position:absolute;
top:0px;
right:0px;
z-index:2;
}
I am not sure if this is the effect you want to accomplish, but this will position the first container on the top.