Align Error Message Under Input - HTML/CSS - html

I have the following images that represent each case for user input-validation.
No Error:
Input Error:
I would like to maintain the alignment of the upper image while moving the error message underneath the input field (keep Label on left and Input on right, place error message underneath and aligned with input).
.group {
display: flex;
flex-wrap: nowrap;
margin-top: 4px;
}
.group label {
display: flex;
width: 15%;
line-height: 30px;
justify-content: center;
align-items: center;
}
.group input[type="text"] {
width: 85%;
border: 1px solid rgb(116, 115, 115);
padding-left: 3px;
}
.group textarea:focus,
input:focus {
outline: none;
}
.group .errorMsg {
color: #cc0033;
display: inline-block;
font-size: 12px;
line-height: 15px;
margin: 5px 0 0;
width: 100%;
}
.group .errorMsg {
display: none;
}
/* Error Styling */
.err label {
color: #cc0033;
}
.err input[type=text] {
background-color: #fce4e4;
border: 1px solid #cc0033;
outline: none;
}
.err .errorMsg {
display: inline-block;
}
<div class="group err">
<label for="time">Time</label>
<input type="text" name="time" id="time" />
<div class="errorMsg">Invalid Time Format</div>
</div>
<div class="group">
<label for="time2">Time2</label>
<input type="text" name="time2" id="time2" />
<div class="errorMsg">Invalid Time Format</div>
</div>
Any recommendations to fix the poor alignment?

You can align them like this. I have added comments to the right of the changes.
.group {
display: flex;
flex-wrap: wrap; /* Wrap the elements */
margin-top: 4px;
}
.group label {
display: flex;
width: 15%;
line-height: 30px;
justify-content: center;
align-items: center;
}
.group input[type="text"] {
width: 85%;
border: 1px solid rgb(116, 115, 115);
padding-left: 3px;
flex: 1; /* Take the remaining width */
}
.group textarea:focus,
input:focus {
outline: none;
}
.group .errorMsg {
color: #cc0033;
display: inline-block;
font-size: 12px;
line-height: 15px;
margin: 5px 0 0;
width: 100%;
}
.group .errorMsg {
display: none;
}
/* Error Styling */
.err label {
color: #cc0033;
}
.err input[type=text] {
background-color: #fce4e4;
border: 1px solid #cc0033;
outline: none;
}
.err .errorMsg {
display: flex; /* Instead of inline-block */
width: 85%; /* Same as input width */
left: 15%; /* Rest of the width */
position: relative; /* Relative to parent */
}
<div class="group err">
<label for="time">Time</label>
<input type="text" name="time" id="time" />
<div class="errorMsg">Invalid Time Format</div>
</div>
<div class="group">
<label for="time2">Time2</label>
<input type="text" name="time2" id="time2" />
<div class="errorMsg">Invalid Time Format</div>
</div>

Related

Div with formcontrol validation error message isn't showing after changes in CSS via Flexbox

