Two parents with the same child css - css

I am surprised this has not come up before. Often times when I am styling my website, I have two parents with the same type of child, say:
form#login-form
and
form#reset-form
And in each of these parents, styling the child:
input[type="submit"]
I would have to do:
form#login-form input[type="submit"], form#reset-form input[type="submit"] {
width: 14.75em;
text-align: center;
font-family: 'Orbitron', sans-serif;
border: none;
}
Surely, there is an easier way to do this in CSS? If not, I have code such as
.sidebar-outer + *:not(:empty), .sidebar-outer + *:empty + *
Where .sidebar-outer is a parent with two children.

Just add a common class to all the input fields
example -
<form id = "login-form">
<input class="common-class" type="submit"/>
</form>
<form id = "reset-form">
<input class="common-class" type="submit"/>
</form>
And under the css class you can have the below -
.common-class{
width: 14.75em;
text-align: center;
font-family: 'Orbitron', sans-serif;
border: none;
}

SASS is like CSS on steroids. Just search for "SASS CSS". That said, the best practice way to use it is to start using NPM+Webpack ... worth the learning effort. With SASS you can do something like:
#mixin myinputbuttonstyles {
width: 14.75em;
text-align: center;
font-family: 'Orbitron', sans-serif;
border: none;
}
form#login-form, form#reset-form {
input[type="submit"] {
#include myinputbuttonstyles;
}
}
This sample shows 2 features of SASS: nesting and mixins.

Related

Change label color when textarea has value

I need to change color of label when textarea receiving some value.
<form action="#" class="form-reverse">
<textarea name="order-background__bussiness" id="order-background__bussiness" cols="30" rows="10"></textarea>
<label for="order-background__bussiness">What are the company’s objectives?</label>
</form>
When we focusing textarea it works fine with this code:
textarea:focus ~ label{
color: #55c57a;
}
But, I need this color: color: #ff8086; when we don't have any values, and green one(as on image above) when anything written on textarea.
I've tried :active , but it works only when Mouse clicked:
textarea:active ~ label{
color: #ff8086;
}
Maybe someone has a solution for this?
PS: I do have a solution for this with JS , but I'm curious if there is any solution with SASS as well?
You can use the css valid property, it will match if the textarea is a valid field you can set the required attribute and it will match the valid selector if valid...
https://www.w3schools.com/cssref/sel_valid.asp
textarea:valid + label{
background: #ff0000;
}
<textarea required="required"></textarea><label>label</label>
You can also try like this, this will work fine as above:
textarea:not(:invalid) + label{
background: #ff0000;
}
One further option, that avoids making the <textarea>, and other form elements, required is to use the :placeholder-shown pseudo-class; this does, of course, require that a placeholder attribute be set (although it can be set to a whitespace, or zero-length, string):
/* selects a <label> element immediately adjacent to
an element which has its placeholder string visible
to the user: */
:placeholder-shown+label {
color: #f90;
}
/* this selects all <label> elements, but is less specific
than the selector above; so will be 'overridden' in the
event that the previous selector matches: */
label {
color: limegreen;
font-size: 1.5em;
}
*,
::before,
::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-size: 1rem;
}
.form-reverse {
display: flex;
flex-direction: column-reverse;
width: 80vw;
margin: 0 auto;
}
textarea {
width: 100%;
min-height: 30vh;
}
:placeholder-shown+label {
color: #f90;
}
label {
color: limegreen;
font-size: 1.5em;
}
<form action="#" class="form-reverse">
<textarea name="order-background__bussiness" id="order-background__bussiness" placeholder=" "></textarea>
<label for="order-background__bussiness">What are the company’s objectives?</label>
</form>
JS Fiddle demo.
References:
:placeholder-shown (Selectors Level 4 spec).

How to change size of font in angular materiel form fields of floating place holder

