Style to specific div class elements containing specific input elements - html

I have a below set of input text elements in my page. I actually need to apply style to div element of "forms_in_ap" class containing the #email, #reEmail, #nogInFirstName, #nogInAccNumber elements alone, in Safari browser of all MAC and IOS devices.
CSS to apply style to specific elements of specific div:
html[xmlns*=""]:root
.form_input_wrap input#email,
.form_input_wrap input#reEmail,
.form_input_wrap input#nogInFirstName,
.form_input_wrap input#nogInAccNumber
{
height: 42px;
}
HTML Code:
<div class="asd removeFocus">
<div class="forms_in_ap removeFocus">
<label for="email">Email address</label>
<div class="removeFocus">
<input type="text" id="email" name="email" class="required error ">
<span id="email-error" class="error">Please enter a Valid Email Address.</span>
</div>
</div>
<div class="forms_in_ap removeFocus">
<label for="reEmail">Re-enter email address</label>
<div class="removeFocus">
<input type="text" id="reEmail" name="reEmail" maxlength="64">
</div>
</div>
</div>
<div class="form">
<div class="forms_in_ap">
<label for="nogInFirstName">First Name</label>
<div>
<input type="text" name="txtFName" maxlength="15" id="nogInFirstName">
</div>
</div>
<div class="forms_in_ap">
<label for="nogInLastName">Last Named</label>
<div>
<input type="text" name="txtLName" maxlength="15" id="nogInLastName">
</div>
</div>
<div class="forms_in_ap">
<label for="nogInAccNumber">Coupon Number</label>
<div>
<input type="text" name="shcCreditCardNumber" maxlength="19" id="nogInAccNumber">
</div>
</div>
<div class=" forms_in_ap">
<div class="ccvDiv">
<label for="cvv"> pin</label>
<div>
<input type="text" class="cvvWidth required" name="cvv" id="cvv" maxlength="3">
</div>
</div>
</div>
</div>
The above CSS works fine but not sure whether this is a correct, standard or optimize code please suggest me.

Since you have each specific input with an HTML ID, there is no need for you to specify the parent class.
This should work for you, it's cleaner and simpler:
input#email,
input#reEmail,
input#nogInFirstName,
input#nogInAccNumber
{
height: 42px;
}
The most important thing to remember here is that IDs are unique. You can't have two elements with the same ID, so adding the parent element when styling is not necessary.
Note about the "input": Since the IDs are unique, there's no need to have the "input" before the ID, but it can be seen as good practice for elements such as inputs to have the selector prior to the class/id, making it clearer what you're styling.
However, this doesn't apply to things like divs, since almost everything on the web is a div, adding it before a class/id is just overkill.

Related

Transforming a form to a read-only layout

