I have the following CSS which I use to set the height of CodeMirror which works well across browsers :
.CodeMirror {
/* Firefox */
height: -moz-calc(100vh - 190px);
/* WebKit */
height: -webkit-calc(100vh - 190px);
/* Opera */
height: -o-calc(100vh - 190px);
/* Standard */
height: calc(100vh - 190px);
}
Now, I am moving to froala which appears to be the only WYSIWYG editor that supports CodeMirror as the 'view code' portion.
I have no problem embedding and setting up CodeMirror to work with this, and the style that I applied works with the embedded CodeMirror, however I am unable to apply the Height to the requisite CSS file in the manner I have done with CodeMirrors CSS file.
There are height, heightMax, heightMin, and fullPage properties for froala which can be set during JavaScript initialization, however this does not support calculated values.
Before using CSS's calc() method, I was using JavaScript to size CodeMirror with mixed results, and a lot of extra checks in place which was much more "jumpy" and often either going out of bounds (box going beyond the containing elements height), or falling just short (leaving a gap between the editor element and the containing elements height).
What I would like to do, is override the containing elements height via CSS (I don't mind editing the froala_editor.css file (or other files directly part of the project) to do this. To figure this out for CodeMirror took a rather long time to find the ONE spot where the height calc css would go into the .CodeMirror block. With froala, I do not know where or what the equivalent is called to size the editable area.
I have tried placing the size code in several places including .fr-box.fr-basic .fr-element, to no avail.
The height parameter of froala accepts valid CSS values like 100px, 100, or 100%. As a result, you can use browser detection to fairly effectively decide which value to set.
Following is one example on how this can be done, while still maintaining the calc method in CSS without the need to modify any of froala's CSS files :
// set the default calc value
var codeHeight = 'calc(100vh - 96px)';
// prepend any browser engine specific names if needed
if((navigator.userAgent.indexOf("Opera") || navigator.userAgent.indexOf('OPR')) != -1 ) {
codeHeight = '-o-' + codeHeight;
} else if(navigator.userAgent.indexOf("Chrome") != -1 ) {
codeHeight = '-webkit-' + codeHeight;
} else if(navigator.userAgent.indexOf("Safari") != -1) {
codeHeight = '-webkit-' + codeHeight;
} else if(navigator.userAgent.indexOf("Firefox") != -1 ) {
codeHeight = '-moz-' + codeHeight;
}
// Initialize froala using calc for height
$('.code').froalaEditor({
height: codeHeight
});
While the above is a hackish way to accomplish this, there are other browser detection methods which could be applied. This just demonstrates how this can be done as elegantly as possible without touching any of froala's sources.
In summary, this works as follows :
Detect the browser (above case uses JavaScript and UserAgent)
Set an accessible variable to the detected browser supported calc CSS method
Pass the accessible variable to froala's height init parameter.
Related
Let's say for example I'm going through my stylesheet but I can't remember what element a certain CSS selector affects.
Is there any method, tool, or anything that I could do/use to find out what exactly it is affecting?
Thanks!
I just opened up a random bootstrap template site and did what you where asking for.
Open up your chrome browser (I prefer this as I feel this is easy to debug both Jquery and css) and Press F12, you will get a developer window as in the image.
Switch to the console tab.
Use Jquery selector to select all
the intended elements (you can use the same css selector here too
but just place them inside $('')) Eg: $('.tab-content') I am trying to find out all the elements with the class tab-content
The result is all the elements
of that selector.
NOTE: This appraoch woud require you to have Jquery loaded into your page. Else the script will throw an error saying $ is not defind.
In addition to using your browser's development tools, there are two easy ways to do it that should work in almost any browser (no matter how bad the developer environment).
Visually
Temporarily set a border or background color for the selector, like:
border: 1px solid red;
Or:
background: red;
This makes it very easy to find the affected elements.
Programmatically
On the JavaScript console, use:
// Replace with something that prints the relevant details
var stringify = function(element) { return element.innerHTML; };
// Iterate over all affected elements and print relevant info
var affectedElements = document.querySelectorAll('.your .selector');
var len = affectedElements.length;
console.log('Elements affected: ' + len);
for (var i = 0; i < len; i++) {
var affectedElement = affectedElements[i];
console.log(
'Element ' + (i+1) + ':\n' +
stringify(affectedElement) + '\n\n');
}
The inspection of elements feature of the browser is meant for the purpose you want.
1.Open up the index file in any browser(preferably Mozilla Developer edition),
2.Right click and Inspect element,
3.Then open the compiled stylesheet. Find out the style element you want to check the effect of.
4. Go back to inspection, remove/add CSS properties and see the effect in real time.
The other way is to use Adobe brackets. It's live preview feature will highlight the section that involves the code snippet, you point your cursor to.
I'm using Foundation. Where I make a form the input field are very high like 40px. I want to make the less height like around 26px. I looked in the css setting file but you can change a lot but not the height. When I add the css code below it works in Chorme, but in FireFox the bottom part of the texted is hidden.
input {
height: 26px;
}
Does anyone have a idee?
Simplest and brutest (most brutey?) way to do it is:
input {
height: 26px !important;
}
in your app.scss and compile your css.
However, there is a better way..
Check in the file at /bower_components/foundation/scss/foundation/components/_forms.scss and you'll see that the height of form elements is based on the font-size and form-spacing
height: ($input-font-size + ($form-spacing * 1.5) - rem-calc(1)); // line 121
Which by default (in the same file) are:
$form-spacing: rem-calc(16) !default; // line 14
$input-font-size: rem-calc(14) !default; // line 26
These can be found and uncommented in your _settings.scss file (same directory as your app.scss) to be overriden there before compiling again. Test well because these are general settings that will have an effect on most (if not all) form elements.
On the plus side, in your _settings.scss file they are immune from updates to the framework files, and will give you more consistency in your site-wide styles.
I'm new to the web side of things, and am confused how to deal with CSS. (Thankfully), there is little direct manipulation of HTML/CSS when using ExtJS4 so far... so now that I'm in need to change the CSS, I'm having problems.
Specifically, I'm trying to dynamically change the color of accordion header backgrounds.
Javascript:
afterrender: function(subForm) {
subForm.getHeader().getEl().addCls('custom-accordion-hd-valid');
// this works - so I know it's the right element.
subForm.getHeader().getEl().setStyle('background', 'hsl(100, 60%, 60%)');
}
CSS:
// attempt 1
.custom-accordion-hd-valid {
background: green;
}
// attempt 2
.custom-accordion-hd-valid .x-accordion-hd {
background: green;
}
So:
setting style via setStyle does work, but it doesn't easily allow me to remove a style
setting via addCls with CSS attempt 1 loads the CSS, but it gets overridden by .x-accordion-item
setting via addCls with CSS attempt 2 fails to load the CSS
Help?
if you for instance wanted to remove the background style you set here:
subForm.getHeader().getEl().setStyle('background', 'hsl(100, 60%, 60%)');
css will allow you to simply override it by setting it again eg:
subForm.getHeader().getEl().setStyle('background', 'none');
or
subForm.getHeader().getEl().setStyle('background', 'blue');
css has a particular priority on how it judges which styles are most "important" when multiple styles are provided - take a look here at this great article on css specificity
and realize by using that setStyle() method you are applying "inline" styles to these elements, where as other css definitions either in a file or in a style html tag have a lower priority
I've read a fair bit about resizing fonts recently, most of it deriding using px as an unforgivable crime (ok, maybe not that bad, you get the idea) because it doesn't resize properly in older browsers.
I really want to have a standard that I use myself, in the past that has been px, simply because it's simple, easy to understand and fairly easy to achieve the exact font sizes that are specified in designs - but I'm now doubting using px.
I used em on a project recently because it needed text-resizing functionality which I made using jQuery. But I found it quite frustrating because of the way that em amplifies if you have two elements inside of each other both with an em size specified (hope that makes sense)
So I was wondering about using % for font resizing, I've seen a couple of big websites use this technique (namely, Yahoo) and from what I understand it seems to have all of the advantages of em without the incredibly annoying amplification thing.
So in short, I'm just wondering if there are any issues with using % for font-sizing in CSS? Would it be better than using PX in terms of font-resizing? And are there any noticeable draw backs?
Apologies if the blurb before the question is a little much :/ I'm still kind of getting used to the whole QA thing
In CSS3, use rem (root em). Sizing will not be affected by em size of the parent element.
The root font size is set by setting the font size on the :root pseudo-element, like so:
:root {
font-size: 16px;
}
try using this
body {
margin: 0px;
padding: 0px;
font-size: 62.5%;
}
body>#wrapper {
font-size:1em;
}
so, when you say something like 1em inside the "wrapper" you'll have a size very similar to 10px. here's a example table:
3em == 30px
.5em == 5px
8.5em == 85px
This will help you in the transition
P.d: of course, you need a wrapper tag after the body
The standard in web design as far as I have experienced it is to use a percent to set the font size in the body, then to use ems to change the font sizing after that. You could use percents outside the body tag with no adverse side effects, but I think many developers find ems easier to work with (anyone is free to check me on this).
The danger comes if you use ems to set the body font size; older browsers choke and incorrectly display the text, especially when zoomed.
There's a jQuery plugin called FitText. It resizes text based on percents. If the visitor for some reason has JavaScript disabled, it'll just display as normal, so set a reasonable PX or EM backup.
It depends on jQuery, so you'll need to include that in your page (if you don't have it already).
jquery.fittext.js for reference:
/*global jQuery */
/*!
* FitText.js 1.0
*
* Copyright 2011, Dave Rupert http://daverupert.com
* Released under the WTFPL license
* http://sam.zoy.org/wtfpl/
*
* Date: Thu May 05 14:23:00 2011 -0600
*/
(function( $ ){
$.fn.fitText = function( kompressor, options ) {
var settings = {
'minFontSize' : Number.NEGATIVE_INFINITY,
'maxFontSize' : Number.POSITIVE_INFINITY
};
return this.each(function(){
var $this = $(this); // store the object
var compressor = kompressor || 1; // set the compressor
if ( options ) {
$.extend( settings, options );
}
// Resizer() resizes items based on the object width divided by the compressor * 10
var resizer = function () {
$this.css('font-size', Math.max(Math.min($this.width() / (compressor*10), parseFloat(settings.maxFontSize)), parseFloat(settings.minFontSize)));
};
// Call once to set.
resizer();
// Call on resize. Opera debounces their resize by default.
$(window).resize(resizer);
});
};
})( jQuery );
Maybe will this make more sense of making a font-resize script.
I had made a script that did exactly what you desire, but I cant find it any more.
Pseudo-code:
var fontSize = 15; // (em) input
var fontResize = -1;// (em)input
fontSize = fontSize+fontResize; //current div
for(every inherent parent classname == 'classname-em')
document.classnameParentSize[numberofclass] = classnameChildSize[numberOfClass]/100*90;
So explained in words. The script needs to resize some fonts, when that is done it will also look for some parent divs to resize those fonts too, except they will be resized 10% less than its inherent. With some puzzling you will have the right conversion. Also try to avoid paddings and border widths.
Is it possible to create a new property in CSS? For example, say you're developing a control that displays a photo and you want to add a property to css to control what style frame to have around the photo. Something like:
#myphoto { frame-style: fancy }
Is there some way to do this in a cross browser compatible manner, and how would you define whether the style inherits or not?
EDIT: It's a custom control - your JS code would deal with the style - I'm not expecting the browser to magically know what to do. I want the user to be able to style the control with CSS instead of JS.
Sure, why not. Check this out as an example: http://bililite.com/blog/2009/01/16/jquery-css-parser/
You may also be able to get away with using CSS classes instead of properties. Not sure if that works for what you're doing.
You can't. Browsers interpret CSS based on how their layout engines are coded to do so.
Unless you took an existing open source engine like WebKit or Gecko, added custom code to handle your custom CSS and made a browser that used your customized layout engine. But then only your implementation would understand your custom CSS.
Re your edit: it'd depend on whether you're able to read that style somehow. Typically browsers just instantly discard any properties they don't recognize, and CSS is not normally reachable by JavaScript because CSS code is not part of the DOM.
Or you could look at Jordan's answer.
If you'd prefer a straight JavaScript solution that uses no JS libraries, you could use the query string of a background-image to keep "custom properties" inside your CSS.
HTML
<div id="foo">hello</div>
CSS
#foo {
background: url('images/spacer.gif?bar=411');
}
JavaScript
getCustomCSSProperty('foo', 'bar');
Supporting JavaScript Functions
function getCustomCSSProperty(elId, propName)
{
var obj = document.getElementById(elId);
var bi = obj.currentStyle ? obj.currentStyle.backgroundImage : document.defaultView.getComputedStyle(obj, null).getPropertyValue('background-image');
var biurl = RegExp('url\\(["\\\']?([^"\\\']+)["\\\']?\\)').exec(bi);
return getParameterByName(propName, biurl[1]);
}
function getParameterByName(name, qs) {
var match = RegExp('[?&]' + name + '=([^&]*)').exec(qs);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
Demo:
http://jsfiddle.net/t2DYk/1/
Explanation:
http://refactorer.blogspot.com/2011/08/faking-custom-css-properties.html
I've tested the solution in IE 5.5-9, Chrome, Firefox, Opera, and Safari.