CSS line spacing between h2 and p

Discussion in 'Web Design and Development' started by cooknwitha, Dec 16, 2005.

  1. macrumors 6502a

    cooknwitha

    Joined:
    May 5, 2005
    Location:
    London
    #1
    I'll try and make this logical as I can manage. Is it possible in CSS code to make the spacing between an h2 and a p field to appear like it's only being separated by a <br>?

    Hello, I'm an H2 Heading

    And I'm a P field. See how I have a gap??

    - - - -

    I would like this to be the H2 heading
    And for this to be the p field.

    Am I making any sense? Obviously I can make the h2 lines p and manipulate them with the <span> command that but I'm trying to keep my code as simple as possible and trying to control appearance from one CSS file.

    I hope I'm making sense. I'm a little muddled in the head at the moment. :)
     
  2. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #2
    In your css (or just in a style tag) set margin and padding to 0px for both h2 and p and they should get considerably closer... :)
     
  3. thread starter macrumors 6502a

    cooknwitha

    Joined:
    May 5, 2005
    Location:
    London
    #3
    You, Mitthrawnuruodo, are a champion!! I think I tried almost every combination with padding and margin except that! I wanted it to go

    HEADING
    Para Para Para Para Para Para Para
    Para Para Para Para Para Para Para
    Para Para Para Para Para Para Para

    HEADING
    Para Para Para Para Para Para Para
    Para Para Para Para Para Para Para
    Para Para Para Para Para Para Para

    And I've now achieved that with:
    Code:
    h2 {
    	padding:0px;
    	margin:0px;
    	}
    p {
    	padding:0px;
    	margin-top:0px;
    	}
    
    Thank you so so much! That's going to save me a lot of trouble. :)
     
  4. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #4
    No problem... just glad to help... :)

    I've never understood why the h-tags and p-tag has all that margin by default... that's just <mumble /> stupid... :rolleyes:
     
  5. thread starter macrumors 6502a

    cooknwitha

    Joined:
    May 5, 2005
    Location:
    London
    #5
    I agree. I'm in the process of changing my old Flash-based website into HTML with CSS and I love the flexibility of it. But the gaps between the h tags were driving me insane. But now I've fixed that. :)

    CSS is great though. I had no idea it was so flexible. Not a table to be seen. Not to mention relatively simple to understand.
     
  6. macrumors newbie

    Joined:
    Sep 4, 2010
  7. Mal
    macrumors 603

    Mal

    Joined:
    Jan 6, 2002
    Location:
    Orlando
    #7
    Sounds like a CSS reset would be of use to you. It's set of CSS rules that are often loaded as a separate file, or appended to the beginning of your CSS stylesheet, which sets margin, padding, etc, to 0 for all tags. Google "CSS reset" to find a couple options that will work well.

    jW
     
  8. macrumors 6502a

    Hermes Monster

    Joined:
    May 4, 2010
    Location:
    UK
    #8
    the reset css is a nice idea, but can sometimes cause work in the long run (as I've just found out after inheriting a website!) when coming to add margins etc later on.
     
  9. Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #9
    You should be manually setting the CSS properties for elements though as browsers are not consistent in the properties they employ. It's the whole reason for having the reset. It may seem like more work, but it's much easier than tracking some obscure difference between browsers later.
     
  10. macrumors 6502

    Joined:
    Jun 8, 2009
    #10
    I completely agree, it's much easier to set your properties for elements rather than the various browser default stylesheets that can cause way too many headaches. I personally use Eric Myers Reset.
     
  11. macrumors 6502a

    Joined:
    Jul 23, 2007
    Location:
    Pennsylvania
    #11
    Another way you can do this with the adjacent selector in CSS:

    HTML:
    h2 + p {
    	padding: 0;
    	margin: 0;
    	}
    
    Basically, this will apply the style to the <p> tag ONLY if it comes directly after a <h2> tag. So now if you were to use another heading tag, such as <h1> or <h3> you can style those however you want (for example, if you don't want the heading and paragraph to be close).
     

Share This Page