HTML + CSS - Overlapping Header Image - html

I have seen the layout similar to the image below used on some sites before and I really like the design but don't exactly know how to implement the overlapping image (Profile Image). I am using bootstrap if that helps. Any ideas?
Thanks!

I can see three ways to do this generally.
position: absolute
You could give the image or the image's wrapper the attribute of position:absolute and giving its container (in your example the green box) position:relative. Then you would apply top: -100px or whatever and a left attribute of left: 100px or whatever. This gives the effect of the image being out of flow, aligned to the left and offset by 100px, and 100px offset from the top of the green container. The disadvantage of this approach would be that any body content in your green container could appear under the image.
position: relative
This is the same approach as the first one with the exception of how the image flows in the document. Instead of giving the image position:absolute, you would give it position:relative. Relative works differently from absolute. instead of being x and y coordinates of the parent container, it's just shifted by however much you give as a value for top and left. So in this case, you would apply top:-100px and just leave the other directional values as default. this would shift your element by that amount but also leave its original spot in the document flow. As such you end up with a gap below the image that other content will flow around.
negative margin
I honestly would prefer this method in your case. In this method, you can give the image a negative margin (e.g. margin-top:-100px). This will offset the image, collapse the area below the image, and it will still retain some of its flow in the document. This means that the content of the green container will flow around the image but only around the part that is still inside the container. It won't have a ghost area that content flows around like with relative positioning, but it also doesn't entirely take the image out of flow like absolute positioning. One thing to keep in mind, however, is that if you try to use overflow of any kind other than the initial value, it will cause undesirable effects to your image.
Demo
Here's a quick little demo demonstrating all three methods in a simple use case: http://jsfiddle.net/jmarikle/2w4wqfxs/1

The profile image can be set with position: absolute; top: 20px; left: 20px, or something like that to keep in from taking up space in the flow of the page.

make the html element that holds the header image "position:relative". Then put the header image and the profile image in that element. then make the profile image "position:absolute" and utilize "top: XXpx" depending on how far you want it from the top of the header element. Same for "left".
see fiddle here
<div class="header">
<img src="" alt="my image" class="floatdown">
this is my header, image could go here too
</div>
<div class="body">
this is my body content
</div>
.header {
position: relative;
width: 100%;
height: 150px;
border: 2px solid #000;
text-align: right;
}
.body {
position: relative;
width: 100%;
border: 2px solid #000;
height: 500px;
text-align: right;
}
img {
width: 90px;
height: 90px;
border: 2px solid #ddd;
}
.floatdown {
position: absolute;
top: 100px;
left: 20px;
}

You can use the float property on your profile image to take it out of the "flow" of the document, and play with the margins to place it properly.
CSS :
#profile-image{
width: 100px;
height: 100px;
float: left;
margin: 100px;
}
The marginis used to push it down and place it properly.
You can see an example of this in a Fiddle : http://jsfiddle.net/y706d77a/
I wouldn't recommand using position: absolute as you can get very strange results with different resolutions. I would only use that as a last resort.

This can be done many ways.
Anytime you see something like that on the web you can just use your inspector or firebug and see how they are doing it to get some ideas.
It wouldn't hurt to do some research on the web about CSS positioning.
http://www.w3schools.com/css/css_positioning.asp
Another great site.
http://css-tricks.com/

I just finished it.
Here is a codepen link:
http://codepen.io/anon/pen/zxYrxE
HTML:
<div class="main-container">
<div class="header">
<p>This is the header div</p>
</div>
<div class="profile">
<p>Profile</p>
</div>
<div class="content">
<p>Some dummy content div</p>
</div>
</div>
CSS is to big to be pasted here, so just open the link.

Put the profile image in the header, make the position: absolute; and the image position: relative;, and give it a negative bottom value that's half the height of the image, and set left to position it horizontally to taste.
HTML
<header>
<img class="profile">
</header>
<div>Content</div>
CSS
header, div{
min-height: 110px;
background: darkgray;
}
header{
position: relative;
background: gray;
}
img{
position: absolute;
bottom: -50px;
left: 100px;
}
http://jsfiddle.net/dekqn84c/

Related

when overlapping an img-tag it renders background / border behind and text in front of img

For a simple landing page I wanted to let some text box overlap an header image. To make it simple, I just have a structure like:
<header>
<img src="path/to/img.png" />
<h1>Awesome headline</h1>
</header>
All elements are set to display:block and the h1 is dragged inside the image with a negative margin. I also gave the headline some padding and background:white.
Now the problem: The headline text is shown on top of the image but the background colour is behind it! You can see an example here: https://jsfiddle.net/cv12evLn/
My guess is, that a browser renders all sibling blocks in layers, starting with all backgrounds and borders, then rendering images (img-tags) and finally text on top of everything else.
Is that right? And why the actual… I mean, that seems crazy unexpected to me.
To solve the issue, I've put the headline in a wrapper and set this to position:absolute. See here for a live example: https://jsfiddle.net/f5sd1u6o/
Use position:relative rather than negative margin. Then the z-index works automatically.
#container {
width: 500px;
height: 300px;
margin: auto;
}
#container img {
display: block;
width: 100%;
}
#container h1 {
display: block;
width: 50%;
height: 1em;
margin: auto;
padding: .5em 1em 1em;
font-size: 3rem;
background: yellow;
text-align: center;
border: 1px solid red;
position: relative;
top: -4.6rem;
}
<div id="container">
<img src="//placekitten.com/500/300">
<h1>
headline
</h1>
</div>
To get the Z-index to work, you need to apply position:relative anyway but you can still use negative margin if that is a design requirement.
JSfiddle demo (with negative margin)
Basically, backgrounds are rendered first before anything else (as I understand it) so they always come at the bottom of the stacking order. You just need to create a new stacking context and changing the position property does that.
As it happens so does changing the opacity of the element so a quick fix is to set opacity:.9999;
JSfiddle Demo (opacity 'hack')

