Truly hidden clear icon when using -webkit-search-cancel-button - html

Implemented a clear button using -webkit-search-cancel-button following the next post:
https://stackoverflow.com/a/64267916/1065145
The problem with the implementation referenced above is that it renders the clear icon invisible when out of focus, but it still occupies space and it doesn't look nice at all:
It can be either a long placeholder on a small device or a long user query:
Trying to solve it with display: none doesn't really work, because it still renders invisible when the input is in focus, but there is no user input yet and only the placeholder is shown (and part of it is eaten by the invisible icon).
Q. Is there a way to make icon truly invisible so it is shown only on two conditions:
The input is in focus.
User has provided some input and the placeholder is gone.

display: none; and display: block; worked for me (the newest Chrome).
It it necessary to add dispay block when in focus (input[type="search"]:focus::-webkit-search-cancel-button).
If it still not working, try with !important. Sometimes browser style is harder to override. (I'm not recommending using !important, but sometimes there is no other way).
if you want show clear button even with no tekst provided by user add:
input[type="search"]::-webkit-search-cancel-button {
opacity: 1 !important;
}
input[type="search"] {
border: 1px solid gray;
padding: .2em .4em;
border-radius: .2em;
}
input[type="search"].dark {
background: #222;
color: #fff;
}
input[type="search"].light {
background: #fff;
color: #222;
}
input[type="search"]::-webkit-search-cancel-button {
-webkit-appearance: none;
height: 1em;
width: 1em;
margin-left: .5em;
border-radius: 50em;
background: url(https://pro.fontawesome.com/releases/v5.10.0/svgs/solid/times-circle.svg) no-repeat 50% 50%;
background-size: contain;
display: none;
pointer-events: none;
}
input[type="search"]:focus::-webkit-search-cancel-button {
display: block;
pointer-events: all;
}
input[type="search"].dark::-webkit-search-cancel-button {
filter: invert(1);
}
<input type="search" placeholder="search" class="light">
<input type="search" placeholder="search" class="dark">
Edited:
I manage to do it, like you wanted.
I used: https://developer.mozilla.org/en-US/docs/Web/CSS/:placeholder-shown
I also left outline borders for better understanding.
input[type="search"] {
border: 1px solid gray;
padding: .2em .4em;
border-radius: .2em;
}
input[type="search"].dark {
background: #222;
color: #fff;
}
input[type="search"].light {
background: #fff;
color: #222;
}
input[type="search"]::-webkit-textfield-decoration-container {
width: 100%;
}
/* normal state, with placeholder and no value */
input[type="search"]:placeholder-shown::-webkit-textfield-decoration-container {
outline: 1px dotted yellow;
}
/* focused state, with placeholder and no value */
input[type="search"]:placeholder-shown:focus::-webkit-textfield-decoration-container {
outline: 1px dotted red;
}
/* focused state, with value */
input[type="search"]:not(:placeholder-shown):focus::-webkit-textfield-decoration-container {
outline: 1px dotted green;
}
input[type="search"]::-webkit-search-cancel-button {
-webkit-appearance: none;
height: 1em;
width: 0;
border-radius: 50em;
background: url(https://pro.fontawesome.com/releases/v5.10.0/svgs/solid/times-circle.svg) no-repeat 50% 50%;
background-size: contain;
pointer-events: none;
}
input[type="search"]:not(:placeholder-shown):focus::-webkit-search-cancel-button {
display: block;
width: 1em;
margin-left: .5em;
}
input[type="search"]:focus::-webkit-search-cancel-button {
pointer-events: all;
}
input[type="search"].dark::-webkit-search-cancel-button {
filter: invert(1);
}
<input type="search" placeholder="search search search search search search" class="light">
<input type="search" placeholder="search search search search search search" class="dark">

I have checked you're issue and from my point of view the easiest way to solve the issue is the following:
input[type="search"]::-webkit-search-cancel-button {
display: none;
}
input[type="search"]:focus::-webkit-search-cancel-button {
display: block;
}
You should add those 2 lines to you're existing code and should be fine
:)
Code is here : https://codepen.io/BaciuTudor/pen/YzxaYKV

Related

Is it possible to style input type=range with just a left and right color and no thumb?

An <input type="range"> looks like this on Chrome(103)/MacOS
And this in Firefox(101)
Is there a CSS only way to get it to look like this?
It seems so close, just
Hide the thumb (easy)
Set the border-radius to none (easy)
Set the border to none (easy)
Set a size (easy)
Set the color left of the track different than the right (???)
But I've had no luck
input[type="range"] {
-webkit-appearance: none;
width: 200px;
height: 20px;
border-radius: none;
background-color: blue;
}
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
cursor: ew-resize;
}
<input type="range">
In particular step 5. Is there a way to set left and right colors of the track? Ideally cross browser?
I was trying to think if there was away to use calc and a ::before pseudo element or some recently introduced CSS features like css container queries but nothing came to mind.
PS: I know it's relatively easy to do this in JavaScript by coding my own slider using divs or other elements but I'd love to do it CSS only.
Here is a working example.
function sliderValueChange(e) {
console.log(e.value);
}
:root {
--slider-width: 300px;
--slider-height: 20px;
}
input[type='range'] {
cursor: ew-resize;
overflow: hidden;
}
#media screen and (-webkit-min-device-pixel-ratio:0) {
input[type='range'] {
width: var(--slider-width);
-webkit-appearance: none;
background-color: #353535;
}
input[type='range']::-webkit-slider-runnable-track {
height: var(--slider-height);
-webkit-appearance: none;
color: #13bba4;
margin-top: -1px;
}
input[type='range']::-webkit-slider-thumb {
width: 0px;
-webkit-appearance: none;
height: var(--slider-height);
box-shadow: calc(-1 * var(--slider-width)) 0 0 var(--slider-width) #43e5f7;
}
}
/* FF */
input[type="range"]::-moz-range-progress {
background-color: #43e5f7;
}
input[type="range"]::-moz-range-thumb {
height: var(--slider-height);
width: 0;
border: none;
box-shadow: calc(-1 * var(--slider-width)) 0 0 var(--slider-width) #43e5f7;
box-sizing: border-box;
}
/* IE */
input[type="range"]::-ms-fill-lower {
background-color: #43e5f7;
}
input[type="range"]::-ms-fill-upper {
background-color: #13bba4;
}
<input type="range" onchange="sliderValueChange(this)">
Here you go a CodePen by Noah Blon. A cross-browser range input slider. A box-shadow on the pseudo-thumb element creates a solid color fill effect¹. The ::-webkit-slider-thumb is width: 0 and border: 0 makes it invisible yet it's functionality as a handle remains because it's height: 40px.
¹Also see this article
body {
height: 100vh;
margin: 0;
display: flex;
}
input[type="range"] {
margin: auto;
-webkit-appearance: none;
position: relative;
overflow: hidden;
height: 40px;
width: 200px;
cursor: pointer;
border-radius: 0;
}
::-webkit-slider-runnable-track {
background: #ddd;
}
/*
* 1. Set to 0 width and remove border for a slider without a thumb
* 2. Shadow is negative the full width of the input and has a spread
* of the width of the input.
*/
::-webkit-slider-thumb {
-webkit-appearance: none;
width: 0px;
/* 1 */
height: 40px;
background: #fff;
box-shadow: -200px 0 0 200px dodgerblue;
/* 2 */
border: 0;
/* 1 */
}
::-moz-range-track {
height: 40px;
background: #ddd;
}
::-moz-range-thumb {
background: #fff;
height: 40px;
width: 20px;
border: 3px solid #999;
border-radius: 0 !important;
box-shadow: -200px 0 0 200px dodgerblue;
box-sizing: border-box;
}
<input type='range'>

