Make an <input> look like normal text in a paragraph - html

Take the code here:
<p>Lorem ipsum <input type="text" value="algo" /> dolor sit ...</p>
Sample: http://codepen.io/dbugger/pen/KrAmPx
How can I make the input look like totally normal text, inside the paragraph? I set it to display: inline but the width seems still fixed.

Elements inherit certain default values from browsers. You need to "reset" all of them in order to make the input element appear as the surrounding text:
p input {
border: none;
display: inline;
font-family: inherit;
font-size: inherit;
padding: none;
width: auto;
}
This is as close as you can get with CSS alone. If you want a variable width, you will have to resort to JS instead of CSS, as adjusting an element to it's value is way beyond the scope of CSS. Modifying elements after the fact, based on user input or changes due to just-in-time effects, is what JS/jQuery are used for.
Note that depending on the browser you're using (and due to the possibility that future browsers might do things radically different that nowadays' practices), this list is not necessarily exhaustive.
The only way you can "fake" this effect in a clean manner without JS is to use an element with a contenteditable attribute, which (unlike an input element) will store user input in the content of the element instead of its value. For an example of this technique, see this answer
Though while you won't need JS to get the effect, you would need it to retrieve the content of the element. The only use past that I can imagine is if you're providing a printable document that never needs to programmatically handle the input or store it.

It looks like this is possible now. I found a post describing how to style the input so the HTML form styles are stripped.
Styling HTML forms
They used the following CSS, and for what I was trying to do, it worked perfectly:
input, textarea {
font : .9em/1.5em "handwriting", sans-serif;
border : none;
padding : 0 10px;
margin : 0;
width : 240px;
background: none;
}
Obviously this is too late for the original author, but I'm hoping other people will benefit from it.

Yes it is possible to do this by mimicing the styling with CSS and by using javascript to automatically adjust the length of the text.
Resize an input to the size of its content.
$(function(){
$('#hide').text($('#txt').val());
$('#txt').width($('#hide').width());
}).on('input', function () {
$('#hide').text($('#txt').val());
$('#txt').width($('#hide').width());
});
body,
#txt,
#hide{
font:inherit;
margin:0;
padding:0;
}
#txt{
border:none;
color:inherit;
min-width:10px;
}
#hide{
display:none;
white-space:pre;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Lorem ipsum
<span id="hide"></span><input id="txt" type="text" value="type here ...">
egestas arcu.
</p>

Probably the best way to hack this is just to make your text field into an edit text field but make it uneditable, that way your text field and edit text fields will look the same.

2020 update. Partially you can make it look like normal text with appearance property, setting it to none. Unfortunately, there is nothing much you can do to make the lines wrap, except use js to replace it with the value. https://developer.mozilla.org/en-US/docs/Web/CSS/appearance

I hope this is the answer you're looking for, but if you want to make an input field look like a normal paragraph (assumably so you can edit some of the text of the paragraph), the best way to do so is to:
Disable the input's border
.maskedinput {
border-style: none;
}
And then give it the same styles as the parent element, i.e. text color and bacground color etc etc, and then add a :focus to your CSS that changes the background color such that when the field is clicked, it will be highlighted.

Instead of using the <input> tags, you can use the <textarea> tags. They work almost exactly like <input> tags.
<textarea name="variable" rows="4" cols="50">
Placeholder
</textarea>

Related

Override standard CSS for hidden input tags

For HTML tags such as <input type="hidden"> when I look at the browser's standard CSS, I see:
input[type="hidden" i] {
display: none;
}
So I added this to my CSS:
input[type="hidden" i] {
display: block !important;
}
And for HTML I have <input name=countrycode type=hidden value=US>
When I open up the developer tools I can see that the old display:none is ignored, however, the field never shows up in the page and is still hidden!
Why doesn't the browser follow my CSS rules, is there another way to force it to do so? If I add width: 200;height:200;background: black this should make it appear in the page, right?
The reason I want to do this is because I have a lot of hidden inputs in many pages that now I want them to be visible (to get input from user) so I decided to do it fast with CSS. I know this can be easily done with JavaScript, but just curious why CSS is not working or maybe it will work on other browsers but not Google Chrome?
The reason that your input[type=hidden] is hidden, is because it doesn't have a control type to show the user it's value, so your display: block; should work, but the browser'll have nothing to render. Your hidden input could be a checkbox, a text field or a number.
More info: https://www.w3.org/wiki/HTML/Elements/input
You should give that input an id="countrycode" and just use javascript to set the value of type attribute from "hiden" to "text" like this:
document.getElementById("countrycode").setAttribute("type", "text");
OR if you prefer to use the name attribute, then do this:
document.getElementByName("countrycode").setAttribute("type", "text");
Voila!