I added a div with a button next to the password input fields to show/hide the password text and to position it next to the input field to give the illusion/impression, that it is part of the input field (positioning it in the input field didn't work for me) with flexbox (display: flex, flex-direction: row).
The validation error messages showed up before adding the div and flexbox/css.
I tried putting the input/show-hide-button + the validation error div in one div, with a css class (display: flex, flex-direction: column), but it doesn't work.
Here's the css file:
body-container {
width: 100%;
padding: 4.8% 35%;
background-color: var(--white-color);
margin: 1em 0;
.content-container {
flex-direction: column;
align-items: center;
justify-content: center;
border: 1px solid var(--light-border-light);
border-radius: 16px;
padding: 2.5em 2em 3em 2em;
.logo-container {
.logo {
display: block;
width: 28px;
height: 34px;
margin: 0 auto 7.08%;
}
}
.login-content-container {
.input-field-container {
display: flex;
justify-content: center;
align-items: center;
.login-form {
.login-label {
font-size: medium;
width: 100%;
margin: 1em 0 1.3em 0;
}
.username-container {
margin-bottom: 1.3em;
}
.form-group {
}
}
input {
margin: 0;
padding: 14px 32px 14px 5px;
border-color: darkgray;
}
.username-container {
}
.enter-password-container {
display: flex;
flex-direction: column;
.enterpassword-input {
display: flex;
flex-direction: row;
align-content: center;
.password-input-field {
border-radius: 6px 0px 0px 6px;
}
}
.error-message {
}
.visibility-container {
border-top: 1px solid var(--darkgrey-border-color);
border-right: 1px solid var(--darkgrey-border-color);
border-bottom: 1px solid var(--darkgrey-border-color);
border-radius: 0 6px 6px 0;
.password-visibility {
font-size: 11px;
padding: 0px 0.3em;
border: 1px transparent;
background-color: transparent;
margin: 1.8em 0.2em 1.7em 0.2em;
}
.password-visibility:hover {
curser: pointer;
}
}
}
}
}
HTML file:
<div class="form-group enter-password-container">
<div class="enterpassword-input">
<input id="login-login" class="form-control password-input-field"
[type]="hideEnteredPassword ? 'password' : 'text'" matInput
formControlName="enteredPassword"
[ngClass]="{'is-invalid': f['enteredPassword'].errors, 'is-valid': f['enteredPassword'].valid}"
placeholder="Password"
(click)="onEnterPasswordInput($event)"
[(ngModel)]="enteredPassword"
(ngModelChange)="onEnteredPasswordChange($event)" required/>
<div class="visibility-container">
<button class="password-visibility"
(click)="hideEnteredPassword = !hideEnteredPassword">{{hideEnteredPassword ? 'Show' : 'Hide'}}</button>
</div>
</div>
<div class="error-message" *ngIf="submitted && f['enteredPassword'].errors" class="invalid-feedback">
<div *ngIf="f['enteredPassword'].errors['required']">Password is required</div>
</div>
</div>

responsive html and css code with any devices

I want to render my screen responsive with any kind of device, so my code display very good on a big screen but not the same thing with the young screen so I want my screen to be fitted for all sizes.I spend a lot of time to target it but I can't found any solution my code:
* {
box-sizing: border-box;
padding: 0%;
margin: 0%;
background-color: #dfe3ee;
}
.text {
float: left;
margin-top: 200px;
margin-left: 160px;
font-size: 2rem;
font-family: sans-serif;
color: rgb(100, 5, 5)
}
form {
border: 3px solid #f1f1f1;
width: 30%;
display: flex;
flex-wrap: wrap;
flex-direction: column;
margin-top: 5rem;
margin-right: 250px;
float: right;
}
input[type=text],
input[type=password] {
width: 90%;
padding: 8px 12px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
box-sizing: border-box;
}
button {
background-color: #04AA6D;
color: white;
padding: 8px 10px;
margin: 8px 0;
border: none;
cursor: pointer;
width: 100%;
}
button:hover {
opacity: 0.8;
}
/* Extra style for the cancel button (red) */
/* Center the avatar image inside this container */
.container {
padding: 16px;
}
/* The "Forgot password" text */
span.psw {
float: right;
padding-top: 16px;
}
#media screen and (max-width: 600px) {
span.psw {
display: block;
float: right;
}
.form {
border: 3px solid #f1f1f1;
width: 30%;
display: flex;
}
}
import {Link} from "react-router-dom"; const RegisterPage =() => { return (
<div>
<div className="text">
<h1>Private</h1>
</div>
<form>
<div class="container">
<label for="uname"><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="uname" required />
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required />
<button type="submit">Login</button>
<label>
<input type="checkbox" checked="checked" name="remember" /> Remember me
</label>
</div>
<div className="container" style={{ "backgroundColor": "#f1f1f1"}}>
Forgot password?
<div className="btn">
<button>Create New Account</button>
</div>
</div>
</form>
</div>
) }; export default RegisterPage;
Check this out, I added flexbox for your main div and modified your media query part with flexbox and removed margins. Keep in mind I changed className with class for snipping, for testing in your local you should change them again with className
* {
box-sizing:border-box;
padding: 0%;
margin: 0%;
background-color: #dfe3ee;
}
.mainDiv {
width: 100%;
height: 100vh;
display: flex;
justify-content: space-evenly;
align-items: center;
}
.text{
float: left;
font-size: 2rem;
font-family: sans-serif;
color:rgb(100, 5, 5)
}
form {
border: 3px solid #f1f1f1;
width: 30%;
display: flex;
flex-wrap: wrap;
flex-direction: column;
}
input[type=text], input[type=password] {
width: 90%;
padding: 8px 12px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
box-sizing: border-box;
}
button {
background-color: #04AA6D;
color: white;
padding: 8px 10px;
margin: 8px 0;
border: none;
cursor: pointer;
width: 100%;
}
button:hover {
opacity: 0.8;
}
/* Extra style for the cancel button (red) */
/* Center the avatar image inside this container */
.container {
padding: 16px;
}
/* The "Forgot password" text */
span.psw {
float: right;
padding-top: 16px;
}
#media screen and (max-width: 600px) {
.mainDiv {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.text {
margin-left: 0;
}
span.psw {
display: block;
float: right;
}
form{
border: 3px solid #f1f1f1;
width: 30%;
display: flex;
float: none;
margin-right: 0;
min-width: 250px;
}
}
<div class="mainDiv">
<div class="text" >
<h1>Private</h1>
</div>
<form >
<div class="container">
<label for="uname"><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="uname" required />
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required />
<button type="submit">Login</button>
<label>
<input type="checkbox" checked="checked" name="remember" /> Remember me
</label>
</div>
<div className="container" style={{"backgroundColor":"#f1f1f1"}}>
<a href="" >Forgot password?</a>
<div className="btn" >
<button >Create New Account</button>
</div>
</div>
</form>
</div>

Gap between border and child element with border radius

I'm trying to implement radio buttons which kind of work like segmented controls:
* {
margin: 0;
padding: 0;
}
.container {
background-color: brown;
width: 80vw;
}
.box {
display: flex;
flex-direction: row;
border: 2rem solid skyblue;
border-radius: 999px;
}
label {
flex: 1;
padding: 2rem;
border-radius: 999px;
text-align: center;
}
input {
display: none;
}
input:checked + label {
background-color: skyblue;
}
<div class="container">
<div class="box">
<input type="radio" id="hello" name="test" checked />
<label for="hello">Hello</label>
<input type="radio" id="world" name="test" />
<label for="world">World</label>
</div>
</div>
However, there's an annoying gap of about 1px between the nested label and the parent div:
This issue is similar to this question, but the workarounds suggested don't really work for my use case since I can't change the background color. I'm also curious if this is a browser bug or some kind of anti-aliasing issue.
just add box shadow to the input:check + label
input:checked + label {
background-color: skyblue;
box-shadow: 0px 0px 0 1px skyblue;
}
Link to Jsfiddle
I don't know the reason why you are getting that 1 px gap but you can add "transform: translateX(-1px);" to shift it left by 1px. This can work as a temp solution.
label {
flex: 1;
padding: 2rem;
border-radius: 200px;
text-align: center;
transform: translateX(-1px);
}
Change label border-radius 999px to 35px
label {
flex: 1;
padding: 2rem;
border-radius: 35px;
text-align: center;
}
* {
margin: 0;
padding: 0;
}
.container {
background-color: brown;
width: 80vw;
}
.box {
display: flex;
flex-direction: row;
border: 2rem solid skyblue;
border-radius: 999px;
}
label {
flex: 1;
padding: 2rem;
border-radius: 35px;
text-align: center;
}
input {
display: none;
}
input:checked + label {
background-color: skyblue;
}
<div class="container">
<div class="box">
<input type="radio" id="hello" name="test" checked />
<label for="hello">Hello</label>
<input type="radio" id="world" name="test" />
<label for="world">World</label>
</div>
</div>

Trouble aligning one of the labels to the right

View problem at:
https://jsfiddle.net/xa3u5w8j/5/
The reason why I want my button div to be flex is it has this CSS:
.button {
background-color: aqua;
text-align: center;
width: 150px;
height: 50px;
cursor: pointer;
display: flex;
}
And thus, removing the flex property will cause its text to mis-align.
I want all 3 labels to be aligned to the right, but it somehow is not working for the 3rd label. Any insight can help! Thanks
EDIT: Code
/* Gives the form left/right margin and sets text color */
.course-info {
margin: 0 20px 0 20px;
color: #9E7772;
}
/* Makes sure the form contents are lined up */
.course-info label {
display: inline-block;
width: 540px;
text-align: right;
margin: 20px 0 20px 0;
align-items: top;
}
/* Styles the input and textarea and make sure they line up */
.course-info input {
width: 400px;
background-color: white;
border-bottom: 3px solid #E0DEDE;
}
.course-info textarea {
word-break: break-all;
width: 394px; /* 400px(intended length) - 2*3px(border width) */
height: 100px;
border: 3px solid #E0DEDE;
display: inline-block;
}
/* Makes sure they don't break into diff lines */
.course-title label, .course-title input{
float: none;
display: inline-block;
}
.invitation-section {
display: flex;
justify-content: right;
}
/* Special length and height */
.invitation-link{
max-width: 250px; /* 400px - button width to align on the right side */
max-height: 20px;
}
.button-wrapper{
display: inline-block;
}
.button {
background-color: aliceblue;
width: 150px;
display: flex; /*This is necessary*/
}
<form class="course-info" onSubmit={handleSettingsChange}>
<label>
Course Title:
<input type="text" value="Whatever is here"/>
</label>
<label>
<span>Description:</span>
<textarea type="text">Random text here</textarea>
</label>
<label>
<div class="invitation-section">
<span>Invitation:</span>
<textarea readOnly
class="invitation-link" type="text">
</textarea>
<label class="button-wrapper">
<div class="button">Button</div>
</label>
</div>
</label>
</form>
label {
display: inline-block;
width: 140px;
text-align: right;
}
.button {
background-color: aqua;
text-align: center;
width: 150px;
height: 50px;
cursor: pointer;
display: flex;
}
.invitation-link {
max-width: 250px;
/* 400px - button width to align on the right side */
max-height: 20px;
}
.block {
margin-bottom: 10px;
}
label {
display: inline-block;
width: 140px;
text-align: right;
}
.button {
background-color: aqua;
text-align: center;
width: 150px;
height: 50px;
cursor: pointer;
display: flex;
}
.invitation-link {
max-width: 250px;
/* 400px - button width to align on the right side */
max-height: 20px;
}
.block {
margin-bottom: 10px;
}
.course-info textarea {
word-break: break-all;
width: 394px; /* 400px(intended length) - 2*3px(border width) */
height: 100px;
border: 3px solid #E0DEDE;
display: inline-block;
}
button {
display: inline-block;
margin-left: 10px;
margin-top:-2px;
background-color: aqua;
width: 80px;
height: 30px;
cursor: pointer;
}
<form class="course-info" onSubmit={handleSettingsChange}>
<div class="block">
<label> Course Title:</label>
<input type="text" value="Whatever is here" />
</div>
<div class="block">
<label>Description:</label>
<textarea type="text">Random text here</textarea>
</div>
<div class="block invitation-section">
<label>Invitation:</label>
<textarea readOnly class="invitation-link" type="text">
</textarea>
<button>
Copy
</button>
</div>
</form>

How Can I Make the Clickable Area be Applied to Whole Div, not Just Label?

I have been banging my head against the wall on this one for a while and can't seem to find how to do this. I have a card with two rows (card-body) classes. each row represents a radio button (input) with a corresponding label. When the user clicks the radio button I want the background to change colors. Right now that is working, but the background is only working for the label and not the whole div that contains the card row. I'm looking for a HTML/CSS-only answer, as I think it's probably something simple that I'm doing wrong. Thanks!
CSS
.card {
position: relative;
display: flex;
flex-direction: column;
min-width: 0;
word-wrap: break-word;
background-color: #fff;
background-clip: border-box;
border: 1px solid rgba(0, 0, 0, 0.12);
border-radius: 0.25rem;
}
.card-body {
flex: 1 1 auto;
padding: 1.25rem;
}
.pick input[type="radio"] {
display: none;
/* comment this line to see the radio buttons */
}
.pick label {
display: inline-block;
background-color: red;
}
.pick input[type="radio"]:checked+label {
background-color: blue;
}
.card {
border-style: solid;
border-width: 5px;
}
label {
display: block;
width: 100%;
}
HTML
<div class="card">
<div class="card-body pick" for="i1">
<input type="radio" name="g1" id="i1" value="you" data-target="r65">
<label class="ben" for="i1" id="r1">you</label>
</div>
<div class="card-body pick" for="i2">
<input type="radio" name="g1" id="i2" value="me" data-target="r65">
<label class="ben" for="i2" id="r2">me</label>
</div>
</div>
JSFiddle: https://jsfiddle.net/j3mskfjv/11/
You need to set your labels to display: block with width of 100%, like so:
.card {
position: relative;
display: flex;
flex-direction: column;
min-width: 0;
word-wrap: break-word;
background-color: #fff;
background-clip: border-box;
border: 1px solid rgba(0, 0, 0, 0.12);
border-radius: 0.25rem;
}
.card-body {
flex: 1 1 auto;
}
.pick input[type="radio"] {
display: none;
/* comment this line to see the radio buttons */
}
.pick label {
display: inline-block;
background-color: red;
}
.pick input[type="radio"]:checked+label {
background-color: blue;
}
.card {
border-style: solid;
border-width: 5px;
}
.pick {
display: flex;
}
label {
display: block;
flex: 1;
padding: 1.25rem;
}
<div class="card">
<div class="card-body pick" for="i1">
<input type="radio" name="g1" id="i1" value="you" data-target="r65">
<label class="ben" for="i1" id="r1">you</label>
</div>
<div class="card-body pick" for="i2">
<input type="radio" name="g1" id="i2" value="me" data-target="r65">
<label class="ben" for="i2" id="r2">me</label>
</div>
</div>