HTML PRIMER

by Jim Gordley
2008-2016

Controlling Element Width

By Default most text elements extend the full width of the browser window. If you don't control the width of these elements the length of your text can be so long it becomes less readable. When designing your web pages controlling the width of elements can be very important in establishing your desired page layout. Leaving elements at their default settings limit design options.

CONTROLLING WIDTH

Controlling width with style sheets is the most effective way of keeping width and presentation design under control. Using the width property with a pixel value in a CSS declaration.

p {width:700px;}

Using the example above all <p>: elements would be 700 pixels in width. It is important to keep in mind that this width is independent of the browser window size. In other words if the bowser window is less than 700 pixels the text will not word wrap and some of the paragaph will not be seen.

A flexible width can be accomplished by using a percentage value instead of a pixel value.

p {width:75%;}

Using the example above the width of every <p> element will take up 75% of the browser window regardless of that windows size.

CLASS LAB

Resize your broser window and observe the text in the Gettysburg Address and the text above it. Notice that the text in the speech adjusts to the window size and the text outside does not.

THE GETTYSBURG ADDRESS:

Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation or any nation so conceived and so dedicated can long endure. We are met on a great battlefield of that war. We have come to dedicate a portion of that field as a final resting-place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But in a larger sense, we cannot dedicate, we cannot consecrate, we cannot hallow this ground. The brave men, living and dead who struggled here have consecrated it far above our poor power to add or detract. The world will little note nor long remember what we say here, but it can never forget what they did here. It is for us the living rather to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us--that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion--that we here highly resolve that these dead shall not have died in vain, that this nation under God shall have a new birth of freedom, and that government of the people, by the people, for the people shall not perish from the earth.

Inline and block elements

We learned earlier about block elements and inline elements. A block element creates line breaks. Examples of block elements are <p>, <div> and <h1>-<h6>. These elements create line breaks

Inline elements do not create line breaks. Examples of inline elements are <b>, <span>, and <strong>. These elements do not create line breaks.

Block elements are by default just as wide as their parent element. An example of that is the text you are reading right now. This text is in the block element <p>. The parent element of this <p> is a <table> element with a fixed width of 750 pixels. So by default the <p> element will have a width that is 100% as wide as the <table> element, the parent.

CONTROLLING ELEMENT WIDTH
DO THIS WHY YOU DO IT
  1. On the task bar click the Firefox icon and select the index.html file we have been working with. Check out the widths of the nav and heading elements

  2. On the task bar select the styles.css Notepad.

    Add the following highlited code to the #nav id declaration

    #nav {background-color:#FF9933;
             width;290px;}


    Save the changes to styles.css

  3. Refresh the page in your browser.

  4. Switch to the styles.css Notepad

    Add the following highlited code to the #content id

    #content {margin:18px;
             width:550px;}


    Save the changes.


  5. Refresh the page in your browser.

Notice that each of these elements are both children of their parent element. In this case the <body> element. Their width is the same as the parent element, the width of the entire browser window.

To edit the # nav id


To set the width of the # nav id







To view the effect of the changes.

To edit the file in Notepad

To set the margin width of the #content id








Notice how everything within the #content id has a fixed with of 550 pixels.

Element Spacing