Custom checkbox with CSS ::before - not working in Firefox/Edge

I have come up against a very annoying CSS issue while trying to get a project working cross-browser (not bothered about IE, it's only a hobby project, but it would be nice to get it working on all modern browsers at the very least). It relates to some checkboxes which I wish to apply custom styles to - I know you can't do very much with the standard HTML <input type="checkbox"> so I have done what is recommended in many places, and used a ::before pseudo-element. And I was pleased with the result in Chrome. Imagine my surprise when I find that my custom checkbox simply doesn't display at all in Firefox!
I've been playing with this for a few hours and have stripped it right back to the very root of the problem - and it's something to do with the checkbox itself, rather than any other CSS it's interacting with. Here's the bare minimum example:
input[type="checkbox"] {
visibility: hidden;
}
input[type="checkbox"]::before {
visibility: visible;
content: "";
display: block;
width: 1.1em;
height: 1.1em;
color: #eddc23;
border: 1px solid #eddc23;
background-color: #540123;
border-radius: 35%;
line-height: 1.27;
text-align: center;
cursor: pointer;
}
input[type="checkbox"]:checked::before {
content: "\2713";
}
<input type="checkbox">
This should show a dark red checkbox which has a yellow tick when selected. It works perfectly on Chrome and Opera, but not at all on Firefox or Edge. (Here's a CodePen link of the same in case the Stack Overflow snippet somehow exhibits different behaviour). CSS isn't one of my strong points and despite a few hours of experimenting and googling, I'm baffled.
Would appreciate any pointers, not only as to how to get this working cross-browser, but as to why it's not working on FF/Edge (inspecting the element on Firefox shows no sign of a ::before pseudo-element at all. I've also ruled out it being to do with the empty content property, since changing that to real text fails to make it visible in the browsers concerned).
Sometimes with labels you can solve this type of problems
input[type="checkbox"] {
display: none;
}
span {
visibility: visible;
content: "";
display: block;
width: 1.1em;
height: 1.1em;
color: #eddc23;
border: 1px solid #eddc23;
background-color: #540123;
border-radius: 35%;
line-height: 1.27;
text-align: center;
cursor: pointer;
}
input[type="checkbox"]:checked + label span::before {
content: "\2713";
}
<input type="checkbox" id="checkbox">
<label for="checkbox">
<span></span>
</label>
Just to record it in brief, what I ended up doing was putting a <div> as the next sibling of the checkbox, hiding the checkbox with opacity: 0;, and positioning the div on top of the checkbox but with lower z-index. This means that the "fake" checkbox responds in the same way a real one would, and by keeping the actual checkbox in the DOM hopefully this would still score reasonably on accessibility.
You should reset the appearance:
input[type=checkbox] {
/* Reset */
-webkit-appearance: none;
appearance: none;
background-color: #fff;
}
But the best way is here: https://moderncss.dev/pure-css-custom-checkbox-style/
Example snippet:
/* Checkboxes */
input[type=checkbox] {
/* Reset */
-webkit-appearance: none;
appearance: none;
background-color: #fff;
width: 0;
height: 0;
margin: 0;
margin-right: 25px;
position: relative;
text-align: center;
font-size: 16px;
}
input[type=checkbox]:focus {
box-shadow: none;
outline: none;
}
input[type=checkbox]::before {
content: '';
position: absolute;
top: -16px;
display: block;
width: 20px;
height: 20px;
border: 1px solid #23151d;
background: #fff;
box-sizing: content-box;
border-radius: 3px;
}
input[type=checkbox]:checked::before {
border: 1px solid #e63244;
background: transparent;
background: #e6324403;
}
input[type=checkbox]::after {
content: '✓';
text-indent: 4px;
position: absolute;
top: -14px;
display: none;
width: 20px;
height: 20px;
box-sizing: content-box;
text-align: center;
}
input[type=checkbox]:checked::after {
display: block;
color: #e63244;
}
/* / Checkboxes */
<p>
<input type="checkbox" checked>
<input type="checkbox" checked>
<input type="checkbox" checked>
</p>

How do I make tooltips work on Webapp when running on tablet

I have tooltips for all my options for both the input field itself and the associated label, but unfortunately they are not displayed on a mobile or a PC in tablet mode.
So how can i make them work, or what is the usual alternative.
I understand on a touch screen you dont have the concept of a mouse pointer hovering, but I would have though at the very least if I touched the label with my finger that would cause the tooltip to be displayed, how do I make that happen.
<input name="preview" id="preview" type="checkbox" title="Preview only, a license is required to actually make changes" disabled="disabled">
<label for="preview" id="previewlabel" class="label_disabled" title="Preview only, a license is required to actually make changes">
Preview only</label>
I think the best solution would to implement a hover tooltip which
would display when held done, how I would do that ?
$( document ).ready(function() {
$('.show-tooltip').on('mouseover', function() {
$(this).next().css('display','inline-block');
});
$('.show-tooltip').on('mouseout', function() {
$(this).next().css('display','none');
});
});
.triangle-isosceles.left {
margin-left: 50px;
background: #dedede;
}
.triangle-isosceles {
position: relative;
padding: 15px;
margin: 1em 0 3em;
color: #000;
background: #dedede;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
}
.triangle-isosceles.left:after {
top: 16px;
left: -50px;
bottom: auto;
border-width: 10px 50px 10px 0;
border-color: transparent #dedede;
}
.triangle-isosceles:after {
content: "";
position: absolute;
bottom: -15px;
left: 50px;
border-width: 15px 15px 0;
border-style: solid;
border-color: #dedede transparent;
display: block;
width: 0;
}
.tooltip {
width: 60%;
display: none;
}
.show-tooltip {
padding: 35px 5px;
display: inline-block;
}
.group {
padding: 15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="group">
<input name="preview" id="preview" type="checkbox" disabled="disabled">
<label for="preview" id="previewlabel" class="label_disabled show-tooltip">Preview only</label>
<div class="tooltip triangle-isosceles left">Preview only, a license is required to actually make changes</div>
</div>
Speech bubble style tooltip adapted from Nicholas Gallagher
Please view the demo at full screen otherwise it looks wrong. (You will need to adjust it to fit a mobile device)
You can change positioning and use with your code.
* {
transition: 0.5s;
}
body {
margin: 0;
width: 100vw;
height: 100vh;
font-family: sans-serif;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
overflow: hidden;
}
p, h1 {
margin: 0;
}
.wrapper {
position: relative;
}
.tooltip {
transform: translate(-50%, -50%);
left: 50%;
top: 50%;
width: 1000px;
max-width: 50vw;
opacity: 0;
background: #333;
padding: 5px;
color: #eee;
border-radius: 5px;
position: absolute;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
pointer-events: none;
top: 150%;
}
.arrow {
width: 0;
height: 0;
border: 15px solid transparent;
border-bottom-color: #333;
position: absolute;
top: -30px;
align-self: center;
}
.wrapper:hover .tooltip {
opacity: 1;
}
<div class="wrapper">
<h1 class="text">Hover on Pc</h1>
<p>(Click Here Mobile)</p>
<div class="tooltip">
<div class="arrow"></div>
<p class="tooltip_text">This is the tooltip</p>
</div>
</div>
The media query in my CSS is designed to make it work only on the smaller screens found on mobile devices.
It then takes the content of the title attribute and displays it after the label. (You can add any formatting you like to this - padding, margin, rounded corners etc.)
[click on 'Run code snippet' then 'full page' and then resize the browser window to less than 768px wide to see this working properly.]
#media screen and (max-width: 768px) {
label:after {
content: attr(title);
background-color: #333;
color: #fff;
display: block;
margin-top: 2px;
}
}
<input name="preview" id="preview" type="checkbox" title="Preview only, a license is required to actually make changes" disabled="disabled">
<label for="preview" id="previewlabel" class="label_disabled" title="Preview only, a license is required to actually make changes">
Preview only</label>
the only difference on hover in touch is that You have to hold down on the thing for any trigger related to it , js or css to be activated ... so it may be poor coding , speaking of which
You do realize that you haven't provided the "Tooltip"'s code itself right ?
it may be a positioning problem as the tooltip may overlap the screen when appended due to the code ...
to test it out : provide a background or color change when hovered , now go on touch and see if the hover's working properly , if it did u know the hover's working ... but the tooltip isn't , so provide the code . If it didnt however then again .... provide the code !
Try this:
label:hover:after {
content: "Preview only, a license is required to actually make changes";
background-color: #333;
color: #fff;
display: inline;
padding: 3px;
border-radius: 3px;
margin-left: 6px;
}
label {
padding: 3px;
border-radius: 3px;
display: inline-block;
}
<input name="preview" id="preview" type="checkbox" disabled="disabled">
<label for="preview" id="previewlabel" class="label_disabled">
Preview only</label>

carret in div contenteditable

In first time when I'm clicking in div my carret (cursor) go up, but if I writting text into div text is printed correctly (in the middle of div). How can I fix problem with placeholder in this div? I need the cursor to initially appear in the middle (like text when you are printing).
Demo http://jsfiddle.net/0gr09835/2/, and the related snippet:
[contenteditable=true]:empty:before {
content: attr(placeholder);
display: block; /* For Firefox */
}
[contenteditable=true]:empty:focus:before {
content: "";
}
My screenshot:
It works correctly in Chrome, but not in Firefox.
This seems like a fairly old bug in Firefox (https://bugzilla.mozilla.org/show_bug.cgi?id=904846).
A possible workaround would be to replace content: '' with display: none in [contenteditable=true]:empty:focus:before.
div.msg {
font-size: 16px;
float: left;
width: calc(100% - 80px);
height: 64px;
line-height: 64px;
padding: 0px 10px;
color: #1a1a1a;
border: 0;
background-color: transparent;
outline: none;
font-weight: 400;
border: 1px solid #000;
margin-top: 25px;
}
[contenteditable=true]:empty:before{
content: attr(placeholder);
display: block; /* For Firefox */
}
[contenteditable=true]:empty:focus:before {
display: none;
}
<div class="msg" name="msg" placeholder="Write message..." contenteditable="true"></div>
A similar problem was also mentioned in Firefox sets wrong caret position contentEditable on focus.

how to remove outline of html button

I have button on my html page, when i click on that button it show me outer line to button shown as below image.
here when i click on reset button it show me outer as above image.
Html code:
< input type="reset" value="" class="resetButton" />
css code:
.resetButton
{
margin: 0px;
background:url(../images/button/Reset2.png) no-repeat;
border: none;
width: 90px;
height: 32px;
cursor: pointer;
}
Use this:
input[type="reset"]:focus{
outline: none;
}
or just
input:focus{
outline: none;
}
if you don't want that outline to all the input types.
Just add display: block;
.resetButton
{
margin: 0px;
background:url(../images/button/Reset2.png) no-repeat;
display: block;
border: none;
width: 90px;
height: 32px;
cursor: pointer;
}
This is normally a chrome issue, the main thing to note here is that it is an outline not a border.
Try
.resetButton{ outline: none; }
For more info check out http://www.w3.org/TR/CSS2/ui.html#dynamic-outlines
Also check out this post on the dangers of removing the border completely
Google Chrome > Textboxes > Yellow border when active..?
Take a look on the outline CSS property.
To style a button that is being clicked, you can use :active pseudo-class.
Add an outline: none to your css:
.resetButton
{
margin: 0px;
background:url(../images/button/Reset2.png) no-repeat;
border: none;
width: 90px;
height: 32px;
cursor: pointer;
outline: none;
}
Try this.
.resetButton, .resetButton:visited
{
margin: 0px;
background:url(../images/button/Reset2.png) no-repeat;
display: block;
width: 90px;
height: 32px;
cursor: pointer;
border: none;
outline: none;
}
Just add this to your CSS:
.resetButton:focus {
outline: none;
}
Could also be box-shadow. Try something like:
.resetButton{ box-shadow: none; }
or
resetButton:focus{ box-shadow: none; }