Image vertical height in absolute div - html

I am trying to create my own lightbox, and now i am stuck on design part, i've manager to create slide box with horizontal responsive image as i want
<div class="gallery">
<div class="container">
<span class="centerer"></span>
<div>
<img src="http://images.car.bauercdn.com/pagefiles/18474/bmw_7-series_05.jpg">
</div>
</div>
<a class="arrow left" href="#">
<span class="centerer"></span>
<img src="http://i.imgur.com/GbeQojB.png" />
</a>
<a class="arrow right" href="#">
<span class="centerer"></span>
<img src="http://i.imgur.com/GbeQojB.png" />
</a>
</div>
_
a {
text-decoration: none;
}
.gallery {
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: #333;
}
.container {
width:80%;
height:100%;
position:absolute;
left:10%;
text-align: center;
}
.container div {
display:inline-block;
width:95%;
}
.centerer {
height: 100%;
display: inline-block;
vertical-align: middle;
}
img {
vertical-align: middle;
display: inline-block;
max-width: 100%;
max-height: 100%;
}
.arrow {
width:10%;
height:100%;
display:block;
position:fixed;
text-align: center;
}
.arrow img {
display:inline-block;
opacity:0.5;
width:50%;
}
.arrow:hover img {
opacity:1;
}
.arrow.left {
transform: rotate(180deg);
left:0;
}
.arrow.right {
right:0;
}
https://jsfiddle.net/4zpk43f2/
, but when i use it with vertical photo, it ... sucks.
I've tried to change some display settings, but it's or good for vertical, or good for horizontal.
https://jsfiddle.net/d470vkc9/1/

In your code, you limited the width of the image to 80% by applying width: 80% to the .container. and you gave height: 100% to the same. That is why the image was stretching to full, vertically. Mention height also as 80% as you did to the .container.
Try this:
.container {
height: 80%;
top: 10%;
/* other styles */
}
.container > div {
height: 100%; /* or less as you prefer */
}
Working Fiddle

Related

using percent instead of px to centre text over image

I want my text to overlap my image (only a bit) center left. I am trying things like top: -50%, but doesn't work. I won't have control over the height of the image.
I am using both relative positioned because there is a bunch one after the other, and using relative I can control the spacing between them.
css
.article-txt {
position: relative;
z-index:1;
font-size:35px;
font-weight:bold;
padding: 0 50px;
}
.article-img {
display: block;
position: relative;
margin-top:50px;
margin-bottom:-50px; /* overlap image */
z-index:0;
}
html
<div id="article-txt"></div>
<div id="article-img"></div>
what i can understand from your query you want to acheive this
.container {
position: relative;
width: 50%;
}
.image {
display: block;
width: 100%;
height: auto;
z-index:1;
}
.overlay {
position: relative;
top:-100px;
height: 100%;
width: 100%;
opacity: 1;
z-index:9;
}
.text {
color: red;
font-size: 20px;
}
<div class="container">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSt06aInHbKL6NwLIP-Kx90M-QjhLcnxEDe8LaehoDz7zuqBNFBHg" alt="Avatar" class="image">
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>

CSS/HTML place text under image, doesnt matter which viewport/res

