How to put text bocks over image on a fixed background? - html

I'd like to put news titles on image background like this:
What I came up with, is this:
<div class="image-container">
<img src="/path/to/img" class="img-thumbnail">
<div class="text-block">
<div class="bottom-right">My News Title</div>
</div>
</div>
CSS:
.text-block {
position: absolute;
bottom: 0px;
right: 0px;
background-color: #fff;
opacity: .8;
color: black;
width: 100%;
}
.bottom-right {
color: black;
position: absolute;
bottom: 8px;
right: 16px;
background-color: #fff;
}
Which results in this:
My problem is how to modify my code so that the title appears on a semi-transparent background which covers 1/3 of the bottom of the block, like the coffee picture above?

You could clean up the markup a bit and use a simple container element with a title (e.g. a div and a heading).
As noted by #zero298 if the image doesn't convey any information it should be defined as a background of the container
Codepen demo
Markup
<div style="background-image: url(https://.../coffee-serum-300x240.jpg)">
<h2>
The title of this article
may span across several lines.
but it's always anchored on the bottom
</h2>
</div>
CSS
div {
width: 300px;
height: 240px;
background-size: cover;
background-repeat: no-repeat;
display: flex;
flex-direction: column;
justify-content: flex-end;
}
div h2 {
background: rgba(255, 255, 255, .6);
margin: 0;
padding: 1em 1em 3em 1em;
}
Final result

