I am trying to draw a number of CSS generated circles that have images as background. In my current code, the background image is set as a fixed image in the CSS code.
.container {
text-align: center;
}
.circle {
position: relative;
height: 180px;
width: 180px;
border-radius: 50%;
display: inline-flex;
vertical-align: top;
justify-content: center;
align-items: center;
overflow: hidden;
text-decoration: none;
border: 0;
margin: 10px;
}
.circle:after {
content: "";
position: absolute;
z-index: -1;
top: 0;
left: 0;
height: 100%;
width: 100%;
background: url("http://deepchains.com/images/team.png") center / cover no-repeat;
opacity: .25;
transition: .25s;
}
.circle:hover:after {
opacity: 1;
color: transparent;
}
.circle:hover {
color: transparent;
}
.ccont {
display: inline-block;
margin: 10px;
}
.ccont:hover {
color: transparent;
}
<div class="container">
<a class="circle" href="http://www.url1.html">
<div class="ccont" style="font-weight: bold; text-decoration:none !important;">This is <br>Text1</div>
</a>
<a class="circle" href="http://www.url2.html">
<div class="ccont" style="font-weight: bold; text-decoration:none !important;">This is <br>Text2</div>
</a>
</div>
Here is a sample result that I see in Chrome Browser:
My question is how to change the background images of each circle separately in the HTML code? I will have a number of these circles that I like them to be aligned and in the same line, and I want to set their background images in the HTML code and remove the background image from the CSS. How can I do that?
An easy solution is to transform your peudo element to an element and use background-image as inline style so you can easily change the image for each element and apply all the same properties as the pseudo element:
.container {
text-align: center;
}
.circle {
position: relative;
height: 180px;
width: 180px;
border-radius: 50%;
display: inline-flex;
vertical-align: top;
justify-content: center;
align-items: center;
overflow: hidden;
text-decoration: none;
border: 0;
margin: 10px;
}
.circle .image {
position: absolute;
z-index: -1;
top: 0;
left: 0;
height: 100%;
width: 100%;
background-position:center;
background-size:cover;
background-repeat:no-repeat;
opacity: .25;
transition: .25s;
}
.circle:hover .image {
opacity: 1;
color: transparent;
}
.circle:hover .ccont{
color: transparent;
}
.ccont {
display: inline-block;
margin: 10px;
}
<div class="container">
<a class="circle" href="http://www.url1.html">
<span class="image" style="background-image: url(http://lorempixel.com/400/300/)"></span>
<div class="ccont" style="font-weight: bold; text-decoration:none !important;">This is <br>Text1</div>
</a>
<a class="circle" href="http://www.url2.html">
<span class="image" style="background-image:url(https://lorempixel.com/400/200/)"></span>
<div class="ccont" style="font-weight: bold; text-decoration:none !important;">This is <br>Text2</div>
</a>
</div>
Simply set up the background image on the pseudo to be inherited from parent, set the size on parent to 0 to hide it there, and finally, set style="background-image: url(...)" in the markup.
Updated
You can even drop the inner div and still achieve the same effect
Stack snippet
.container {
text-align: center;
}
.circle {
position: relative;
height: 180px;
width: 180px;
border-radius: 50%;
display: inline-flex;
justify-content: center;
align-items: center;
overflow: hidden;
font-weight: bold;
text-decoration: none;
border: 0;
margin: 10px;
background-size: 0; /* hide image by set its size to 0 */
}
.circle:after {
content: "";
position: absolute;
z-index: -1;
top: 0;
left: 0;
height: 100%;
width: 100%;
background-image: inherit; /* inherit from parent */
background-position: center;
background-size: cover;
background-repeat: no-repeat;
opacity: .25;
transition: .25s;
}
.circle:hover:after {
opacity: 1;
}
.circle:hover {
color: transparent;
}
<div class="container">
<a class="circle" href="http://www.url1.html" style="background-image: url(https://picsum.photos/300/300?image=1070);">
This is <br>Text1
</a>
<a class="circle" href="http://www.url2.html" style="background-image: url(https://picsum.photos/300/300?image=1072);">
This is <br>Text2
</a>
</div>
Related
When I hover my mouse on the background of the image, the 3D effect triggers, but when I move over the girl image area, it doesn't trigger anymore. The transparent image is overlaying the background. How do I make it triggers when the mouse is over the girl image?
See what I did on JSFIDDLE
HTML -
<div class="test-imgplace">
<img src="https://christianluneborg.com/imgs/test-woman.png" class="imgtest">
<div class="card-hover">
<div class="card">
<div class="card-content">
<div class="image" style="background-image: url(https://christianluneborg.com/imgs/test-woman-bg.png);"></div>
</div>
</div>
</div>
</div>
CSS -
.card .image{
height: 275px;
width: 183px;
background-size: cover;
background-position: center center;
}
.imgtest {
position: absolute;
z-index: 1;
text-align: center;
top: 0;
left: 100px;
border: solid 1px;
}
.test-imgplace {
margin: 0 auto;
position: relative;
overflow: hidden;
}
.card .text{
height: 20%;
margin: 0;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
text-transform: uppercase;
font-size: 50px;
position: absolute;
right: 0;
left: 0;
top: 0;
bottom: 0;
}
.card{
width: 183px;
height: 275px;
margin: auto auto;
background: #383030;
border-radius: 5px;
overflow: hidden;
text-align: center;
}
.card-content{
transform-style: preserve-3d;
}
body{
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
}
.hover-in{
transition: .3s ease-out;
}
.hover-out{
transition: .3s ease-in;
}
.card-hover{
margin:0;
}
When the mouse hovers over the semi-transparent image, it causes mouse events to go there instead of the script that is applying the 3D effect.
This is easy to fix - just add pointer-events: none; to .imgtest. JSFiddle of the fix
I'm noticing some lag from some of my images, and I was curious on what the most efficient way of loading a zoomable background image would be: Img tag vs a pseudo element with a background image.
My goal is to not have lag on a high quality image.
Which of these would be best from a performance perspective for zoomable background image?
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.card-list {
display: flex;
justify-content: center;
gap: 20px;
padding: 20px;
}
.card {
height: 300px;
width: 250px;
position: relative;
box-shadow: 0 0 5px grey;
overflow: hidden;
border-radius: 20px;
background-color: #0004;
}
.card-text {
display: flex;
align-items: flex-end;
color: white;
font-size: 24px;
font-family: Arial;
padding: 20px;
height: 100%;
width: 100%;
}
.background-image {
position: absolute;
object-fit: cover;
object-position: center;
height: 100%;
width: 100%;
transition: transform .4s ease;
z-index: -1;
}
.card:hover .background-image,
.psuedo-bg-img:hover::before {
transform: scale(1.1);
}
.psuedo-bg-img {
position: relative;
}
.psuedo-bg-img::before {
content: '';
background-image: url('https://source.unsplash.com/random');
background-position: center;
background-size: cover;
background-repeat: no-repeat;
position: absolute;
height: 100%;
width: 100%;
z-index: -1;
transition: transform .4s ease;
}
<div class="card-list">
<div class="card">
<img class="background-image" src="https://source.unsplash.com/random">
<div class="card-text">
<h3><img> as Background</h3>
</div>
</div>
<div class="card psuedo-bg-img">
<div class="card-text">
<h3>Pseudo Elem w/ Background Image</h3>
</div>
</div>
</div>
I've stuck with styling issues. What I'm trying to reach is make sure that every single h3 tag has same distance between bottom border of his container (pink border) and bottom border of his parent (picture bottom border). Now it looks like this:
Both of them has same css, difference is only with amount of text.
HTML:
<div class="col-6">
<a href='{{link}}' style='background-image: url("{{image}}")' class="histories__image">
<div class="histories__text">
<h3>{{title}}</h3>
</div>
<div class="histories__underline"></div>
</a>
</div>
CSS:
.histories {
margin-bottom: 100px;
&__image {
height: 41vh;
margin-top: 33px;
display: block;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
position: relative;
transition: filter 1s;
&:hover{
filter: brightness(80%);
}
&:hover .histories__text{
bottom: 15px;
}
&:hover .histories__underline{
opacity: 1;
left: 0;
width: 70%;
}
}
&__text {
text-align: center;
display: block;
position: absolute;
width: 100%;
bottom: 0px;
left: 50%;
transform: translate(-50%, -50%);
transition: bottom .3s;
color: white;
border: 1px solid pink;
}
&__underline {
position: absolute;
display: block;
bottom: 10%;
width: 0%;
left: 50%;
margin-left: 15%;
background-color: white;
height: 1px;
opacity: 0;
transition: width .3s, left .3s;
}
}
You can try using translate like this to center your content with the same class.
.histories__text{
width: 100%;
}
.histories__text h3{
transform: translate(50%,50%);
}
<div class="col-6">
<a href='{{link}}' class="histories__image">
<div class="histories__text">
<h3>Your Text</h3>
</div>
<div class="histories__underline"></div>
</a>
</div>
Using http://jsfiddle.net/4UNuB/5/ as an example, the image has been set as background
.box1 {
border: #999 2px solid;
width: 180px;
height: 250px;
background-image: url(http://smilesoftware.com/assets/images/uploads/products/icon_pdfpenipad_140x140.png);
background-position: center;
background-repeat: no-repeat;
}
But, if I can't use a background image, and instead have an img src within the div itself like this
<div class="box1">
<img src="http://placehold.it/180x250">
</div>
The mask no longer covers the image, instead sitting below it.
See this fiddle http://jsfiddle.net/4UNuB/6/
But the mask is still being applied to the same place as before, so why does it move, and how to stop it moving?
Add Position Absolute and relative css for boxes.
Check The Fiddle here
.box1 {
border: #999 2px solid;
width: 180px;
height: 250px;
/*background-image: url(http://smilesoftware.com/assets/images/uploads/products/icon_pdfpenipad_140x140.png);*/
background-position: center;
background-repeat: no-repeat;
position: relative;
}
.black-box {
text-align: center;
font-size: 16px;
color: #fff;
background-color: rgba(00,00,00,0.8);
width: 100%;
height: 100%;
opacity: 1.0;
transition: all 0.5s ease-in-out;
position: absolute;
top: 0;
}
You can put both in the same anchor and use positioning + z-index:
.box1 {
border: #999 2px solid;
width: 180px;
height: 250px;
/*background-image: url(http://smilesoftware.com/assets/images/uploads/products/icon_pdfpenipad_140x140.png);
background-position: center;
background-repeat: no-repeat;*/
}
img {
position: absolute;
z-index: 0;
}
.black-box {
position: relative;
z-index: 1;
text-align: center;
font-size: 16px;
color: #fff;
background-color: rgba(00,00,00,0.8);
width: 100%;
height: 100%;
opacity: 1.0;
transition: all 0.5s ease-in-out;
}
.black-box:hover {
opacity: 0.0;
transition: all 0.5s ease-in-out;
}
h2 {
padding-top: 110px;
margin: 0px;
}
<div class="box1">
<a href="http://placehold.it">
<img src="http://placehold.it/180x250">
<div class="black-box">
<h2>View Details</h2>
</div>
</a>
</div>
Also, I had to remove the h2's margin.
Building my portfolio site and trying to show project name on hover over the image. I found some css people use to achieve this but does not work on my site for some reason. What could I be doing wrong?
.portfolioImage-half {
display: inline-block;
margin: 25px 5px;
width: 30%;
background-color: #111;
overflow: hidden
}
.portfolio-half {
width: 100%;
}
.portfolioImg {
transition: all .3s ease-in-out;
}
.portfolioImg:hover {
opacity: .2;
transform: scale(1.1);
cursor: pointer;
}
span.text-content {
background: rgba(0, 0, 0, 0.5);
color: white;
cursor: pointer;
display: table;
height: 150px;
left: 0;
position: absolute;
top: 0;
width: 150px;
}
span.text-content span {
display: table-cell;
text-align: center;
vertical-align: middle;
}
span.text-content {
background: rgba(0, 0, 0, 0.5);
color: white;
cursor: pointer;
display: table;
height: 150px;
left: 0;
position: absolute;
top: 0;
width: 150px;
opacity: 0;
}
.portfolioImage-half:hover span.text-content {
opacity: 1;
}
<div class="row">
<div id="workBlock" class="portfolio-block fixed-bg dark-bg">
<div class="row portfolioRow">
<div class="portfolioImage-half">
<img src="assets/portfolio/doug-fir-logo-for-instagran.jpg" alt="Doug Fir Digital Logo" class="portfolioImg portfolio-half doug" data-target="#logoDesign">
<span class="text-content"><span>Place Name</span></span>
</div>
<div class="portfolioImage-half">
<img src="assets/portfolio/AmbeCreations-logo.jpg" alt="Ambe creations logo" class="portfolioImg portfolio-half ambe" data-target="#logoDesign">
</div>
</div>
</div>
</div>
Try using CSS Flexbox. Make your
.text-content a flex container using display: flex;
Give .portfolioImage-half position relative
Give width & height in %ages
Like:
.portfolioImage-half {
position: relative;
}
span.text-content span {
display: flex;
width: 100%;
height: 100%;
align-items: center;
justify-content: center;
}
Have a look at the working snippet below:
.portfolioImage-half {
position: relative;
display: inline-block;
margin: 25px 5px;
width: 30%;
background-color: #111;
overflow: hidden
}
.portfolio-half {
width: 100%;
}
.portfolioImg {
transition: all .3s ease-in-out;
}
.portfolioImg:hover {
opacity: .2;
transform: scale(1.1);
cursor: pointer;
}
span.text-content {
background: rgba(0,0,0,0.5);
color: white;
cursor: pointer;
display: block;
height: 100%;
left: 0;
position: absolute;
top: 0;
width: 100%;
}
span.text-content span {
display: flex;
width: 100%;
height: 100%;
align-items: center;
justify-content: center;
}
span.text-content {
background: rgba(0,0,0,0.5);
color: white;
cursor: pointer;
display: block;
height: 100%;
left: 0;
position: absolute;
top: 0;
width: 100%;
opacity: 0;
}
.portfolioImage-half:hover span.text-content {
opacity: 1;
}
<div class="row">
<div id="workBlock" class="portfolio-block fixed-bg dark-bg">
<div class="row portfolioRow">
<div class="portfolioImage-half">
<img src="http://placehold.it/100x100" alt="Doug Fir Digital Logo" class="portfolioImg portfolio-half doug" data-target="#logoDesign">
<span class="text-content"><span>Place Name</span></span>
</div>
<div class="portfolioImage-half">
<img src="http://placehold.it/100x100" alt="Ambe creations logo" class="portfolioImg portfolio-half ambe" data-target="#logoDesign">
</div>
</div>
</div>
</div>
Hope this helps!
No you cannot style the img alt attribute.
Try:
adding a "z-index: 1; position: absolute;" to the .portfolioImage-half:hover span.text-content class.
Then add a "z-index:0; position: relative;" to the .portfolioImage-half class
You will most probably need to position the "span.text-content" element by using "Left"/"Right" properties