i want to center a image and place some text under it.
The picture should be everytime in the middle of the screen, doesnt matter how i scale the viewport. Also the text has to be under the picture everytime.
The Backgroundcolor has to be #EF762F
It worked for me on one screen with this
https://jsfiddle.net/j2Lec36d/ , but if i watch it on another screen, with other resolution, the text is not centered under the image anymore.
Also the span has to be positioned absolute left with -100%, jQuery slides it then to 50%
HTML:
<div id="sh_start">
<img src="test.jpg"></img>
<span>Test</span>
</div>
CSS:
body, html { font-family: arial; font-size: 12px; margin:0; padding:0; width: 100%; height: 100%; background-color:#fff;overflow:hidden;}
#sh_start img {display: block;padding:5% 0 0 30%;width: 40%; height: auto; position:absolute; top:5%;left:0; right:0; bottom:0;}
#sh_start span {width: 100%; height: auto;display:block;position:absolute;top:58%;left:50%; color:#fff; font-family:ReplicaPro-Bold;font-size:50px;opacity:0.4;}
#sh_start {display:block;background-color:#EF762F;position: absolute; top:0;left:0;width:100%; height: 100%; overflow:hidden;}
You can do this quite simple using flexbox:
$(document).ready(function(){
$( "#sh_start span" ).animate({
left: "50%",
left: "toggle"
}, 5000, function() {
// Animation complete.
});
})
#sh_start img {display: block;width: 40%; height: auto; }
#sh_start span{width: 100%; height: auto;display:block; color:#fff; font-family:ReplicaPro-Bold;font-size:50px;opacity:0.4;
position: relative; left: 100%;
}
#sh_start {display:block;background-color:#EF762F;position: absolute; top:0;left:0;width:100%; height: 100%; overflow:hidden;display:flex;align-items: center; justify-content: center;flex-direction: column;text-align: center;}
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<div id="sh_start">
<img src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQNjQ76c71y3CL0xrOhpl3wf3W5X3Mi3ddv1SUH3UbkPBz3SWPAuw">
<span>Test</span>
</div>
You can try to use the framework bootstrap and do like this:
HTML:
<div class="container">
<div class="row text-center">
<div class="col-lg-12">
<img src="http://placehold.it/700x400" class="img-responsive" alt="">
<h1>Text under image</h1>
</div>
</div>
</div>
CSS:
img {
margin: 0 auto;
}
The image CSS:
#sh_start img {
display: block;
padding: 5% 0 100px 0;
width: 40%;
height: auto;
margin: 0 auto;
}
The span CSS:
#sh_start span {
width: 100%;
height: auto;
display: block;
position: relative;
bottom: 20px;
// left: -100%;
left: 0;
color: #fff;
font-family: ReplicaPro-Bold;
font-size: 50px;
opacity: 0.4;
text-align: center;
}
And the container CSS:
#sh_start {
background-color: #EF762F;
}
You can just animate the left property of the span from 100% to 0.
You can see the code in action here: https://jsfiddle.net/7pnrq1xd/1/
I'm not entrirely sure what your restrictions are or if they are restrictions at all but why not just do this to your span:
left:0;
text-align:center
#sh_start {
display:block;
background-color:#EF762F;
position: absolute;
top:0;
left:0;
width:100%;
height: 100%;
overflow:hidden;
}
#sh_start img {
display: block;
padding:5% 0 0 30%;
width: 40%;
height: auto;
position:absolute;
top:5%;
left:0;
right:0;
bottom:0;
}
#sh_start span{
width: 100%;
height: auto;
display:block;
position:absolute;
top:58%;
left:0;
color:#fff;
font-family:ReplicaPro-Bold;
font-size:50px;
opacity:0.4;
text-align:center;
}
<div id="sh_start">
<img src="test.jpg"></img>
<span>Test</span>
</div>
First Create a DIV Tag where you will place both your image and caption.
<div class="imagewrapper">
<img src="imagehere.jpg">
<p class="imagecaption">Image Caption Here</p>
</div>
Now Code your CSS Something like this.
.imagewrapper {text-align:center}
.imagewrapper img {max-width:100%}
Do not forget to add styling for your image caption class. With above code your image and text will stay in center on any screen size.
Hope that helps.
Regards
Manoj Soni
I have removed the position:absolute declarations, and compacted the CSS code
#sh_start
{
background-color:#EF762F;
width:100%;
height: 100vh;
float:left;
}
#sh_start img, #sh_start span
{
float:left;
width:40%;
height:auto;
margin-top:5%;
margin-left:30%;
text-align:center;
}
#sh_start span
{
color:#fff;
font-family:ReplicaPro-Bold;
font-size:50px;
opacity:0.4;
margin-top:1px;
}
Then in the HTML I have fixed the 'img' closing tag.
<div id="sh_start">
<img src="test.jpg" />
<span>Test</span>
</div>
Live demo: https://jsfiddle.net/j2Lec36d/10/

Align 2 different elements vertically

I'm working on an div that comprises of 2 elements: an image and a text. The image occupying the first half (top) of the div, and the text occupying the bottom second part of the div (bottom).
I would like for the image to fit the size of the div, but not to stretch, and also want to apply different hover effects to each element. I've been working on it but I don't get to build this kind of structure. This is what I have so far:
.work-item {
position:relative;
height:300px;
width:350px;
}
.work-item-image {
position:absolute;
top:0;
}
.work-item-image img {
max-height: 150px;
max-width: 350px;
object-fit: cover;
}
.work-item-text {
position:absolute;
vertical-align:middle;
max-height: 150px;
max-width: 350px;
}
<div class="work-item">
<a href="#">
<div class="work-item-image">
<img src="..."/>
</div>
<div class="work-item-text">
<h1>Hello Worl</h1>
</div>
</a>
</div>
Don't fix the width for image. And width of each div must be equal. This code might help you. Resize your image with Photoshop, etc.
.work-item {
position:relative;
height:300px;
width:350px; or width: 100%;
}
.work-item-image {
position:absolute;
top:0;
}
.work-item-image img {
max-height: 150px;
object-fit: cover;
}
.work-item-text {
position:absolute;
vertical-align:middle;
max-height: 150px;
width: 350px; or width: 100%;
}
use this css for .work-item-image
.work-item-image {
float: right;
width: 100%;
}
Just use display: inline-block instead of position absolute like this:
.work-item {
position:relative;
height:300px;
width:350px;
}
.work-item-image {
display: inline-block;
}
.work-item-image img {
max-height: 150px;
max-width: 350px;
object-fit: cover;
}
.work-item-text {
display: inline-block;
max-height: 150px;
max-width: 350px;
}
<div class="work-item">
<a href="#">
<div class="work-item-image">
<img src="..."/>
</div>
<div class="work-item-text">
<h1>Hello Worl</h1>
</div>
</a>
</div>
Try this. Try to find other solutions then absolute.
CSS:
.work-item {
height:300px;
width:350px;
}
.work-item-image {
top:0;
}
.work-item-image img {
max-height: 150px;
max-width: 350px;
object-fit: cover;
}
.work-item-text {
margin: 0 auto;
vertical-align:middle;
max-height: 150px;
max-width: 350px;
}
h1 {
text-align: center;
}
img {
display: block;
max-width: 100%;
height: auto;
margin: 0 auto;
}
HTML:
<div class="work-item">
<a href="#">
<div class="work-item-image">
<img src="..."/>
</div>
<div class="work-item-text">
<h1>Hello Worl</h1>
</div>
</a>
</div>

