How can I do so that css styles are actually inside container? - html

I have a css file with styles:
button-text-only {
padding: .4em 1em;
}
.ui-buttonse {
margin-right: 7px;
}
.ui-datepicker {
left: 2px;
}
... and etc ...
How can I do so that css styles are actualy inside container with id = "date_catr"?

There are a few of ways that I think you can approach this, as has been stated you can prefix the rules with the container ID, e.g.
#date_catr button-text-only {
padding: .4em 1em;
}
#date_catr .ui-buttonse {
margin-right: 7px;
}
#date_catr .ui-datepicker {
left: 2px;
}
etc.
Unfortunately if you have to apply this to 50-100 styles this adds a fair bit of uneeded CSS and may ever so slightly reduce the speed at which the css is applied (which will be more noticeable on mobile devices).
The problem is, apart from the above there are very few ways to actually do what you want.
You could use scoped styles, this sounds like a great idea, until you realise Firefox is literlly the only browser that currently supports this (and by the looks of it, the only browser to support it into the foreseeable future).
You could also try to include your HTML via an iframe, with your intended CSS inline inside said iframe, CSS inside the iframe only applies inside the iframe, and css applied to the page doesn't effect any content inside the iframe. This is basically like the scoped styles solution described above except it has full browser support.
Iframes could however get a bit messy, add unnecessary bloat to the page and be a bit of a pain to maintain.
The only other solution I can think of is prefixing the css and changing it in the css, this means the css doesn't really get much bigger, and also ensures that the speed at which the css is applied shouldn't be effected, this could however be messy and cumbersome to maintain, I'm not sure if you want the elements to inherit base styles from the classes you've posted ... regardless it might look like so:
.i-button-text-only {
padding: .4em 1em;
}
.i-ui-buttonse {
margin-right: 7px;
}
.i-ui-datepicker {
left: 2px;
}
etc.
Personally I'd go for option one, maybe with a class so that you don't have to deal with specificity issues later down the line. You'll end up with a little bit of slowness, but it should be pretty unnoticeable (hopefully)

Prefix every rule above with #date_catr like this:
#date_catr button-text-only {
padding: .4em 1em;
}

Related

Element on same line not causing space to appear in between buttons CSS

Can anyone explain this odd behavior, why extra spacing is adding in between buttons.
Case -
Following is the HTML code which is adding extra space in between buttons if written like this -
<div class="wrapper">
<button class="btn one">First long button with a long text length</button>
<button class="btn two">Second long button with a long text length</button>
</div>
Output -
BUT if I am writing like this then no space is coming -
Code -
<div class="wrapper">
<button class="btn one">First long button with a long text length</button><button class="btn two">Second long button with a long text length</button>
</div>
Output -
CSS Code -
* {
margin: 0;
padding: 0;
}
.wrapper {
padding: 10px;
}
.btn {
font-size: 14px;
line-height: 28px;
background: #FFFFFF;
border: 1px solid #C3C3C3;
position: relative;
padding: 10px;
display: inline;
}
.btn:before {
content: "";
position: absolute;
width: 4%;
height: 5px;
background: #FFF;
right: 0px;
top: -5px;
}
.two {
display: inline;
}
With inline-elements, line-breaks (and multiple spaces) are converted to 1 space.
There are several things you can do in order to eliminate white-space between elements; however it depends on the kind of elements used and the browser's html rendering engine.
Here are some things that work for the "white-space" issues, but bare in mind that ultimately you want your code to look neat.
Good solution .: no white-space & clever CSS
Coding your HTML with no white-space will definitely sort this out for most major browsers, but it makes your code illegible; however you can run your code through an HTML pre-processor (like PHP) and minify your code before serving it to the browser. This may seem "daft" for speed concerns, but if you can get clever with "configuration" of your projects in your pre-processor to "bake" (minify) your source-code as a separate file while in "development" mode; and only "serve" the minified code when in "live" mode.
If you prefer the "pre-processor" option then have a look at: how to minify html code
In your CSS, take note of how different elements render in the browser.
With "block-type" & "view-port" elements, wrap them inside other elements you can control; because styling these to be displayed inline may cause issues, hence why the CSS value for these: display:inline-block, but it's not a 100% guarantee to look as intended.
HTML only .: comments & weird closing-angle placement .: ugly but works
<button>one</button><!--
--><button>two</button>
<button>one</button
><button>one</button>
CSS only .: font-size zero on parent, consistent position & margin
If you're using the font-size:0px then you have to be smart with your CSS selectors. Forcing standard margins/padding for all your elements is a good idea for consistency:
body{font-size:0px;}
p,b,i,a,span{font-size:16px;}
div,svg{position:relative; box-sizing:border-box; margin:0px;}
The \n is a legal character in html, it just don't return line.
You can resolve it by adding font-size:0 to the container of the buttons for example, as the buttons have font-size:14px.
div.wrapper { font-size:0 ; }
Just be carefull with the other elements in your wrapper wich will heritate from your font-size:0.
OR : you can write in on the same line... :)

Get attribute value in LESS?

