Adding Unicode beside divs - html

I have this CSS code below of some data displayed in a form of a pill what i'm trying to accomplish is trying to add a Unicode plus sign inside of my pill something like this image below.
But whenever i try to increase the font-size of the Unicode it just messes up the whole text and shape of the pills is there any way to make it look like the image on-top without messing too much of the original css? Any help would be appreciated thanks.
var red = {};
var key = "Red Fruits";
red[key] = ['Apple', 'Cherry', 'Strawberry', 'Pomegranate', 'Rassberry'];
var redString = '';
$.each(red[key], function(index) {
redString += ('<div class="pilldiv redpill class">' + red[key][index] + '</div>');
});
$('.redclass').html(redString);
.pilldiv {
padding: 0px 19px;
text-align: center;
font-size: 14px;
border-radius: 25px;
color: Black;
margin: 1px;
}
.redpill {
background-color: Pink;
cursor: default;
}
.redpill:before {
content: "\002B";
font-size: 28px;
width: 15px;
vertical-align: baseline;
margin-left: -7px;
}
.class {
font-family: Open Sans;
}
.center {
display: flex;
justify-content: center;
}
.wrappingflexbox {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
h3 {
font-weight: normal;
color: White;
}
.panel {
display: table;
table-layout: fixed;
height: 100%;
width: 30%;
background-color: black;
}
body {
background-color: white;
}
<div class="center">
<div class="panel">
<div id="redid" class="redclass wrappingflexbox"></div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

When you change the actual size of the pseudo-elememt font it will change the shape of the pill accordingly since the pill's size is determined by the size of the contents.
Thus I would suggest that you change the apparent size of the glyph by using a transform instead.
Since this is entirely visual it does not affect the size of the pill.
.redpill:before {
content: "\002B";
/* font-size: 28px;*/ */ remove this */
width: 15px;
vertical-align: baseline;
margin: 0 7px 0 -7px;
display:inline-block; /* required */
transform:scale(2); /* this*/
}
var red = {};
var key = "Red Fruits";
red[key] = ['Apple', 'Cherry', 'Strawberry', 'Pomegranate', 'Rassberry'];
var redString = '';
$.each(red[key], function(index) {
redString += ('<div class="pilldiv redpill class">' + red[key][index] + '</div>');
});
$('.redclass').html(redString);
.pilldiv {
padding: 0px 19px;
text-align: center;
font-size: 14px;
border-radius: 25px;
color: Black;
margin: 1px;
}
.redpill {
background-color: Pink;
cursor: default;
}
.redpill:before {
content: "\002B";
/* font-size: 28px;*/
*/ remove this */ width: 15px;
vertical-align: baseline;
margin: 0 7px 0 -7px;
display: inline-block;
transform: scale(2)
}
.class {
font-family: Open Sans;
}
.center {
display: flex;
justify-content: center;
}
.wrappingflexbox {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
h3 {
font-weight: normal;
color: White;
}
.panel {
display: table;
table-layout: fixed;
height: 100%;
width: 30%;
background-color: black;
}
body {
background-color: white;
}
<div class="center">
<div class="panel">
<div id="redid" class="redclass wrappingflexbox"></div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Make sure :before inherits class font-size:
.redpill:before {
content: "\002B";
padding-right: 2px;
}
.pilldiv {
padding: 5px 22px;
text-align: center;
font-size: 14px;
border-radius: 25px;
color: Black;
margin: 1px;
}

Related

Breaking a word inside a circle span and still keep the circle

I have this span over a product image
<span class="wb_badge wb_badge-rounded wb_badge-bottomright" style="background-color:#dd9933;color:#ffffff;">TEST2TEST 3 HAHAHAHAHAHA</span>
Which has the following CSS:
.wb_badge {
position: absolute;
display: inline-flex;
justify-content: center;
align-items: center;
padding: 12px;
min-width: 15px;
min-height: 15px;
font-size: 14px;
font-weight: bold;
}
.wb_badge-rounded {
border-radius: 50%;
}
.wb_badge-rounded::after {
content: "";
display: block;
padding-bottom: 100%;
}
But I would really like to make the text break at some point so I can avoid these large and almost empty circles just because of some text. I tried with word-break but it didn't worked as expected. How can I make the text break at some point and still keep the circle?
You can use word-break: break-all;. Add this to you wp_badge class. (https://developer.mozilla.org/en-US/docs/Web/CSS/word-break?retiredLocale=de).
.wb_badge {
position: absolute;
display: inline-flex;
justify-content: center;
align-items: center;
padding: 12px;
min-width: 15px;
min-height: 15px;
font-size: 14px;
font-weight: bold;
/* new! I added this lines below. */
width: 100px; /* new */
text-align: center; /* new */
word-break: break-all; /* new */
}
.wb_badge-rounded {
border-radius: 50%;
}
.wb_badge-rounded::after {
content: "";
display: block;
padding-bottom: 100%;
}
<span class="wb_badge wb_badge-rounded wb_badge-bottomright" style="background-color:#dd9933;color:#ffffff;">TEST2TEST 3 HAHAHAHAHAHA</span>
without editing css
.wb_badge {
position: absolute;
display: inline-flex;
justify-content: center;
align-items: center;
padding: 12px;
min-width: 15px;
min-height: 15px;
font-size: 14px;
font-weight: bold;
}
.wb_badge-rounded {
border-radius: 50%;
}
.wb_badge-rounded::after {
content: "";
display: block;
padding-bottom: 100%;
}
<span class="wb_badge wb_badge-rounded wb_badge-bottomright" style="background-color:#dd9933;color:#ffffff; word-break: break-all;text-align: center;">TEST2TEST 3 HAHAHAHAHAHA</span>

Replace an input file into image after selecting that image

i create a form where i upload image into database where i create icon input to select the image i want to convert that icon into that picture which i select after slecting the picture.
<label class="custom-file-upload">
<input asp-for="imge1" name="imge1" type="file" />
<i class="fa fa-camera"></i>
CSS
input[type="file"] {
display: none;
}
.custom-file-upload {
border: 1px solid #ccc;
display: inline-block;
padding: 6px 12px;
cursor: pointer;
height: 80px;
width: 100px;
display: flex;
align-items: center;
justify-content: center;
font-size: 30px;
color: var(--primary-color);
}
Using readAsDataURL you can get file path and set to img tag.
jQuery(document).ready(function() {
var readURL = function(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('.input-img').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("input[type='file']").on('change', function() {
readURL(this);
});
});
.custom-input-file {
position: relative;
cursor:pointer;
}
.custom-input-file .input-file-wrapper {
height: 150px;
width: 150px;
border-radius: 10px;
position: relative;
}
.custom-input-file .input-file-wrapper .input-img {
height: 100%;
-o-object-fit: cover;
object-fit: cover;
border: 1px solid #ced4da;
border-radius: 10px;
width: 100%;;
}
.custom-input-file .input-file-wrapper input[type=file] {
position: absolute;
left: 0;
width: 100%;
height: 100%;
padding: 0;
opacity: 0;
top: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div class="input-wrapper">
<div class="custom-input-file ">
<div class="input-file-wrapper ">
<img src="https://storage.googleapis.com/proudcity/mebanenc/uploads/2021/03/placeholder-image.png " class="input-img">
<input type="file" class="form-control ">
</div>
</div>
</div>
little bit change and add some javascrpt and jquery in it.
firslty add this
<div class="form">
<div class="grid">
<div class="form-element">
<input type="file" id="file-1" asp-for="imge1" name="imge1">
<label for="file-1" id="file-1-preview">
<div>
<span>+</span>
</div>
</label>
</div>
</div>
</div>
then add this javascript and jquery
<script>
function previewBeforeUpload(id){
document.querySelector("#"+id).addEventListener("change",function(e){
if(e.target.files.length == 0){
return;
}
let file = e.target.files[0];
let url = URL.createObjectURL(file);
document.querySelector("#"+id+"-preview div").innerText = file.name;
document.querySelector("#"+id+"-preview img").src = url;
});
}
previewBeforeUpload("file-1");
previewBeforeUpload("file-2");
previewBeforeUpload("file-3");
previewBeforeUpload("file-4");
previewBeforeUpload("file-5");
previewBeforeUpload("file-6");
previewBeforeUpload("file-7");
previewBeforeUpload("file-8");
previewBeforeUpload("file-9");
previewBeforeUpload("file-10");
</script>
Also add this CSS
* {
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
body {
font-family: "Raleway",sans-serif;
background: #f8f8f8;
}
.form {
margin: 20px 0px 20px;
padding: 0px 10px;
}
.form h2 {
text-align: center;
color: #acacac;
font-size: 12px;
font-weight: 100;
}
.form .grid {
margin-top: 5px;
display: flex;
justify-content: space-around;
flex-wrap: wrap;
gap: 20px;
}
.form .grid .form-element {
width: 80px;
height: 80px;
box-shadow: 0px 0px 20px 5px rgba(100,100,100,0.1);
}
.form .grid .form-element input {
display: none;
}
.form .grid .form-element img {
width: 80px;
height: 80px;
object-fit: cover;
}
.form .grid .form-element div {
position: relative;
height: 30px;
margin-top: -40px;
background: rgba(0,0,0,0.5);
text-align: center;
line-height: 40px;
font-size: 13px;
color: #f5f5f5;
font-weight: 600;
}
.form .grid .form-element div span {
font-size: 15px;
}

How to make the iframe scrollable to show all content?

I'm trying to finish off our rehoming page, where a visitor clicks on an animal to open an iframe with more info output from our database. But I can't get the iframe scrollable. I've searched online extensively but can't get any of the suggestions to work.
What am I doing wrong? (I'm way out of my league with code but just doing the best I can with what I find online, so I may have made some basic errors.) Any help at all is greatly appreciated.
HTML:
<!DOCTYPE html>
<html lang="en">
<title>iFrame</title>
<script>
asm3_adoptable_filters = "size agegroup sex breed species";
asm3_adoptable_extra = function(a) {
return '<span class="waitingtime">Time waiting for a home: ' + a.TIMEONSHELTER + '</span>';
}
asm3_adoptable_iframe = true;
asm3_adoptable_iframe_fixed = true;
asm3_adoptable_translations = {
"(any sex)": "Any sex",
"(any size)": "Any size",
"(any breed)": "Any breed",
"(any age)": "Any age",
"(any species)": "Any species"
}
asm3_adoptable_sort = "-#DAYSONSHELTER";
</script>
<style>
.waitingtime {
color: black;
font-family: filson soft regular,sans-serif;
</style>
<div id="asm3-adoptables"></div>
<script src="https://service.sheltermanager.com/asmservice?method=animal_view_adoptable_js&account=sm1510"
></script>
Page CSS:
#asm3-adoptable-iframe-overlay{
background-color: rgba(0 0 0 / 57%) !important;
}
#asm3-adoptable-iframe{
overflow-y: scroll !important;
width: 479px !important;
max-width: 100%;
margin: 0 auto;
display: block;
height: 90vh !important;
background-color:white;
margin-top:3em;
}
#asm3-adoptable-iframe-close{
background-color: #c92327;
color: white;
padding: 7px 23px;
/* margin-top: 39px !important; */
position: absolute;
border-radius: 5px;
text-align: center;
right: 0;
left: 0;
margin-top: 34px;
width: 200px;
margin: 0 auto;
top:44px;
}
Element CSS:
.asm3-adoptable-item {
display: inline-block;
text-align: center;
background-color: white;
margin-bottom: 12px;
padding-top: 25px;
width: 31.8%;
margin: 10px;
}
.asm3-adoptable-item:hover {
background-color: black;
}
.asm3-adoptable-item:hover .asm3-adoptable-tagline {
color: white;
}
.asm3-adoptable-name {
font-family: "proxima soft extrabold", sans-serif;
font-size: 44px;
color: #c92327;
margin-top: -16px;
display: block;
}
.asm3-adoptable-tagline {
text-transform: lowercase;
font-family: "proxima soft extrabold",sans-serif;
font-size: 18px;
color: black;
margin-top: -10px;
display: block;
}
.asm3-adoptable-item:hover .asm3-adoptable-name {
color: #c92327;
}
.asm3-adoptable-item:hover .waitingtime {
color: white;
}
.asm3-adoptable-thumbnail {
margin-top: 15px;
width: 425px;
max-width: 100%;
height: 425px;
}
.asm3-adoptable-reserved {
position: relative;
color: white;
display: inline-block;
padding: 5px;
overflow: hidden;
font-family: "proxima soft extrabold", sans-serif;
font-size: 18pt;
}
.asm3-adoptable-reserved span:before {
content: "RESERVED";
}
.asm3-adoptable-reserved span {
position: absolute;
display: inline-block;
right: -60px;
text-align: center;
top: 50px;
background: #c92327;
width: 275px;
transform: rotate(45deg);
}
#asm3-adoptable-filters {
overflow:hidden;
}
#asm3-adoptable-filters select {
width: 17%;
float: left;
margin: 20px;
}
#asm3-adoptable-filters {
font-family: "proxima soft extrabold", sans-serif;
font-size: 22px;
background-color: #dab348;
}
.asm3-adoptable-list {
display: flex;
flex-wrap: wrap;
}
#media only screen and (max-width: 992px) {
.asm3-adoptable-item {
width:50%;
}
}
#media only screen and (max-width: 768px) {
.asm3-adoptable-item {
width:100%;
}
}
You are missing a closing bracket "}" in your <style> tags. After playing around with your website a little I believe you are talking about some sort of full screen modal. But then it seems you are using a iframe in the modal. You might consider not using an iframe because there is really no need and it may solve your problems. Another thing I noticed is that you don't have any links between your html and your css which you might want to look into.

