it is a simple problem but i can't get through it, i am trying to highlight an input box when its focused, but all the div as i styled it differently to add icons inside of it.
here is my code:
input:focus .form-input {
border: 2px solid var(--theme-color);
}
<form>
<div class="form-input">
<i class="fa fa-user"></i>
<input type="text" id="username" name="username" placeholder="Enter Your Username"><br>
</div>
<div class="form-input">
<i class="fa fa-user"></i>
<input type="password" id="password" name="password" placeholder="Enter Your Password">
</div>
</form>
As soon as we have widespread browser support for :has(), a solution would look like this:
.form-input:has(input:focus) {
border: 2px solid orange;
}
<div class="form-input">
<input type="text" id="username" name="username" placeholder="Enter Your Username" />
</div>
Currently this is only supported in Safari 15.4+ though, but it will soon come to all major browsers.
Until we have that, you can still work with the well-supported :focus-within pseudo class:
.form-input:focus-within {
border: 2px solid orange;
}
<div class="form-input">
<input type="text" id="username" name="username" placeholder="Enter Your Username" />
</div>
Please note that this solution comes with the drawback of triggering on any focusable element within div.form-control. You cannot limit it to certain elements.
The element you are trying to style (div.form-input) is the parent of that input. As far as I know there's currently no way for selecting parent elements based on child state in CSS (or at least not supported by all browsers).
You'd have to rely on js for that:
function highlight(evt) {
// get parent with class .form-input
var parent = evt.target.closest('.form-input');
if (parent) {
if (evt.type === 'focus') {
// add highlight class on focus
parent.classList.add('highlight');
} else {
// remove highlight class on blur
parent.classList.remove('highlight');
}
}
}
// get all inputs of the form
document.querySelectorAll('form input')
.forEach((input) => {
// attach focus and blur events to toggle highlight class
input.addEventListener('focus', highlight, true);
input.addEventListener('blur', highlight, true);
});
.form-input {
display: inline-block;
padding: 5px;
border: 2px solid rgba(0,0,0,.1);
}
.form-input.highlight {
border: 2px solid #000;
}
<form>
<div class="form-input">
<i class="fa fa-user"></i>
<input
type="text"
id="username"
name="username"
placeholder="Enter Your Username"
/><br />
</div>
<div class="form-input">
<i class="fa fa-user"></i>
<input
type="password"
id="password"
name="password"
placeholder="Enter Your Password"
/>
</div>
</form>
Related
I'm new to HTML and I have some questions regarding input tags.
Here I have two inputs seperated by a span tag(-). What I want to do is to wrap two inputs like a single input like the image below.
Do I need to add styles using css or change the whole structure of the code?
<div class="form-group">
<label for="form-realname" class="col">test*</label>
<div class="input-group col">
<input type="text" class="form-control" placeholder="" />
<span class="input-group-text" >-</span>
<input type="text" class="form-control" placeholder="" />
</div>
ideal structure
Here is easy solution:
Set border for inputs to 0 to make them invisible.
Add border 1px with color you need to parent div.
Don't forget to make this parent div display: inline-block;
* {
margin: 0;
padding: 0;
}
input.form-control {
border: 0;
padding: 2px;
}
.input-group {
border: 1px solid gray;
display: inline-block;
}
<div class="form-group">
<label for="form-realname" class="col">test*</label>
<div class="input-group col">
<input type="text" class="form-control" placeholder="" />
<span class="input-group-text" >-</span>
<input type="text" class="form-control" placeholder="" />
</div>
</div>
I have this code in HTML:
<div class="firstname-input">
<input type="text" id="firstname" name="firstname" placeholder="First name">
<button onclick="showName()"><figure></figure></button>
</div>
I want to add border: 2px solid #f90; to .firstname-input, at focus the input
And if I write this:
.firstname-input input:focus .firstname-input {
opacity: .1;
}
This isn't working.
Use
.firstname-input:focus-within {}
and add your chosen styles
<div class='ur-form-row'>
<div class="ur-form-grid ur-grid-1"
style="width:99%">
<div class="ur-field-item field-user_login">
<p class="form-row validate-required"
id="user_login_field" data-priority=""><label
for="user_login">Username <abbr class="required"
title="required">*</abbr></label><input data-rules=""
data-id="user_login" type="text" class="input-text input-
text ur-frontend-field " name="user_login" id="user_login"
placeholder="" value="" required="required" data-
label="Username" /></p>
</div>
I want to apply css border-radius to this class but it doesn't work
Tried all the options above such as:
.ur-form-row {border-radius: 20px;}
give border and border-radius to input-text class
.input-text
{
border: 1px solid;
border-radius: 26px;
}
<div class='ur-form-row'>
<div class="ur-form-grid ur-grid-1"
style="width:99%">
<div class="ur-field-item field-user_login">
<p class="form-row validate-required"
id="user_login_field" data-priority=""><label
for="user_login">Username <abbr class="required"
title="required">*</abbr></label><input data-rules=""
data-id="user_login" type="text" class="input-text input-
text ur-frontend-field " name="user_login" id="user_login"
placeholder="" value="" required="required" data-
label="Username" /></p>
</div>
here it is. just add this CSS to end of your style and add "makeRadius" to your input class.
.makeRadius {
border-radius: 8px 8px 8px 8px;
}
<input data-rules=""
data-id="user_login" type="text" class="input-text input-
text ur-frontend-field makeRadius" name="user_login" id="user_login"
placeholder="" value="" required="required" data-
label="Username" />
Try adding border before radius to the element
.ur-form-row input {
border:1px solid black;
border-radius: 20px;
}[it works fine check the image](https://i.stack.imgur.com/qrWq4.png)
is there any way to achieve something like this:
I've tried options that I found here in stack (with using span) but even if i made it work it is impossible for me to do that white line on the right from icon, can someone help me?
My current code looks like that:
<form>
<input type="text" name="firstname" placeholder="Name">
<input type="text" name="firstname" placeholder="e-mail">
<input type="text" name="firstname" placeholder="website">
</form>
I've also tried adding that icon in container with borders to achieve effect:
<form>
<span class="form_frame"><i class="fa fa-user-circle-o" aria-hidden="true"></i>
</span>
<input type="text" name="firstname" placeholder="Name">
And css:
.form_frame {
height: 60px;
border-width: 1px solid;
border-color: white;
width: 100%;
background-color: #2b2b2b;
But no succes
Thank's for any help.
Add the icon and the input inside a span. You can then add a border to the whole span if you want to our you can put a border on just the icon or input box.
.fa-address-book-o {
border: solid;
border-color: red;
}
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<span><i class="fa fa-address-book-o" aria-hidden="true"></i><input type="text"/></span>
You can use Labels
I thinks this is a good start.
.wr{border:2px solid black;padding:12px;}
<form>
<diV class="wr">
<label for="user"> <i class="fa fa-camera-retro">(icon_img)</i></label><input type="text" name="user" placeholder="Username"/> </diV>
<div class="wr">
<label for="pass"> <i class="fa fa-futbol-o">(icon_img)</i></label><input type="password" name="password" placeholder="Password"/> </div>
</form>
Trying to change the border color for error message. this is my html code
<div class="form-group">
<label>Name:</label>
<div class="wpr">
<div class="wpr__icon">
<i class="fa fa-user"></i>
</div>
<input #name="ngModel" id="name" name="name" type="text" class="form-control text-line" [(ngModel)]="PersonInfo.name"
pattern="[a-zA-Z0-9\s]+" required>
</div>
<ul class="alert-error" *ngIf="name.touched && name.errors">
<li *ngIf="name.errors.required"> Name is required. </li>
<li *ngIf="name.errors.pattern">Invalid name.</li>
</ul>
</div>
Currently error messages are showing up, but I want to change the textbox border-color to red. How to do that.
Here is another solution.
input.ng-invalid.ng-touched {
border: 1px solid red;
}
If you inspect your input field, you can see some css classes that Angular dynamically attach to your element that you can take advantage of.
You can use ngClass directive to add css class to your input field when it is invalid:
<input #name="ngModel" id="name" name="name" type="text" class="form-control text-line"
[ngClass]="{'red-border-class': name.errors}" [(ngModel)]="PersonInfo.name" pattern="[a-zA-Z0-9\s]+" required>
Hope you don't need help writing css. :-)
Just find .alert-error class in css file and add border property.
.alert-error{
...
border:1px solid red;
color:red;
}
We can achieve different ways, but I personally preferred the following way.
HTML
<form [ngClass]="{ 'form-submit': isSubmit}" (ngSubmit)="onSubmit()" name="forgotPasswordForm" [formGroup]="forgotPasswordForm">
<input name="email" type="email" class="form-control" id="email" placeholder="Email" formControlName="email">
<div class="invalid-feedback form-error" *ngIf="...">
.......
</div>
</form>
CSS:
.form-group input.ng-invalid.ng-touched,
.form-group input.ng-invalid:focus,
.form-group select.ng-invalid.ng-touched,
.form-group textarea.ng-invalid.ng-touched,
.form-submit input.ng-invalid,
.form-submit select.ng-invalid,
.form-submit textarea.ng-invalid
{
border-color: #ff4c6a;
}
.invalid-feedback.form-error {
display: block;
}