Below is the form field of angular material.
How can I add 2 different customize font-size for placeholder when it's normal and when it floats.
font-size : 20px; (When it is normal)
font-size : 13px; (When it floats up and get smaller)
<mat-form-field class="form-group example-full-width">
<input matInput id="Fname" placeholder="First" class=" " formControlName="FirstName">
<mat-error *ngIf="firstName.invalid" class="">First name is required</mat-error>
</mat-form-field>
The floating text is a label that can be targeted with css. Something like this may work.
mat-form-field label {
font-size: 20px;
}
And when the label is floated
mat-form-field.mat-form-field-should-float label {
font-size: 13px;
}
The previous answer no longer works, here is how I did it :
.mat-form-field-can-float.mat-form-field-should-float .mat-form-field-label,
.mat-form-field-can-float .mat-input-server:focus + .mat-form-field-label-wrapper .mat-form-field-label {
transform: translateY(-1.34375em) scale(1) !important; /* originally scale(0.75) */
}
This solution works for me, you could try this:
HTML
<div>
<mat-form-field>
<mat-label>Email Address</mat-label>
<input matInput placeholder="Ex. example#example.com">
</mat-form-field>
</div>
SCSS
.mat-form-field{
font-size: 17px;
width: 280px;
&.mat-focused{
font-size: 20px;
}
.mat-input-element{
width: 100%;
font-size: 14px;
}
&::placeholder{
font-size: 14px;
}
}
.mat-form-field-should-float {
font-size: 20px;
}
Official Angular Material documentation for Form Field (since v5) has a recommendation for changing the font size as it follows:
mat-form-field inherits its font-size from its parent element. This can be overridden to an explicit size using CSS. We recommend a specificity of at least 1 element + 1 class.
mat-form-field.mat-form-field {
font-size: 16px;
}
The same way may be changed label in form field:
mat-label.mat-label {
font-size: 16px;
}
Nevertheless, if you want to keep your styles more readable, you can use simple css class to apply new font properties:
.html
`<mat-form-field class="new-form-theming">..`.
.css
.new-form-theming {
font-size: 24px;
line-height: 28px;
font-weight: 500;
}

Override global type-selector class

My application has different form pages. Most of text box elements on those forms share the same global css rule such as width property (150px), font-family, font-size, and so on. So, I make a global css type selector class for most of those text boxes. However, there some text boxes on those forms need another value of the width property (smaller or wider). How can I override such global type for particular text boxes?
The following is my global type-selector class for most of form text boxes:
input[type=text]{
width: 150px;
font-family: verdana,
font-size: 12px;
...
}
Thank you in advance.
Create a class for those particular textboxes, and apply styles to that class:
<input type="text" class="short" />
input[type=text]
{
width: 150px;
font-family: verdana,
font-size: 12px;
...
}
input[type=text].short
{
width: 90px;
...
}
This textbox will then use the following styles:
width: 90px;
font-family: verdana,
font-size: 12px;
Alternatively if an entire form needs these different styles, set the selector for a parent element:
<div class="shortform">
<input type="text" />
</div>
input[type=text]
{
width: 150px;
font-family: verdana,
font-size: 12px;
...
}
.shortform input[type=text]
{
width: 90px;
...
}

Styling email link / href="mailto:" with CSS