Since the image is decorative, use background-image:
.card {
width: 100px;
height: 200px;
color: white;
background-color: blue;
background-image: url(https://cdn.sstatic.net/Sites/stackoverflow/img/sprites.svg);
background-size: contain;
background-position: center;
background-repeat: no-repeat;
display: flex;
flex-direction: column;
justify-content: flex-end;
}
.text{
background-color: rgba(1,1,1,0.5);
}
<div class="card">
<div class="text">
<h2>Hello World</h2>
<p>Foo bar</p>
</div>
</div>

You could do the following :
.bottom-right {
color: black;
position: absolute;
bottom: 50%;
right: 50%;
background-color: #fff; }
.image-container {
position: relative;
text-align: center;
color: white; }
.text-block {
position: absolute;
top: 95%;
left: 50%;
height: 40%;
transform: translate(-50%, -50%); }
Also, I think this is very helpful form w3schools.
https://www.w3schools.com/howto/howto_css_image_text.asp

Related

Position image inside and behind a background image

I'm planning to position some images in a circle background image but it doesn't seem to work well.
My goal is to center these images behind and inside the circle.
Any help?
div.info_image {
text-align: center;
border: none;
background: url("http://japancrystal.altervista.org/themes/crystal/imgs/colonna_sx/cerchio_descrizioneluogo.png");
background-repeat: no-repeat;
background-position: center;
height: 220px;
width: 100%;
}
img.img_place{
text-align: center;
border-radius: 100%;
width: 155px;
height: 155px;
margin-top: 30px;
margin-left: 30px;
}
<div class="info_image">
<img src="http://japancrystal.altervista.org/themes/crystal/imgs/locations/ingresso.png" class="img_place">
</div>
JSFIDDLE DEMO
RESULT DEMO
You can add this code to your img_place class :
img.img_place {
position: relative;
z-index: -1;
}
Try this :
div.info_image {
position: relative;
background: url(http://japancrystal.altervista.org/themes/crystal/imgs/colonna_sx/cerchio_descrizioneluogo.png);
background-repeat: no-repeat;
background-position: center;
height: 220px;
}
img.img_place {
position: absolute;
top: calc(50% + 3px);
left: calc(50% + 7px);
transform: translate(-50%, -50%);
border-radius: 100%;
height: 173px;
z-index: -1;
}
Try this.
div.info_image {
border: none;
background: url("http://japancrystal.altervista.org/themes/crystal/imgs/colonna_sx/cerchio_descrizioneluogo.png") no-repeat center center;
height: 220px;
width: 244px;
display: flex;
justify-content: center;
align-items: center;
}
img.img_place {
border-radius: 100%;
width: 173px;
height: 173px;
transform: translate(8px, 3px);
position: relative;
z-index: -1;
}
<div class="info_image">
<img src="http://japancrystal.altervista.org/themes/crystal/imgs/locations/ingresso.png" class="img_place">
</div>
Not sure why your images don't show in snippet, but they do in this fiddle...
FIDDLE
You can use display: grid to place them on top of each other.
Because the "ring" image isn't placed with the circle in the middle, I had to use transform: translate to move it, which changed the stacking order too. That's why there is a z-index of -1 on the city image.
For some reason, justify-content: center and align-items: center on .info_image didn't work as well as using self-justify and self-align on the images.
div.info_image {
display: grid;
grid-template-areas: "stacked";
height: 220px;
width: 100%;
}
.info_image > img {
--img-size: 155px;
border-radius: 50%;
grid-area: stacked;
width: var(--img-size);
justify-self: center;
align-self: center;
}
.info_image > .city {
--img-size: 110px;
transform: translate(5px, 2px);
z-index: -1;
}
<div class="info_image">
<img class="city" src="https://japancrystal.altervista.org/themes/crystal/imgs/locations/ingresso.png" />
<img src="https://japancrystal.altervista.org/themes/crystal/imgs/colonna_sx/cerchio_descrizioneluogo.png" />
</div>

How to keep a button centered on an image on all viewpoints in CSS (or bootstrap)?

I have a button centered on my hero image but with my current way I would have to use many media querys to keep it centered on all viewpoints.
<style>
.hero-container{
position: relative;
text-align: center;
}
.hero-btn-div{
position: absolute;
top: 50%;
left: 42%;
display: inline-block;
}
.hero-btn{
background-color: transparent;
font: var(--main-sans-serif);
font-size: 1.2em;
border: 1px solid black;
border-radius: 100px;
width: 300px;
height: 50px;
}
</style>
<div class="hero-container">
<img src="images/dream%20of%20winter%20cut.jpg" class="img-fluid" width="1920px" alt="Artwork">
<div class="hero-btn-div"><button class="hero-btn">Button</button></div>
</div>
you will update left:50% and add this transform:translateX(-50%;)
<style>
.hero-container {
position: relative;
text-align: center;
}
.hero-btn-div {
position: absolute;
top: 50%;
left: 50%;
transform:translateX(-50%);
display: inline-block;
}
.hero-btn {
background-color: transparent;
font: var(--main-sans-serif);
font-size: 1.2em;
border: 1px solid black;
border-radius: 100px;
width: 300px;
height: 50px;
}
</style>
<div class="hero-container">
<img src="images/dream%20of%20winter%20cut.jpg" class="img-fluid" width="1920px" alt="Artwork">
<div class="hero-btn-div"><button class="hero-btn">Button</button></div>
</div>
It is hard to say what you are trying to accomplish, since we cannot see the image or don't know what the .img-fluid class is supposed to do. But, since your main issue appears to be the centering of content, I would advise looking into the "display: flex" property (more info here). Moreover, this could be a handy tool for you.
I would try if the following solution works for you to keep the button centered:
.hero-container{
display: flex;
justify-content: center;
position: relative;
}
.hero-btn-div{
position: absolute;
}
.hero-btn{
background-color: transparent;
font: var(--main-sans-serif);
font-size: 1.2em;
border: 1px solid black;
border-radius: 100px;
width: 300px;
height: 50px;
}
<div class="hero-container">
<img src="images/dream%20of%20winter%20cut.jpg" class="img-fluid" width="1920px" alt="Artwork">
<div class="hero-btn-div">
<a href="#">
<button class="hero-btn">Button</button>
</a>
</div>
</div>
You can stretch the wrapper for the button to the full size of the container and center the button with display: flex. Dont forget about pointer-events.
.hero-container {
position: relative;
}
.hero-btn-div {
display: flex;
align-items: center;
justify-content: center;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.hero-btn {
background-color: transparent;
font: var(--main-sans-serif);
font-size: 1.2em;
border: 1px solid black;
border-radius: 100px;
width: 300px;
height: 50px;
pointer-events: auto;
}
There are many ways but I would use Bootstrap's flexbox classes...
<div class="hero-container">
<img src="..." class="img-fluid" width="1920px" alt="Artwork">
<div class="hero-btn-div d-flex align-items-center justify-content-center"><button class="hero-btn">Button</button></div>
</div>
With a full size absolute container...
.hero-btn-div {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
Responsive demo
One solution is to add picture as a CSS background-image property inside container div, then center button via flexbox.
Another solution is to absolute position image inside container div, and then center button via flexbox -> example:
.hero-container {
position: relative;
text-align: center;
display: flex;
justify-content: center;
align-items: center;
height: 500px;
overflow: hidden;
}
.hero-container .bg-img {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
}
.hero-btn {
position: relative;
background-color: transparent;
font: var(--main-sans-serif);
font-size: 1.2em;
border: 1px solid black;
border-radius: 100px;
width: 300px;
height: 50px;
}
<div class="hero-container">
<div class="bg-img">
<img src="https://picsum.photos/200/300" class="img-fluid" width="100%" alt="Artwork">
</div>
<div class="hero-btn-div">
<a href="#">
<button class="hero-btn">Button</button>
</a>
</div>
</div>
PS: Don't ever use fixed sizes (1920px) if you want to build a responsive web page.

How do I clear a filter placed by a parent element in CSS?

I have a <div> with a background image and I'd like the background image to have a filter applied. But I'd also like the div to contain a <p> tag which overlays the image -- and I DONT want the <p> to have that same filter.
How do I clear the filter set by the <div> in the <p>?
What I've got so far:
<div className="artistDetailImage" style={{backgroundImage: url(${this.props.artistImage})`}}>
<p className="artistDetailText">{name}</p>
</div>
.artistDetailImage {
background-size: cover;
background-position: top;
width: 100%;
height: 300px;
filter: contrast(60%);
}
.artistDetailText {
width: 100%;
font-size: 20px;
text-align: left;
padding-left: 5px;
position: relative;
top: 240px;
filter: none; //Have also tried contrast(100%), brightness(1), etc.
color: white;
}
This answer seems to work, but I was hoping there was a better way to do it.
https://stackoverflow.com/a/32677739/3010955
You won't be able to reset the filter for the child element, but you can encase both in a container div, then position the <p> accordingly.
.container-div {
position: relative;
}
.artistDetailImage {
background-size: cover;
background-position: top;
width: 100%;
height: 300px;
filter: contrast(60%);
}
.artistDetailText {
width: 100%;
font-size: 20px;
text-align: left;
padding-left: 5px;
position: absolute;
top: 240px;
color: white;
}
<div class="container-div">
<div class="artistDetailImage" style="background-image: url('http://static.pexels.com/photos/33688/delicate-arch-night-stars-landscape.jpg');">
</div>
<p class="artistDetailText">NAME</p>
</div>

Align an image in div with text below

I've looked for a previous answer that helped me but couldn't find one.
HTML:
<section id="pane_one">
<h1 id="logo">lel</h1>
<h2 id="hello"><span>Hello.</span> You’ve reached my page.</h2>
</section>
CSS:
section#pane_one {
display: block;
position: relative;
width: 100%;
text-align: center;
min-height: 450px;
height: 700px;
}
h1#logo {
position: absolute;
z-index: 0;
width: 200px;
height: 200px;
background-color: #000;
text-indent: -9999px;
top: 35%;
left: 44.5%;
margin-top: -65px;
margin-left: -33px;
background: url('https://i.imgur.com/RMQi9Js.jpg') center center no-repeat;
background-size: 100%;
border-radius: 50%;
border: solid black;
}
How do I set it up so that no matter the zoom of the page, the div/image is always horizontally aligned with the text below it?
The site is: http://libeclipse.me/
<html>
<head>
<style>
section#pane_one {
display: block;
position: relative;
width: 100%;
text-align: center;
min-height: 450px;
height: 700px;
}
h1#logo {
position:relative;
z-index: 0;
width: 200px;
height: 200px;
background-color: #000;
text-indent: -9999px;
top: 200px;
display: inline-block;
background: url('https://i.imgur.com/RMQi9Js.jpg') center center no-repeat;
background-size: 100%;
border-radius: 50%;
border: solid black;
}
h2#hello {
position:absolute;
top:0px;
width:100%;
margin:auto;
}
</style>
</head>
<body>
<section id="pane_one">
<h1 id="logo">lel</h1>
<h2 id="hello"><span>Hello.</span> You’ve reached my page.</h2>
</section>
</body>
<html>
Review this and make note of the margin:auto (Centered the h2text in absolute position) and display:inline-block(Centered the bunny image).
Removed the left % and margins that were present from h1#logo.
Enjoy.
Thanks.
On the h1#logo rule change the left to 50% and the margin-left to -100px

How to keep a div constantly above a single point on the background?

I have a div which has a background of a map. The map is centred and has a background size of 'contain'. The page is responsive so when the window resizes, so does the map. I need to be able to have a div on top of a certain country on the map, and on resize of the background map, the div stays directly on top of it.
So far I have
<div id="map-holder">
<div class="content">
<div class="placeholder"></div>
</div>
</div>
The div with the class of placeholder is the div i wish to keep on top of a certain country. The div with map-holder for ID is the div with the map background. Content is just to keep it all in place.
CSS
.content {
text-align: center;
width: 1200px;
margin: 0 auto;}
#map-holder {
position: relative;
height: 1000px;
width: 100%;
background: #F0F0F0;
background-image: url(../images/image-mapster.min.png);
background-size: contain;
background-position: center center;
background-repeat: no-repeat;
padding: 30px;
}
.placeholder {
position: absolute;
right: 30px;
background: #fff;
width: 80px;
height: 50px;
border: 1px solid #000;
margin: 5px;
padding: 5px;
padding: 0;
cursor: pointer;
}
.placeholder img {
width: 100%;
height: 100%;
padding: 0;
}
.placeholder:before {
position: absolute;
top: 30%;
left: 45%;
font-weight: bold;
content: '+';
}
The only solution I can think if actually putting an image over the map.
You can do this by having multiple CSS backgrounds. Just change your code for #map-holder to this:
#map-holder {
position: relative;
height: 500px;
width: 500px;
background: #F0F0F0;
background-image: url(this_image_goes_on_top.png), url(your_map.jpg);
background-size: contain, contain;
background-position: center center, center center;
background-repeat: no-repeat, no-repeat;
padding: 30px;
}
I made a little JSFiddle out of your code for demonstration: https://jsfiddle.net/zamofL9g/1/
Basically, it's a little difficult, as I recall, when using background images.
Since the image is, technically speaking "content" you can use an inline image and suitable wrapping divs. The 'pins' can then be positioned using % based positioning values.
Here's a Codepen demo I made some time ago. This one has a tooltip too!
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.map {
width: 90%;
margin: 10px auto;
position: relative;
}
.map img {
max-width: 100%;
}
.box {
width: 2%;
height: 5%;
background-image: url(http://www.clipartbest.com/cliparts/ncX/qyL/ncXqyLdcB.png);
background-position: top center;
background-repeat: no-repeat;
background-size: contain;
position: absolute;
}
#pin-1 {
top: 25%;
left: 38%;
}
.box:hover > .pin-text {
display: block;
}
.pin-text {
position: absolute;
top: -25%;
left: 110%;
width: 300%;
display: none;
}
.pin-text h3 {
color: white;
text-shadow: 1px 1px 1px #000;
}
<div class="map">
<img src="http://connect.homes.com/wp-content/uploads/2012/05/200392710-0012.jpg" alt="" />
<div id="pin-1" class="box">
<div class="pin-text">
<h3>My House</h3>
</div>
</div>
</div>