change background colour of p element when touch

This html page may have more than one <p> element. I wish to change the background of the one that gets touched by the mobile user. And if the user touches another, then the previous touched paragraph returns back to the its original background colour "default white".
I tried few selectors for no avail. How can it be done with CSS? thanks
edit
There are 2-3 lines of data which need to be displayed to the user, I used paragraph for each group of 2-3 lines, since I can do a bit of formatting as well as showing the different pieces of data like a vertical list.
So I wish to be able to select an item from the "list" and apply other actions relating to the selected item.
.selectable:focus {
background-color: lightgray;
}
<template name="myName">
<p class="selectable"><b>{{value.[0]}}</b><br/>{{value.[1]}}<br/>{{value.[2]}}</p>
</template>
Following the #CBroe suggestion, you could do the following:
<p class="selectable" tabindex="0"> Paragraph 1 </p>
<p class="selectable" tabindex="0"> Paragraph 2 </p>
<p class="selectable" tabindex="0"> Paragraph 3 </p>
<p class="selectable" tabindex="0"> Paragraph 4 </p>
.selectable:focus {
background-color: lightgray;
}
See the jsfiddle.
You can learn more about tabindex here.
If you can use jQuery you can do it like this:
jQuery:
$("p.selectable").click(function(){
$("p.selectable").removeClass("clicked");
$(this).addClass("clicked");
})
CSS:
p.selectable {
background:white;
height:100px;
}
p.selectable.clicked {
background: red;
}
See this jsfiddle. It is hard to do with CSS only. CSS is ment to change formatting. If you want to trigger actions, use javascript. (It can be done with a hacky method in CSS like adding a invisble checkbox over the paragraphs or something but lets not get into that)
EDIT: apparently can also be achieved by adding a tabindex as #CBroe suggested. Neat trick :) However, if you want to trigger more actions when the paragraphs are clicked, use javascript approach. If you only want to change the style, you can use this CSS only trick.
This would be a hacky way of doing it, but you can utilize the :checked pseudo-selector in combination with the text as a way to toggle its "selected" state.
.selectable {
display: none;
}
.selectable:checked + label {
color: red;
}
<input type="checkbox" id="unique-id" class="selectable">
<label for="unique-id">
<b>{{value.[0]}}</b><br/>{{value.[1]}}<br/>{{value.[2]}}
</label>
The benefit to this is that you are not reliant on JavaScript and it should be pretty stable across the various devices and platforms.
Let me know if you have any questions or need further clarification!
Add a listener for the event you want
Apply the listener to the elements you want
Add the functionality you want, in this case: adding a class to give a background color (and removing the previous element's color).
$("p").on("tap",function(){
$('.add-color').removeClass('add-color');
$(this).addClass('add-color');
});

Make readonly textarea's height determined by it's content

I would to make a readonly input appear like a pre or div tag with CSS.
I would have thought that it would have been easy to do with a textarea but it seems to be quite the task. I'm able to hide the border and the resizing but, for whatever reason, I can't make the textarea's height be determined by it's content.
I've seen a lot of stuff on using javascript to auto-resize textareas but is there anything I can do if it's static text that doesn't require javascript?
UPDATE
I just wanted to clarify the purpose of this: I'm looking to write, re-write with javascript, and submit a single readonly element with forms and, at the same time, not have it constrained to a single inline area which forces, at best, scrolling and, at worse, loss of data.
UPDATE 2
Per the request, I've created a fiddle to show an example of what I'm trying to do: http://jsfiddle.net/BUwdE/1/ .
textarea[readonly] {
width: 100%;
border: 0;
resize: none;
overflow: hidden;
}
You'll see that the content is cutoff at the bottom because the textarea's height isn't determined by its content.
I actually tried to do what you have been doing. But since it is going to be a read-only input, I actually ended up applying a CSS to a div element. This will be a hack which releases our headache.
HTML
<div class="faketextarea"> some long long text </div>
CSS
.faketextarea {
// css of a text area
}
You can specify the height of a textarea in HTML using the rows attribute, but that doesn't automatically resize. You might have to appeal to the W3C CSS Working Group to get what you want.
<textarea name="whatWillBeSentToServer" rows="4" readonly="readonly">
Modified from here:
function auto_grow(){
var ts = document.getElementsByTagName('textarea')
for (i in Object.keys(ts)){
ts[i].style.height = "5px";
ts[i].style.height = (ts[i].scrollHeight+49)+"px";
}
}
textarea {
resize: none;
overflow: hidden;
min-height: 50px;
max-height: 100px;
...
(properties for your needs)
}
<body onload='auto_grow()'>
<textarea>anytexts</textarea>
<textarea>texts 2</textarea>
</body>
The differences being I have assigned the auto_grow() function on the html <body> tag instead of the <textarea> tag
fiddle: https://jsfiddle.net/btq7m3a6/
More: https://jsfiddle.net/8o67huq2/

How to emulate Google greyed-out suggested text in input element?

I'd like to know how to do something like this in CSS:
How is it possible to change the text color halfway through like that on an <input> tag ? I've done a View Source already, but it's hard to make sense of.
Google uses two divs which are absolutely positioned on top of the input box. The first div contains the word stackoverflow, and the text is styled in a light gray. The second dvi contains "stacko" and the text is black.
If you inspect the source, look for divs with class="gsfi".
First off, look into implementing autocompletion. This should give you another element [beneath the one the user types; probably another div] for styling.
its not purely a CSS thing, you need JS too.
Have a look at this autocomplete demo: http://www.pengoworks.com/workshop/jquery/autocomplete.htm
Now you could use CSS for styling text selections in that input to gray the text out.
like this:
::selection {
color: white;
background-color: red;
}
::-moz-selection {
color: white;
background-color: red;
}

Change 'src' value by css for input tag with type="image"

Is it possible to change the value of src attribute of <input type='image' alt="Text will be shown if pics are disabled" src='somepic.png'../> by css?
The problem is:
I want to specify which pic will be shown as submit button just using css (so the design team will change only css files!).
If I use the alternative way like <input type="submit" class="cssclass" value=" " alt="Text will be shown if pics are disabled"/> and specify the background of this element in css - it doesn't work well if pics are disabled. - No any alternative text is shown instead of pic. However the first way solves this situation...
Please advice something
Thanks.
Here it is: http://jsfiddle.net/kizu/66JXn/
Some notes about this solution:
Use <button></button>, 'cause it can include other blocks.
You'll need a bit of extra code to make all these work in Fx and IE:
For Fx you need an extra wrapper inside (there are positioning bug) and some extra -moz- properties reset.
For IE you must shrink the original button, 'cause there are some extra padding that is hard to remove.
You place the text and another element inside, that would overlay the text. So when the images would absent, the text would be accessible.
That's it :)
No, and this is bad practice. CSS is for static content only.
What you should do, is define a template file with variables in it such as:
template.js
my_backgroundImage = "url('somepic.png')";
then your file would load
x = document.createElement('image');
x.src = my_backgroundImage
Attribute selectors might work, but they aren't very flexible. Try this one:
img[src=""] {
background-image: url('none.png');
height: 100px; /* Height of BG image */
width: 100px; /* Width of BG image */
}
It doesn't change the image's src= attribute, but it performs the same function.
Here's my idea.
You can use JavaScript to read the stylesheets of <img> tags, and modify them accordingly.
I'm talking about a class whitelist, like big, small, center and all other classes applied to the images are interpreted via JavaScript. The design team could use CSS, but it would not render in the expected manor, like this (Python + JavaScript):
for every <img> tag:
if tag.classes contains class not in whitelist:
for every class not in whitelist:
this.src = newClass.backgroundImage;
this.removeClass(newClass)
It reads the CSS for the background-image property, but it just steals the URL of the image and sets the src= attribute using that URL. Then, the JavaScript would delete that class, causing it not to render.
(This is a problem for which JS is the solution, but ignoring that:)
One option is to wrap the button and an extra div (lets call it div.overlay) in a parent container.
Set the container to to position:relative.
Set the button to only display text, as usual. Set the div.overlay to position:absolute, width and height to 100%, and left and top to 0, and a z-index higher than the button. Set the image you want to display as the background-image of div.overlay.
With images enabled, the user sees the image, and the image can be changed using only CSS.
With images, or CSS disabled, the user only sees the plaintext submit button.
You might have to do some trickery to get clicking div.overlay to submit the form, perhaps just make div.overlay a duplicate submit button. Also, who knows what Googlebot makes of overlay techniques like these.
It's ugly, but the only pure CSS solution that immediately jumps to mind is a kind of image replacement with relatively poor support. That's using :after. It's kind of a poor practice due to the misuse of :after, and the support is pretty iffy, and I think it'd be iffier for an input element, based on the last time I tried to use :after on an input...
.cssclass,
.cssclass:after{
display:block;
width:100px;
height:100px;
}
.cssclass{ position:relative; }
.cssclass:after{
position:absolute;
top:0;left:0;
content:url("button.jpg");
}
See http://www.rachaelmoore.name/best-practices/css-image-replacement-ii/ for more.
Or setting the default src to a shim and always using CSS to set the desired button as a background image. Which I just noticed you've already thought of. I imagine that should work just fine.
Ok... So I hate it when I ask a specific question and, instead of answering it, they give me some crappy work-around instead of answering the original question that I asked... But for some reason, I've decided that I'm going to do it to you.
If I understand the problem correctly, you just want to have a form button with a background image and if the background image doesn't load, you want some sort of alt text displayed to the user with the caption of the button? If that's not right, stop reading and "down arrow" me.
In apps that I've made, I've always just styled the input with a background image, but left it up to the HTML control to insert text... It's good for three reasons... buttons can be styled, developers can change the value of the text on the button without having to bother me to make a new image, and if the background image doesn't load, the button is still readable.
So my html was like this:
<input type="submit" id="btnSearch" class="searchButton" value="Search">
then my class may read something like:
.searchButton {
backgorund-image: url('searchButtonImage.png');
font-family: sans serif;
font-size: 10px;
color: #808080;
padding-left: 50px 0px 0px 0px; // Assuming a magnifying glass icon or whatevs is on the left and is 20-ish pixels
width: 100px; // you can put this as in-line style if you make a more generic class
}
If you want to make the BG more generic, move the width of the button to make it in-line on the button, so the devs can change the width with the text value and make your generic bg image like 200px wide.
Depending on the browser, the text might not be as nice and ani-aliased as in others, but IMO, it's a small price to pay.
(Disclaimer: Please forgive me if you copy and paste this and it doen't work. I just hand-wrote it without testing it.)
Can you do it with javascript?
I have an image on my page that, when clicked, will show another button, and also change the src attribute of the first.
Here is what I use:
<script type="text/javascript">
function apps()
{
var element = document.getElementById("app_frame");
if (element.width != "0%")
{
parent.document.getElementById("frame").setAttribute("width","100%");
parent.document.getElementById("app_frame").setAttribute("width","0%");
parent.document.getElementById("appbutton").setAttribute("src","site/main/images/apps/show.gif");
parent.document.getElementById("wthrbutton").style.visibility="hidden";
}
else
{
parent.document.getElementById("frame").setAttribute("width","65%");
parent.document.getElementById("app_frame").setAttribute("width","35%");
parent.document.getElementById("appbutton").setAttribute("src","site/main/images/apps/hide.gif");
parent.document.getElementById("wthrbutton").style.visibility="visible";
}
}
</script>
What that says, is: set the "app_frame" as variable "element",
then check variable "element" for its width.
if its width is not 0, then it gets the element "frame",
by using getElementById, and then sets the attribute "width" to 100%
you can see slightly lower down that you use the same method, but use the SRC attribute rather than width, and set it to whatever you want, in my case, site/main/images/apps/show.gif
hope that helps