Elements outside of scroll area

I'm making a messaging app in Electron. I'm using backdrop-filter to blur the messages as they scroll past the title bar, so I need the messages to pass behind the title bar, rather than being contained inside their own div in the middle of the screen.
When the page is not scrolled, the messages behave as expected. However, at the bottom of the page, the messages are cut off by the bottom bar for entering messages.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Chat</title>
<link rel="stylesheet" href="css/chat.css" />
<script src="js/chatWindow.js"></script>
</head>
<body>
<div class="header">
<span class="name">Room Name</span>
</div>
<div class="messageBar">
<input id="messageBox" type="text" placeholder="Enter a message..." />
<div class="sendBtn">Send</div>
</div>
<div class="messageList"></div>
</body>
</html>
CSS
#font-face {
font-family: "Roboto Light";
font-style: "normal";
font-weight: 400;
src: url("fonts/Roboto-Light.ttf") format("truetype");
}
body {
background-color: white;
font-family: "Roboto Light";
padding: 45px 0px;
}
.header {
position: fixed;
top: 0px;
left: 0px;
width: 100%;
height: 45px;
z-index: 100;
background-color: rgba(56, 92, 254, 0.75);
display: flex;
text-align: center;
justify-content: center;
align-items: center;
backdrop-filter: blur(3px);
-webkit-backdrop-filter: blur(3px);
-webkit-app-region: drag;
}
.header span {
font-weight: bold;
}
.messageBar {
position: fixed;
top: calc(100% - 45px);
left: 0px;
width: 100%;
height: 45px;
z-index: 100;
background-color: rgba(57, 93, 255, 0.75);
padding: 0px;
display: flex;
text-align: center;
justify-content: center;
align-items: center;
vertical-align: middle;
backdrop-filter: blur(3px);
-webkit-backdrop-filter: blur(3px);
}
#messageBox {
width: 90%;
height: 100%;
background-color: rgba(255,255,255,0.5);
font-size: 1em;
margin: none;
border: none;
padding: 0px 5px 0px 5px;
}
#messageBox::-webkit-input-placeholder {
color: dimgray;
}
.sendBtn {
float: right;
width: 10%;
height: 100%;
padding: 0px 3px;
line-height: 45px;
transition: 0.2s;
}
.sendBtn:hover {
background-color: rgba(0,0,0,0.25);
}
.messageList {
width: 100%;
height: 100%;
padding: 0px;
margin: 0px;
}
.message {
display: block;
width: auto;
max-width: 50%;
border-radius: 10px;
padding: 4px;
margin: 5px 0px;
box-sizing: border-box;
background-color: lightblue;
overflow-wrap: break-word;
float: left;
clear: both;
}
.message.mine {
float: right;
background-color: lightgreen;
}
Does anybody know how to fix this issue? Here's a CodePen for an example of what's happening. Any help would be greatly appreciated!
Try this javascript code may this help you.
window.addEventListener("load", function(){
var messageList = document.querySelector(".messageList");
var messageBox = document.getElementById("messageBox")
var sendBtn = document.querySelector(".sendBtn");
for(var i = 0; i < 100; i++) {
var content = "Message body: " + i + "\nThe quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.";
createMessageBubble(content, Math.random() > 0.5);
}
sendBtn.onclick = function() {
var message = messageBox.value;
messageBox.value = "";
if(!message || message.length <= 0) {
return;
}
createMessageBubble(message, true);
// TODO: actually send the message
};
function createMessageBubble(content, isUser) {
var el = document.createElement("div");
el.classList.add("message");
if(isUser) {
el.classList.add("mine");
}
el.innerText = content;
messageList.appendChild(el);
}
var div = document.createElement("div");
div.style.width = "100%";
div.style.height = "45px";
div.style.float = "left";
messageList.parentNode.appendChild(div);
});

