Im trying to create a text field which is readonly but it should not look like a text field.
The normal readonly text field is
Your name is [ SAM ]
but I want it like
Your name is SAM
That is it should look like the continuation of the sentence and can still act as a text field on to which we can show value (here SAM).
Any way to do that?
What your looking for is this:
HTML:
Your name is <input type="text" value="SAM" readonly="readonly" />
CSS:
input {
border: 0;
}
You can set the input field to readonly in the HTML, this will make it so you cannot edit the field. Then you want to get rid of the border to so it makes it look like its apart of the text. Then customise it as you see fit.
DEMO HERE
Update:
If the input is in a div/span you can just the inputs within that div/span like so:
HTML:
<span class="test">Your name is <input type="text" value="SAM" readonly="readonly" /></span>
CSS:
.test input {
border: 0;
}
DEMO HERE
to style all readonly textboxes apply following CSS rule
input[readonly="readonly"] {
border:0px;
}
It can be done by css. LIVE DEMO
<style>
*
{
font-family: Tahoma;
font-size: 13px;
}
input
{
background-color:transparent;
border:0px;
padding:0px;
}
</style>
this is the <input type='text' value='textbox' readonly >
Set the border, outline and background to none and you'll get the desired effect.
input[disabled] {
background:none;
border:none;
outline:none;
}
It would still take the default min-width though, so you might have to set the width to a smaller value.
DEMO
Add this to your CSS:
input[type="text"], input[type="text"]:focus { border: none; outline: 0; }
Yes, why don't you use CSS for this?
Add:
input[type="text"] { border: none; }
Also an option: adding background transparancy as stated above.
<input type="text" value = "Your Name is SAM" readonly= "true" style ="border: none" name="name"/>
Set the Border to 0px and the background to transparent, This should be work.
In CSS file:
*
{
font-size: 14px;
font-family: Arial;
}
input[type="text"]
{
border:0px solid red;
background: none;
}
In HTML file:
Hello <input type="text" value="John">
Related
for example I have form element in multiple places like
form {
display: table;
margin: 20px auto;
text-align: center;
}
Now this style properties are overriding another form element's style properties, now form does not have id attribute , I gave the form a name & tried with name attribute but it is not working.
form[name="myform"] {
display: table;
margin: 20px auto;
text-align: center;
}
Please suggest a solution
You have multiple choices, but a simple way is to use different IDs or different classes like below:
<form id="first"> </form>
<form id="second"> </form>
For each page use another CSS style with new define element.
OR Use Class like class="form_1"
OR Use ID like id="form_1"
OR For no rewrite use like font-size: 18px !important
assign classes to your form tag, like:
<form class="x"> ... </form>
and set up CSS for those classes:
.x {
color: red;
...
}
Concerning question in comment: Here is an example that demonstrates that this works (note the class color overriding the form color):
form {
display: block;
margin-top: 0em;
color: blue;
}
.x { color: red;
font-size: 24px;
}
<form action="demo_form.asp" class="x">
First name: <input type="text" name="FirstName" value="Jane"><br>
Last name: <input type="text" name="LastName" value="Doe"><br>
<input type="submit" value="Submit">
</form>
I was looking for a way to display the value of an
<input type="submit">
on 2 lines, so potentially add a line break in it, but i tried multiple stuff such as :
<br>
\r\n
\n
The result should be like this (On the right side of the picture) :
Nothing works. Anyone got a clue on this ?
Add this to your css:
A white-space property will allow to have input in multiple lines
input[type="submit"] {
white-space: normal;
width: 150px;
float:right;
text-align: right;
}
<input type="submit" value="J'essaie gratuitement 30 jours" />
Two other methods are
<button type="submit">Multiple line<br/>input</button>
and
using
carriage return in between the input value as:
<input type="button" value="Multiple line
input" style="text-align:center;">
The last method however doesn't work in IE10
Use button instead of input:
.right-aligned {
text-align: right;
}
<button type="submit" class="right-aligned">Text <br /> broken </button>
Buttons can accept a variety of other tags inside, such as <br />, <span>.
Then, you can style it with CSS however you wish (see the CSS class and rules in the code snippet).
I think you try this in HTML:
Just as example help for you:
<input type="button" value="Really
Tall
Button">
This is working for me:
div.full {
width:500px;
background-color:grey;
}
div.left {
float:left;
width:60%
}
button {
width:40%;
text-align:right;
cursor:pointer;
}
div.underline {
width:100%;
}
<div class='full'>
<div class='left'>
there is a part of text
</div>
<button>J'essaie gratuitement
<div class='underline'>30 jours</div>
</button>
</div>
I just added some CSS to keep the size of the button. and line breaks are not a very good practice. You'd better do it with css.
Alternatively, use a standard <a> or <span> tag.
var submits = document.getElementsByClassName('submit');
for (var i = 0; i < submits.length; i++) {
submits[i].addEventListener('click', function() {
alert('submit!');
document.getElementById('form_to_submit').submit();
});
}
.submit {
text-decoration: inherit;
color: inherit;
display: inline-block;
border: 1px solid #222;
border-radius: 2px;
padding: 2px 4px;
background: #eee;
cursor:pointer;
text-align:right;
}
<p>J'essaie gratuitement<br>30 jours</p>
<p><span class="submit">J'essaie gratuitement<br>30 jours</span></p>
Please take a look at http://jsfiddle.net/JHMqG/
I'm trying to figure out how to change the background of the radio button when clicked.
So far, I've been successful with the cat option, but I'm stuck at the dog option. I need the dog option to work because I want the background change to include the circle button.
Please advise. Thank you.
Here you go: http://jsfiddle.net/JHMqG/1/
On the dog, the label element only contained the text. On the cat, the label element contained the text and the radio button. Also, I cleaned up your HTML a bit.
See this:
DEMO
I changed a bit the HTML:
<div>
<input type="radio" name=1 Value=420 id="a1">
<label for="a1" class="radiostyle" >Cat ($420)</label>
</div>
<div>
<input type="radio" name=1 Value=375 id="a2">
<label for="a2" class="radiostyle">Dog ($375)</label>
</div>
and added a few bits to the CSS, so it now looks like this:
div { margin: .5em; }
input, label {
position: relative;
display: inline-block;
vertical-align: middle;
}
input[type=radio] { margin-right: -1.65em; z-index: 2; }
.radiostyle{
background-color: #CCC;
border-radius: 8px;
padding: 4px 4px 4px 1.75em;
}
.radiostyle:hover{
background-color: #0F6;
cursor:pointer;
}
input[type=radio]:checked+label {
/* Or `#a1:checked+label` if you only want it for that input */
background-color: #0F6;
}
The problem was the <input> was just preceding the <label> in the cat option, but in the dog option the <input> was inside the<label.
I corrected it by moving the <input> of the dog option to be preceding the label, you can see it working here:
http://jsfiddle.net/SxPvz/
I have some HTML that displays a text input and 2 checkboxes side by side. The HTML is generated by a tool and so I cannot alter the way the HTML is built, I can only apply styling to it using CSS. My simplified HTML and CSS is:
<html>
<head>
<style type="text/css">
td {
padding: 0;
}
fieldset {
display: inline;
padding: 0;
margin: 0
}
input[type="text"] {
margin: 0;
padding: 0;
}
table.checkboxs {
border-collapse:collapse;
}
table.checkboxs td {
border: none;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<input type="text" id="item1" value="" />
<fieldset>
<table summary="" class="checkboxs" border-collapse="">
<tr>
<td><input type="checkbox" id="chkbox_0" name="chkbox" value="1" /><label for="chkbox_0">One</label></td>
<td><input type="checkbox" id="chkbox_1" name="chkbox" value="2" /><label for="chkbox_1">Two</label></td>
</tr>
</table>
</fieldset>
</body>
</html>
This ends up with the checkboxes displayed higher up than the text item like this:
What I would like is them aligned together like this:
That seems simple enough, but I cannot figure out how to achieve it. If you remove either the text input or the fieldset then the remaining element takes up the minimum vertical space, it is only when they are together that the text input gets pushed down the page.
By all means point out any shortcomings in the HTML, but as I said that is really not under my control, I can only influence the layout using CSS.
There are two quick solutions I can think of. The first is to get the table to behave and align more like regular text by setting:
table.checkboxs {
display:inline-block;
vertical-align:middle;
}
JSFiddle example
Or by aligning the textbox to behave more like the table:
input[type="text"] {
vertical-align:top;
}
JSFiddle example
Try to give float:left to both of them, input and fieldset and then use margin-top if needed to achieve what you want.
try
input {
display: inline-block;
}
fieldset {
display: inline;
padding: 0;
margin: 0
}
Given the following html
<label for="inputelement">label</label>
<input type="text" id="inputelement" name="inputelement" />
You can style the input on focus using
input:focus { background: green; }
Is there a way of also styling the <label /> without JavaScript?
Thanks all
No. there is unfortunately no predecessor selector in css
input:focus -+ label { ... }
would be lovely.
having the label after the input would be dooable:
input:focus + label { ... }
you could use some positioning to display before...
For completeness, if your input field is within the label you can use focus-within:
HTML:
<label>
<input name="example" type="text">
</label>
CSS:
label:focus-within {
background: #DEF;
}
UPDATED
Make sure you check the draft as this may change: https://drafts.csswg.org/selectors-4/#relational
The :has() relational pseudo-class will allow the selection of parents for example, the following selector matches only <a> elements that contain an <img> child:
a:has(> img)
This can be combined with other selectors such as :focus, :active or :not to offer a lot of potential.
Unfortunately browser support isn’t great at the time of writing: https://caniuse.com/#feat=css-has
Adding this for people finding this page in the future. CSS4 will have a parent selector allowing you to choose what element to apply the style to:
I think the current spec allows you to specify which item is matched with a ! sign - the subject selector.
label! > input {
font-weight: bold;
}
This allows far greater control than just parent, for example in this scary chain below the p tag is the target!
article > h1 + section > p! > b > a {
font-style: italic;
}
You can use an attribute selector:
label[for=inputelement]:focus,
label[for=inputelement]:active {
/*styles here*/
}
Note that this isn't supported by IE6, but should work in all other browsers, including IE7 and IE8.
That will obviously only work for that specific ID. If you would like it to work for all IDs, simply leave out the ID:
label[for]:focus,
label[for]:active {
/*styles here*/
}
This will now work for all labels with a for attribute.
If you need something in between, you'll need to use classes.
You can, so long as the label follows the input in the Mark-up:
input:focus + label,
input:active + label {
/* style */
}
Okay the idea is to wrap the input, label, help, error etc. in a Flexbox Container.
Then use the + selector, to select the label element.
Note: it will work only when <label> comes after <input>
Then you define the <label> order by using the flexitem order property.
Sure you can also using classnames.
.container {
display: flex;
flex-direction: column;
}
input {
border: none;
}
label {
order: -1;
}
input:focus {
border: 1px solid red;
}
input:focus + label{
color: red;
}
<div class="container">
<input id="username" />
<label for="username">Username</label>
</div>
Yes, of course you can.
You'll need to:
Group both label and the form into a parent element (like a div)
Style the label with focus pseudo selector selector for the parent, ie .parent:focus label { color: green }
You can see a very minimal sample at jsfiddle I made.
<div class='workarea'>
<div class='hasinput'>
<label>Label 1 (should be green when active)</label>
<input />
</div>
<div class='hasinput'>
<label>Label 2 (should be green when active)</label>
<input />
</div>
</div>
.workarea {
max-width: 500px;
}
label,
input {
width: 100%;
}
.hasinput {
margin-bottom: 1rem;
}
.hasinput label {
color: blue;
}
.hasinput:focus-within label {
color: green;
}
Give your input button a style class
css style:
INPUT.book:hover, INPUT.book:focus:hover {
background-image:url(book_over.png);
background-repeat:no-repeat;
height: 40px;
width: 140px;
font-family:calibri, Tahoma;
font-size:20px;
color:#ffffff;
text-align: center;
font-weight: bold;
}
INPUT.book {
background-image:url(book_active.png);
background-repeat:no-repeat;
height: 40px;
width: 140px;
font-family:calibri, Tahoma;
font-size:20px;
color:#ffffff;
text-align: center;
font-weight: bold;
}
and the input html:
<input name="Bestil2" type="submit" class="book" value="Book møde" />
I haven't figured out yet, how to avoid grey background even though I have a transparent png file, maybe just an jpg will do. But I hope this helps.
Good luck :-)
Here is an alternative usign CSS grid:
As some sugested if the label is after the input then using flex or in my case using CSS grid you can place the label first.
body {
font-family: Arial;
}
.form-field {
display: grid;
gap: 4px;
}
.form-field label {
grid-row: 1;
font-size: 12px;
color: #737373;
}
.form-field input {
outline: unset;
border-radius: 6px;
padding: 6px 10px;
font-size: 14px;
border: 1px solid #737373;
}
.form-field input:focus {
border-color: #328dd2;
}
.form-field input:focus + label {
color: #328dd2;
}
<div class="form-field">
<input id="myinput" />
<label for="myinput">
My Input
</label>
</div>
This can be done if you target browsers that support flexbox - see this: http://plnkr.co/edit/g376cf38iphfvGfSubOz?p=preview
For brevity, the css there is minimal but you'll need some browser specific prefixes to extend support to somewhat older browsers.