Text below image in grid made with divs - html

I made a grid with divs and there are images on it. Now i want to get the text directly below image somehow. I don't know how to do it. This is my code:
<div id="grid">
<div id="gridtop1">
<img src="img/img.jpeg" />
<p>Text here</p>
</div>
<div id="gridtop2">
<img src="img/img.jpeg" />
<p>Text here</p>
</div>
<div id="gridtop3">
<img src="img/img.jpeg" />
<p>Text here</p>
</div>
<div id="gridbottom1">
<img src="img/img.jpeg" />
<p>Text here</p>
</div>
<div id="gridbottom2">
<img src="img/img.jpeg" />
<p>Text here</p>
</div>
<div id="gridbottom3">
<img src="img/img.jpeg" />
<p>Text here</p>
</div>
</div>
And CSS:
#grid {
position: relative;
left: 10%;
width: 80%;
height: 600;
background-color: yellow;
}
#gridtop1 {
position: absolute;
left: 10%;
top: 5%;
}
#gridtop2 {
position: absolute;
left: 40%;
top: 5%;
}
#gridtop3 {
position: absolute;
right: 10%;
top: 5%;
}
#gridbottom1 {
position: absolute;
top: 55%;
left: 10%;
}
#gridbottom2 {
position: absolute;
top: 55%;
left: 40%;
}
#gridbottom3 {
position: absolute;
top: 55%;
right: 10%;
}
Now i tried this:
#grid p {
text-align: justify;
width: [width of img];
}
But it didn't help.
I don't want to make different id for all the p tags. I'm pretty close to giving up and using tables for this. But would be nice to get this to work. The text is below the image but it starts in the middle and goes all the way to the next image. Please someone help i spent like 3 hours trying to solve it.

I dont think you have to sweat so much on the CSS.
Please check this fiddle out and let me know if that solves your problem -
You can implement it by simple CSS (using float and position)
**Fiddle

You can use HTML5 tags as <figure> and <figcaption> (instead of div & p )and display <figure> as inline-boxes.
DEMO (see image caption set below or hover at bottom of image)
<figure>
<img src="http://dummyimage.com/120x150&text=img-2" />
<figcaption>
<p>Caption image</p>
</figcaption>
</figure>
Basic CSS :
figure {
margin:2px;
display:inline-block;
vertical-align:top;
}
to center them, use text-align:center on figure parent container. to have them on the right or left, use text-align:rightorleft;.
text in figcaption , will use width of image as reference given for the width of figure.
if you set figcaption in absolute position, you do not have to mind their width.

Related

How To Overlap Text Over Two Side By Side Images

I am creating a website and want to have two images side by side with a piece of text in the center of both images.
Html:
<div id="body">
<div class="image-row-container">
<img class="image-row image-row-1" src="assets/team-photo.JPG">
<p class="text-overlap" style="position: absolute; left: 25%; transform: translateX(-75%);">The Team</p>
<img class="image-row image-row-2" src="assets/test.JPG">
<p class="text-overlap" style="position: absolute; left: 75%; transform: translateX(-25%);">The Vehicle</p>
</div>
</div>
CSS:
.image-row {
display:inline-block;
width:50%;
height:500px;
}
.image-row-1 {
float:left;
}
.image-row-2 {
float:right;
}
So far, I have managed to place the two images side by side and horizontally align the text, but I can't figure out how to simply vertically align the text to the center of the images. If you have any tips of either my primary issue or any recommendations for tips on formatting, different methods, or poor code, it would be greatly appreciated. I am a novice so any help is good help!
I wrapped each image and text in an additional container, and then absolute positioned the text to be centered on top of the image. I think this is what you were looking for.
.image-row {
display: flex;
}
.image-container {
width: 50%;
position: relative;
}
.image-container img {
width: 100%;
height: auto;
}
.image-container p {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: white;
font-size: 24px;
font-weight: bold;
margin: 0;
}
<div class="image-row">
<div class="image-container">
<img src="https://placeimg.com/300/500/nature">
<p>The Team</p>
</div>
<div class="image-container">
<img src="https://placeimg.com/300/500/nature?t=1529100921702">
<p>The Team</p>
</div>
</div>

