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?

Dani AI

Generated

Quick summary and a few practical patterns that add value beyond the replies from , , , and .

A simple, modern pattern (minimal duplication)
One central rule for the nested elements and a per-parent CSS custom property keeps background URLs out of many selectors. Define a single style for the child boxes and set a variable on the parent type:

<div class="items type-a">
  <div class="unit">...</div>
  <div class="unit">...</div>
</div>
.items .unit {
  background-image: var(--unit-bg);
  background-repeat: no-repeat;
  background-size: cover;
}

/* per-type image only needs one declaration */
.type-a { --unit-bg: url('/images/type-a.png'); }
.type-b { --unit-bg: url('/images/type-b.png'); }

This keeps one child rule and shifts only the image value to each parent class. CSS custom properties are widely supported in modern browsers and prevent the combinatorial selector bloat noted.

Parent background vs child backgrounds
Putting the graphic on the parent (the approach and mention) is perfectly fine when the children are transparent and should all show the same underlying art. Caveats: any child with its own background or an opaque color will hide the parent; absolutely positioned children or new stacking contexts can break the visible alignment; background-position and sizing apply to the parent box, not each child.

Other options and quick troubleshooting

  • Data attributes plus a small script can set inline styles if HTML-only assignment is preferred.
  • Remember URL resolution: a url() inside a stylesheet is relative to the CSS file; inline styles are relative to the HTML file.
  • If an image does not appear, check path, browser cache, and whether a child element has a nontransparent background.
  • For many small icons, consider a sprite or inline SVG to reduce requests.

These patterns avoid repeating many rules while keeping markup semantics clear and easy to maintain.

Recommended Answers

All 5 Replies

you can have same class for all the divs. and use class selector.

Hi,

continuing DangerDev's advised.

if you just need to apply the same style rule over a nested divs', then you could try this instace in your CSS file:

.item_type1, .item_type1 div, .item_type1 div div /* applies the same backgroud on the div that is inside the class(item_type1) div */, .item_type1 div div div /* apply more combination of classes+selectors... */ {
background-image : url(images/type1.gif); }

looks like everyone is giving me the same answer, which involves me putting the background URL's in the CSS file which I dont want, I want something like this:

<div id="itemstable" style="SET BACKGROUND IMG FOR NESTED DIVS HERE">
...other divs here...
</div>

EDIT: figured it out, I forgot i could set the style in the <head /> section.

Just leave the divs transparent. As long as you don't specify a background, they will show the background under them.

Be careful putting images under text. Many people have trouble reading that.

I might be wrong but i think you can style the div and put a backround image in it.
I didnt have time to test it yet.

Style Sheet

#itemstable
{
background:url('YOUR GRADIENT IMAGE.gif') repeat-x;
height: 30%;
width: 50%;
margin: 0 auto;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.