Solved

CSS question: How to change P tag style within one Div only

Posted on 2011-02-17
5
677 Views
Last Modified: 2012-06-22
This is an HTML or CSS question dealing with the Paragraph and Division tags.
If say  the P tag has a default width of  600px. Then assume you want the
P style to have a width of 400px but only with in one Div.
How can this be done without having to add a style to each P in the Div?
Is there some sort of style look up function?

0
Comment
Question by:jimr111998
  • 2
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
HagayMandel earned 500 total points
ID: 34916769
You give your containing div an id:
<div id="sample">
<p>
Sample text
</p>

Open in new window


then you can apply the style the following way
#sample p {
	width: 400px;
}

Open in new window


The text in the P tag that is in the "sample" div (and only there),  will have a width of 400px

0
 
LVL 2

Expert Comment

by:moehler
ID: 34917042
Are you saying you have something like this:

<html>
<head>
      
</head>

</body>
      <div>
      <p>para1</p>
      <p>para2</p>
      <p>para3</p>
      <p>para4</p>
      </div>
      <div>
      <p>para1</p>
      <p>para2</p>
      <p>para3</p>
      <p>para4</p>
      </div>
      <div>
      <p>para1</p>
      <p>para2</p>
      <p>para3</p>
      <p>para4</p>
      </div>
</body>

and you want to only apply a given style to para1?

if you want to limit it to just the first para of the first div, you can do this

<style>
      div:first-child p:first-child{
            background:yellow;
      }
</style>

if you want it to be all first para1 then do this

<style>
      div p:first-child{
            background:yellow;
      }
</style>

If so
0
 
LVL 2

Expert Comment

by:moehler
ID: 34917062
the comment was submitted before I was done editing it.

Sorry, that was for background color. If you want to do width, just make appropriate replacement.
0
 

Author Comment

by:jimr111998
ID: 34918037
HagayMandel,

Yes tried it and it works but I wanted something more general.
Something that can be used whenever needed without having
to create another style def.

So I took your information and tried this

.P400px P {
  width: 400px;
}

Then used it like this
<div class="BaseDef  P400px">

</div>
and it works. So now whenever I need to limit paragraph widths in a div to 400px
i just add P400px to the class def.

Thanks for pointing me in the right direction. You get the points.









0
 

Author Closing Comment

by:jimr111998
ID: 34918053
see my response to HagayMandel
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now