Apply the CSS of a child of element A to another element B

Asked

Viewed 215 times

2

It is possible to somehow use CSS rules applied to #elemento > .filho in another element #dots?

#elemento > .filho{...}

In case I would need <div id="dots"></div> received the style of the class filho.


What I have is dots out of the inheritance, and wanted to apply to him the style filho.

<div id="elemento">
   <div class="filho"></div>
</div>

<div id="dots"></div>
  • You can’t just do: #elemento > .filho, #dots {...} ?

2 answers

2


For this you can use the plugin copyCss, example:

(function($){
	
	$.fn.getStyles = function(only, except) {
		
		// the map to return with requested styles and values as KVP
		var product = {};
		
		// the style object from the DOM element we need to iterate through
		var style;
		
		// recycle the name of the style attribute
		var name;
		
		// if it's a limited list, no need to run through the entire style object
		if (only && only instanceof Array) {
			
			for (var i = 0, l = only.length; i < l; i++) {
				// since we have the name already, just return via built-in .css method
				name = only[i];
				product[name] = this.css(name);
			}
			
		} else {
		
			// prevent from empty selector
			if (this.length) {
				
				// otherwise, we need to get everything
				var dom = this.get(0);
				
				// standards
				if (window.getComputedStyle) {
					
					// convenience methods to turn css case ('background-image') to camel ('backgroundImage')
					var pattern = /\-([a-z])/g;
					var uc = function (a, b) {
							return b.toUpperCase();
					};			
					var camelize = function(string){
						return string.replace(pattern, uc);
					};
					
					// make sure we're getting a good reference
					if (style = window.getComputedStyle(dom, null)) {
						var camel, value;
						// opera doesn't give back style.length - use truthy since a 0 length may as well be skipped anyways
						if (style.length) {
							for (var i = 0, l = style.length; i < l; i++) {
								name = style[i];
								camel = camelize(name);
								value = style.getPropertyValue(name);
								product[camel] = value;
							}
						} else {
							// opera
							for (name in style) {
								camel = camelize(name);
								value = style.getPropertyValue(name) || style[name];
								product[camel] = value;
							}
						}
					}
				}
				// IE - first try currentStyle, then normal style object - don't bother with runtimeStyle
				else if (style = dom.currentStyle) {
					for (name in style) {
						product[name] = style[name];
					}
				}
				else if (style = dom.style) {
					for (name in style) {
						if (typeof style[name] != 'function') {
							product[name] = style[name];
						}
					}
				}
			}
		}
		
		// remove any styles specified...
		// be careful on blacklist - sometimes vendor-specific values aren't obvious but will be visible...  e.g., excepting 'color' will still let '-webkit-text-fill-color' through, which will in fact color the text
		if (except && except instanceof Array) {
			for (var i = 0, l = except.length; i < l; i++) {
				name = except[i];
				delete product[name];
			}
		}
		
		// one way out so we can process blacklist in one spot
		return product;
	
	};
	
	// sugar - source is the selector, dom element or jQuery instance to copy from - only and except are optional
	$.fn.copyCSS = function(source, only, except) {
		var styles = $(source).getStyles(only, except);
		this.css(styles);
		
		return this;
	};
	
})(jQuery);


$(document).ready(function(){
  $('button').click(function(){
    $("#dots").copyCSS('.filho');
  });
});
#elemento > .filho{
  color:red;
  background-color:yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="elemento">
   <div class="filho">filho</div>
</div>

<div id="dots">Dots</div>
<button type="button">
Aplicar
</button>

NOTE: I PUT THE CONTENTS OF THE PLUGIN IN THE ANSWER BECAUSE I DID NOT FIND THE CDN

0

If your question is right just do this the Dots id will get the child class style.

$( "#dots" ).addClass( "outra" );
.filho, .outra {
	color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="elemento">
   <div class="filho">filho</div>
</div>

<div id="dots">dots</div>

  • That I am aware (rs), the problem is that I do not want / can not play .filho out.

  • The css needs to look like this: #elemento > .filho{color:red}

  • You cannot apply the same Id twice. Id is unique.

  • Then see if the issue meets.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.