Thanks to StackOverflow I finally found a way to style my email link, but I wonder why it doesn't work without the solution I found on here.
Since the link is part of the span with the attributed class "about", which has font size and style defined, shouldn't the email link show up in 11px and sans serif?
and while
a[href^="mailto:"]
{
font-family: sans-serif;
color: black;
font-size: 11px;
}
works great, as soon as i try to change it into
.about a[href^="mailto:"]
{
font-family: sans-serif;
color: black;
font-size: 11px;
}
it does not function as it's supposed too.
do tags not listen to span formatting or class nesting?
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
html {
height:100%;
}
body {
height: 100%;
margin-left: 20px;
margin-top:0px;
}
.bottom-left {
position: absolute;
font:sans-serif;
bottom: 15px;
left: 15px;
}
.bold {
font-family: serif;
}
.about {
font-size: 11px;
font-family: sans-serif;
}
/*a[href^="mailto:"]
{
font-family: sans-serif;
color: black;
font-size: 11px;
}*/
.address {
font-size: 11px;
border-bottom: 1px grey dotted;
}
</style>
<title>TEMP</title>
</head>
<body>
<div class="bottom-left">
<span class="about">
<span class="bold">XYZ</span> is a project space . |
<span="address">Website Information</span> — info#info.eu
</span>
</div>
</body>
</html>
Hi actually you have commented your email link css:-
so now write the css like this method its working fine......
a[href^="mailto:"]
{
font-family: sans-serif;
color: red;
font-size: 11px;
}
see the demo:- http://jsbin.com/ijofoq/edit#html,live
UPDATED
Now its working fine...edit your HTML and add in your HTML
<div class="bottom-left">
<div class="about">
<span class="bold">XYZ</span> is a project space . |
<span="address">Website Information</span> — info#info.eu
</div>
basically you have to remove the span tag from .about class.
check this :- http://jsbin.com/ijofoq/2/edit
I think .about take precedence over a.
cf. Css Rule Specificity.
Basically, a css ruleset is assign a priority like a version number like this:
{#id}.{#class}.{#element}.{order}
with
{#id} : count of id selectors
{#class} : count of classes, pseudo-classes, attributes
{#element} : count of elements, pseudo-elements
{order} : the index of this rule across all files
So, we have the following order:
0.2.1.* .about a[href^="mailto:"] (0 id, 1 class + 1 attr, 1 element)
0.1.1.a span.about (0 id, 1 class, 1 element)
0.1.1.b a[href^="mailto:"] (0 id, 1 attr, 1 element)
0.1.0.* .about (0 id, 1 class, 0 element)
span.about and a[href^="mailto:"] have same specifity (1 class or attribute, and 1 element), so the order is important, the last wins.
If you remove the span then the rule is less specific and loose.
(Also, distinguish between rules directly applied to an element, and other inhertited from parent elements...)

Am I subclassing my CSS correctly?

I am making a set of buttons for my site, and I am in need of some professional insight.
In order to reduce CSS bloat, I want to subclass my buttons for different colors, ex .button.blue .
Will the following incur issues in the future? (assuming I don't make a class of just .blue)
Do I have to use something like .button.button-blue instead?
.button {
display:inline-block;
padding: 9px 18px;
margin: 20px;
text-decoration: none;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
font-weight: bold;
text-align: center;
background: #FFE150;
}
.button.blue {
background: #49b8e7;
border:1px solid #54abcf;
border-bottom:1px solid #398fb4;
color:#FFF
text-shadow: 0 1px 0 rgba(255,255,255, 0.5);
}
.header{
height: 50px;
}
.header.blue {
background: blue;
color: #fff;
}
What you have there with the multi-classes will work fine assuming you want them to work like so:
<div class="button blue">
Will use .button and .button.blue
</div>
<div class="button">
Will only use .button
</div>
<div class="header blue">
Will use .header and .header.blue
</div>
<div class="header">
Will only use .header
</div>
<div class="blue">
Will use neither of the .blue declarations because it doesn't contain header or button.
</div>
A selector like .button.blue actually selects for an element with that has both "blue" and "button" as classes, not a class called .button.blue. See http://www.w3.org/TR/CSS21/selector.html#class-html.
You can use the .button.blue style rule you have listed, but you'll need to rearrange your HTML so that you have something like <button type="button" class="button blue"/>. However, you don't really need to have a button class since it being a button (or <input type="submit">, etc.) is enough to use in your selector. You could write a CSS rule that is simply button.blue, input[type=submit].blue{}
Seems like button.blue is enough.
The only difference between the two is if you use <button class="button blue">, or <button class="button button-blue">.
You even don't need to duplicate the painting in blue... You can just do something like this:
.button
{
// button style
}
.header
{
// header style
}
.blue
{
background: blue;
color: #fff;
}
Of course if you add the blue class to each of them. (<div class="header blue">and<button class="button blue">)
Combine the classes applying the color you want to theme.
HTML:
<input type="text" class="text-field-required default" .../>
<select class="autocomplete-drop-down blue">...</select>
<a href="#" class="button-link green" .../>
CSS:
.text-field-required {
//component css theme without colors
}
.default {
//default color css theme for any component
}
.blue {
//blue css theme for any component
}
.green {
//green css theme for any component
}

Resources