In the following HTML, I want the small delete icon to appear in the upper left corner of its container (the div). The larger image (a cat) needs to be inside of the div and scale so that it does not exceed the height of the div. I need the div to float left because of how its used elsewhere. The delete icon is suppose to overlay on top of the larger image (if the larger image fills the width of the div). Please note, that the larger image may actually have a width that is much less than the div and it gets centered in the div. In this case, the delete icon, still is in the upper left corner but is not really overlaying on top of the larger image since the larger image would be too small. The width of the div always remains the same regardless of the width of the larger image.
Here is my html:
<div style="float:left; width:120px; height:90px; text-align:center; border:1px solid #c0c0c0">
<img src="http://hellopune.mobi/site2/Images/icon_delete.png" />
<img src="http://www.petfinder.com/wp-content/uploads/2012/11/100691619-what-is-cat-fostering-632x475.jpg" style="height:90px" />
</div>
And in fiddler:
http://jsfiddle.net/AndroidDev/eJZ7X/
Do you need something like this?
Demo
div {
position: relative;
}
div img {
max-width: 100%;
max-height: 100%;
}
div img:first-of-type {
position: absolute;
top: 5px;
right: 5px;
}
Here, am positioning the delete img to absolute with top and right properties, if you want left than you can do that too, and make sure you wrap them inside a position: relative; container.
Note: Am using first-of-type pseudo so you do not have to alter your
DOM, but if you think that the order of the img might change than
better assign a class to the delete img instead.
As you feel my selectors are too generic, assume that you have a parent with a class called .img_holder and this will have div nested further and than you nest both the img inside that so your selector will be
.img_holder > div {
position: relative;
}
.img_holder > div > img {
max-width: 100%;
max-height: 100%;
}
.img_holder > div > img:first-of-type {
position: absolute;
top: 5px;
right: 5px;
}
And the DOM would look like
<div class="img_holder">
<div>
<img src="#" />
<img src="#" />
</div>
<div>
<img src="#" />
<img src="#" />
</div>
<div>
<img src="#" />
<img src="#" />
</div>
</div>
I've updated your fiddle here
The icon img now has an icon class and is absolute positioned in the div.
html:
<div style="float:left; width:120px; height:90px; text-align:center; border:1px solid #c0c0c0">
<img class="icon" src="http://hellopune.mobi/site2/Images/icon_delete.png" />
<img src="http://www.petfinder.com/wp-content/uploads/2012/11/100691619-what-is-cat-fostering-632x475.jpg" style="height:90px" />
</div>
css:
.icon {
position: absolute;
}
Related
I tried to position my logo and headerpic on top of each other like this:
img {
position: absolute;
top: 25px;
left: 25px;
}
.imgA1 {
z-index: 1;
}
.imgB1 {
z-index: 3;
}
<img class="imgA1" src="https://placehold.it/200/333333">
<img class="imgB1" src="https://placehold.it/100">
But my logo is still at the top end of my header pic.
My css code looks like this:
.header img.headerpic {
max-width:100%;
float:left;
position:relative;
background:transparent url(../images/header.jpg)
}
.header img.logo {
position: relative;
float:left;
max-width:100%;
background:transparent url(../images/logo.png )
}
and i added this in my index.php:
<body id="home">
<!-- header area -->
<div class="header">
<id="logo">
<img src="<?php echo TEMPLATE_DIR; ?>/images/logo.png" alt="logo"/>
<img class="headerpic" src="<?php echo TEMPLATE_DIR; ?>/images/headspacer.jpg" alt="" />
<div class="infobox"><div class="inner">
</div>
</body>
What do i need to change that my Header-Picture is the background and my logo is on the left in the center of the Picture?
My actual view
position:absolute is relative to the nearest positioned parent, or the entire page. Therefore, here you are setting the images to be at the exact same location.
What you need is for .imgB1 to set position: relative and then move it to place with the top and others. E.g something like so:
#logo img {
position:absolute;
}
.header img.headerpic {
max-width:100%;
top: 10px;
left: 10px;
position: relative !important;
}
<div class="header" id="logo">
<img src="https://placehold.it/200/333333" alt="logo"/>
<img class="headerpic" src="https://placehold.it/100" alt="" />
<div class="infobox"><div class="inner">
</div>
I'm not actually tested this but, if it works thumbs up. I forget to complete my css classes and goes to php and not I have full knowledge of php
margin-left: 25%;
margin-top:25%;
Increase the z-index, case may be that your logo has more z index than img divs
If you want both images to stack on top of each other, what you need to do is to set header as relative and img to absolute like so:
.header{
position: relative;
}
.header img{
position: absolute;
}
<div class="header">
<img class="imgA1" src="https://placehold.it/200/333333">
<img class="imgB1" src="https://placehold.it/100">
</div>
By setting both img.headerpic and img.logo to position:relative, both will occupy their own space and thus won't stack on top of each other.
By defining the parent's position as relative, in this case .header, anything inside .header with an img tag that's positioned absolute will occupy the same space, relative to the parent.
Hi i feel really stupid for asking this, I have a site that I am working on and I am trying to position my profile picture absolutely to a div with a position of relative, however it doesn't work and the parent div does not wrap around the img. I'm sure it's a simple solution. Does the parent div have to have a height and width?
.parent {
position: relative;
}
#profilepic {
position: absolute;
}
<div class="parent">
<a href="https://uk.linkedin.com/pub/siavoush-redhai/a5/377/b02" target="_blank">
<img id="profilepic" src="Images/Portraits%20circle.png" alt="profile picture"/>
</a>
</div>
I have not made the changes to the live site yet but the issue can be replicated in the browser
EDIT Thank you for the help
I assume, you got something wrong there about absolute positioning of HTML elements.
By setting an element to position:absolute; you take it entirely out of the document flow. Therefore there is no kind of automatic wrapping around of any parant element. The only connection to the actual parent Element is that you might have that one set to position:relative so the coordinates of your absolutely positioned elements depend on the position and dimension of its parent.
Your Idea only works properly here, if you know exactly the size of the final image. Then you could use something like that: https://jsfiddle.net/k98cLdvq/
CSS for this looks like:
.parent {
position: relative;
border:1px solid #c00;
width:200px;
height:200px;
}
#profilepic {
position: absolute;
border:1px solid #0c0;
width:100px;
height:100px;
left:50px;
top:50px;
box-sizing:border-box;
}
By adding the following you would get the effect you described:
a:hover #profilepic{
width:120px;
height:120px;
left:40px;
top:40px;
}
You are going to have to set the width and height of the container to provide a big enough base for the image to sit on top of (the container will not wrap around it as the absolutely positioned image is out of the document flow and instead sits on top) - ensure you have position:relative on the container and presuming you want the image at top left then top:0 and left:0 on the image. I presume you are positioning absolutely for layering? If not I would question using absolute positioning given the extra complication of making the underlying container big enough.
When positioning absolutely, you should remember, about absolute positioning is that these elements are removed from the flow of elements on the page. An element with this type of positioning is not affected by other elements and it doesn't affect other elements. This is a serious thing to consider every time you use absolute positioning. It's overuse or improper use can limit the flexibility of your site.
https://developer.mozilla.org/en-US/docs/Web/CSS/position#Absolute_positioning
JS Fiddle
.parent {
position: relative;
background-color: green;
height: 150px;
width: 200px;
display: inline-block;
margin: 2px;
}
#profilepic {
position: absolute;
width: 100%;
height: 100%;
transition: all 0.5s;
border: 2px solid navy;
}
#profilepic:hover {
width: 120%;
height: 115%;
z-index: 10;
transition: all 1s;
}
<div class="parent">
<a href="https://uk.linkedin.com/pub/siavoush-redhai/a5/377/b02" target="_blank">
<img id="profilepic" src="//placehold.it/200x150?text=image1" alt="profile picture" />
</a>
</div>
<div class="parent">
<a href="https://uk.linkedin.com/pub/siavoush-redhai/a5/377/b02" target="_blank">
<img id="profilepic" src="//placehold.it/200x150?text=image2" alt="profile picture" />
</a>
</div>
<div class="parent">
<a href="https://uk.linkedin.com/pub/siavoush-redhai/a5/377/b02" target="_blank">
<img id="profilepic" src="//placehold.it/200x150?text=image3" alt="profile picture" />
</a>
</div>
<div class="parent">
<a href="https://uk.linkedin.com/pub/siavoush-redhai/a5/377/b02" target="_blank">
<img id="profilepic" src="//placehold.it/200x150?text=image4" alt="profile picture" />
</a>
</div>
When setting the position properties, you're best off setting top/bottom and left/right to get the results you expect. But you shouldn't, as you've said above,
set the top and bottom to 0
Instead, you should set either the top or the bottom property because setting both properties will cause them to contradict each other and the browser is likely to ignore both or pick one depending on the implementation.
Try this out: https://jsfiddle.net/9vbojg0w/
HTML
<div class="parent">
<a href="https://uk.linkedin.com/pub/siavoush-redhai/a5/377/b02" target="_blank">
<img id="profilepic" src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2e/Charles_Darwin_seated_crop.jpg/220px-Charles_Darwin_seated_crop.jpg"/>
</a>
</div>
CSS
/* background-color and width/height only for demonstration
* width/height can be removed if other elements cause the
* parent's size to expand
*/
.parent {
position: relative;
background-color:#ddd;
width:200px;
height:300px;
}
#profilepic {
position: absolute;
top:20px;
left:20px;
}
I have an image within a parent div. I also want to have some text underneath the image within that parent div, but I only want the width of that text div to be as large as the image.
<div class="parent">
<div class="child">
<div class="image-container">
<img src="..." />
</div>
<div class="text">
...
</div>
</div>
</div>
Here is a jsfiddle that illustrates my problem:
jsfiddle
How can I resolve this? I can't put the text inside the same div as the image because the image is cut off using a max-height css.
Is this what you were after? Can you use jquery?
$('.child').width($('.image-container').width());
http://jsfiddle.net/YRYZA/
I simplified your markup and css a little bit. You can keep them in the same parent. use position absolute for the text and add position relative to its parent. that way it will take the parent's width. and the parent's width will be set by whatever size the image is, hence the text will be the same width as the image at the end of the day.
html:
<div class="parent">
<div class="image-container">
<img src="http://lorempixel.com/400/600/" />
<div class="text">
text
</div>
</div>
</div>
CSS:
.parent {
width: 700px;
}
.image-container {
background: green;
float:left;
position: relative;
}
div.text {
background: green;
position: absolute;
width:100%;
left:0;
}
jsfiddle
Do this:
.child{ position: relative; }
.text{ position: absolute; left: 0px; right: 0px; }
Then .child div would be as wide as the image (not influenced by .text width) and .text would fit in the space.
JSFiddle: jsfiddle.net/8hV2E/12
I'm using twitter bootstrap 2.1.1 with a responsive layout and I'd like to float a label in the bottom right hand corner of the image. One of the problems i'm having is because it's responsive when the thumbnail is wider then the image it floats too far. The other issue is I can't seem to get it to float to the right. I should also add that although I want it in the right hand bottom corner I do want it offset by a few pixels to it isn't right on the edge of the image. Also the text might always be photo missing, that is just when a default image is shown.
Here is my html so far
<li class="span4">
<div class="photo-group thumbnail">
<a href="/recipes/50500235aa113eb1870001d8" class="photo-wrapper">
<img alt="300x200&text=photo" src="http://www.placehold.it/300x200&text=Photo">
<span class="label label-inverse photo-label">Photo Missing</span>
</a>
<div class="caption">
<div class="pull-right">
by <a href="/users/50494983aa113ebd5c000001">
hadees
</a>
</div>
Chocolate Crackle Cookies
</div>
</div>
</li>
and here is my css
.content-header {
padding-bottom: 10px;
}
.thumbnail > a > img {
display: block;
margin-left: auto;
margin-right: auto;
max-width: 100%;
}
.photo-group .photo-wrapper {
position: relative;
}
.photo-group .photo-wrapper .photo-label {
position: absolute;
bottom: 0;
}
.photo-group .photo-wrapper .photo-label {
float: right;
}
I've also got a jsfiddle for a better example.
Maybe the right way to handle this is to just make the photogroup's max width 300 but I think that kind of looks funny on a desktop full screen.
Here's my version of the fiddle: http://jsfiddle.net/AdVCT/9/
Essentially, you need to put a wrapper around both the image and the label, so that you can absolutely position the label within this wrapper:
<div class="photo">
<img alt="..." src="..." />
<span class="label label-inverse photo-label">Photo Missing</span>
</div>
And then with some CSS, you can alter the .photo to be centered but also only as large as the image inside it (the caption is absolutely positioned, so it is effectively taken out of the page flow and doesn't affect width of parents etc.):
.photo-group .photo-wrapper .photo {
position: relative;
margin: 0 auto;
display: table;
}
And remove margin-left: auto/margin-right: auto from the .thumbnail > a > img rule. Finally, to offset the label slightly from the sides of the image, use:
.photo-group .photo-wrapper .photo-label {
position: absolute;
bottom: 5px;
right: 5px;
}
And set 5px to whatever you want the offsets to be.
I've got 2 divs on top of another div, but when I resize the window they stay put, instead of moving with the div behind them. I thought that if there position is relative they move along with the previous div, but it doesn't seem to work like this. What am I missing?
CSS and HTML below, simplified.
<div class="wrapper" style="margin: 0 auto; text-align: center;">
<div id="0">
<img src="0.gif" width="960px" alt="" />
<div class="top">
<div id="tag" ><img src="tag.png" alt="" /></div>
<div id="tag2"><img src="5500.png" alt="" /></div>
</div>
</div>
</div>
And CSS like this:
#0 {
clear: right;
z-index: 0;
}
.top {
float: left;
z-index: 1;
clear: left;
position: relative;
}
#tag {
margin-top: -500px;
margin-left: -600px;
}
#tag2{
margin-left: 750px;
}
It seems like you're expecting the top div, with its associated tag divs, to move with the 0.gif image when you resize the page. Instead, it's moving relative to the left edge of the wrapper div, which is pegged to the left-hand side of the screen. You will probably get the result you want by adding width: 960px to the style of your .wrapper div.
For further enlightenment, check the bounding boxes on each of your divs. You may find that a temporary border: 2px solid green style helps clear things up.