How do I prevent a pseudo-element or input from widening a flex item in a flexbox?

I have created a flexbox container that acts as a form control with two input fields, representing a range between two dates. If the user has entered something into any of the two fields a cross appears next to that field. The user can click on this cross to clear the field.
The cross is an :after pseudo-element, created and interacted with, with some nifty css and JavaScript, largely based on this answer on Stack Overflow. Here's my implementation (somewhat simplified, but not much):
$('.field.clearable').on('change input', 'input', function($e) {
$(this).parent().toggleClass('non-empty', !!this.value);
})
.on('mousemove', 'span.non-empty', function($e) {
var $this = $(this);
$this.toggleClass('onX', $this.width() < $e.clientX - $this.offset().left);
$this.hasClass('onX') ? $this.attr('title', 'Clear field') : $this.removeAttr('title');
})
.on('mouseleave', 'span.non-empty', function($e) {
var $this = $(this);
$this.removeClass('onX')
.removeAttr('title');
})
.on('click', 'span.non-empty.onX', function($e) {
$e.preventDefault();
var $this = $(this);
$this.removeClass('non-empty onX')
.removeAttr('title')
.children('input')
.val('')
.trigger('clear');
});
/* reset and cosmetic css */
* {
margin: 0;
padding: 0;
color: inherit;
font-family: inherit;
font-size: inherit;
}
::-webkit-input-placeholder {
color: #ccc;
font-size: 80%;
opacity: .8;
}
:-moz-placeholder {
color: #ccc;
font-size: 80%;
opacity: .8;
}
::-moz-placeholder {
color: #ccc;
font-size: 80%;
opacity: .8;
}
:-ms-input-placeholder {
color: #ccc;
font-size: 80%;
opacity: .8;
}
:focus::-webkit-input-placeholder {
opacity: .5;
}
:focus:-moz-placeholder {
opacity: .5;
}
:focus::-moz-placeholder {
opacity: .5;
}
:focus:-ms-input-placeholder {
opacity: .5;
}
html {
font-family: Arial;
font-size: 10px;
font-weight: normal;
text-align: left;
height: 100%;
}
body {
margin: 4em auto;
width: 400px;
color: #2c5ba0;
font-size: 1.5rem;
line-height: 1.5em;
background-color: #fff;
}
/* relevant css */
div.field {
display: inline-block;
margin: .4em 0;
}
div.field>label {
display: block;
margin: 0 0 .3em .2em;
font-size: 80%;
line-height: 1em;
}
div.field>span {
position: relative;
display: -webkit-flex;
display: flex;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: space-between;
justify-content: space-between;
box-sizing: border-box;
height: 2em;
border: .1em solid #ddd;
background-color: #fff;
}
div.field>span>span {
-webkit-flex: 1;
flex: 1;
}
div.field>span>span>input {
width: 100%;
border: none;
}
div.field.range>span input {
text-align: center;
}
div.field.range>span label {
-webkit-flex: 0;
flex: 0;
margin: 0 .2em;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
div.field.clearable span.non-empty {
position: relative;
padding-right: 2em;
}
div.field.clearable span.non-empty:after {
position: absolute;
right: 0;
top: 0;
content: 'x';
display: inline-block;
width: 2em;
height: 2em;
color: #ddd;
vertical-align: middle !important;
text-align: center !important;
font-style: normal !important;
font-weight: normal !important;
text-transform: none !important;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
cursor: pointer;
}
div.field.clearable span.non-empty.onX:after {
color: #f37e31;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="field clearable range">
<label for="start">created</label>
<span>
<span>
<input type="text" id="start" placeholder="from">
</span>
<label for="end">–</label>
<span>
<input type="text" id="end" placeholder="until">
</span>
</span>
</div>
When both <input> fields contain values and thus have a cross next to them, their width-ratios cancel each other out nicely and nothing is wrong. However, when only one of the <input> fields contains a value and the cross appears next to only that <input> field, the cross / <input> field nudges the other content aside.
This is not supposed to happen. I suspect the <input> fields are the culprit. Even though the container div.field should keep its display: block flexibility, the ratios of the inner content should stay constant.
Here are the relevant bits I thought I could achieve this with:
/* the span that acts as the flexbox container */
div.field>span {
position: relative;
display: -webkit-flex;
display: flex;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: space-between;
justify-content: space-between;
box-sizing: border-box;
height: 2em;
border: .1em solid #ddd;
}
/* let the spans, containing the <input> fields, take available space */
div.field>span>span {
-webkit-flex: 1;
flex: 1;
}
/*
let <input> fields take 100% content space of span
I thought this should take into account any padding on its parent
*/
div.field>span>span>input {
width: 100%;
border: none;
}
/* let center label (the dash) not be flexible */
div.field.range>span label {
-webkit-flex: 0;
flex: 0;
margin: 0 .2em;
}
/* if .non-empty class was set by JavaScript */
/*
make room for :after by setting padding-right accordingly
but this is the issue:
I thought this would also make the <input> shrink accordingly,
but it appears it doesn't shrink the amount I anticipated
*/
div.field.clearable span.non-empty {
position: relative;
padding-right: 2em;
}
/* show :after pseudo-element */
div.field.clearable span.non-empty:after {
position: absolute;
right: 0;
top: 0;
content: 'x';
display: inline-block;
width: 2em;
height: 2em;
/* etc. */
}
Do you know how I can let the complete control element maintain its flexibility, including its :after trickery, but have the <input>s shrink accordingly, when one of the crosses appear?
Or put more simply, I guess: is there a way to let two flex-items, with flex: 1, both take up an equal amount of space, at any given time?
The reason I'm using :after elements and not background-image like in the original answer I took the inspiration from, is because I'm using a custom icon font and not icon images.
However, when only one of the fields contains a value and the
cross appears next to only that field, the cross /
field nudges the other content aside.
This is not supposed to happen. I suspect the fields are the
culprit. Even though the container div.field should keep its display:
block flexibility, the ratios of the inner content should stay
constant.
This is because of the way your elements are structured for the close button trickery. Typically the padding on spans which then causes the flex items to gain or lose their size and affect the flex layout. But, if you remove the padding, then you would have a hard time calculating the offset for click-to-close.
Also, you have to keep on tinkering with the width and computing the position of mouse cursor on mousemove to keep the close button working.
One of the possible better solutions would be to use padding on inputs instead, and use only the position and opacity of the pseudo-element to work out the appearance and disappearance of the close button. This will keep the flex working without problems.
Then, rather than tracking the mousemove, you simply check the click on the span.
Example Fiddle (no jQuery): http://jsfiddle.net/abhitalks/4fnvukc2/
Example Snippet:
var closers = document.querySelectorAll('.closer'),
inputs = document.querySelectorAll('.closer input');
for (i=0; i < closers.length; i++) {
closers[i].addEventListener('click', clearer);
}
for (i=0; i < inputs.length; i++) {
inputs[i].addEventListener('input', closer);
}
function clearer(e) {
if (e.target.tagName == 'SPAN') {
e.target.firstElementChild.value = '';
e.target.classList.remove('dirty');
}
}
function closer(e) {
e.stopPropagation();
e.target.parentElement.classList.add('dirty');
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-size: 1em; font-family: sans-serif;}
div.field {
margin: 4em;
display: flex; justify-content: space-between;
}
span.closer {
display: inline-block; position: relative;
flex: 1 1 auto;
}
span.sep { flex: 0 1 auto; margin: 0 1em;}
span.closer::before {
content: '×'; font-weight: bold;
position: absolute;
right: -10px; top: 4px; opacity: 0;
transition: all 0.2s ease-out;
}
span.closer.dirty::before {
right: 6px; opacity: 1; cursor: pointer;
}
span.closer.dirty:hover::before { color: #00f; }
.closer input { padding: 3px 16px 3px 4px; width: 100%; }
::-webkit-input-placeholder { color: #ccc; }
:-moz-placeholder { color: #ccc; }
<div class="field range">
<span class="closer">
<input type="text" id="start" placeholder="from" />
</span>
<span class="sep">–</span>
<span class="closer">
<input type="text" id="end" placeholder="until" />
</span>
</div>
If you drop the padding-rightand resize the input it will work
You need to fix the click handler though
div.field.clearable span.non-empty {
position: relative;
}
div.field.clearable span.non-empty input {
width: calc(100% - 2em);
}
$('.field.clearable').on('change input', 'input', function($e) {
$(this).parent().toggleClass('non-empty', !!this.value);
})
.on('mousemove', 'span.non-empty', function($e) {
var $this = $(this);
$this.toggleClass('onX', $this.width() < $e.clientX - $this.offset().left);
$this.hasClass('onX') ? $this.attr('title', 'Clear field') : $this.removeAttr('title');
})
.on('mouseleave', 'span.non-empty', function($e) {
var $this = $(this);
$this.removeClass('onX')
.removeAttr('title');
})
.on('click', 'span.non-empty.onX', function($e) {
$e.preventDefault();
var $this = $(this);
$this.removeClass('non-empty onX')
.removeAttr('title')
.children('input')
.val('')
.trigger('clear');
});
/* reset and cosmetic css */
* {
margin: 0;
padding: 0;
color: inherit;
font-family: inherit;
font-size: inherit;
}
::-webkit-input-placeholder {
color: #ccc;
font-size: 80%;
opacity: .8;
}
:-moz-placeholder {
color: #ccc;
font-size: 80%;
opacity: .8;
}
::-moz-placeholder {
color: #ccc;
font-size: 80%;
opacity: .8;
}
:-ms-input-placeholder {
color: #ccc;
font-size: 80%;
opacity: .8;
}
:focus::-webkit-input-placeholder {
opacity: .5;
}
:focus:-moz-placeholder {
opacity: .5;
}
:focus::-moz-placeholder {
opacity: .5;
}
:focus:-ms-input-placeholder {
opacity: .5;
}
html {
font-family: Arial;
font-size: 10px;
font-weight: normal;
text-align: left;
height: 100%;
}
body {
margin: 4em auto;
width: 400px;
color: #2c5ba0;
font-size: 1.5rem;
line-height: 1.5em;
background-color: #fff;
}
/* relevant css */
div.field {
display: inline-block;
margin: .4em 0;
}
div.field>label {
display: block;
margin: 0 0 .3em .2em;
font-size: 80%;
line-height: 1em;
}
div.field>span {
position: relative;
display: -webkit-flex;
display: flex;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: space-between;
justify-content: space-between;
box-sizing: border-box;
height: 2em;
border: .1em solid #ddd;
background-color: #fff;
}
div.field>span>span {
-webkit-flex: 1;
flex: 1;
}
div.field>span>span>input {
width: 100%;
border: none;
}
div.field.range>span input {
text-align: center;
}
div.field.range>span label {
-webkit-flex: 0;
flex: 0;
margin: 0 .2em;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
div.field.clearable span.non-empty {
position: relative;
}
div.field.clearable span.non-empty input {
width: calc(100% - 2em);
}
div.field.clearable span.non-empty:after {
position: absolute;
right: 0;
top: 0;
content: 'x';
display: inline-block;
width: 2em;
height: 2em;
color: #ddd;
vertical-align: middle !important;
text-align: center !important;
font-style: normal !important;
font-weight: normal !important;
text-transform: none !important;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
cursor: pointer;
}
div.field.clearable span.non-empty.onX:after {
color: #f37e31;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="field clearable range">
<label for="start">created</label>
<span>
<span>
<input type="text" id="start" placeholder="from">
</span>
<label for="end">–</label>
<span>
<input type="text" id="end" placeholder="until">
</span>
</span>
</div>