I am doing a project that has a input like this:
<div className="search__bar__description form__control">
<input
placeholder="Filter by title, companies, expertise..."
aria-label="Enter company, title, or expertise here"
onChange={e => setSearchInput(e.target.value)}
value={searchInput}
/>
</div>
When the site is in desktop I want to have that whole long placeholder but when in mobile I want the placeholder just to say Filter by title...
I am trying to figure out how to do this is CSS. Can I put a span inside of the placeholder & then just hide it? Would that be valid HTML? If it isn't W3C valid HTML can you please tell me how to do this?
Thanks in advance!
First, you don't need JavaScript to do this. Although you cannot put a span inside an input, you can perform some trickery using the :placeholder-shown pseudo-class to achieve what you're after. Browser support for this pseudo-class, at the time of this post, is really good.
From MDN:
The :placeholder-shown CSS pseudo-class represents any <input> or
<textarea> element that is currently displaying placeholder text.
This example makes the input placeholder color transparent on smaller-sized screens, visually hiding it. The span containing the short label is then shown at this break point. Notice it's styled and positioned so that it looks as close to the original placeholder text as possible. Finally, using the pseudo-class mentioned above, we hide the short label when the original placeholder is not shown (when input is not blank).
.form__control {
position: relative;
}
.short-label {
display: none;
pointer-events: none;
}
#media screen and (max-width: 700px) {
::placeholder {
color: transparent;
}
.short-label {
position: absolute;
left: 4px;
top: calc(50% + 1px);
transform: translateY(-50%);
font-size: 11px;
color: rgb(43%, 43%, 43%);
font-family: sans-serif;
letter-spacing: 0.4px;
display: block;
font-weight: lighter;
}
input:not(:placeholder-shown)+.short-label {
display: none;
}
}
<div class="search__bar__description form__control">
<input placeholder="Filter by title, companies, expertise..." aria-label="Enter company, title, or expertise here" />
<span class="short-label">Filter by title...</span>
</div>
jsFiddle
You can't put a span inside a placeholder.
The common solution to what you want to achieve is to have the placeholder text as a separate element, underlaid behind the text field. And then you can do whatever markup and styling you want to it. This, of course, is more effort, but such is the way of bespoke engineering.
You can see an advanced example here: https://css-tricks.com/float-labels-css/
If you ignore the animations, the take-away from his code example is that the <label> element is used instead of the placeholder attribute, and is underlaid behind the input field, so it looks the same, but then can be manipulated with all the same CSS (int his case, adding some fancy transitions) and sub-elements as any other standard element.
Related
First things first!
Ingredients: We have a <input type="text"/>.
Problem: Every time the user types, delete or change a letter, under the text area must grow a line, as shown in the visual demo. I imagined different solutions:
dynamic size: the part of the line that adds/deletes/changes to the precedent one has the exact width of the letter added/deleted/changed.
static size: the part of the line that adds/deletes/changes to the precedent one doesn't count the exact width of the letter added/deleted/changed.
Question: How can I achieve one of those goals with HTML5 + CSS3 (preferibly without using javascript/jQuery) so the underline will grow (from left to right) while the user changes the text inside the input? I'M ASKING FOR THE ANIMATION.
Actual visual DEMO: From first to the last step, the user is typing, then he finished and as last, he delete everything.
[EDIT]: Note that the placeholder word in the visual demos is not random: the input tag contains everything, so if the user types something, then the underline is present and showed with the animation I'm asking for, otherwise the placeholder without any underline is showed. What I'm asking is the animation/transition! The underline of the input tag will seems to the user like it's growing/decreasing.
Just use css text-decoration property of input field
input {text-decoration: underline; }
Use this to achieve your requirement
not sure if i understood what you want. but it seems kind of simple. just use this
input {
text-decoration:underline
}
<input type="text" placeholder="insert text">
EDIT : sorry for the duplicate answer . Stackoverflow went in maintenance
mode while i was answering and the answer appeared now
This is possible trough the use of pseudo-elements , some CSS trickery for the width and a :hover setting for your anchor, to control your pseudo-elements.
.link {
text-decoration: none;
position: relative;
color: tomato;
}
.link::after {
content: '';
position: absolute;
bottom: 0;
left: 0;
width: 0%;
border-bottom: 2px solid #3366FF;
transition: 0.4s;
}
.link:hover {
color: blue;
}
.link:hover::after {
width: 100%;
}
<a class="link" href='#'>This link here</a>
I am trying to underline an <input type="date"> field using style="text-decoration: underline;" but it doesn't seem to work. Is there another way to underline the set date value? I would also like to apply this for time.
NOTE I am able to apply font-weight and color properties to these inputs but not text-decoration
EDIT
It seems the above is not a sufficient explanation of what I am trying to achieve.
Basically:
<input type="date"> will yield 15-02-17
I am trying something like:
<input type="date" style="text-decoration: underline;"> to yield an underlined 15-02-17
But this does not work. Is there another way?
ANOTHER EDIT
Preferably the solution should be generalized to all browsers.
Put this code in the CSS.
input::-webkit-datetime-edit-fields-wrapper {
text-decoration:underline;
}
The date tag consists of different div and span tags, which are enveloped by input::-webkit-datetime-edit-fields-wrapper in Chrome
EDIT : Example date input tag in Chrome
I'm not sure you can style <input type="date"> with text-decoration.
Not the nicest solution, but you could manually add an underline..
.wrap {
position: relative;
display: inline-block;
}
.wrap:after {
content: '';
position: absolute;
bottom: 2px;
left: 4px;
height: 1px;
width: 54%;
background: red;
}
<div class="wrap">
<input type="date">
</div>
CODEPEN
Note that you can't add pseudoelement to input, so you'll have to position it relative to something else.
I'm affraid the visualization of html5 inputs is browser-specific (kinda like fieldsets), thats why text-decoration: underline; won't work. You will likely run into issues too when trying to style the calendar it is showing.
I think your best bet is to use a plugin. There are plenty ot there, ie: https://github.com/fengyuanchen/datepicker
Also, you can edit your question and ask for the specific browser you want to do this, I'm sure we can find out the style needed.
The problem
I cannot alter the way in whcih the HTML below is output, but I do need to amend the way in which it is displayed. I know I can achieve my desired results with jQuery, but I'd like to do it with pure CSS possible?
What exactly I need to do
I need to show the input from the HTML below, but the label itself is superfluous. The tag can stay or go, I'm not bothered by that, but Username and <br> need to be gone.
Obviousely I cannot use label[for="user_login"]{ display: none; }, as this will fail because it will hide everything within the selector.
The original HTML
<label for="user_login">
Username
<br>
<input id="user_login" class="input" type="text" size="20" value="" name="log">
</label>
The desired HTML
<label for="user_login">
<input id="user_login" class="input" type="text" size="20" value="" name="log">
</label>
jQuery approach
$(document).ready(function(){
var username_label = $('label[for="user_login"]'),
username_input = username_label.find('input');
username_label.html(username_input);
});
Why I don't want to use jQuery
The login for in question is displayed differently depending on screen size. On smaller devices I requre the labels hidden (to save space), while on larger screens the label should still be visible. Using the jQuery approach will remove the labels in both cases.
While I know I can check the screen widht on load, and then use resize event (if necessary), should the screen size change (on a tablet, portrait to landscape for example) the labels could be removed and then not visible when they need to be.
Question
Can I achieve this result (or similar) with pure CSS?
Try this css:
label {
visibility:collapse;
}
label input {
visibility: visible;
}
Here is a jsfiddle:
http://jsfiddle.net/entgqjzk/
I modified the css I found at Hide text node in element, but not children
edit:
This doesn't remove the whitespace as you indicated.
I can't come up with anything better than a combination of jQuery and css, it's not the most beautiful code ever but it does work for this situation in which you don't have control over the html:
http://jsfiddle.net/entgqjzk/2/
The css class "hidden-for-mobile" could be used for example combined with a seperate stylesheet for mobile devices (or pseudo selectors for different screen sizes)
Have you tried pseudo classes? http://css-tricks.com/a-call-for-nth-everything/
Thanks to #geoffreydv for the idea of using visibility.
In conjunction with that, I was able to use the CSS below to effectively hide the whitespace occuped by the text within the label.
body.login #loginform label[for="user_login"],
body.login #loginform label[for="user_pass"]{
display: inline-block;
height: 50px;
position: relative;
visibility: collapse;
width: 300px;
}
body.login #loginform input#user_login,
body.login #loginform input#user_pass{
margin: 0;
position: absolute;
top: 0;
visibility: visible;
width: 300px;
}
Instead of labeling each field in a form, it is sometimes preferable (from a design standpoint) to have placeholder text in each field. For example, instead of having this:
----------------------------------
Full Name: | |
----------------------------------
you have this:
----------------------------------
| Full Name |
----------------------------------
The when you click in the field, the text disappears and you can write whatever you want. If you skip over the field without entering any text, then the placeholder reappears.
I've seen this done many ways, but all methods involve JavaScript. For example, Twitter does a decent job on their signup page but if Javascript is disabled you end up typing your name over the word 'Full name'.
I'm looking for a CSS-only method that would work even with JavaScript disabled. The only potential solution I've come up with is to set the background of the <input> tag to an image of the desired text and then use the input:focus pseudo-class to clear the background image when someone clicks on the text box. This seems to work but it would be nice not to have to use images.
Does anyone know of a good resource on how to do this?
This is the preferred method, and works in all current browsers:
<input type="text" name="" placeholder="Full Name"/>
This version works for IE9 and before:
<input type="text" name="" value="Full Name" onfocus="value=''" onblur="value='Full Name'"/>
You can do this with a <label> placed behind the index using z-index and a transparent background-color on the <input>. Use :focus to change to a white background.
:first-line has some Firefox issues.
Demo: http://jsfiddle.net/ThinkingStiff/bvJ43/
Note: See code-sushi's comment below for blur issues: Placeholder text in an input field with CSS only (no JavaScript)
Output:
HTML:
<label class="input">enter name<input /><label>
CSS:
.input {
color: gray;
display: block;
font-size: small;
padding-top: 3px;
position: relative;
text-indent: 5px;
}
input {
background-color: transparent;
left: 0;
position: absolute;
top: 0;
z-index: 1;
}
input:focus, input:first-line {
background-color: white;
}
Try this:
HTML
<div>
<input type="text" id="text"></input>
<label for="text">required</label>
</div>
CSS
.text-wrapper {
position: relative;
}
.text-input-label {
position: absolute;
/* left and right properties are based on margin, border, outline and padding of the input text field */
left: 5px;
top: 3px;
color: #D1D1D1;
}
#text:focus + label {
display: none;
}
Working Fiddle
All of the presumably CSS-only answers above have neglected a critical component which is required in order to prevent the label acting as a pseudo-placeholder from "bleeding through" once the user is no longer focused on that particular field.
Hint:
input:valid { background-color:white; }
The pseudo-class :valid obtains whenever a field has any value other than ''. So when your user enters anything in the field of his or her own, the label displayed there will stop being displayed.
Be advised with <input type="email" /> fields, the pseudo-class :valid does and will actually require input of a valid email format (e.g. "xxxx#xxx.com" -- or .net or .org, etc.).
Full instructions how to do this here: http://css-tricks.com/float-labels-css/
Try this: it solves the overflowing placeholder and multi-input cases. The trick is to move the labels behind their inputs and reorder them visually.
You don't need an extra div to achieve what you want.
We have buttons of many sizes and colors that use background images. There is a label on the background image itself, but we need to keep the button's text in the HTML for usability/accessibility. How do I make the text disappear in all browsers?
Modern browsers are easy, I just used -
color: transparent;
It's Internet Explorer 7 that I can't get to comply. I've tried these CSS properties, and none of them can remove the text completely without destroying my site's layout in the process.
font-size: 0px;
line-height: 0;
text-indent: -1000em;
display: block;
padding-left: 1000px;
I would very much appreciate any help.
Personally, I go for the all CSS approach:
{ display: block;
text-indent: -9999em;
text-transform: uppercase; }
For whatever reason, text-transform: uppercase; does the trick for IE7. Of course, you'll probably have your own CSS along with that for additional styling (if needed).
Additional to your
color: transparent;
You can use something like
padding-left: 3000px;
overflow: hidden;
Regards
In some cases you can use the propery "content" to change what is contained in the element, personally though I would use javascript to do it.
Just write blank text into the element.
If the button is an input submit button, use the image
<input type="image" src="/images/some_image.png" />
You can style this with CSS
input[type="image"] {
border: 1px solid red;
width: 150px;
height: 35px;
}
If they are links, Dave provided the answer.
How do I make the text disappear in
all browsers?
I suppoose you want the altarnative text to disappear if the image is loaded.
For this puprpose you can use this:
<INPUT TYPE="image" SRC="images/yourButtongif" HEIGHT="30" WIDTH="100" ALT="Text In Case There Is No Image" />
You can apply additional styles if needed, but this minimum will do the job for you.
If I understand the question correctly, this might work (I don't have IE7 to test on at the moment, so not 100% sure)
For markup like this:
<a href="javascript:return false;" class="button" id="buttonOK"><span
class="icon">Ok</span></a>
Use this css:
span.icon {
/*visibility: hidden;*/
display:block;
margin-left:-1000;
width:100px;
}
or this might work depending on your requirements for usability/accessibility:
span.icon {
visibility: hidden;
}
I don't know what users / programs the labels need to be in the HTML for, but if it's for text browsers and such, maybe you could insert a JavaScript that removes the labels onLoad?
JQuery or Prototype would make that very easy.