?
Solved

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

Posted on 2011-02-17
5
Medium Priority
?
682 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
HagayMandel earned 2000 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

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
The viewer will learn how to count occurrences of each item in an array.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Suggested Courses

770 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