Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styling SPAN within a DIV

Tags:

html

css

Let's say I have the following:

<DIV class="msg-warn">
 <SPAN>some message</SPAN>
</DIV>

I would like to add a background-color to the msg-warn class on the DIV as to have the SPAN inherit the said background-color. I can't apply a class to the SPAN.

How can I achieve this? (no javascript please, just CSS)

EDIT: I found that the DIV inherits something that makes it 1px in height...

like image 787
jldupont Avatar asked Apr 06 '13 17:04

jldupont


People also ask

Can I put span inside div?

It should be used only when no other semantic element is appropriate. <span> is very much like a <div> element, but <div> is a block-level element whereas a <span> is an inline element.

Can I style a span element?

The <span> tag is easily styled by CSS or manipulated with JavaScript using the class or id attribute. The <span> tag is much like the <div> element, but <div> is a block-level element and <span> is an inline element.

Can we add style in span?

The span tag is just like a div, which is used to group similar content so it can all be styled together. But span is different in that it is an inline element, as opposed to div , which is a block element. Also, keep in mind that span itself does not have any effect on its content unless you style it.

How do you use div and SPAN together?

Span and div are both generic HTML elements that group together related parts of a web page. However, they serve different functions. A div element is used for block-level organization and styling of page elements, whereas a span element is used for inline organization and styling.


2 Answers

Just add it to .msg-warn if you want to style the whole class and have it inherit.

.msg-warn {
    background-color:#F00;
}

If you want to style all <span>s within .msg-warn target like this:

.msg-warn span {
    background-color:#F00;
}

If you want to style all direct descendant <span>s (immediate children, not children of children) use >

.msg-warn > span {
    background-color:#F00;
}
like image 177
Daniel Imms Avatar answered Oct 20 '22 02:10

Daniel Imms


Have you tried something like the following?

div.msg-warn > span {
   background-color: #f00;
}
like image 25
sidoh Avatar answered Oct 20 '22 01:10

sidoh