apply css to nested divs - css

I use the following code on my page:
<div id="itemstable" class="item_type1">
...other divs here...
</div>
And in my CSS file I have this code:
.item_type1 div {
background-image: url(images/type1.giff);
}
the problem is there are a lot of different item types so I will need to have a lot of lines in my CSS file. I was wondering how to apply the background-image: url(images/type1.giff); style to the nested divs without assigning it to each one. eg. I want to change the code for the "itemstable" div so that it applies a css rule to the nested divs.
Is this possible?
EDIT: I'm looking for something like this:
<div id="itemstable" style="SET BACKGROUND IMG FOR NESTED DIVS HERE">
...other divs here...
</div>

(If I'm understanding the question correctly:)
Think about using a different ID/class scheme. I don't know about the further specifics of your structure, but id="itemstable" class="item_type1" seems slightly redundant to me. Can itemstable be anything else than item_type1? Try to apply more generic class names and keep the specific cases for IDs.
Failing that, you can add another class that is responsible for adding the background image: class="item_type1 item_types".
EDIT
Since it seems sheer mass is the main problem (not applying the style as the title suggests) it's probably best to dynamically insert a style in the page header. Something along the lines of:
<head>
...
<style type="text/css" media="screen">
<?php echo "#$myelement"; ?> div { background: url(<?php echo $image; ?>) ...; }
</style>
</head>
Inline styles can only apply to the element directly, not one of its children. I.e.:
<div style="background: ...;">
The background only applies to this one div.
You can't use selectors in inline styles like:
<div style="div { background: ...; }">

I think including a little more of your HTML would make your question easier to understand.
You can certainly include multiple rules in a compound selector:
.item_type1 div.a, .item_type1 div.b, .item_type1 div.c {
background-image: url(xyz.gif);
}
But since you are pulling images from the database dynamically, you will need to either include them in your dynamic code-- in the divs themselves, or dynamically create CSS as suggested above:
<style>
<% for $i in $images { echo "div.image$i div { background-image: url(/path/to/$i) }" %>
</style>

Related

Apply stylesheet to half a page with shadow-dom?

Can I apply an external stylesheet to a specific div/element with shadow-dom or via any other means? I've heard about shadow-dom and I believe it lets you constrain your styles, but that's about all I know.
Specifically, I want half the page to use bootstrap, and the other half to use MUI or something else. This is just to show how my library works nicely with different themes.
I don't want to modify the CSS in anyway to constrain it to a specific element, nor do I want to use iframes.
Yes, you can apply an external stysheet in a Shadow DOM using the #import url CSS rule.
div.attachShadow( { mode: 'open' } )
.innerHTML = `
<style>
#import url( './external-style.css' )
</style>
<!-- other elements -->`
NB: The #import rule must placed at the top of the <style> element.
You can then manipulate the Shadow DOM like a normal DOM:
div.shadowRoot.appendChild( firstSection.cloneNode( true ) )
If your content is already existing in the normal DOM, you can move it with appendChild(), duplicate it with cloneNode() as in the above example), or reveal it with the help of <slot> element:
div1.attachShadow( { mode: 'open' } )
div1.shadowRoot.innerHTML = `
<style>
:host { display: inline ; background: #cfc ; }
::slotted( span ) { color: red ; }
</style>
<slot></slot>`
<div id=div1>
<span>Hello</span> world
</div>
In the last case you'll need to use the ::slotted pseudo-element to change the style of the original DOM, so maybe you'll have to modify already existing stylesheet. The best solution depends on your use case.

Applying CSS to a table using a SharePoint script editor

What am I doing wrong with applying CSS to my table (Webpart: WebPartWPQ6) in SharePoint?
I have this script editor CSS (note the Font-size is just trying to see in an instant if what I apply works or not):
<style type="text/css">
#WebPartTitleWPQ6 .ms-viewheadertr
{
font-size: 100px !important;
}
</style>
I've tried replacing ms-viewheartr with diidSort16RequestID, ms-headerSortTitleLink, ms-viewheadertr .ms-vhltr, ms-vhltrm, ms-vh-div. But none of them seem to be working.
I'm getting the webpart name from inspecting the element:
<div id="MSOZoneCell_WebPartWPQ6" class="ms-webpartzone-cell ms-webpart-cell-vertical ms-fullWidth s4-wpcell" onkeyup="WpKeyUp(event)" onmouseup="WpClick(event)">
<div class="ms-webpart-chrome ms-webpart-chrome-vertical ms-webpart-chrome-fullWidth ">
<div webpartid="69f4fe83-8a06-4eca-bbb2-fb143cdc2859" haspers="false" id="WebPartWPQ6"
and I'm trying to get the name of the header from:
There isn't any element with an id of WebPartTitleWPQ6 in the HTML you posted. The closest thing is the element with the id WebPartWPQ6.
Try using a CSS selector of #WebPartWPQ6 instead of #WebPartTitleWPQ6.

Set child div which has attribute matching parent div attrobite via style sheet

I have this html code here:
<div default_name="RandomName1">
<div name="RandomName1">RandomName1</div>
<div name="RandomName2">RandomName2</div>
<div name="RandomName3">RandomName3</div>
</div>
The property default_name on parent div changes from time to time. I would like to set the child div which has name matching default_name to background-color:red.
Like:
<style>
div > div[name=default_name_of_parent] { background-color: red }
</style>
I have no control over what the name values are, users set it. Is this possible via style sheet?
Thanks
This can be done, if you make a rule containing a selector for each possible “combination”, like so:
div[default_name=RandomName1] > div[name=RandomName1],
div[default_name=RandomName2] > div[name=RandomName2],
div[default_name=RandomName3] > div[name=RandomName3]
{ background-color: red }
http://jsfiddle.net/wc5whfwa/
But j08691 is totally right with their comment – this should be avoided at all cost if possible, data- attributes would be the way to go.

Add values in .css file dynamically

I want to know is it possible to add values for classes created in .css file to a tag dynamically?
Now, if I have <div class="textFrame"> in .html file and in css file I have declaration as-
.textFrame
{
overflow:hidden;
position:absolute;
}
Now, I want to add style attribute for <div> with left, top, height and width etc attributes. My question is, is it possible to create some variables let us say, left, top, height or width in .textFrame class(in .css file) and assign values dynamically to these variables for each <div> tag. Actually there are no of textFrames(<div> tags) with different values of top, left, height and width.
With use of jquery you are able to add classes dynamically in tags
eg:
<div class="textFrame">
.......
.......
</div>
Jquery :
$(document).ready(function() {
$('.textFrame').css('height','500px');
}
For multiple div has same class
eg:
<div class="textFrame" id="1"> ....... ....... </div>
<div class="textFrame" id="2"> ....... ....... </div>
Jquery :
$(document).ready(function() {
$('.textFrame').each(function(e){
if($('.textFrame').attr('id') == '1'){
$('.textFrame').css('height','500px');
} else if($('.textFrame').attr('id') == '2'){
$('.textFrame').css('height','300px');
}
});
If they all have different attributes, it makes no sense to dynamically change the source stylesheet. You should probably use Javascript to change each element's style dynamically. jQuery makes that very easy.

Is this CSS reference correct and supported syntax: .slider.wide {}

I have a slider that's marked up like so:
<div class="slider wide">
//slider html in here
</div>
And another marked up like so:
<div class="slider narrow">
//slider html in here
</div>
Is it possible to reference each of these like this in my CSS file by in a way concatenating the class names:
.slider.wide { //css specific to the wide slider goes here }
.slider.narrow { //css specific to the wide slider goes here }
No, you make three classes .slider, where you put common slider css, and .narrow where you put narrow slider specific css, and .wide where you put wide slider specific css.
.slider { //css common among all sliders goes here }
.wide { //css specific to the wide slider goes here }
.narrow { //css specific to the narrow slider goes here }
Yes, .slider.narrow is valid. It's not exactly concatenating the class names, it's making two different class selectors and applying them to the same element. So .narrow.slider is also valid and will match the same elements.
The problem with using multiple class selectors against a single element is that is doesn't work in IE6. This browser will ignore all but the last class selector. So to support that browser you typically end up using something like class="slider wide-slider".

Resources