In my website, the users have to enter markdown in a textarea, for which I am using a markdown editor. The problem is: it uses icomoon font, and my websites too. Both uses the same class to define the fonts, but not both uses the same icons. The question is simple: is there a way to define the editor.css for a special div?
Like that:
<div css="editor.css"></div>
Give the DIV a Class and then add a CSS file like this:
.markdown
{
color: red;
}
If you import a new css dynamic, the old styles will be overwritten.
Some help, for dynamic css loading: How to apply inline and/or external CSS loaded dynamically with jQuery
Namespace your editor styles
You can add a selector that namespaces your editor and allows you to style it:
<div class="editor-style">
<div id="cool-custom-editor">...</div>
</div>
In your css:
.editor-style .icon-thumbs-up { color: green; }
Using Scoped Styles (needs polyfill)
As mentioned in #adeneo's comment below your question there is the option of using scoped style tags.
Supposing your editor looks like this:
<div id="cool-custom-editor">...</div>
You can apply a specific style using the scoped attribute like so:
<div>
<style scoped>#import url(editor.css);</style>
<div id="cool-custom-editor">...</div>
<div>
Caveats
According to can I use the scoped attribute is only natively supported by Firefox 26+.
If you want to use it you will have to polyfill:
Plain JavaScript polyfill
jQuery scoped plugin
Further Reading
HTML5 Doctor - The scoped attribute
CSSTricks - Saving the day with scoped styles
HTML5Rocks - A new experimental feature - Scoped Styles
You dont need multiple files. You can give the div an id or class like so
<div class="div1">
<span></span
...
</div>
and now in you css you do this
.div1 span{
font:(whatever font);
}
I don't think so, no. At least not without using any js workarounds. The best solution would be to use kind of namespace for user-specific css classes.
No you can't do that.. I think you should solve the conflit by changing the icomoon class names in this file.
OK solved: renaming the classes in editor for icomoon was a lot easier than I dared tough.
not a good way but it can help:
$("[css]").each(function(i){
var elm = $(this);
$.when($.get(elm.attr("css"))).done(function(response) {
var newresponse=response.replace(/(.+{)/gi,".customCss"+i+" $1");
elm.addClass("customCss"+i);
elm.before('<style>'+newresponse+'</style>');
});
});
Related
All,
Is there any way to define a custom CSS class that uses existing bootstrap classes?
For example, consider the following HTML snippet:
<div class="text-primary center-block">Here is some text</div>
Bootstrap will automatically make it blue and centered and displayed block. But adding all of those classes is a lot to remember. Can I just use my own class:
<div class="my_class">Here is some text</div>
And somehow in a CSS file add those Bootstrap properties?
The only solution I can think of is using JQuery like this:
$(document).ready(
function() {
$(".my_class").each(function() {
$(this).addClass("text-primary center-block");
});
}
);
But is there a better solution?
Hi yes there is a better way, if you are using the Less Source version of Bootstrap you can setup Bootstrap's Classes as Less Mixins by importing the Less files as a Reference
Which would mean you could setup something like this:
.custom-class { .text-primary; center-block; }
This post discusses the technique in detail:
http://transmission.vehikl.com/using-bootstrap-as-a-semantic-mixin-library/
You can do the following: but this is work with SCSS not with the standard CSS
.my_class
{
#extend .text-primary;
#extend .center-block;
}
Hope it helps!
I want to apply the more css class directly to inline style tag dynamically, I tried like below not working (In my situation I can't able to create a class)
It is working for tag but not work for
<script type='text/javascript'>
function CommonClassCreateFunction()
{
var css_cls="{color: #900;margin-top:20px;}:hover {margin-top:0px;}";
return css_cls;
}
</script>
-----
-----
<script type='text/javascript'>
var css_cls=CommonClassCreateFunction();
$('#iiffrraammee').contents().find('#newtxtid').css(css_cls);
</script>
-----
-----
<textarea class="expand close" id="newtxtid" name="ta" wrap="hard" spellcheck="false"></textarea>
CSS
.txtClass{ color: #900; margin-top:20px;}
.txtClass:hover{ margin-top:0px;}
JQuery
$('#iiffrraammee').contents().find('#newtxtid').addClass('txtClass');
Demo:
http://jsfiddle.net/qx2QY/
jQuery allows you to change the css with it's .css() function. You do this by passing the css rules as arguments:
$('#iiffrraammee').contents().find('#newtxtid').newtxtid.css({
'color': '#900',
'margin-top': '20px',
});
$('#iiffrraammee').contents().find('#newtxtid:hover').newtxtid.css({
'margin-top': '0px'
});
Check out the .css() function documentation.
You can`t do this, inline style supports only proprieties, what you have to do is define the class proprieties in a .css file or between tags and add this class to the DOM.
Exemple:
CSS
<style>
.NewClass{ color: #900; margin-top:20px;
}
.NewClass:hover{ margin-top:0px;
}
</style>
jQuery:
$('#iiffrraammee').contents().find('#newtxtid').addClass('NewClass');
Javascript runs as soon as it appears in the html source. In your example the javascript is located above the html it wants to modify, this results in an error.
Having all the HTML download and display un-formatted and then applying CSS will cause the browser to render the un-formatted html and then change it. The site will appear to many as broken and the reformatting will be distracting and create the feeling that the site is slow. It is not a good user experience.
jquery basicly changes the tags inline property.
:hover is a pseudo-selector, which is used in a stylesheet. It is not impossible to use javascript to create a stylesheet but it is impractical ... How to write a:hover in inline CSS? ... but for the sake of the user I would not recommend the practice.
Note: inline pseudo-selectors were considered in the working draft, http://www.w3.org/TR/2002/WD-css-style-attr-20020515 But they were never adopted and do not work in modern browsers.
I just started using bootstrap for my site. I love the look and I want to make some changes to a ton of different blog post to include the bootstrap style. I don't want go through hundreds of post to change the div's class element. Can I do something like this:
<div class="important_note">
this is a old note that I want to use bootstrap styling on.
</div>
css:
<style>
.important_note{
mimick(.alert)
}
</style>
alert is a bootstrap styling.
I apologize if this is a simple question, but web dev isn't much my thing. I also couldn't find any similar questions. Thanks for your help!
with css you can do the following:
.important_note, .alert{
//styling
}
this will apply the same styling to important_note and alert classes
Without "upgrading" your CSS, if it's just about adding a class to each affected element, you can use a small script:
[].forEach.call(document.getElementsByClassName('important_note'), function(node) {
node.classList.add('alert');
});
jQuery equivalent:
$('.important_note').each(function() {
$(this).addClass('alert');
}
My website has an external widget that displays a table.
The widget comes with some inline CSS. Is there a way to remove this CSS before appending the widget to my page, so that only my css file rules will apply?
The markup looks like this:
<div id="widgetContainer">
<script language='javascript' src='http://www.esake.gr/scripts/create_widget.php?pw=1&maw=242&fof=Verdana&tifs=14&tic=3A3A3A&tibc=FFFFFF&dafs=12&dac=3A3A3A&dabc=FFFFFF&ces=0&cep=1&langid=gr'></script>
</div>
You can do this using jQuery,
Assuming you'd have a parent container for your widget, you could do the following,
$("#parentContainer").children().removeAttr("style");
Just created a quick mockup here: http://jsfiddle.net/j3GPL/
your best bet will be to override the css. e.g.
td { background: #f00!important;}
see here for an example.
Is it possible in my css file to do something like that?:
.myclass:after{
content:"click me";
onclick:"my_function()";
}
I want to add after all instances of myclass a clickable text, in the css style sheet.
Is it possible in my css file to do something like [see code above]?
No
The important question to ask is why.
HTML has control of the data within the webpage. Any CSS or JS is specified via the HTML. It's the Model.
CSS has control of the styles, there is no link between CSS and HTML or JavaScript. It's the View.
JavaScript has control of the interactions within the webpage, and has hooks to any and all DOM nodes. It's the Controller.
Because of this MVC structure: HTML belongs in .html files, CSS belongs in .css files, and JS belongs in .js files.
CSS pseudo-elements do not create DOM nodes. There is no direct way for JavaScript to access a pseudo-element defined in CSS, and there's no way to attach an event to said pseudo-elements.
If you've got a set structure in place, and can't add the additional content necessary to produce new links within the HTML, JavaScript can dynamically add the new elements necessary which can then be styled via CSS.
jQuery makes this very simple:
$('<span class="click-me">click me</span>').appendTo('.myclass').click(my_function);
Well,
using expression it might actually be possible for internet explorer only.
Otherwise:
No. Not at all, that's not what css is made and intended for.
If something can be done with jQuery, then it is sure that it is possible to do it without that. Lets see a data model:
<div id="container">
<div id="hasblock" onclick='clickFunc(this, event)'></div>
</div>
We need some stylesheet:
<style>
div#container { width:100px; height:50px;position relative; border:none;}
div#hasblock {width:100px; height:50px;position absolute;border:solid black;}
div#hasblock::after {content:"Click me"; position: absolute; border:solid red;
top:80px;left:0px;display:block;width:100px;height:20px; font-size:18px;}
</style>
And a code:
<script>
function clickFunc(his, event)
{if (event.clientY>his.clientHeight){console.log("It was a click");}; }
</script>
Use jQuery's After:
http://jsfiddle.net/PCRnj/