Image set to absolute is out of alignment = not inside parent div

I have a little problem i was hopeing you guys could help with. I have create an empty white box on my frontimage. To do that i have to change the image to absolute. After this, the image is now out of alignment. To make it easier to understand, there are two images i want to show you, so you can see the problem.
Image when it is set to Relative:
Its the water image in top.. here you see that it is perfect align (left and right side) with the aque color boxes under.
Image when it is set to Absolute:
Somehow when i set the image to absolute, it gets bigger in the right side. I have read some earlier post that the parant should be set to relative.. I mean that I already have done that.
Code:
<div class="row">
<div class="col-sm-12 contentpage">
<img id="frontimage" src="~/images/index.jpg" />
<div class="col-lg-4">
<div class="bookingbox">
</div>
</div>
</div>
</div>
Css:
#frontimage{
z-index: 1;
position: absolute;
}
.contentpage {
height: 650px;
background-color: white;
position: absolute;
}
.bookingbox {
background-color: green;
position: absolute;
height: 450px;
width: 300px;
z-index: 2;
position: absolute;
top: 30px;
left: 30px;
}
img {
max-width: 100%;
}
Now it works.. take a look at the picture :D
Of course it's out of alignment!
When you set the image to absolute, it breaks off the flow of the page, and basically creates a new "layer" to be on. You can say it's "absolutely" off the page flow.
When you set the image to absolute, to position it correctly, make the parent of the image relative, then position and resize the image according to the origin of the parent.
Make sure to add another div so it fits inside it! winks

How do I position an image to the top left corner of a div, and keep it there if the div moves?

I apologize if this has been answered time and time again. I remember searching thoroughly for an answer a couple years ago when I first wrote up my website script, but I couldn't ever find one. The same for now.
Recently I reworked my website's script so I can host it onto Weebly. Here is one of the four pages of my site that I need help with. As you can see, the images that pop up when the thumbnail is hovered over are absolutely positioned. For most computer resolutions and/or browsers, this will have the image appear out of the designated box.
How could I position them to the inner top left corner of the div? Or better yet, horizontally and vertically centered within it?
<section id="Sizes" style="float: left">
<a href="#Space">
<img class="Small" src="/files/theme/SampleD_Fun_Icon.png" width="150" height="150" alt="Sample 1: Day of Fun" />
<img class="Large" src="/files/theme/SampleD_Fun.png" width="150" height="150" alt="Sample 1: Day of Fun" />
</a>
...
</section>
<a id="Space"></a>
<span class="Popup">Hover over thumbnail to display sample artwork.</span>
<br style="clear: left" />
a:hover img.Small
{
border: 5px solid #21568b;
margin: 10px;
text-decoration: none;
}
section#Sizes a img.Large
{
border-width: 0;
height: 0;
left: 438px;
position: absolute;
top: 326px;
width: 0;
}
section#Sizes a:hover img.Large
{
height: 526px;
left: 438px;
position: absolute;
top: 326px;
width: 520px;
}
.Popup
{
border: 3px solid;
float: left;
height: 272px;
margin: 8px 20px 0px 0px;
padding-top: 254px;
text-align: center;
width: 520px;
}
Thank you for your time. :)
Your whole design is a bit fragile, and I wouldn't recommend building this this way in the first place, but you're looking for practical answers, so here's the smallest change I can think of that fixes your problem:
1) Add this to your style sheet:
body { position: relative; }
2) On line 40 from your main_style.css, change top: 326px to top: 316px and left: 438px to left: 428px, so that it becomes like this:
section#Sizes a:hover img.Large {position: absolute; top: 316px; left: 428px; width: 520px; height: 526px;}
How does that work?
Your images are place using absolute positioning. By default, that works relative to the viewport (the window). But by turning the body into position relative, it becomes a containing block, and position absolute is relative to the nearest containing block ancestor.
So now, your images are fixed within the body element, instead of being fixed relative to the window. Since the margins of the body element is what's changing size when you resize the window, that makes the various pieces of your content fixed relative to each other. You then just need to remove 10px from the top and left side, since that's the size of the border of your body element, and we're now measuring from inside the border.
TLDR: You can't do this in pure CSS.
You can easily position the image inside the container div if you place the image element inside the div element, and then use absolute positioning like top: 0; left: 0; (or with a number of other methods). But then you'd need JavaScript to correlate the hovered thumbnail with the popup full-size image.
Alternatively, you can have the full-size image be nested in the thumbnail element (like you currently have), but then you'd need JavaScript to position the full-size popup image inside the container div.
Of the two alternatives, I recommend the first: put all the popup images inside the target container, and use JavaScript to show or hide them when a thumbnail is hovered. Correlating the thumbnail and the full size image via JavaScript is going to be easier then writing positioning code.
I see you're using jQuery already so why not do something like this?
$('.Small').on('mouseover', function(){
$('.Popup').empty().html($(yourtarget).attr('img' , 'src'));
});
$('.Small').on('mouseout', function(){
$('.Popup').empty().html('Hover over thumbnail to display sample artwork.');
});
Just because everyone was saying it can't be done with pure css, I wanted to demonstrate that it can, and it is even quite easy. Have a look at the folowing example:
http://jsfiddle.net/aafa2zp5/
<div id='images-wrapper'>
<ul>
<li>
<img class='small' src='http://placehold.it/50/ff0000'/>
<img class='big' src='http://placehold.it/300/ff0000'/>
</li>
<!-- and some more similar thumb / image groups -->
</ul>
<div class='preview-area'></div>
</div>
CSS (or the relevant part at least)
#images-wrapper {
position: relative;
}
.big {
display: block;
position: absolute;
top: 54px;
right: 54px;
opacity: 0;
transition: opacity .5s;
}
.preview-area {
width: 350px;
height: 350px;
border: 4px solid blue;
position: absolute;
top: 21px;
right: 21px;
}
li:hover .big {
opacity: 1;
}
The key is to set a position relative to the wrapper (and keep all of the descendants as their default static). Then you can use this to position the preview area and the big images against by setting them to postion absolute and carefully calculating the correct postion. I even added a cross fade, just because it is so easy, but you could just as well work with display block / none if you prefer.
For smaller screens you may want to alter the dimensions and positioning inside a media query, but it still should be doable (though depending on the hover state is perhaps not the best idea on a touch device)
I hope you get the idea and you can figure out how to apply this technique to your own site. Feel free to ask if you want me to explain further or when you get stuck.

