0

I have to modify a website that another developer wrote.

I need the font and size from one class, but I need the formatting from another.

html:

 <div class="box1">
     <div class="box2">
         <p class="p1">
            The text I need printed
         </p>
     </div>
 </div>

css:

.box1
{
    float:left;
    width:500px;
    padding:10px 0px 10px 15px;
}

.box1 p.p1
{
    font-size:15px;
    color:#ff00ff;
    line-height:22px;
}

    ...

.box2
{
    padding:10px;
}

.box2 p.p1
{
    font-size:12px;
    color:#ff0000;
    line-height:22px;
}

How do I use p1 from box1 while keeping the alignment the same? I would rather not define a new .box2 p.p2 unless that is the only way.

Thanks.

Raymond
  • 105
  • 1
  • 3
  • 13

3 Answers3

1

From the examples you posted, I don't think you need multiple classes here (as others are suggesting). Do you realize what is going on? Your paragraph is being applied both paragraph rules you have defined, .box1 p.p1 and .box2 p.p1. Since the latter overwrites all properties from the former, those are the styles you see.

I think all you need to do is remove the second rule (.box2 p.p1) entirely.

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
0

I am not entirely clear on what you are trying to accomplish, but the basic answer to your question (about basically needing to combine two classes) is that you can have an element with more than one class, just separate them by spaces

class="class1 class2 ...."
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
0

Try this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Untitled Document</title>
        <style type="text/css">
            .box1
            {
                float:left;
                width:500px;
                padding:10px 0px 10px 15px;
            }
            .box1 .box2 p.p1
            {
                font-size:15px;
                color:#ff00ff;
                line-height:22px;
            }
            .box2
            {
                padding:10px;
            }
            .box2 p.p1
            {
                font-size:12px;
                color:#ff0000;
                line-height:22px;
            }
        </style>
    </head>
    <body>
        <div class="box1">
            <div class="box2">
                <p class="p1">The text I need printed</p>
            </div>
        </div>
    </body>
</html>

Here's a demo - http://jsfiddle.net/fedev/ZwrfV/

UPDATED

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Untitled Document</title>
        <style type="text/css">
            .box1
            {
                float:left;
                width:500px;
                padding:10px 0px 10px 15px;
            }
            .box1 p.p1, .box1 .box2 p.p1
            {
                font-size:15px;
                color:#ff00ff;
                line-height:22px;
            }
            .box2
            {
                padding:10px;
            }
            .box2 p.p1
            {
                font-size:12px;
                color:#ff0000;
                line-height:22px;
            }
        </style>
    </head>
    <body>
        <div class="box1">
                        <p class="p1">The text I need printed under box 1</p>
            <div class="box2">
                <p class="p1">The text I need printed under box 2</p>
            </div>
        </div>
    </body>
</html>

Another demo - http://jsfiddle.net/fedev/M9mWN/

techlead
  • 779
  • 7
  • 24
  • 44
  • This almost worked! "The text I need printed" is correct. And other texts where box2 p.p1 are in the other format. The only problem is other texts that were under box1 p.p1 are not formatted. How do I keep that formatting? Thanks – Raymond Mar 16 '12 at 16:45
  • Please see under *UPDATED*. I created another demo for you. – techlead Mar 16 '12 at 17:07