Text And Button in Full Width Image

I have a page wherein I have multiple full-width <img>'s - I have to add a <button> and <h2> overlaid upon each image. Images will have variable heights, so elements within need to conform to the perimeter set by their width + height.
An image is styled thus:
CSS
.FullWidthImg {
width: 100vw;
position: relative;
left: 50%;
right: 50%;
margin-left: -50vw;
margin-right: -50vw;
}
HTML
<div id="container"> <!--ONLY STYLING FOR container IS position: relative-->
<img src="xx" alt="xx" style="FullWidthImg"/>
<h2>TEXT GOES HERE</h2>
<button>i'm a button</button>
</div>
Most approaches suggest styling <h2> and <button> with position: absolute - this works if you have one image element and the image always has the same height, however neither is the case for me.
Another approach I've seen is making something like:
<div style="background-image: url(/img.com)">
<h2>TEXT GOES HERE</h2>
<button>i'm a button</button>
</div>
... And then positioning elements within, which could work but I'd like to avoid in-line styling if possible.
Are there any alternative approaches that would work for this use-case?
Building off your first suggestion, here's how you could accomplish your desired layout without inline styles.
.img-wrapper {
position: relative;
}
.img-wrapper img {
width: 100%;
}
.img-wrapper .overlay {
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
}
.img-wrapper h2 {
margin: 0 0 .5em;
}
<div class="img-wrapper">
<img src="http://via.placeholder.com/600x150/eee/ddd" />
<div class="overlay">
<h2>Heading</h2>
<button>Button</button>
</div>
</div>
<div class="img-wrapper">
<img src="http://via.placeholder.com/600x400/eee/ddd" />
<div class="overlay">
<h2>Heading</h2>
<button>Button</button>
</div>
</div>

How do I set an image background, using the <img> tag rather than CSS?

I have created 4 columns. Each column consisting of an image, where I want to place text over each image.
To achieve this, I obviously need to set the image as a background. I am aware that this can be achieve through CSS (which is what I am currently doing) but I would like to place the image as a background, within my HTML file. The reason; so that I can enter the relevant 'alt' text for SEO purposes.
How can I best achieve this?
Just put the img in the col container, set that element to position: relative; then use absolute positioning to put the text over the image.
* {margin:0;}
.col {
float: left;
width: 25%;
position: relative;
}
img {
display: block;
width: 100%;
}
.overlay {
position: absolute;
top: 50%; left: 50%;
transform: translate(-50%,-50%);
background: black;
color: white;
padding: 1em;
}
<div class="col">
<img src="http://kenwheeler.github.io/slick/img/fonz1.png">
<div class="overlay">
<h2>text</h2>
</div>
</div>
<div class="col">
<img src="http://kenwheeler.github.io/slick/img/fonz1.png">
<div class="overlay">
<h2>text</h2>
</div>
</div>
<div class="col">
<img src="http://kenwheeler.github.io/slick/img/fonz1.png">
<div class="overlay">
<h2>text</h2>
</div>
</div>
<div class="col">
<img src="http://kenwheeler.github.io/slick/img/fonz1.png">
<div class="overlay">
<h2>text</h2>
</div>
</div>
#img-as-background {
position: relative;
overflow: hidden;
}
#img-as-background .container {
height: 500px;
padding: 20px;
}
#img-as-background img {
position: absolute;
width: 100%;
top: 0;
bottom: 0;
z-index: -1;
}
<div id="img-as-background">
<img src="https://image.freepik.com/free-vector/modern-abstract-background_1048-1003.jpg" alt="" />
<div class="container">
Overflow text Overflow text Overflow text Overflow text Overflow text Overflow text Overflow text
</div>
</div>

