Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to display a header and paragraph separated by only one line break 1

Status
Not open for further replies.

gnubie

Programmer
Apr 16, 2002
103
0
0
US
I am trying to find out the PREFERRED method to display a header <h1> followed by a paragraph <p> where the paragraph starts on the next line. As shown.


HEADER TEXT
Paragraph text


Not:

HEADER TEXT

Paragraph text


This is the code that I think should work, but the <p> causes a line break before the paragraph. I don't want to have to have omit the <p> of 1st paragraph after a header.

[tt][blue]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<title>Inline Test</title>
<style type="text/css">
h1 {display: inline;}
</style>
</head>
<body>
<h1>Header</h1>
<p>Line 1</p>
</body>
</html>
[/blue]
[/tt]

Is there any way to use the HTML as is, but change the CSS to accomplish my goal?

BTW, I know I can futz around by doing an h1 display: inline, followed by a <br>, followed by plain text (no <p>) but that seems like such a hack.

I also tried display: compact and run-in, but it didn't help.
 
After more experimentation, I came up with this solution which achieves the goal. Let me know if you come up with anything better.

[tt][blue]
p {
margin-top:0;
margin-bottom: 1em;
}
h1,h2,h3 {
margin-top:0;
margin-bottom:0;
}
[/blue]
[/tt]
 
Your second solution is perfect, there is no better way of dealing with it. There are default margins on elements, which define the spacing between elements and you removed them. You could be more specific and try with the adjacent selectors (applying the rules only when the elements appear next to each other) to make sure that the gaps are nullified only when the specific paragraph following h element occurs. However this method would then not work in IE6.
Code:
h1 + p {
  margin-top: 0;
  /* this will only affect a paragraph that immediately follows h1 */
}
Also, before using made up values such as compact and run-in for display, I suggest you look up an online reference on CSS (from W3 for instance) to learn what the possible values are for specific properties.

[small]Do something about world cancer today: Comprehensive cancer control information at PACT[/small]
 
Thanks for the explanation and tip.

I did checkout the references regarding compact and run-in and found it to be too brief. Including my favorite, W3C. It pretty much says "... depending on the context, blah blah. I wanted to know what context caused what behavior. Sigh. I'll keep looking.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top