In css we have something similair for :pseudo elements. According to this, it will come in the future. At the moment its not supported by any major browser http://caniuse.com/#feat=css3-attr
.test {
display: inline-block;
background-color: gray;
color: attr(data-color); /* Doens't work in css */
width: attr(data-width px);
}
.test:after {
content: attr(data-name);
}
<div class='test' data-name=" - CONTENT" data-color="#f00" data-width="200">test</div>
But what i want is, lets say ive the following div
<div data-color="#f00">...</div>
In LESS i want to be able to pick that color through the data attribute.
.example-class {
color: attr(data-color); /* Something like this */
}
Is this, or something similar, possible using LESS?
Considering that LESS compiles to CSS anyway, and therefore never knows about the HTML, that doesn't seem possible. That is the entire reason why the attr() function is in CSS, not LESS.

Can you make custom aliases?

Is there any way to alias / make custom elements behave like others
E.g. <large> should work like <h1>
There are a few ways to do this, but they reach have their own problems.
Using Classes - This would create the same effect, but isn't what you asked for.
Assigning default h1 values to large like so:
large{font-size:32px;}
This isn't a true alias though, since you have to apply your rules for every alias. This is also problematic, because older versions of IE won't render rules for custom HTML tags, like "large."
Using JavaScript.
Apply no CSS rules to the custom tags, and use JS to generate them. Of course, JavaScript doesn't have universal support, but older browsers should still handle it just fine.
I recomend do class .h1.
large will only looks like h1.
.h1 {
display: block;
font-size: 2em;
margin-top: 0.67em;
margin-bottom: 0.67em;
margin-left: 0;
margin-right: 0;
font-weight: bold;
}
<large class="h1"> AAA </large>
<h1>AAA</h1>
In CSS:
.stop-p-lf p { display: inline; }
Then in HTML:
<div class="stop-p-lf"><p>This will be inline</p></div><p>And this don't</p>

How do you completely remove a style from bootstrap by overriding it

So I'm completely familiar with overriding a style in bootstrap by using my own Style.css. But let's say that bootstrap has a style that is put on a table or something that is a standard html element. Let's use labels, because it's a rather short example. Let's first assume that there is no other label style or label element styling anywhere else except for the following css code:
label {
display: inline-block;
max-width: 100%;
margin-bottom: 5px;
font-weight: bold;
}
Now if I want to override this style in my Style.css file and change the margin and weight, I could do this:
label {
margin-bottom: 3px;
font-weight: normal;
}
Easy enough, this would change those two items and allow the other styles to cascade through. But what if I wanted to completely remove any styling added by bootstrap for the element label. Is there a short and easy way to do this without having to do something like:
label {
display: inline;
max-width: None;
color: none;
margin-bottom: 0;
font-weight: normal;
}
Or basically going through line by line and changing each styles property to something like none or normal or whatever? All while keeping the original Bootstrap file in an untouched state and not commenting anything out of it.
BTW I would also be fine with using JavaScript if it's concise and easy?
It depends on what browsers you want to support. You could use
label {
all: initial; // or all: unset
}
but be aware that it's not really widely supported yet. It works on IE 11, Firefox, Opera & Chrome, but not Safari or most mobile browsers. Still, a good one to know if and when it becomes more widely supported :)
No if it is already include the only way to override it is to give it properties like none, alternatively the best way to handle it is to use their SASS/LESS implementations and not include the component at all.

How to switch left and right in a css file?

I have an HTML django template page that is both RTL and LTR (depends on user's locale).
The CSS of this page is stored in another file, and that file is currently static.
What is the best way to switch the attribute left and right according to the locale? Is there a built in attribute in CSS for this problem? (I don't want to use JS, it feels too messy)
I have:
.elem{
left: 10px;
bottom: 10px;
position: absolute;
}
I want something like this:
.elem{
right-or-left-according-to-html-dir: 10px;
bottom: 10px;
position: absolute;
}
Currently the only option I can think of is turning the file into a template also:
.elem{
{{dir}}: 10px;
bottom: 10px;
position: absolute;
}
Is there a better way that will let me keep my CSS file static?
You say you're making the document rtl or ltr depending on locale. In that case you can use the :lang() selector to make certain parts of your document have styling depending on the locale.
http://www.w3.org/wiki/CSS/Selectors/pseudo-classes/:lang
http://www.w3.org/TR/css3-selectors/#lang-pseudo
If you want a little more support (IE7+) you could use the attribute selector selector[lang='en'] though that will only test the attribute on the specified selector.
http://www.w3.org/TR/css3-selectors/#attribute-selectors
If you specify the language in the html element (which you should, with lang="en" for example) you can just put the html selector in front of the class you want to apply in certain locales:
.elem {
margin: 0 10px 0 0;
color: blue;
}
html[lang='en'] .elem {
margin: 0 0 0 10px;
}
Even better, if you specified the dir attribute you can directly use that in css like so:
.elem[dir='rtl'] {
margin: 0 10px 0 0;
}
Please note that with a class on the body element you will always depend on that class always being there. But the dir and lang attribute can be specified on a more specific scope, like a single div, and still be used in the css along with styles for the 'other' reading directions.
Edit
Lastly, to gaze into the future, the CSS Selectors 'Level 4' will include a psuedo tag which will be able to filter on text directionality. Of course the specs are in development and adoption by browsers may take years before it is possible to reliably use it:
http://dev.w3.org/csswg/selectors4/#dir-pseudo
How about adding the direction to your body element via a special class, then you can write according selectors:
<body class="rtl">
and in the CSS:
.rtl .myclass {
text-align: right;
}