how to add a caption like text under image in css

Hi I want to add text over an image at its bottom center. Its a photo gallery project so all the images are of different dimensions. Css needs to work with the percentages so it works on images of all sizes
<div class="images">
<img src="dummy.jpeg">
<p>Some text to appear>
</div>
I tried reading many questions on stackoverflow but all of them works for one image. I have multiple images with different dimensions.
I want to put the text at the bottom center of the image and a black strip that goes with width 100% over the image. It could be the text background.
Use absolute positioning on the caption <p>
Make the container inline-block
Fiddle: jsfiddle.net/eohtwd1u
.images {
position: relative;
display: inline-block;
}
.images p {
position: absolute;
width: 100%;
text-align: center;
bottom: 0;
left: 0;
}
<div class="images">
<img src="https://placehold.it/350x150">
<p>Some text to appear</p>
</div>
You can use CSS Flexbox. And for the text at the bottom of each image use position: absolute and making your parent position: relative.
Have a look at the snippet below:
.images {
display: flex;
padding: 5px;
}
.img-holder {
position: relative;
margin: 5px;
align-self: flex-start;
}
.img-holder p {
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
width: 100%;
text-align: center;
margin: 0;
}
<div class="images">
<div class="img-holder">
<img src="http://placehold.it/100x100">
<p>Some text</p>
</div>
<div class="img-holder">
<img src="http://placehold.it/120x120">
<p>Some text</p>
</div>
<div class="img-holder">
<img src="http://placehold.it/150x150">
<p>Some text</p>
</div>
</div>
Hope this helps!
<!-- comment to fool the edit limiter -->
img{
width:500px;
height:200px;
}
.images{
width:500px;
height:200px;
align-items:center;
position:relative;
}
p{
position:absolute;
bottom:0;
color:yellow;
left:50%;
}
span{
position: relative;
left: -50%;
}
<div class="images">
<img src="https://i.stack.imgur.com/wwy2w.jpg">
<p><span>Some text to appear></span></p>
</div>

How to bottom align two elements in a DIV element?

I am currently doing this with a table with 2 bottom-aligned cells. I am okay with the table solution, but just wondering if this is possible with (just css and html, no javascript).
Requirement:
* The sizes of the text and image are unknown, but the combined width of the two will not exceed the width of the containing element. (e.g. if i later want to change the image or the text, i do not want to dive into the ccs file)
* Image is aligned to the left, and the text (actually, a horizontal list) is aligned to the right.
Edit: In response to Kos,
the sizes of the text and images are dynamic, be it height or width, BUT the combined width of the two elements will not exceed the width of the containing element.
the image and text should be bottom aligned
the containing element should fit tightly the tallest element.
HTML
<div class="wrapper">
<img src="" class="image" />
<p class="text">Hello world!</p>
</div>
CSS
.wrapper {
position: relative;
width: 500px;
}
.image {
position: absolute;
display: block;
left:0;
bottom: 0;
}
.text {
position: absolute;
right:0;
bottom: 0;
}
EDIT: I added the appropriate HTML code.
EDIT 2: In case the height of the wrapper is unknown (only restriction is that .image has always to be higher than .text)
CSS
.wrapper {
position: relative;
width: 500px;
}
.image {
vertical-align: bottom;
}
.text {
position: absolute;
right: 0;
bottom: 0;
}
HTML
<div class="wrapper">
<img class="image" src="" />
<p class="text">
Hello world!
</p>
</div>
This should work I think:
HTML
<div class="outer">
<img src="" title="" />
<div class="text">Some text </div>
</div>
CSS
.outer {display: inline-block; width: 350px; }
.outer img {float: left;}
.outer .text {float: right; }
<div style="width:400px; overflow:visible; position:relative;">
<img src="#" alt ="#" style="float:left;"/>
<p style="position:absolute; bottom:0; float:right;">Lorem Ipsum</p>
</div>