Adding an overlay to an image with a hover

I have the page set up as three images in columns with a hover overlay to them. I am trying to add an additional overlay to them, basically a transparent png, over the main image but before the hover overlay.
Here is my code and I am having issues figuring it out.
fiddle: http://jsfiddle.net/r4mp0v4v/
.wrapper img{
float:left;
width:100%;
}
#media screen and (min-width: 700px) {
.wrapper img{
width:33%;
height:100%;
}
}
body {
display: block;
margin: 0px;
}
img:hover{
opacity: 1;
}
img {
opacity: 0.5;
background: #000;
}
overlay_image{
position: absolute;
top: 60px;
left: 80px;
}
<div class="wrapper">
<a href="Northside.html">
<img src="https://upload.wikimedia.org/wikipedia/commons/6/6d/Good_Food_Display_-_NCI_Visuals_Online.jpg" alt="">
<img src="http://creativitywindow.com/wp-content/uploads/2013/02/PNG-Portable-Network-Graphics.png" class="overlay_image">
</a>
</div>
<div class="wrapper">
<a href="Catering.html">
<img src="https://upload.wikimedia.org/wikipedia/commons/6/64/Foods_(cropped).jpg" alt="">
</a>
</div>
<div class="wrapper">
<a href="test/index.html">
<img src="http://www.healthsguardian.com/wp-content/uploads/2015/05/Improper-time-to-have-some-foods.jpg" alt="">
</a>
</div>
Like this: http://jsfiddle.net/r4mp0v4v/1/
I removed some stuff just so I could see your code better.
.wrapper { position: relative; }
.wrapper img{
width:33%;
}
body {
display: block;
margin: 0px;
}
img.overlay_image:hover{
opacity: 0;
}
img.overlay_image {
opacity: 0.5;
background: #000;
}
.overlay_image{
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
z-index: 5;
}
.main_image{
position: relative;
z-index: 0;
}

Responsive image inside vertical centered responsive container

I'm trying to create a responsive vertically centered lightbox, supporting different image sizes and controls relativ positioned to the image without the use of javascript.
I got it working in Safari, but unfortunately the image doesn't scale heightwise in Firefox and Chrome where it overflows its parent.
Here's the JSFiddle
Here is my code:
.overlay {
position:fixed;
top:0;
left:0;
right:0;
bottom:0;
background-color: rgba(0,0,0,.75);
text-align:center;
font-size:0;
}
.overlay:before {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}
.container {
display:inline-block;
vertical-align:middle;
position:relative;
box-shadow:0 0 5px rgba(0,0,0,.5);
font-size:1rem;
max-width:80%;
max-height:80%;
}
.container img {
vertical-align: bottom;
max-width:100%;
max-height:100%;
}
.container .caption {
position:absolute;
bottom:0;
width:100%;
background-color:rgba(0,0,0,.5);
}
.container .prev, .container .next {
position:absolute;
top:50%;
-webkit-transform: translateY(-50%);
transform: translateY(-50%);
}
.container .prev {
left:0;
}
.container .next {
right:0;
}
<div class="overlay">
<div class="container">
<img src="http://placehold.it/1920x1080" alt="" />
<div class="caption">Some text...</div>
<a class="prev" href="#">Previous</a>
<a class="next" href="#">Next</a>
<div>
</div>
Is it even possible to solve this problem without javascript?
Right, here's a solution (as best as i could tell from what you're trying to accomplish):
<div class='container'>
<img class='image' src='http://placehold.it/1920x1080'>
</div>
body, .container{
position: absolute;
width: 100%;
height: 100%;
overflow:hidden;
}
.container {
top: 0;
left: 0;
}
.image {
position: absolute;
max-width: 80%;
max-height: 80%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
fiddle