Pseudoelement selector for inserting icon inside of another element - css

I would like to use some pseudo-element selector (:before, :after) for including a text or icon on :hover inside some other element. Is there a way how to do that only with pseudo-element selectors? I would like to NOT actually change the content of that element as it's content is loaded from localStorage.
My vision is to visually add some click-able icon (✗, 🖉) when hovering over the header.
<header contenteditable="true" id="title" onkeyup="store(this.id);"></header>
Thank you for your suggestions.

You can add your icon as background-image .
header{
position:relative;
}
header:before{
content:" ";
position:absolute;
top:0;
left:0;
width:5px;
height:5px;
background: url(icon.png) no-repeat ;
}

i guess you wanted something like this ?
you can also use fontAwesome in the after element or a background-image.
header {
height:100px;
width:100px;
position:relative;
background:Red;
}
header:after {
content:"X";
position:absolute;
right:0;
top:0;
color:#fff;
transition:0.3s ease-out;
opacity:0;
}
header:hover:after {
opacity:1;
}
<header contenteditable="true" id="title" onkeyup="store(this.id);"></header>
option2. using plain Javascript
added the fontAwesome icon using javascript ( beforeend = inserted right before the end of the header tag -> you can use beforebegin,afterbegin,beforeend,afterend see more here insertAdjacentHTML )
then, you can add a click event on the previously added icon. ( here i am toggling the class of header changing it's color )
let me know if it helps
var newElem = document.getElementById("title");
newElem.insertAdjacentHTML('beforeend', '<i id="clickme" class="fa fa-times-circle" aria-hidden="true"> </i>');
document.getElementById("clickme").onclick = function() {
newElem.classList.toggle('blue');
};
header {
height: 100px;
width: 100px;
position: relative;
background: Red;
transition: 0.3s;
}
header:hover .fa {
opacity: 1;
}
.fa {
color: white;
position: absolute;
top: 0;
right: 0;
cursor: pointer;
opacity: 0;
}
header.blue {
background: blue;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"/>
<header contenteditable="true" id="title" onkeyup="store(this.id);"></header>

try using the content attribute in the pseudo selectors or add it as a background image

Related

Image hover hitbox not big enough

I’m trying to make an hover effect with an image that increase the size but it doesn’t react everywhere. For other words, how do increase the Hitbox for a image without it actually expanding
Ok, first of all. I tried to use different kinds of scaling, margin, padding, etc. but I just don’t have enough experience
You can set the width and height of the image by doing this into your css file :
your class / attriute{
width:50px;
height:50px;
transition: transform .2s; /* Simple animation */
}
And then you can add this property :
your class / attriute:hover{
transform:scale(2.5);
}
Here are a few ways to enlarge the hit-hover area of an element.
Option 1: large transparent border
img {
border: 50px solid transparent;
}
img:hover {
filter: contrast(0.3);
}
<img src="https://via.placeholder.com/150" />
Option 2: add the image in CSS background: url()
div {
width: 300px;
height: 300px;
background: url(https://via.placeholder.com/150) no-repeat 50% 50%;
}
div:hover {
filter: contrast(.3);
}
<div></div>
Option 3: Target a sibling element
.wrap {
position: relative;
}
.wrap div,
.wrap img {
position: absolute;
}
.wrap div {
width: 300px;
height: 300px;
z-index: 2;
}
.wrap div:hover + img {
filter: contrast(.3);
}
<div class="wrap">
<div></div>
<img src="https://via.placeholder.com/150" />
</div>

CSS overlay over image background

I want to have an overlay over my image background, in order to see the white text above the image more clearly.
Why won't this solution work ?
HTML:
<div id="myDiv" class="bg1 image-cover">
<p>H</p>
</div>
CSS:
#myDiv {
width: 300px;
height: 300px;
color: #fff;
position: relative;
font-size: 100px;
font-weight: bold;
}
.image-cover:before {
content:'\A';
position: absolute;
width:100%;
height:100%;
top:0;
left:0;
background:rgba(0,0,0,0.9);
opacity: 1;
}
.bg1 {
background-size: cover;
background: url('https://2zpt4dwruy922flhqyznip50-wpengine.netdna-ssl.com/wp-content/uploads/2014/12/lock-and-stock-photos.jpg');
}
while this one does:
HTML:
<div id="myDiv" class="bg1">
<div class="image-cover">
<p>H</p>
</div>
</div>
CSS:
...
.image-cover {
content:'\A';
position: absolute;
width:100%;
height:100%;
top:0;
left:0;
background:rgba(0,0,0,0.9);
opacity: 1;
}
...
I think I am misunderstanding the way :before works, but I am not fan of the second solution as it has one more div than the first.
I'm glad you're already aware of the second solution; this tends to be the approach I normally use (though not for any particular reason). You can simply modify your original approach as follows and get the desired effect:
#myDiv > p {
position: relative;
}
Namely, give the nested <p> tag a non-static position value. See here: CodePen
You can just increase the z-index of the text which is to be overlaid over the image like this:
#myDiv{
z-index: 1;
}
#myDiv p{
z-index: 2; /* should be more than the z-index of the background */
}

