Set the same value to multiple properties (CSS) - css

Is there a way to set multiple CSS properties to one value?
border-left, border-right: 1px solid #E2E2E2;
The way that you can do with selectors?
.wrapper, .maindiv { ... }

Nope. But for your example, you can do this:
border: solid #E2E2E2;
border-width: 0 1px;
The attributes where there can be separate values for top, right, bottom, and left (eg, border-*, margin, padding) can usually be combined in a single attribute.

Not possible, unless you do:
border: 1px solid #E2E2E2;
..which sets the same border on all sides. If you want to control them individually, you have to write them as separate statements.
Note that in some cases, you can set multiple values for one attribute, but you can not have multiple attributes with one value in one statement.

Not possible with plain css, but you may have a look at scss or less who might have solutions for your problem.
A solution with plain css is the following:
border: 1px solid #E2E2E2;
border-width: 0px 1px;

If you're using sass, try this:
#mixin border($properties , $value){
#each $property in $properties{
border-#{$property}: $value;
}
}
selector{
#include border(left right, 1px solid #E2E2E2);
}

CSS does not allow such control. A workaround is to use larger rule, then restrict it:
border: 1px solid #E2E2E2;
border-top: 0;
border-bottom: 0;
But you end up with more code. Another solution is to use CSS "compiler", like SASS or Less

If the attributes are related, as is the case with border-left and border-right, there usually is a common attribute that allows you to set them:
border: 1px solid #e2e2e2;
On the other side, there are some libraries like Less CSS out there that extend CSS so that you can easily group related properties and attributes.

Related

How can i export shorthand CSS properties (border, border-left, border-right...) as CSS variables

If i do this:
.a {
border: var(--a-border);
border-left: var(--a-border-left);
border-right: var(--a-border-right);
border-top: var(--a-border-top);
border-bottom: var(--a-border-bottom);
}
Setting --a-border never works because individual properties like a-border-left have reset the value. What i'm thinking is that, i'm not giving any default values on those exported variables. Therefore, if --a-border is set, it should take precedence over other unset values.
As I explained in this previous answer when using border-left: var(--a-border-left) it means that border-left will always have a valid value whataver the definition of the CSS variables. Considering this your shorthand property will always be ignored because the longhand ones will always override it.
One fix is to consider the fallback property and redefine the variable used inside the shorthand in each individual propery:
.a {
border: var(--a-border);
border-left: var(--a-border-left,var(--a-border));
border-right: var(--a-border-right,var(--a-border));
border-top: var(--a-border-top,var(--a-border));
border-bottom: var(--a-border-bottom,var(--a-border));
width: 200px;
height: 100px;
display:inline-block;
}
:root {
--a-border: 5px solid red;
}
<div class="a"></div>
<div class="a" style="--a-border-bottom:5px solid green"></div>

LESS: combine properties (rules) with same value

I am using LESS CSS preprocessor. For example if there are two (or more) rules with same value
.my-class {
border-top: 2px solid #main-bg-color - #123456;
border-bottom: 2px solid #main-bg-color - #123456;
}
can I combine those two into one somehow like this?
.my-class {
border-top, border-bottom: 2px solid #main-bg-color - #123456;
}
UPDATE: You shouldn't be concentrated on the props (border) I wrote just as example. I'd like to know if there's any way to combine any multiple props with same value. If there is no way to do so, please say it explicitly.
Why not just use something like:
.my-class {
border: 2px solid #main-bg-color - #123456;
border-width: 2px 0;
}
As #seven-phases-max says, there is no way to combine several props with same value in LESS. Use variables or mixins instead.

A border around Each Data Cell In Css

I am new to web development and coding. I might need a little help with something I am trying to get a border around each data cell using CSS below is my code.Please help me. For some reason this is not working.
.table-box table td{
background-color: #ccc
border: 2px soild #000
}
.table-box table td{
background-color: #ccc;
border: 2px soild #000
}
You have to end the statement with semicolon for the last one it is optional

Table border not supported in HTML5

Table border isn't supported in HTML5, but CSS will apply changes to all of my tables, instead of just 1, is there a way around?
I want to make one table to have borders but the "table border" option is not supported in HTML5, what should I do?
CSS will make changes to all of my tables, instead of just one... is there a way around it? :)
For the border, I think you can do :
table {
border: 1px solid black;
}
/* or, for each cell of your table */
td {
border: 1px solid black;
}
If you want to add this style to only one table, then just add a class to this table, and instead in the css something like :
.your-class {
border: 1px solid black;
}
Hope I could help,

CSS property - set global border first, and then specify border-bottom?

What is the best CSS practice to achieve a border on all sides of a container, apart from, for example, the bottom?
The border property cannot specify different values for each side.
Option 1: Overwriting Rules
border: 1px solid red;
border-bottom: none;
Seems that an extra computation is needed — similar drawbacks as CSS resets (at least philosophically).
Option 2: Setting Specific Rules
border-top: 1px solid red;
border-left: 1px solid red;
border-right: 1px solid red;
Might be more correct (in terms of CSS "semantics")
But if you want to change the border specifics, then it'll require
multiple changes (harder to manage).
Actually it totally depends on you, what is more convenient to you, it also depends on some state like if I want the color of all borders to be same I'll go for 1st but If I think I need to change the colors of each side of the border in near future I'll go with the second 1, but for now, I'll stick to first option
Reasons:
Less CSS to be stated
Specifically it shows that I want border-bottom as none
As you said I don't need to change each and every property: value if I need any changes
If you say proper semantics, proper semantics define very specifically like
border-color: /*Whatever*/;
border-width: /*Whatever*/;
border-style: /*Whatever*/;
Now am sure you don't want to be this specific
If you only want to specify the values once, you can specify the color and width for all, then the style specifically for the sides:
border-color: red;
border-width: 1px;
border-style: solid solid none solid;

Resources