This question already has answers here:
How to overlay images
(11 answers)
Closed 3 years ago.
I'm trying to make a div with some information about an image appear when I hover over the image, my code doesn't seem to work.
HTML:
< div class="batmobile-info">This is Batman's batmobile, he uses it to chase his enemies or to flee from the police.< /div>
< img class="batmobile" src="images/batmobile.png" alt="batmobile" >
CSS:
.batmobile{
position:relative;
width:500px;
height:200px;
top:350px;
left:200px;
}
.batmobile-info{
visibility:hidden;
}
.batmobile:hover .batmobile-info{
background-color:white;
text-align:center;
width:290px;
height:40px;
position:absolute;
top:500px;
left:700px;
visibility: visible;
}
If you reverse the order of your elements, so the img comes before the div to give you:
<img class="batmobile" src="images/batmobile.png" alt="batmobile">
<div class="batmobile-info">This is Batman's batmobile, he uses it to chase his enemies or to flee from the police.</div>
Then you could use the following CSS to show the div on hover:
.batmobile:hover + .batmobile-info {
visibility: visible;
}
The + is an adjacent sibling selector - a super useful selector!
One way to make this work is to use container where you'll put your img and div with information.
I used opacity instead of visibility because opactity is the property you can use in transitions.
* {
box-sizing: border-box;
}
html, body {
margin: 0;
}
/* contiainer to center my content, not relevant to question*/
.main-container {
margin-top: 2rem;
height: calc(100vh - 2rem);
display: flex;
align-items: center;
justify-content: space-around;
}
/* image and info container */
.img-container {
display: inline-block;
position: relative;
border: 2px solid #333;
}
/* style for div with information */
.img-info {
position: absolute;
background-color: rgba(0, 0, 0, 0.8);
color: #eee;
transition: opacity .3s ease-in-out;
opacity: 0;
display: flex;
align-items: center;
justify-content: center;
padding: .5rem;
}
/* information positioning styles */
.img-info-overlay {
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.img-info-above {
left: -2px;
top: -2rem;
height: 2rem;
width: calc(100% + 4px);
}
/* this part show information on hover */
.img-container:hover .img-info {
opacity: 1;
}
<div class="main-container">
<div class="img-container">
<img src="https://dummyimage.com/250x100/fff/aaa" alt="Filler image">
<div class="img-info img-info-overlay">Some info about this image</div>
</div>
<div class="img-container">
<img src="https://dummyimage.com/250x100/fff/aaa" alt="Filler image">
<div class="img-info img-info-above">Some info about this image</div>
</div>
</div>
The problem is in both - the HTML and CSS, you need to put the image and text in one parent block and start styling from it. I'd avoid absolute positioning and also try using opacity CSS property in case you will need to add some extra transition effects to your text block .batmobile:hover state. Check out the code and run the snippet:
body {background:#ccc}
.batmobile {
position: relative;
width: 500px;
height: 200px
}
.batmobile div p {
visibility: hidden;
background-color: white;
width: 300px;
height: 40px;
margin: 0px auto; /* Think you wanted to center the block as width is less than parent */
text-align: center /*In case you need to center text only */
}
.batmobile:hover div p {
visibility: visible;
}
.batmobile img {width: 100%}
<div class="batmobile">
<div>
<p>An excelent drone - Ryze Tello with price under $100</p>
</div>
<img src="https://bestdroneforkids.com/wp-content/uploads/2019/09/ryze-tello-drone-forkids.jpg" alt="Ryze tello drone in the air">
</div>
Related
This question already has answers here:
Image inside div has extra space below the image
(10 answers)
Closed 1 year ago.
I am not asking how to round corners of image. In given case when I mouse-hover on the image you can see that the image is not covering whole div as well as the lower corners are not rounded, but if I add display: block to the image, it covers the div and the lower corners seems to be rounded, I am unable to understand why this is happening?
See images for before and after display:block.
HTML:
<div class="row">
<div class="campus-col">
<img src="./images/london.png" alt="london campus" />
<div class="layer">
<h3>LONDON</h3>
</div>
</div>
CSS:
.row {
margin-top: 5%;
display: flex;
justify-content: space-between;
}
.campus-col {
flex-basis: 32%;
border-radius: 10px;
margin-bottom: 30px;
position: relative;
overflow: hidden;
}
.campus-col img {
width: 100%;
display: block; /**** THIS PROPERTY ROUNDS THE LOWER CORNER *****/
}
.layer {
background: transparent;
height: 100%;
width: 100%;
position: absolute;
top: 0;
left: 0;
transition: 0.5s;
}
.layer:hover {
background: rgba(226, 0, 0, 0.7);
}
BEFORE display:block on mouse-hover the image is-
AFTER display:block on mouse-hover the image is -
You can just write the following code instead of display: block;
opacity:1;
This will also work the same as display block. Just try this code and check wether again it is rounding the corners?
I am having the following Issue:
I got a Website with a quite large DIV element, in which there is a img (sullsize).
As on the main index site, I want to have the "Site Title" called Palette within the DIV, not on the outside. Whenevery I try to do it with the Flex CSS, the Image will not go to the end (fill the element).
"My Team Site"
So my current issue is, I can't get the Palette Text into the item, and also I will be listing Photos and Jobdescriptions for the individuals, each has it's own flexbox, so it will behave right.
As said, I can't get them to be within the box.
The easyest way is if you look at my (Sample) Website:
LINK (it should look like on the main site. Maybee even with
Flex, instead of old-fashioned way)
Dashed Orange: Text Box
Dashed Red: Future Team Div (1 per Person)
This is the basic HTML and a part (ONLY A PART!) of the CSS, look at the Rest on the Website:
.content_item .content_title {
position: relative;
overflow: visible;
display: inline-block;
height: 100%;
width: 45%;
float: right;
flex-grow: 1;
flex-shrink: 1;
}
.content_item h2 {
position: absolute;
bottom: 0;
right: 0;
margin: 0;
padding-left: 20px;
width: 100px;
flex-grow: 1;
flex-shrink: 1;
color: rgba(255, 255, 255, .9);
white-space: nowrap;
letter-spacing: 1px;
font-size: 5em;
-ms-transform: rotate(270deg);
-webkit-transform: rotate(270deg);
transform: rotate(270deg);
}
<body>
<div class="site_palette">
<div class="header_div">
<h1 class="header_title">Schwarz & Torf Maler AG</h1>
<h3 class="header_subtitle">Wir Malen wo andere nur zuschauen</h3>
</div>
<div class="flex_container">
<a href="index.html">
<div class="content_item shadowbox_red">
<img src="http://maler1.calmarsolutions.ch/images/center_3.jpg" />
<div class="team_section">
<div class="team_part">
<img src="">
</div>
</div>
<div class="content_title">
<h2>Palette</h2>
</div>
</div>
</a>
</div>
</div>
</body>
The flex container is .content_item.
This container has three children (flex items):
img
div.team_section
div.content_title
Since these are three in-flow flex items, they respect each other's space and don't overlap.
The text you want to layer over the image is in an absolutely-positioned h2, which is a child of relatively-positioned .content-title.
What you need to do is add position: relative to the primary container, then apply position: absolute the non-image flex items.
Add this to your code:
.site_palette .content_item {
position: relative;
}
.team_section {
position: absolute;
}
/* adjustment below may be unnecessary */
.content_item .content_title {
/* position: relative; */
position: absolute;
right: 0;
}
.site_palette .content_item {
max-width: 70vw;
position: absolute;
}
.team_section {
display: flex;
position: absolute;
}
Locate the following code in your css:
.team_section {
display: flex;
}
And change it to:
.team_section {
display: none;
}
I have the following markup (Fiddle Example Here: http://jsfiddle.net/9gvj11o5/8/)
<div class="header">
<div class="image">
<img src="http://placehold.it/2200x800" alt=""/>
</div>
<div class="menu">This is the menu</div>
<div class="tools">These are the tools</div>
</div>
<div class="content">content</div>
And the following CSS:
.header {
position: relative;
}
.image {
position: absolute;
}
img {
width: 100%;
height: auto;
outline: 0;
}
I need the image to be responsive and have 100% width aligned to top.
But I also need the menu and tools to be over the image and having their normal flow.
Content should be after the image, so after the header.
The image would be the "background" of the header div. (I cannot use background-image)
I am using position but the menu and tools disappear the moment I use it.
What am I missing? Do I need another wrapper div somewhere?
I would wrap the 2 divs .menu & .tools so you need only to apply z-index to the wrapper div instead of each child. which make .menu & .tools (wrapped) in front of the .image.
then change position:absolute to position:relative to .image in order to have .content below header.
Below you can see the snippet, very lightweight.
.header {
position: relative;
}
.image {
position: relative;
z-index:1
}
#menu-all {
position:absolute;
top:0;
z-index:2
}
img {
width: 100%;
height: auto;
outline: 0;
}
<div class="header">
<div class="image">
<img src="http://placehold.it/2200x800" alt="" />
</div>
<div id="menu-all">
<div class="menu">This is the menu</div>
<div class="tools">These are the tools</div>
</div>
</div>
<div class="content">content</div>
You can use z-index to define the layer order of your elements. The smaller the number, the closer to the "bottom" of the stack. So we give the img a very small number, and menu and tools a very large one.
.header {
position: relative;
}
.image {
position: absolute;
z-index: 1; /* here you can use -1 as Paulie_D points out in the comments */
}
img {
width: 100%;
height: auto;
outline: 0;
z-index: 2; /* here you can use -1 as Paulie_D points out in the comments */
}
.menu {
position: absolute;
top: 0;
left: 0;
z-index: 888; /* You can remove this declaration entirely if you set -1 above */
}
.tools {
position: absolute;
top: 0;
right: 0;
z-index: 888; /* You can remove this declaration entirely if you set -1 above */
}
I found a nice tutorial for making my images enlarge (like a zoom effect) on hover. The main difference between my needs and a tutorial is that I want my all images contained in a single box like container. So when I implemented the tutorial I realize that part of the enlarged image gets cut off when you hover. The effect is constrained to the container. I would like a way for the zoom to go wherever it needs to go on the page. (So you can see the whole zoomed image)
Here is my implementation of the tutorial: http://mulnix.contestari.com/wp/example225/1.php
JSFiddle: http://jsfiddle.net/dsRAH/
Original Code
Remove the overflow: hidden and all other overflows,
than for your images containers DIV remove float:left; and add display:inline-block;
* {
margin: 0;
box-sizing: border-box;
}
.wrapper {
position: relative;
background: #000;
color: #fff;
z-index: 0;
}
.photos {
position: relative;
display: flex;
flex-flow: row wrap;
}
.photo {
box-shadow: 0 0 0 2px #444;
margin: 5px;
position: relative;
max-height: 200px;
transform: translateZ(0);
transition: transform 0.5s;
}
.photo:hover {
z-index: 1;
transform: translateZ(0) scale(1.6);
}
.photo img {
display: block;
width: 100%;
height: 100%;
object-fit: cover;
}
.photo-legend {
position: absolute;
bottom: 0;
width: 100%;
padding: 1em;
background: rgba(0,0,0,0.3);
}
<div class="wrapper">
<div class="photos">
<div class="photo">
<img src="https://placehold.it/200x150/0bf" />
<div class="photo-legend">TEST DESCRIPTION</div>
</div>
<div class="photo">
<img src="https://placehold.it/200x200/f0b" />
</div>
<div class="photo">
<img src="https://placehold.it/200x150/bf0" />
</div>
</div>
</div>
It's not perfect but it's a start. I changed the overflow:hidden; in the wrapper to visible. I also put your code into jsfiddle so people can tinker with it.
http://jsfiddle.net/m8FXH/
You can try to use z-index. An element with greater z-index is always in front of an element with a lower z-index. If you main container is not overflow:hidden than you can try this out.
here is an example where you can see how it works. Hope that is helpful.
https://developer.mozilla.org/en-US/docs/Web/CSS/z-index
I would suggest giving your divs one of the following classes:
colleft for the ones that are at left column
colright for the ones that are at right column
rowtop for the ones at the top row
rowbottom for the ones at the bottom row
And then assign them the following properties
.colleft {
transform-origin-x: 0%;
}
....
transform-origin-x: 100%;
transform-origin-y: 0%;
transform-origin-y: 100%;
(respectively)
That will make the zoom go in the desired direction.
evan stoddard modified fiddle
I'm creating a hover effect so that when someone mouse-over's on an image scan lines appear, but can't get the damn overlay image to be the same size as the image.
Take a look at this fiddle: http://jsfiddle.net/number8pie/wwmPL/
Here's the HTML:
<div class="container">
<a href="#">
<div class="overlay"></div>
<img src="http://www.mainlymunros.co.uk/images/green%20square.bmp" repeat>
</a>
</div>
Here's the CSS:
.container {
position: relative;
max-width: 200px;
}
img {
width: 100%;
position: relative;
z-index: 1;
padding: 7px;
border: 1px solid #333;
}
.overlay {
display: none;
position: absolute;
z-index: 5;
width: 100%;
height: 100%;
margin: 8px;
background: url(https://dl.dropbox.com/u/29825082/scanlines.png) repeat;
}
a:hover .overlay {
display: block;
}
If you hover over the green block you can see the scan lines overlap at the bottom, I want to remove this overlap.
The image is dynamic and changes size depending on the size of the browser size.
Anyone got any suggestions?
The problem is that you're giving it 100% height and 100% width but then you're giving it margin. You're telling it to be the exact size of it's containing a element, but then pushing it down a bit.
You need to add an extra container, remove the image's padding and border and assign that to the new container.
<a href="#">
<div class="image">
<div class="overlay"></div>
<img src="http://www.mainlymunros.co.uk/images/green%20square.bmp"/>
</div>
</a>
.container a {
display:block;
padding:7px;
border: 1px solid #333;
}
.image {
position:relative;
width:100%;
height:100%;
display:block;
}
img {
width: 100%;
position: relative;
z-index: 1;
}
.overlay {
display: none;
position:absolute;
z-index: 5;
width: 100%;
height: 100%;
background: url(https://dl.dropbox.com/u/29825082/scanlines.png) repeat;
}
Example: JSFiddle.
Your problem now is that your img isn't square. Being a rectangular image with greater width than height means it will fill 100% width but cut off part of the height. Make your image a square or give your overlay the same aspect ratio and this will work perfectly.
its padding property which creates overflow. so change the height of both image and .overlay
that will do.
fiddle :http://jsfiddle.net/wwmPL/2/
i hope this that solves your problem :)