Overlay image on radio button selection

Below is a code snippet from a page that shows attribute options. The code shown is for an item selected by hidden radio button, and the requirement was for the attribute image to be overlaid with a checkmark when selected.
Whilst I have got the checkmark to show on selection, it is always behind the original image. I have tried using z-index, opacity, and also absolute and relative positioning. What is the way to achieve this? I've run out of ideas.
I have already looked at multiple answers for similar questions, but none have worked for me.
<div class="attribImg">
<input type="radio" name="id[11]" value="61" checked="checked" id="attrib-11-61" />
<label class="attribsRadioButton four" for="attrib-11-61"><img src="images/attributes/18mg.png" alt="" width="50" height="50" />
</label>
<br />
</div>
Css used is:
input[type="radio"] {
visibility:hidden;
}
label {
cursor: pointer;
}
.attribImg {z-index:0;}
input[type="radio"]:checked+label{
background:url(../images/checkmark.png) no-repeat 7px -20px;
}
Fiddle is http://jsfiddle.net/1jcz1xyn/
The problem is caused by the <img> being inside the <label>. No matter the z-index you use on the label, the child (img), will always be above it's parent background, unless you set the child with z-index: -1 (updated fiddle - click the 2nd):
.attribImg {
position: relative; /** this is the positioning context **/
width: 50px;
height: 50px;
}
input {
visibility: hidden;
}
label {
position: absolute;
width: 100%;
height: 100%;
cursor: pointer;
}
img {
position: relative;
z-index: -1;
}
input[type="radio"]:checked+label { background:url(https://cdn1.iconfinder.com/data/icons/mimiGlyphs/16/check_mark.png) no-repeat center center;
}
i suggest you try to manipulate the icon/image with CSS background-image only, instead of having one case with img and another with CSS. If you do that, z-index won't fix cause it's a logical error.
If you are going to use images/icons (in some cases that's useful for transitions), its also better to add both with the same technique and change the state with css.
So, you can manipulate with CSS:
<div class="form-item">
<label for="radio-1">
<input type="radio" name="radios" id="radio-1"> <span>Sample 1</span>
</label>
</div>
label > input[type="radio"]{
display: none;
}
input[type="radio"]:checked + span:after{
font-family:"FontAwesome";
content: "\f00c";
margin-left:10px;
}
And you can add both images in the same form-field, and manipulate them with some CSS (And FontAwesome):
<div class="form-item">
<label for="radio-switch-2">
<input type="radio" name="radios-2" id="radio-switch-2">
<div class="radio-switch-state">
<span class="icon-off"></span>
<span class="icon-on"></span>
</div>
</label>
</div>
input[type="radio"]{
display: none;
}
.radio-switch-state{
background-color:#aaa;
display: inline-block;
color:#fff;
transition:all .5s ease;
width:50px;
height:50px;
position: relative;
overflow:hidden;
vertical-align:middle;
}
.radio-switch-state .icon-on,
.radio-switch-state .icon-off{
display: inline-block;
position: absolute;
width:100%;
top:0;
}
// If you are using font
.radio-switch-state .icon-on:after,
.radio-switch-state .icon-off:after{
font-family:"FontAwesome";
width:100%;
display: block;
line-height:50px;
position: absolute;
transition:all .5s ease;
}
.radio-switch-state .icon-on:after{
content: "\f00c";
margin-left:100;
left:100%;
}
.radio-switch-state .icon-off:after{
content: "\f056";
left:0;
}
.radio-switch-state:hover{
background-color:#FFA374;
cursor:pointer;
}
input[type="radio"]:checked + .radio-switch-state{
background-color:#FA8144;
}
input[type="radio"]:checked + .radio-switch-state .icon-on:after{
content: "\f00c";
margin-left:100;
left:0;
}
input[type="radio"]:checked + .radio-switch-state .icon-off:after{
content: "\f056";
left:-100%;
}
I made a Pen with both samples.
Also, when using icons try to use an icon font library, like FontAwesome or Glyphicon.
Edit - Added a sample with images
Maybe this variant will help you? It's without any changes to HTML. I've used ::after instead and here's the Fiddle: https://jsfiddle.net/mvchalov/1jcz1xyn/2/
input[type="radio"]:checked+label::after{
background:url(https://cdn1.iconfinder.com/data/icons/mimiGlyphs/16/check_mark.png) no-repeat center center;
width:50px;
height:50px;
position: absolute;
content:'';
margin:0 0 0 -50px;
}

How to apply hover effect to image when hovering over a button placed on top of that image?

I have applied a hover effect to an image and want to keep this hover effect when hovering over a button placed on top of that image. I know questions around these types of parent/child issues have been asked before but these answers were not extensive enough to help me resolve this particular issue on my own. A CSS only solution would be nice but I guess it requires some JS to work around it.
Here is the JSfiddle:
http://jsfiddle.net/stijn777/k8dbfs3r/3/
As you can see in the HTML below I want the hover effect on the image class to also be applied when hovering over the button inside the picture class.
<div class="picture">
<a class="image" href="./profile-picture.html">
<img alt="" src="http://www.liverpoolblogg.no/wp-content/uploads/2013/09/Daniel-Sturridge1.jpg">
</a>
<input class="image btn btn-6 btn-6d" a href="www.test.com" type="button" value=" ADD " />
<div class="player">
Name
</div>
</div>
I used the following CSS for the image and the button on top of the image.
.image:after {
content:'\A';
position:absolute;
width:55%; height: 70%;
top:0; left:0;
background:rgba(0,0,0,0.25);
opacity:0;
border-radius: 6px 6px 0px 0px;
}
.image {
width: 100%;
}
.image:hover:after {
opacity:1;
}
.picture:hover input {
display: block;
}
.picture .player {
position:absolute;
margin-top: -40px;
background-color: white;
opacity: 0.7;
width: 55%;
height: 40px;
padding-top: 8px;
text-align: center;
}
.player a {
color: #000000;
font-family: sans-serif;
font-size: 14px;
text-decoration: none;
}
.picture input {
position:absolute;
top: 0;
left: 0;
margin-top: 10px;
width: 55%;
}
Hope someone can help me out,
Stijn
change the .image:hover:after rule to .picture:hover .image:after. :hover applies to whatever your mouse is over, as well as its parent element(s), but the button and image are siblings. changing the hover to be detected on their mutual parent div (.picture) causes the hover to be correctly detected
.picture:hover .image:after {
opacity:1;
}
http://jsfiddle.net/5n7gj8uc/
You could activate the hover effect using the parent div, like so:
.picture:hover > .image:after {
opacity:1;
}
http://jsfiddle.net/k8dbfs3r/5/

Add an image layer on hover

I tried hard but could not find a solution...
I need, just using HTML/CSS to add an image layer over another image on mouseover.
The new layer should not replace the old one, only be summed (because it is semi-transparent).
My problem is: I have multiple different images and on mouseover the same layer should be added to them.
Is this possible?
I tried
<style type="text/css">
.image1
{
opacity:1.0;
filter:alpha(opacity=100); /* For IE8 and earlier */
}
.image1:hover
{
background: url(http://bridgeditalia.it/wp-content/uploads/2014/07/over.png) no-repeat;
} </style>
but not working :(
Try to change the Z-INDEX on the hover and add some transition.
Here is one option using a wrapping div and a pseudo element
JSfiddle demo
HTML
<div class="img-wrap">
<img src="http://lorempixel.com/output/sports-q-c-200-200-9.jpg" alt=""/>
</div>
<div class="img-wrap">
<img src="http://lorempixel.com/output/animals-q-c-200-200-4.jpg" alt=""/>
</div>
CSS
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.img-wrap {
display: inline-block;
margin: 25px;
position: relative;
}
.img-wrap:after {
content:"";
position: absolute;
width:100%;
height:100%;
top:0;
left:0;
background-image: url(http://bridgeditalia.it/wp-content/uploads/2014/07/over.png);
background-size: cover;
background-repeat: no-repeat;
opacity:0;
transition:opacity .5s ease;
}
.img-wrap:hover:after {
opacity:1;
}
.img-wrap img {
display: block;
}

Resources