I'm trying to create the layout for a profile page.
When the user is editing his profile, I want to display a form such as this one from the Bootstrap library: https://getbootstrap.com/docs/4.4/components/forms/#form-row . When the user is done editing, I want to convert the form to a read-only layout, by replacing the input fields with the text that has been entered.
I found an example on bootsnipp: https://bootsnipp.com/snippets/QoM7g (tab "Education&Career", for example). But when looking at the code, I noticed that this layout was constructed using the form-group class, and by replacing the input field with a paragraph field.
Is there a cleaner solution? I can't imagine this is how this kind of layout is meant to be written?
I would do something like this.
Add a fieldset with disabled attribute, all form element will be disabled.
Now add style to default form-control elements so that it will look like text.
Example below
fieldset[disabled] .form-control {
color:red;
border: none;
background-color:inherit;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="p-3">
<form>
<fieldset disabled>
<input class="form-control" type='text' value='John' />
<input class="form-control" type='text' value='John' />
<input class="form-control" type='text' value='John' />
<input class="form-control" type='text' value='John' />
</fieldset>
</form>
</div>
If you want to have elements in your form styled as plain text, use the .form-control-plaintext class to remove the default form field styling and preserve the correct margin and padding.
for Example:
<form>
<div class="form-group row">
<label for="staticEmail" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="text" readonly class="form-control-plaintext" id="staticEmail" value="email#example.com">
</div>
</div>
<div class="form-group row">
<label for="inputPassword" class="col-sm-2 col-form-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="inputPassword">
</div>
</div>
</form>
Refer to this: https://getbootstrap.com/docs/4.4/components/forms/#readonly-plain-text
There is no need to replace the fields. You can just style them to look as text: hide the border and outline, make it read-only, make the background same as the outer background.
Example:
<input value="Regular input" />
<br>
<input class="like-text" value="I look like a text" readonly />
.like-text {
border: none;
outline: none;
}
https://jsfiddle.net/z879pbcn/

How do I align the endpoints of my input-fields in form, using css?

I am making a copy of a pen-and-paper character sheet for a RPG, as a way of learning html/css. However I got stuck right at the beginning when trying to style a form, holding some background information about the character.
Currently I've managed to make my form of labels and input-fields to look like the picture to the left. However the pen-and-paper character sheet (and the desired look) is formatted like the one on the right.
Below is the code I'm using.
.sheet-character-background form input,
label {
margin-bottom: 10px;
}
.age-input {
width: 60px;
}
<div class="sheet-character">
<div class="sheet-character-background">
<form>
<label>Name</label>
<input type="text" name="attr_name">
<br>
<label>Race</label>
<input type="text" name="attr_race">
<br>
<label>Gender</label>
<input class="gender-input" type="text" name="attr_gender">
<label>Age</label>
<input class="age-input" type="number" name="attr_age" min="0">
<br>
<label>Religion</label>
<input type="text" name="attr_religion">
<br>
<label>Occupation</label>
<input type="text" name="attr_occupation">
<br>
<label>Archetype</label>
<input type="text" name="attr_archetype">
<br>
<label>Environment</label>
<input type="text" name="attr_environment">
<br>
<label>Background</label>
<input type="text" name="attr_backgrund">
</form>
</div>
</div>
What are the steps for going from what I have to what I want? I played around with surrounding each "row" with a <div> and class and setting their width in css. However this didn't work out so I reverted to my initial version and got stuck.
Many people would probably suggest to get a css framework, but what you want can be done with some simple css.
First, your html basically consists of a form with a series of rows, except for one row where it consists of two fields in one row. So I modified your html slightly that each row is wrapped by a div with a class as .form-row and delete the <br> (let css to do the rendering instead of using html tag):
To achieve what you want will then come down to set a width for the form, and how each row will behave, and set the width of input, and last override the setting for the special case of .age-input.
This is just a 'quick-and-dirty' way to achieve what you want, hopefully it provide you some ideas and suggestions in your learning.
form {
width: 300px;
}
.form-row {
display:flex;
}
input {
width: 100%;
}
.age-input {
width: 60px;
}
<form>
<div class="form-row">
<label>Name</label>
<input type="text" name="attr_name">
</div>
<div class="form-row">
<label>Race</label>
<input type="text" name="attr_race">
</div>
<div class="form-row">
<label>Gender</label>
<input type="text" name="attr_gender">
<label>Age</label>
<input class="age-input" type="number" name="attr_age" min="0">
</div>
<div class="form-row">
<label>Religion</label>
<input type="text" name="attr_religion">
</div>
<div class="form-row">
<label>Occupation</label>
<input type="text" name="attr_occupation">
</div>
<div class="form-row">
<label>Archetype</label>
<input type="text" name="attr_archetype">
</div>
<div class="form-row">
<label>Environment</label>
<input type="text" name="attr_environment">
</div>
<div class="form-row">
<label>Background</label>
<input type="text" name="attr_backgrund">
</div>
</form>

style to specific div class elements containing a specific input elements [duplicate]

This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 6 years ago.
I have a below set of input text elements in my page. I actually need to apply style to div element of "forms_in_ap" class containing the #email, #reEmail, #nogInFirstName, #nogInAccNumber elements alone, in Safari browser of all MAC and IOS devices.
CSS to apply style to specific elements of Specific Div:
html[xmlns*=""]:root
.form_input_wrap input#email,
.form_input_wrap input#reEmail,
.form_input_wrap input#nogInFirstName,
.form_input_wrap input#nogInAccNumber
{
height: 42px;
}
HTML Code:
<div class="asd removeFocus">
<div class="forms_in_ap removeFocus">
<label for="email">Email address</label>
<div class="removeFocus">
<input type="text" id="email" name="email" class="required error ">
<span id="email-error" class="error">Please enter a Valid Email Address.</span>
</div>
</div>
<div class="forms_in_ap removeFocus">
<label for="reEmail">Re-enter email address</label>
<div class="removeFocus">
<input type="text" id="reEmail" name="reEmail" maxlength="64">
</div>
</div>
</div>
<div class="form">
<div class="forms_in_ap">
<label for="nogInFirstName">First Name</label>
<div>
<input type="text" name="txtFName" maxlength="15" id="nogInFirstName">
</div>
</div>
<div class="forms_in_ap">
<label for="nogInLastName">Last Named</label>
<div>
<input type="text" name="txtLName" maxlength="15" id="nogInLastName">
</div>
</div>
<div class="forms_in_ap">
<label for="nogInAccNumber">Coupon Number</label>
<div>
<input type="text" name="shcCreditCardNumber" maxlength="19" id="nogInAccNumber">
</div>
</div>
<div class=" forms_in_ap">
<div class="ccvDiv">
<label for="cvv"> pin</label>
<div>
<input type="text" class="cvvWidth required" name="cvv" id="cvv" maxlength="3">
</div>
</div>
</div>
</div>
The above css works fine but not sure whether this is a correct, standard or optimize code please suggest me.
If you have access to the HTML you can simply add a new class to the divs that contain the input fields and that need to be modified. For example "modify-this", then style that class accordingly.
If your HTML is dynamic and might change, or if you can't modify the HTML directly for some reason, the second easiest way to achieve this is using some jQuery to add a class to the elements you want to modify, you can achieve this by using the .parent() function, like so:
$(document).ready(function () {
$('#email').parent('.forms_in_ap').addClass('modify-this');
$('#reEmail').parent('.forms_in_ap').addClass('modify-this');
$('#nogInFirstName').parent('.forms_in_ap').addClass('modify-this');
$('#nogInAccNumber').parent('.forms_in_ap').addClass('modify-this');
});
This will add the "modify-this" class to the divs that contain the 4 inputs with the IDs specified above. You can then style that class as normal.
Note that this works because each input is inside the div that you need to modify, meaning that the div is the parent of the input element. By entering the class "forms_in_ap" into the parent() function, we tell jquery to find the parent of the input that contains that class.

Bootstrap form-horizontal vertical alignment of checkboxes without label text

I have changed from Bootstrap 3.0.0 to 3.2.0 this morning because I needed some of the new features for my web application. Everything seemed to work as expected until I observed an issue with the vertical alignment of checkboxes in a .form-horizontal form.
An example is available at http://www.bootply.com/AYN64feYze. The markup for this minimum example is:
<div class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label">With label text</label>
<div class="col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox"> label text
</label>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Without label text</label>
<div class="col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox">
</label>
</div>
</div>
</div>
</div>
If a checkbox has no following text it is shifted below the row it should appear in.
Is there a solution to this problem? Since I already have the leading label I do not need a following text for my checkboxes. My current workaround is adding text to the <label> that contains the <input type="checkbox"> and use the background color as the font color to hide the text.
Thank you for your help.
I'm not sure if this will affect the rest of your form layout, but the issue seems to be resolved if you change the display attribute of <label> (currently set to inline-block) to:
label{
display:inline;
}
Here's an updated Bootply. Hope this helps! Let me know if you have any questions.
This worked for me:
<div class="form-group">
<div class="col-lg-3">
<label class="pull-right" for="MyCheckBox">My Checkbox</label>
</div>
<div class="col-lg-9">
<input type="checkbox" name="MyCheckBox">
</div>
</div>
First, I had to remove the <div class='checkbox'> element. I then made the following changes to the checkbox label element:
Place the label in its own column div <div class="col-lg-3"></div>
Remove class="control-label"
Add class="pull-right".
I ended up with a checkbox that aligned with the other inputs horizontally and with its label vertically.
If you don't need following text for the checkboxes, why not just remove the <label> surrounding the checkboxes. Like so.
<div class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label" for="check1">With label text</label>
<div class="col-sm-10">
<div class="checkbox">
<input type="checkbox" id="check1">
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="check2">Without label text</label>
<div class="col-sm-10">
<div class="checkbox">
<input type="checkbox" id="check2">
</div>
</div>
</div>
</div>
This code appeared to work in your Bootply when I tried it.
And remember if you have a label to use the for attribute for screen readers and to make it easier for your users (they can just click the label instead of the checkbox).

nested element inheriting parent Div's margin in ie7

Trying to figure out why in IE7, the nested input element is taking on the margin of the parent div. So in essense the margin is being doubled.
<style>
<!--
h1.redsubhead{font-size:14px;}
.accountInfo,.loginInfo{-moz-border-radius: 6px 6px 6px 6px;background: #EBFBFF; border: 1px solid #8DCAD9;margin-bottom: 30px;padding:10px;}
#ai_pw_wrap,#li_pw_wrap{border:1px solid #f0f;margin-right: 30px;padding:0;}
#ai_email_wrap{margin-right:30px;padding:0;}
.ai_wrap,.li_wrap{float:left;}
.ai_email_input, .li_email_input,.li_pw_input{width:170px;}
.ai_pw_input{width:130px;}
.ai_label,.li_label{font-size: 11px; font-weight: bold;}
.ai_link,.li_link{font-size: 9px; float:right}
h1.redsubhead{float:left;}
#li_btn_wrap{margin-top:10px;float:right;}
.ai_wrap input{margin:0 !important;}
.ai_label{margin:0}
-->
</style>
<div class="accountInfo">
<h1 class="redsubhead">Account Info</h1>
<a class="ai_link" href="#">Returning Member Login</a>
<div class="clear"></div>
<div id="ai_email_wrap" class="ai_wrap">
<label for="edit-payment-new-card-cc-cardholder" class="ai_label">E-mail: </label><br>
<input type="text" class="ai_email_input" value="John Doe" size="60" maxlength="128">
</div>
<div id="ai_pw_wrap" class="ai_wrap">
<label for="edit-payment-new-card-cc-cardholder" class="ai_label">Password: </label><br>
<input type="text" class="ai_pw_input" value="John Doe" size="60" maxlength="128">
</div>
<div id="ai_pwc_wrap" class="ai_wrap">
<label for="edit-payment-new-card-cc-cardholder" class="ai_label">Password Confirm: </label><br>
<input type="text" class="ai_pw_input" value="John Doe" size="60" maxlength="128">
</div>
<div class="clear"></div>
</div>
<div class="loginInfo hide">
<h1 class="redsubhead">Login</h1>
<a class="ai_link" href="#">New User Signup</a>
<div class="clear"></div>
<div id="li_email_wrap" class="li_wrap">
<label for="edit-payment-new-card-cc-cardholder" class="li_label">E-mail: </label><br>
<input type="text" class="li_email_input" value="John Doe" size="60" maxlength="128">
</div>
<div id="li_pw_wrap" class="li_wrap">
<label for="edit-payment-new-card-cc-cardholder" class="li_label">Password: </label><br>
<input type="text" class="li_pw_input" value="John Doe" size="60" maxlength="128">
</div>
<div id="li_btn_wrap">
<input type="image" src="/img/checkout/li_login.png" class="li_submit" value="start" name="submit_order">
</div>
<div class="clear"></div>
</div>
Any suggestions?? I did find a fix, if I change the margin-right:30px to padding-right:30px. I still want to know why the margin on the nested input is taking on the margin of the parent div.
You're looking at a known bug in IE7 (and, I think, 6) called the "inherited margin bug".
The first child input of an element with the hasLayout property triggered (for example via zoom:1, a clearfix, setting a height or width) will inherit the sum of the left margins of its ancestors.
The workarounds (from this article) are:
(Most common) Set a negative margin-left on the input. That will counteract the inherited margins. Use something like input { *margin-left: -30px; } to only target IE7 and below.
Don't use margins on the ancestors of the input.
Un-do whatever triggered hasLayout on the parent element of the input.
Put an inline element (say, a <label>) and some text immediately before the input.
Wrap the input in another inline element (<span>, <label>, whatever).
Can you try applying a display: inline rule to your floated element.
.ai_wrap,.li_wrap{float:left; display:inline;}
and check again?