how do I apply background image on the checkbox.I know this is something which is very easy but still I am finding it difficult
here is my HTML code . I don't know what is the issue in this
<div>
<style>
input[type="checkbox"], input[type="radio"] {
box-sizing: border-box;
padding: 0px;
background: transparent url("http://ilexsquare.com/finalhot/wp-content/uploads/2015/12/hotlogo-2.png") repeat scroll 0% 0%;
}
</style>
<input type="checkbox" />
</div>
please Help
Julian Be is correct you can't apply background images directly to checkboxes.
The only way to achieve this is to spoof the result. You could do this by having a <label> wrapped around the checkbox with it's overflow set to hidden and then move the checkbox itself into the overflow. This way ensures the checkbox is still clickable but not visible to the user. You can then add a <span> after the checkbox and apply the background image to this element depending on the state of the checkbox using the :checked selector and + selector.
Something like this
/* Set overflow of label to hidden so the checkbox can disappear */
.label-with-hidden-checkbox {
overflow: hidden;
position: relative;
}
/* Move the checkbox out of view */
.label-with-hidden-checkbox .hidden-checkbox {
position: absolute;
left: -9999px;
}
/* Set the background image of the spoof checkbox to the unchecked state */
.label-with-hidden-checkbox .hidden-checkbox + .spoof-checkbox{
display: inline-block;
margin-right: 10px;
background: transparent url("unchecked-state-image.png") repeat scroll 0% 0%;
}
/* When the checkbox is checked set the background image of the spoof checkbox to the checked state */
.label-with-hidden-checkbox .hidden-checkbox:checked + .spoof-checkbox{
display: inline-block;
width: 20px;
height: 20px;
margin-right: 10px;
background: transparent url("checked-state-image.png") repeat scroll 0% 0%;
}
</style>
<label class="label-with-hidden-checkbox">
<input class="hidden-checkbox" type="checkbox" /><span class="spoof-checkbox"></span> This is a checkbox
</label>
</div>
Hope that helps.
You can Use label to apply the image here is the jsfiddle
jsfiddle
this is your HTML code:
<input type="checkbox" />
<label></label>
and CSS:
input[type="checkbox"], input[type="radio"] {
box-sizing: border-box;
padding: 0px;
opacity: 0;
cursor: pointer;
}
label{
height:100px;
width:100px;
}
label:before{
content:"";
width: 100px;
height: 60px;
display: block;
z-index: 1;
border-radius: 2.5px;
background: url("http://ilexsquare.com/finalhot/wp-content/uploads/2015/12/hotlogo-2.png") repeat scroll 0% 0%;
}
You cannot add images to checkboxes.
Related
I'm looking for a CSS solution that adapts to div contents, with the functionality of clip-path but dynamic. This is my code:
.background {
background: yellow;
text-align: center;
}
.text {
display: inline-block;
margin: 20px;
padding: 20px;
background: teal;
}
<div class="background">
<div class="text">
My text is in here
</div>
</div>
Yellow and teal are just used for illustration. I want to replace the yellow background with an image, but only show it in the teal area. The div.background spans the width of the browser, but I cannot make assumptions about the width of div.text. Can this be done with only CSS or does it require JS and dynamically setting background-position?
Use a pseudo element that you make relative to the background element
.background {
background: yellow;
text-align: center;
position: relative;
z-index: 0;
}
.text {
display: inline-block;
color: #fff;
margin: 20px;
padding: 20px;
clip-path: inset(0); /* clip to only text element */
}
.text:before {
content: "";
position: absolute;
z-index: -1;
inset: 0;
background: url(https://picsum.photos/id/1056/800/600) center/cover;
}
/* to illustrate */
.text:hover {
clip-path: none;
}
<div class="background">
<div class="text">
My text is in here
</div>
</div>
Here is one way of doing what you want through JS. The image is in the background element, and it is clipped according to the dimensions of the child element. There's a resize observer applied to the child element to trigger the calculation of the clipping mask whenever the dimensions of the child change.
I've added an animation to show how the clipping is calculated in real-time, but as you can see there is some slight stutter.
let text = document.querySelector('.text');
let bg = document.querySelector('.background');
let observer = new ResizeObserver(() => {
calculateClipPath(bg, text);
})
observer.observe(text);
function calculateClipPath (parent, child) {
parent.style.clipPath = `inset(
${child.offsetTop}px
${parent.clientWidth - (child.offsetLeft + child.clientWidth)}px
${parent.clientHeight - (child.offsetTop + child.clientHeight)}px
${child.offsetLeft}px
)`;
}
.background {
background: url(https://c4.wallpaperflare.com/wallpaper/368/148/1024/flowers-roses-drawing-light-wallpaper-preview.jpg);
text-align: center;
position: relative;
}
.text {
display: inline-block;
margin: 20px;
padding: 40px;
width: 200px;
animation: 3s infinite change;
}
#keyframes change {
0% {
width: 200px;
}
50% {
width: 150px;
}
100% {
width: 200px;
}
}
<div class="background">
<div class="text">
My text is in here
</div>
</div>
I'm still experimenting to see if there is a purely CSS version of the solution because that would always be smoother than the JS solution. If I can figure it out, I'll edit this answer and add it here
I'm guessing these two attributes don't actually work together, but my situation:
I'm trying to create a tooltip component. My tooltip is positioned absolutely, and as I don't know what the length of the content would be, has no width. So with width-related css, text just forms a tall, skinny column. I tried max-width, but on it's own, that does nothing. So I decided to try white-space: nowrap, and while it nicely doesn't wrap text, it also doesn't seem to honor max-width in a useful way, with text instead going out of the boundaries of the element.
I can't think of how to solve my problem, if there is a clean solution. I'd like to have an absolutely positioned div that expands to fit it's content until a maximum, at which point it wraps. One suggestion I saw was making the element a flexbox, but from what I can tell, that's not great with IE, so I don't think is viable in my situation. Any advice?
.wrapper {
position: relative;
display: inline;
}
.info {
position: absolute;
bottom: 1.2em;
left: 0;
}
<div class="wrapper">
<span>[ ? ]</span>
<div class="info">Any long text can go in here to test what goes wrong with wrapping.</div>
</div>
Avoid using white-space:nowrap as that will constrain your text to one line. max-width should work with a block level element with display absolute but not inside an inline element. To resolve this, I place the tooltip outside of your wrapper block and use javascript to position it at the mouse location.
Here is a simple solution for your issue. Click on "open tooltip" to display the tooltip and move the slider to change the value of max-width.
showContext = function() {
var e = window.event;
var posX = e.clientX;
var posY = e.clientY;
var info = document.getElementById("info");
info.style.top = posY + "px";
info.style.left = posX + "px";
info.style.display = "block";
}
changeWidth = function(value) {
var info = document.getElementById("info");
info.style.maxWidth = value + "px";
}
.wrapper {
position: relative;
}
.info {
position: absolute;
max-width:300px;
display:none;
border:1px solid black;
background-color:white;
}
.range {
margin:10px 0px 10px 0px;
display:block;
}
<div class="wrapper">
max-width slider
<input id="range" class="range" type="range" min="100" max="600" oninput="changeWidth(this.value)"/>
<input type="button" value="open tooltip" onclick="javascript:showContext();" />
</div>
<div id="info" class="info">Any long text can go in here to test what goes wrong with wrapping.</div>
I'm not exactly sure what your goal is as there are a lot of contradictory things going on. But I'll try and hopefully you can guide me towards your desired solution:
https://jsfiddle.net/q7dyf6xh/
.wrapper {
position: relative;
display: run-in;
}
.info {
position: absolute;
max-width: 200px;
white-space: pre-line;
}
Have a look at this fiddle, as you can see the tooltip now has a max-width. Have a look at what I'm using:
display: run-in;: Displays an element as either block or inline, depending on context
white-space: pre-line;: Sequences of whitespace will collapse into a single whitespace. Text will wrap when necessary, and on line breaks
For a better understanding of how things work look here:
white-space: If you use nowrap text will never wrap to the next line. The text continues on the same line until a tag is encountered!
This said your max-width is still working but with nowrap you overflow your element now. Try and give your element a background-color and you'll see that it actually is only as wide as your max-width defines.
See here how it overflows the element: https://jsfiddle.net/fcnh1qeo/
And now width overflow: hidden only the text inside your box will be displayed. Everything else is cut off! See here: https://jsfiddle.net/0qn4wxkg/
What I used now is display: run-in; and white-space: pre-line; as well as max-width: 200px which will give you hopefully your desired solution. Not knowing the context and code you using it is more of a guess than it is a answer. But maybe I can guide you towards a solution which fits your needs
Cheers!
Add a min-width:100% and a white-space: nowrap;
.wrapper {
position: relative;
display: inline;
}
.info {
position: absolute;
min-width: 100%;
white-space: nowrap;
}
<div class="wrapper">
<span>[ ? ]</span>
<div class="info">Any long text can go in here to test what goes wrong with wrapping.</div>
</div>
display="runin"The element generates a run-in box. Run-in elements act like inlines or blocks, depending on the surrounding elements. That is:
If the run-in box contains a block box, same as block.
If a block box follows the run-in box, the run-in box becomes the first inline box of the block box.
If an inline box follows, the run-in box becomes a block box.
pre-line Sequences of whitespace are collapsed. Lines are broken at newline characters, at <br>, and as necessary to fill line boxes.
The following table summarizes the behavior of the various white-space values:
The max-width CSS property sets the maximum width of an element. It prevents the used value of the width property from becoming larger than the value specified by max-width.
.wrapper {
position: relative;
display: run-in;
top: 100px;
}
.info {
position: absolute;
bottom: 1.2em;
left: 0;
max-width: 200px;
white-space: pre-line;
background-color: #ddd;
}
Not that ling ago i had a very similar problem myself. I fixed it using flexbox what is already suggested in the comments here.
My code looks like this:
.has-tooltip {
display: inline-flex; align-items: flex-start
}
.has-tooltip > .tooltip {
padding: 1em;
max-width: 300px;
background: #bdc3c7;
word-wrap: break-word;
transform: translate(-50%,-110%)
}
I also copied this into this fiddle just in case you want to have a look at it. (:
You are correct that this does not work.
Here's a solution that works if you are allowed to use BR tags. I have worked on tooltips many times and this is the best solution that I have.
Codepen:
https://codepen.io/btn-ninja/pen/JNJrgp
It works by using white-space nowrap with a css translate:
<button type="button" class="btn btn-block hasTooltip">
Tooltip on top
<i class="tip">Most tooltips are short<br>but you can add line breaks.</i>
</button>
<button type="button" class="btn btn-block hasTooltip right">
Tooltip on the right.
<i class="tip">Tooltip on right<br>vertically centered.</i>
</button>
.hasTooltip .tip {
position: absolute;
bottom: 100%; left: 50%;
transform: translateX(-50%); }
.hasTooltip.right .tip {
bottom: auto; left: 100%; top:50%;
transform: translateY(-50%); }
The translate allows the absolutely-positioned tooltip to horizontally or vertically center itself vs the content. White space with a BR achieves wrapping for long content while allowing shorter tooltips to match width of the tooltip text.
Here's the full css:
.hasTooltip {
position:relative; }
.hasTooltip .tip {
display:block;
background: #333; color: #fff;
box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.5);
font-size:inherit;
font-style:normal;
line-height: 1rem;
text-align:center;
padding: 8px 16px;
border-radius:4px;
margin-bottom:5px;
pointer-events: none;
position: absolute;
bottom: 100%; left: 50%; transform: translateX(-50%);
opacity: 0;
transition: opacity .34s ease-in-out;
white-space:nowrap;
z-index:99; }
.hasTooltip .tip:before {
content: "";
display:block; position:absolute; left:0; bottom:-5px;
width:100%; height:5px; }
.hasTooltip .tip:after {
border-left: solid transparent 6px;
border-right: solid transparent 6px;
border-top: solid #333 6px;
bottom: -4px;
content: "";
height: 0;
left: 50%;
margin-left: -6px;
position: absolute;
width: 0; }
.hasTooltip:focus .tip,
.hasTooltip:hover .tip {
opacity: 1;
pointer-events: auto; }
.hasTooltip.right .tip {
bottom: auto; left: 100%; top:50%; transform: translateY(-50%);
margin-bottom:0;
margin-left:5px; }
.hasTooltip.right .tip:before {
left:-5px; bottom:auto; }
.hasTooltip.right .tip:after {
border-left: 0;
border-top: solid transparent 6px;
border-bottom: solid transparent 6px;
border-right: solid #333 6px;
bottom:auto;
left: -4px;
top:50%;
margin-left: 0;
margin-top: -6px; }
How can I make that the hover option allows me to see the image with opacity and the p tag inside not affected by opacity style? Please note that the img is inside an input tag and when I click it a bootsrap modal will appear with input form
.img_wrap:hover
#updateImg{ opacity: 0.7; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="img_wrap">
<input type='image' id='updateImg' src='http://placehold.it/350x150'>
</input>
<span></span><p class="imgText">Click here to change your image</p>
</div>
You need to handle each children's opacity separately. Also, if you want them overlapped, you need one of them (the text in my example, positioned absolute and the parent relative).
I'm guessing the following example does what you want to achieve? If not, please state your request clearer.
.img_wrap {
position: relative;
display: inline-block;
}
.img_wrap input[type="image"]{
opacity: 1;
display: block;
transition: opacity .3s cubic-bezier(.4,0,.2,1)
}
.img_wrap:hover input[type="image"] {
opacity: .3;
}
.img_wrap .imgText {
position: absolute;
height: 100%;
width: 100%;
left: 0;
top: 0;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
opacity: 0;
transition: opacity .3s cubic-bezier(.4,0,.2,1)
}
.img_wrap:hover .imgText {
opacity: 1;
pointer-events: none;
}
<div class="img_wrap">
<input type='image' id='updateImg' src='http://placehold.it/350x150' />
<div class="imgText">Click here to change your image</div>
</div>
Notes:
<input> is a self-closing tag, so <input></input> is invalid.
<input type="image"> is supposed to be used instead of a (submit) button. It's a shortcut for a button with a background image, that resizes itself to the image ratio. If you want to submit an image to the server using an <input>, you probably want to use <input type="file" />
here is what you want
.img_wrap{
display: inline-block;
position: relative;
}
.img_wrap:hover #updateImg{
opacity: 0.7;
position: relative;
}
.img_wrap:hover .imgText{
position:absolute;
top:0;
bottom: 0;
left: 0;
right:0;
}
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;
}
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/