Some CSS "must knows" from me, and please share yours

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • StuartD
    Sofa King Band
    • Jul 2002
    • 29903

    #1

    Some CSS "must knows" from me, and please share yours

    More and more people are learning CSS to not just affect the look, but the over all design of their sites.... and not just designers, but people making up their own pages with an average to lower knowledge of design.

    For those people, here are some HUGE fundamentals that I have learned that make getting that layout right, much easier.


    1. DOCTYPE - cross browser compatibility.
    The hardest thing in the world is getting something to look the same in IE, Firefox, Safari and Opera. And there's a reason for that, they all were created unequally. However, there is a way to tell them to try their best to do things the same... and that's the DOCTYPE.

    Now, the BEST thing you can do, is learn the subtle differences inherent in XHTML, which includes putting /> as a closer to tags. These things are annoying to learn but once you do, you'll get pages looking just how you want. To make your page an XHTML document, you use this at the very top:

    <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
    'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
    <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en' id='yoursitename'>

    Notice the word "strict" in there. That is what tells all the browsers that this page MUST use the standards set out by W3C... or at least, to the best of it's ability.

    Now, XHTML is a tad annoying in it's differences. So if you don't want to learn those differences as well as CSS all in one shot, then you can simply force your HTML4 to be "strict" as well.
    To make your page an HTML4 strict document, you use this at the very top:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>

    And that's it... your browsers, no matter what make or model, will try their very best to make sure that 0px really means 0px.

    If you use a WYSIWYG editor, pay attention to this because chances are it filled in this line for you and used "loose" settings, rather than strict.
    If the line is not used at all, the browser will assume "loose" and try to use it's own best judgment rather than standards.


    2. float: left and float:right

    For those of you who are actually using divs to position things on a page, float is your best friend. If you want div2 to be to the right of div1, you set div2 to "float:right;". Easy right?

    Well, what happens a lot of the times is that yes, div2 will be to the right. But it'll be right justified way over on the right side of the screen.

    What went wrong?

    Well, it's floating right of div1, but of the entire page as well. The solution to this is to float div1 left. This puts it left justified as it would have been anyway while left alone.... and then float div2 left as well.

    This means div1 = left.... div2 = left. That will infact still put them beside each other and in order... but keep them left justified.

    You can do this over and over again with however many divs.


    3. float:right puts something underneath still

    What happens a lot of the times is that someone makes a main div with width 750px. It fits on most resolutions.

    Then a left column will be div1 = 250px; and then make div2 float right and width 500px;. The problem? div2 will still be under div1.

    The cause for this is that divs have padding and margins by default, and borders even add pixels if you have them on. So if you add padding, margins and borders... 250 + 500 will actually become much more than 750 in the end. This means that div1 and div2 won't fit... and so it wraps down div2 much like a word processor wraps text.

    The solution is to manage that padding, margin and border and make sure you keep very close count on just how many pixels are being used for each div. This means counting the LEFT and RIGHT side of the div.


    3. clear: both;

    So you have some divs floating left and right... but the next div you make, even though it doesn't have a float set... still shows up to the right of the last div! How come???

    It's because it's still listening to the CSS of the previous div.

    It's for this reason that "clear:both;" was created, but not everyone knows about it.

    clear:both is actually just the fast way of saying clear: left AND clear: right... so you could use them individually if you only have one set in the divs above it.

    If you have div1 float left, and div2 float left... and want div3 to be neatly underneath, then you'll need a dummy div... like this.

    <div1 style='float: left;'> text </div>
    <div2 style='float: left;'> text </div>
    <div style='clear:both;'></div>
    <div3>Text goes underneath here</div>






    Most people who know CSS well will say "well, duh" to this stuff. But for people who are just learning to move divs around and use them instead of tables to make layouts.... these things will save you many many headaches down the road.

    I hope it helps... and I hope others share their discoveries that have made CSS much easier.
    This is me on facebook
    This is me on twitter
  • ServerGenius
    Confirmed User
    • Feb 2002
    • 9377

    #2
    CSS rules......yet still way to less people seem to be using it
    | http://www.sinnerscash.com/ | ICQ: 370820 | Skype: SinnersCash | AdultWhosWho |

    Comment

    • StuartD
      Sofa King Band
      • Jul 2002
      • 29903

      #3
      Originally posted by ServerGenius
      CSS rules......yet still way to less people seem to be using it
      Well, CSS has been around for a very long time and only now are designers really starting to use it to it's real potential.

      It can be intimidating, especially since some problems are difficult to find solutions too.

      This is why I created this thread.
      This is me on facebook
      This is me on twitter

      Comment

      • cool1
        sex is good
        • Sep 2001
        • 24939

        #4
        trying to learn it all now
        it reminds of the first days of learning html
        lots of stuff to learn, and long hours doin it.

        Comment

        • StuartD
          Sofa King Band
          • Jul 2002
          • 29903

          #5
          Originally posted by cool1
          trying to learn it all now
          it reminds of the first days of learning html
          lots of stuff to learn, and long hours doin it.
          Yup, it's one of those things that you'll never stop learning better ways to do simple things on your pages. Even after years, you'll do something different one day and go "wow, that's way better than the way I was doing it before!"

          CSS, as little vocab or syntax as there is to learn, has millions of different combinations to put together, which makes learning last a long time.

          And it's worth it.
          This is me on facebook
          This is me on twitter

          Comment

          • fris
            Too lazy to set a custom title
            • Aug 2002
            • 55679

            #6
            their is a bug with ie and relative
            Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence.

            Comment

            • ServerGenius
              Confirmed User
              • Feb 2002
              • 9377

              #7
              Originally posted by Fris
              their is a bug with ie and relative
              IE has a another definition of pixel size
              | http://www.sinnerscash.com/ | ICQ: 370820 | Skype: SinnersCash | AdultWhosWho |

              Comment

              • StuartD
                Sofa King Band
                • Jul 2002
                • 29903

                #8
                Originally posted by Fris
                their is a bug with ie and relative
                IE has a lot of bugs, and using the "strict" DOCTYPE will not be a guarantee of anything... there will still be some things that IE simply will refuse to do according to standards. That's M$ for you.

                But at least it's a start.
                This is me on facebook
                This is me on twitter

                Comment

                • fris
                  Too lazy to set a custom title
                  • Aug 2002
                  • 55679

                  #9
                  ya and i noticed yesterday when specifying a height in a table example <table width="468" height="60">

                  it doesnt like that according to w3 it wants it height="style: height 60"
                  Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence.

                  Comment

                  • StuartD
                    Sofa King Band
                    • Jul 2002
                    • 29903

                    #10
                    Another tip that took me a while to figure out.....

                    4. Centering a div that has absolute positioning

                    If you've ever set something to absolute, then tried to put margin: auto on it... you'll be thinking "why does it work in one browser, but not the other one if doctype is set to strict?"

                    There is a trick to this that will work in all browsers...

                    First you take the width of the div. In my example, we'll go with 500px;

                    So we have
                    #div {
                    width: 500px;
                    }

                    Now we need it to be in the middle... so we apply a "left: 50%;"

                    #div {
                    width: 500px;
                    left: 50%;
                    }

                    That puts the left edge of the div in the middle. Which is close, but not right because the center of the div should be in the center of the page.
                    To solve this, we margin it back by half. Half of 500px is 250px;

                    #div {
                    width: 500px;
                    left: 50%;
                    margin: 0px 0px 0px -250px;
                    }

                    And presto, your absolute positioned div is now dead center in the page.

                    It's also right at the top edge. Centering it vertically is a whole other beast and will require some javascript, especially if you scroll. And this is a CSS thread, not a javascript thread. Sorry, but it is possible.... keep working and you'll get it.
                    This is me on facebook
                    This is me on twitter

                    Comment

                    • StuartD
                      Sofa King Band
                      • Jul 2002
                      • 29903

                      #11
                      Originally posted by Fris
                      ya and i noticed yesterday when specifying a height in a table example <table width="468" height="60">

                      it doesnt like that according to w3 it wants it height="style: height 60"
                      Yeah, height= is technically not an attribute in html by most browsers.
                      Browsers insist that height be set via CSS if at all ( style='height: 60px;' )

                      If you try to validate your page using W3C's validaters, you'll actually find that a lot of HTML is "deprecated" now, in favor of CSS.

                      Height never was something common, but in the not too distance future, HTML tags themselves such as <center> and <b> and so on will be unacceptable in good web design.
                      This is me on facebook
                      This is me on twitter

                      Comment

                      • Goodings Media
                        Confirmed User
                        • Apr 2007
                        • 1987

                        #12
                        just a headsup.

                        Theres ways to get around having to use a 'dummy' div with a "clear:both". Having your divs fit snugly into containers means you wont need to, only in a few cases (usually for footers and when you cba to play around)

                        just as ".clear { clear:both } " with make a pretty big gap too, so will look a little weird.

                        Good idea to do this thread Shame GFY doesnt have post editing, or you could just keep expanding on the initial post like you would on any other 'tutorial' style forum
                        ICQ: 446-568-913 Email: liam||goodingsmedia.com msn: [email protected]

                        Comment

                        • Goodings Media
                          Confirmed User
                          • Apr 2007
                          • 1987

                          #13
                          ^^^ sorry for the engrish, noticed too late to edit
                          ICQ: 446-568-913 Email: liam||goodingsmedia.com msn: [email protected]

                          Comment

                          • StuartD
                            Sofa King Band
                            • Jul 2002
                            • 29903

                            #14
                            Originally posted by Goodings Media
                            just a headsup.

                            Theres ways to get around having to use a 'dummy' div with a "clear:both". Having your divs fit snugly into containers means you wont need to, only in a few cases (usually for footers and when you cba to play around)

                            just as ".clear { clear:both } " with make a pretty big gap too, so will look a little weird.

                            Good idea to do this thread Shame GFY doesnt have post editing, or you could just keep expanding on the initial post like you would on any other 'tutorial' style forum
                            It's very true... fitting things in snuggly is the best route to go. Then the next div will simply wrap below. But this can lead to things being off centered or just not quite where you want them sometimes. It's a matter of trial and error... use what serves best at the time.

                            And yes, it would have been nice to keep adding more and more to post #1. Oh well
                            This is me on facebook
                            This is me on twitter

                            Comment

                            • Elli
                              Reach for those stars!
                              • Apr 2003
                              • 17991

                              #15
                              Bookmarked!
                              email: [email protected]

                              Comment

                              Working...