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>
Related
I'm trying to put two elements on the top right of a card, but I don't want them to stack. In front of that I want them to stay next to each other but at the top right.
I've seen this W3S page, more in detail the 'Positioning Text in an Image'.
This is the example I'm using right now but only works for one element, if I add a new element with the same class they overlay each other:
<!DOCTYPE html>
<html>
<head>
<style>
.container {
position: relative;
}
.topright {
position: absolute;
top: 8px;
right: 16px;
font-size: 18px;
}
img {
width: 100%;
height: auto;
opacity: 0.3;
}
</style>
</head>
<body>
<h2>Image Text</h2>
<p>Add some text to an image in the top right corner:</p>
<div class="container">
<img src="img_5terre_wide.jpg" alt="Cinque Terre" width="1000" height="300">
<div class="topright">Top Right</div>
<div class="topright">Top Right overlayed</div>
</div>
</body>
</html>
Is there any way of automate the positioning the topright elements next to each other without making two classes and setting the positions manually?
Thank you.
You can simply use flex and other flex properties to make sure the top-right is not stacking each each and this way it will be responsive as well on modern browsers.
I would not suggest using position: absolute or custom top or right for this type of thing.
Live Demo:
.container {
display: flex;
position: relative;
}
.topright {
font-size: 18px;
}
.top_right_item {
display: flex;
justify-content: space-between;
flex-grow: 0;
flex-shrink: 0;
flex-basis: 35%;
}
img {
width: 100%;
height: auto;
opacity: 0.3;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Image Text</h2>
<p>Add some text to an image in the top right corner:</p>
<div class="container">
<img src="img_5terre_wide.jpg" alt="Cinque Terre" width="1000" height="300">
<div class="top_right_item">
<div class="topright">Top Right</div>
<div class="topright">Top Right overlayed</div>
</div>
</div>
</body>
</html>
.topright {
display:flex;
flex-direction:row;
position: absolute;
top: 8px;
right: 16px;
font-size: 18px;
}
<div class="topright">
<div>Top Right</div>
<div>Hello</div>
</div>
Your problem is solved.
What you have to do is put them in separate divs and wrap them in topright div, and set display to flex,thats all.
You can make an wrapper for the two elements
<div class="topright-wrapper">
<span>Top Right</span>
<span>Top Right Right</span>
</div>
CSS:
.topright-wrapper {
display: flex;
position: absolute;
top: 8px;
right: 16px;
font-size: 18px;
}
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>
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>
I have the following code
<body>
<div style="height: 35%; background-color: black;"></div>
<div id="header">
<div>
<h1>Title</h1>
<h3>Subtitle</h3>
</div>
</div>
<div id="content" class="card">
<div>
<p>
One
</p>
<p>
Two
</p>
</div>
</div>
</body>
Ideally, I would like the top portion of the page to be a certain color (black in the example), and I want the header area (which contains the <h1> and <h3> elements) to be inside the black box. Then I would like the first paragraph of the content to also be included inside the black box. Very similar to this picture:
What is the best way to go about this?
The simplest way is to use an absolute positioned pseudo element on the header
Stack snippet
body {
background-color: #ddd;
margin: 0;
}
#header {
position: relative;
color: white;
background-color: black;
}
#header::before {
content: '';
position: absolute;
top: 100%;
left: 0;
height: 60px;
width: 100%;
background-color: inherit;
}
#header div {
width: 80%;
margin: 0 auto;
padding: 10px;
}
#content {
position: relative;
width: 80%;
margin: 0 auto;
background-color: white;
}
<div id="header">
<div>
<h1>Title</h1>
<h3>Subtitle</h3>
</div>
</div>
<div id="content" class="card">
<div>
<p>
One
</p>
<p>
Two
</p>
<p>
Thre
</p>
<p>
Fou
</p>
</div>
</div>
Three steps:
Apply a gradient background to the <body>.
Create two sectioning elements: <header> and <section>
Ensure all the relevant elements in <header> and at the top of <section> have an explicitly declared height in pixels which, combined, match the height of the first part of the gradient.
Make sure that the html and body have height: 100% or min-height: 100% otherwise height 35% is not going to be 35% of the viewport height.
You can make the black background with an absolute positioned element. I suggest to look into css position(relative, absolute, fixed, static).
Here's a demo and the code:
https://jsfiddle.net/n617L6rh/
<div id="bg"></div>
<div id="header">
<div>
<h1>Title</h1>
<h3>Subtitle</h3>
</div>
</div>
<div id="content" class="card">
<div>
<p>One</p>
<p>Two</p>
</div>
</div>
html,
body {
height: 100%;
}
#bg {
height: 35%;
background: black;
position: absolute;
top: 0;
left: 0;
right: 0;
}
#header {
height: 35%;
color: #fff;
position: relative;
}
Currently I can not find a solution that is responsive and scrollable to put text on an image. I need the height to be flexible and the width at 100%. I tried to use position:relative; and css background images with no luck. When I use position: relative; there is a space at the top of the image and the only way to delete it is negative margins which I think is not sustainable it there are multiple posts. css backgrounds does not show the full image unless you set dimensions and when is responsive you cant set dimensions. I dont think I can use position absolute because it would not scroll. so I dont not know what to use.
I have this HTML code here:
<div class="post">
<span><a>Posted By Adam</a></span>
<img width="100%" src="uimg/adam-levine-600.jpg">
</div>
Use position: absolute; and add a spacer for the nav:
http://jsfiddle.net/ryanpcmcquen/p3bes5xq/
.nav {
height: 100px;
width: 100%;
position: fixed;
background-color: #21A7F0;
text-align: center;
z-index: 10;
color: #ffffff;
}
.spacer {
height: 105px;
}
.post span {
position: absolute;
color: #ffffff;
text-shadow: 1px 1px 2px #000000;
}
.post img {
width: 100%;
}
<div class="nav">nav bar</div>
<div class="spacer"></div>
<div class="post"> <span><a>Posted By Adam</a></span>
<img src="//fillmurray.com/880/450" />
</div>
<div class="post"> <span><a>Posted By Adam</a></span>
<img src="//fillmurray.com/880/260" />
</div>
<div class="post"> <span><a>Posted By Adam</a></span>
<img src="//fillmurray.com/880/194" />
</div>