css tabs-changing from absolute to relative positioning

Now after several hours, I am still stuck at this problem; I am trying to make this box relatively positioned so that the results do not overlap my footer. I tried to achieve this via javascript and that did not work and now I am not sure how to make this relatively aligned.
Here is the jsfiddle: http://jsfiddle.net/Lp2kV/1/
I am sure the problem can be solved if I change content from absolute to relative but after that I am not able to align it the same way as now.
This is the part, where I think I need to edit positioning.
.content {
position: absolute;
top: 28px;
left: 0;
background: #eee;
right: 81px;
min-height: 200px;
padding: 20px;
border: 1px solid #ccc;
height:auto;
}
If you only want the result contents to be contained inside the DIV area and have a scrollbar at the right side, you can add the following property inside your .content selector. This is if you want the height to stay the same.
overflow: auto;
But if you want the height to be flexible, then you should implement this approach..
First, remove the height property.
<div id="results_1">
<div style="clear: left; height: 0; margin: 0; padding: 0;">
will become
<div id="results_1">
<div style="clear: left; margin: 0; padding: 0;">
Before the closing tag of <div class="content">, you should add <div style="clear: both;"></div>. This will automatically expand the gray container height depending on content.
Maybe what you'll have to do next is have a javascript code to compute height of the contents box then adjust the top margin of your footer via javascript so they won't overlap.
I highly suggest just use a jquery plugin instead to create a tabbed box and disregard my solutions. This will solve your positioning problems. The only way you were able to overlap .content boxes is because they are absolutely positioned.. and absolute positioned elements do not add up to the size of their parents. So the moment you change absolute to relative, the tabs will scatter because each tab will expand to the size of their child elements. You can't achieve tabbed boxes and not overlap the footer with your current approach, unless you use javascript, or apply overflow: auto; on the .content selector.
Hope this helps.

CSS positioning relative to total page area

I hope I can explain this.
I have an image which is "stuck" to the right hand side of the browser window, so when the browser window changes size it remains stuck to the right. The style I use is:
style="position :absolute; top :4px; right :4px"
I also have a minimum body width of 1024px:
body
{
min-width: 1024px;
}
This all works great until the window is resized less that 1024, then the image is still stuck to the right hand side of the window. But I would like it to stick to the right hand side of the total page area. If you see what I mean.
Thanks,
AJ
the css position attribute calculates based on a positioned parent. If your image does not have any parents which are positioned, then the absolute will be relative to the screen. You should be able to fix this by putting position:relative; in your css for the body or by wrapping everything in a div of the correct size with position:relative;
You may succeed with using a position: relative in a div containing your image, with some code like this :
<div style="position:relative; min-width: 1024px">
<div style="position: absolute; top: 10px; right: 4px; width: 20px; height: 20px; background-color: red"></div>
<div style="width: 1024px; margin: auto 0px; height: 400px; background-color: #AAA"> </div>
</div>
Example here: http://jsfiddle.net/7jafS/