How to make multiple images with a tag name as a link on top of each one?
I can make only one image with a tag name on top of the image, but not other ones. Somehow when I copy the whole div (contains the img and the tag's div), it doesn't show the tags on other images.
I've tried: position: relative for the parent div, position: absolute for the tag (child div), make the image float: left.
I've also tried to "stick" the child div with its parent.
I've tried adding div and class for each item: image, image container, tag, a tag...
.container {
position: relative;
width: 400px;
height: 400px;
}
.container img {
width: 100%;
/*I've tried with px, doesn't make a difference*/
float: left;
/*I've also tried adding a class for img and position: absolute*/
}
.nameTag {
width: 100%;
height: 50px;
position: absolute;
background-color: gray;
}
.nameText {
color: white;
}
<div class="container">
<img src="assets/images/img1.png" />
<div class="nameTag">Mobile App</div>
</div>
<div class="container">
<img src="assets/images/img2.png" />
<div class="nameTag">Mobile App</div>
</div>
<div class="container">
<img src="assets/images/img3.png" />
<div class="nameTag">Mobile App</div>
</div>
Not sure if this is exactly what you are looking for but this is what I've changed in your CSS:
.container {
position: relative;
display: inline-block; //allowed images to float
width: 400px;
height: 400px;
}
.nameTag {
width: 100%;
height: 50px;
position: absolute;
background-color: gray;
top: 100px; //positioned about half way down the image
text-align: center; //center the link text
}
Here is a link to the CodePen for you to view. Let me know!
Try changing your float to "none" or copy/paste this into your html/css files.
Hope this helps.
.container {
position: relative;
width: 400px;
height: 400px;
}
.container img {
width: 100%;
/*I've tried with px, doesn't make a difference*/
float: none;
/*I've also tried adding a class for img and position: absolute*/
}
/* ^^^ Change float to "none"...seems to work here */
.nameTag {
width: 100%;
height: 50px;
position: relative;
background-color: gray;
}
.nameText {
color: white;
}
<div class="container">
<div class="nameTag">Mobile App</div>
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRlUiyrmhTXFppqk4aYzqTOU9nimCQsYibukwAV8rstsDkAVQT-mA" />
</div>
<div class="container">
<div class="nameTag">Mobile App</div>
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRlUiyrmhTXFppqk4aYzqTOU9nimCQsYibukwAV8rstsDkAVQT-mA" />
</div>
<div class="container">
<div class="nameTag">Mobile App</div>
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRlUiyrmhTXFppqk4aYzqTOU9nimCQsYibukwAV8rstsDkAVQT-mA" />
</div>
Related
I'm trying to attach an "information div" to the left bottom edge of a fullscreen/centered/fitted image.
The image has to fit into the screen size (just like css property object-fit: contain does) and the information div should be attached to the bottom left side of the image.
Here is my css try (not even close) and here are three images to describe the wanted behavior!
Note: I'm looking for a CSS-only solution, so, please, no JS!
Edit: Attach div to a fullscreen image to the corner of another div with no predefined size / fullscreen.
* {
margin: 0;
padding: 0;
}
#out {
}
#in {
}
#info {
background-color: red;
color: white;
display: inline-block;
position: absolute;
bottom: 10px;
left: 10px;
}
img {
width: 100vw;
height: 100vh;
object-fit: contain;
}
<div id="out">
<div id="in">
<img src="https://images.unsplash.com/photo-1501630834273-4b5604d2ee31?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80" alt="">
<div id="info">This is the image info!</div>
</div>
</div>
If I understand you correctly, you're looking for something like this? Open it fullscreen, resize the page, throw different images in there, cheers.
div {
border: red 1px dashed;
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
}
div:after {
content: attr(data-overlay);
position: absolute;
bottom: 1rem;
left: 1rem;
color: white;
font-size: 2rem;
}
img {
object-fit: contain;
height: 100vh;
width: 100wh;
}
<div data-overlay="TEXT TEXT TEXT">
<img src="https://images.pexels.com/photos/1498923/pexels-photo-1498923.jpeg"/>
</div>
Wrap the image in a div that will be the relative parent of your info div.
section {
display: flex; /* centers everything in the section */
flex-direction: column;
align-items: center;
}
.image-wrap {
display: inline-block; /* keeps the wrapper div the same size as the child image */
position: relative; /* for positioning the info div */
}
.image-info {
position: absolute;
left: 10px;
bottom: 10px;
font-size: 18px;
text-transform: uppercase;
color: white;
}
<section>
<div class="image-wrap">
<img src="https://picsum.photos/200/300" />
<div class="image-info">Nice image</div>
</div>
<div class="image-wrap">
<img src="https://picsum.photos/300/200" />
<div class="image-info">This one is cool</div>
</div>
<div class="image-wrap">
<img src="https://picsum.photos/450/180" />
<div class="image-info">Pretty!</div>
</div>
</section>
you need to use Viewport unit
for that you need to set the size of your div.#out
which is width: 100vw; height: 100vh
Is it possible to make a div absolute to a specific relative div rather than just the parent?
For example. I have a div that's contained inside of a row. But, I want it to be absolute in the section rather than the row. Both divs are positioned relative because of a WordPress themes styling. If I use position absolute it will just make it absolute to the row.
How can I get around this issue?
.section {
width: 100%;
height: 100%;
position: relative;
background: #f5f5f5;
}
.row {
width: 80%;
height: 100%;
position: relative;
background: #000000;
margin: 0 auto;
}
.content {
width: 200px;
height: 200px;
background: pink;
position: absolute;
right: 0;
top: 0;
}
<div class="section">
<div class="row">
<div class="content">
</div>
</div>
</div>
This is not how positioning works. A div or any other element is relevant to its parent regarding its positioning. In case you want to position an element inside the section that you have, it's better to construct your code as follows:
<div class="section">
<div class="absoluteDiv">
</div>
<div class="row">
</div>
</div>
You could find some more examples here
Hope it helps,
Konstantinos.
Although you can not make a div absolute to a specific div, one way to get the results you are looking for is to add overflow:visible; to the row and left:100%; to content container. I changed the section height to 300px for demonstration purposes but it will behave the same with 100%.
.section {
width: 100%;
height: 300px;
position: relative;
background: #f5f5f5;
}
.row {
width: 80%;
height: 100%;
position: relative;
background: #000000;
margin: 0 auto;
overflow:visible;
}
.content {
width: 200px;
height: 200px;
background: pink;
position: absolute;
left: 100%;
top: 0;
}
<div class="section">
<div class="row">
<div class="content">
</div>
</div>
</div>
My HTML:
<div class="container-fluid">
<div class="row">
<div class="header-img">
<img src="test_bg.jpg" class="bg" />
<div class="col-xs-2 col-xs-offset-1 logo"></div>
<div class="col-xs-5 col-xs-offset-3 logo"></div>
</div>
</div>
CSS:
$test-color : red;
.header-img {
width: 100%;
height: auto;
}
.bg {
width: 100%;
height: auto;
display: block;
}
.logo {
background-color: $test-color;
position: absolute;
height: 100px;
z-index: 2;
}
Here is the result:
What i want:
The header div has the size of the image it contains and the two other divs are on the image at the top of it.
The problem is, if I make the img absolute the two divs are on top but the header div hasn't the size of the image.
Is it possible to have both?
Why not just wrap the red divs with container with position:absolute?
Like this?
.header-img {
width: 100%;
height: auto;
}
.bg {
width: 100%;
height: auto;
display: block;
}
.s {
position:absolute;
top:0;
left:0;
width:100%;
}
.logo {
background-color: red;
position: absolute;
height: 100px;
z-index: 2;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div class="row">
<div class="header-img">
<img src="http://i.stack.imgur.com/6MqG7.jpg" class="bg" />
<div class="s">
<div class="col-xs-2 col-xs-offset-1 logo"></div>
<div class="col-xs-5 col-xs-offset-3 logo"></div>
</div>
</div>
</div>
</div>
You are not using good approach, the case like this we don't use image throug image tag. You should use background image like
.header-img{
background-image: url("test_bg.jpg");
height: 700px; /*put height of your image here*/
background-size: cover;
}
Now you not need to apply position absolute to your both of divs.
Put above style in your css and make sure path of your image is correct
Hope it would be helpful
Thanks
You should prepare a jsfiddle, but make .header-img position:relative and then set the bottom or top properties of .logo to what you want.
.header-img { position: relative;}
.logo { bottom: 0px;}
The issue is you'll need to set left or right for the .logo divs because they'll render on top of each other.(you can set for example left: 15px for the left one and right:15px to the right one)
https://jsfiddle.net/ivankovachev/wodpdrzq/
You can position your parent header element to relative and position your image to absolute so the red div's come over the image.
.header{
position: relative;
width: 100%;
height: auto;
}
.bg{
position: absolute;
width: 100%;
height: auto;
display: block;
}
.logo{
background-color: red;
position: absolute;
height: 100px;
z-index: 2;
}
See a working fiddle here: Fiddle
I have a few div containers with overlays inside of them:
<div class="container">
<div class="overlay"></div>
</div>
<div class="container">
<div class="overlay"></div>
</div>
<div class="container">
<div class="overlay"></div>
</div>
<div class="container">
<div class="overlay"></div>
</div>
The problem is, when I set overlay display to table (it has to be table as I'm centering stuff both vertically & horizontally there - just simplified this example for SO) like so:
.container {
position: relative;
display: inline-block;
background: #fed900;
outline: solid 5px #000;
width: 25%;
height: 200px;
}
.overlay {
position: absolute;
top: 0;
display: table;
background: rgba(0,0,0,0.4);
width: 100%;
height: 100%;
}
I'm getting weird glitch - although developer tools tell me the overlay has the same width as container - in some cases the overlay width equals container's width minus 1 pixel. Why? What am I missing? And how to fix that? :)
http://jsfiddle.net/v13mdq57/
Depending on the width, the calculation is giving you a half pixel (which can't be rendered). We can achieve this without display: table. I'm not quite sure why this only occurs with display: table, but leaving the overlay as a block element fixes the problem.
In this example:
.overlay:before brings inline elements into the middle. It is an invisible element that is lined up on the left hand side inside the overlay.
The closing and opening div tags have no white space between them, which prevents the inline gap.
Read more about removing the gap between inline elements.
CSS / HTML / Demo
body {
margin: 0;
}
.container {
position: relative;
display: inline-block;
vertical-align: middle;
background: #fed900;
width: 25%;
height: 200px;
}
.overlay:before {
content:'';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.overlay {
position: absolute;
top: 0;
left: 0;
background: rgba(0, 0, 0, 0.4);
width: 100%;
height: 100%;
text-align: center;
}
<div class="container">
<div class="overlay">Centered</div>
</div><div class="container">
<div class="overlay">Centered</div>
</div><div class="container">
<div class="overlay">Centered</div>
</div><div class="container">
<div class="overlay">Centered</div>
</div>
* {
margin:0px;
padding:0px;
}
.container {
position: relative;
display: inline-block;
background: #fed900;
width: 25%;
height: 200px;
margin-right: -3px;
}
I have a wrapper div (#main-wrapper), and a div centered horizontally inside (#image). It contains a big image.
I'd like to have a div overlaying #image in the bottom-right corner (#thumbs).
Here's what I have:
<div id="main-wrapper">
<div id="image">
<img src="img.jpg" />
</div>
<div id="thumbs">
Link 1
Link 2
Link 3
</div>
</div>
div#main-wrapper
{
width: 100%;
background-color: #000000;
position: relative;
}
div#main-wrapper div#image
{
margin: 0 auto;
width: 800px;
height: 600px;
background-color: #1D9DDD;
}
div#main-wrapper div#thumbs
{
position: absolute;
width: 600px;
height: 400px;
background-color: #FF212C;
bottom: 0;
right: 0;
}
The problem is that #thumbs goes to the bottom-right of #main-wrapper, and not the center of the centered div.
You can see what I'm doing here: http://jsfiddle.net/22NnS/1/
If I'm understanding correctly then this will work. Put the thumbs div within the image container like so:
<div id="main-wrapper">
<div id="image">
<img src="img.jpg" />
<div id="thumbs">
Link 1
Link 2
Link 3
</div>
</div>
</div>
And then change where the position:relative is defined:
div#main-wrapper
{
width: 100%;
background-color: #000000;
}
div#main-wrapper div#image
{
margin: 0 auto;
width: 800px;
height: 600px;
background-color: #1D9DDD;
position: relative;
}
div#main-wrapper div#thumbs
{
position: absolute;
width: 600px;
height: 400px;
background-color: #FF212C;
bottom: 0;
right: 0;
}