Simple learning excercise - 2 blocks of text with css (no table)

Hi, I am in my starting part of the learning curve to try using css instead of tables.

I want to start to do something simple.

I need a left block of text and a right block of text (same width) as if we have

</tr>
<td> text 1 </td>
<td> text 2 </td>
</tr> 

So i tried this:

{
	margin-right: 60%;
	margin-left:10px;
	margin-top: 10px;
	margin-bottom:0;
	border:thin;
	background-color:#FFF;
	font: 9pt "trebuchet ms", arial, sans-serif;
	color:#003318;
	display:block;
}




.introtxt_r
{
	margin-left: 60%;
	margin-right:10px;
	margin-top: 10px;
	border:thin;
	background-color:#FFF;
	font: 9pt "trebuchet ms", arial, sans-serif;
	color:#003318;
	display:block;
}

<div class="introtxt_l"> text1 </div>
<div class="introtxt_r"> text2 </div>




The negative results:
1: No border
2: The left hand side box is good, but the right hand side box is ot next to it but below it (well aligned to the right)

I played a bit with some css elements (like float) but now I gave up!

Help!

To get straight into it the below points are the key things to know.

  • By default DIV elements are display: block which means they fill the entire space they are used in
  • You need to use float: left when aligning elements side by side unless they are inline, as per my first point
  • Using margin-left and margin-right will cause the columns to drop below one another as the margin takes affect from the first column, not the edge of the page (unless its floated)

See the following demo which is an example of floating 2 columns side by.

http://cdpn.io/f22794171f99f5f84891d321d604e4cb

CSS

.column {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  background-color: red;
  color: #fff;
  float: left;
  padding: 10px;
  width: 50%;
}

Be aware I wrote this example with the intention that you’re only supporting IE8+ which support box sizing.

I’ve adapted the code as follows:


.introtxt_l
{
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  background-color:#DDD;
  color: #000;
  float: left;
  padding: 10px;
  width: 49%;
  padding:10px;
}

.introtxt_r
{
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  background-color:#DDD;
  color: #000;
  float: right;
  padding: 10px;
  width: 49%;
  padding:10px;
}

I cannot however set the two boxes to have an inset margin from the left (for the left box) or the right (for the right box) so as they are aligned vertically with teh content above/below. Using the margin-left / -right, the boxes will wrap over each other if the window is resized.

Secondly, there is a wide gap between the boxes and the content below it. Can’t figure out how to eliminate it either.

Check website here: Testpage

The link you posted points to your local server which I can’t see, also you don’t need to duplicate your selector code as you can simply comma separate your selectors to share the same CSS rules.

.introtxt_l,
.introtxt_r {
     -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
     box-sizing: border-box;

     background-color:#DDD;
     color: #000;
     float: left;
     padding: 10px;
     width: 49%;
}

.introtxt_r {
     float: right;
}

Ooops!

http://www.maltanaturetours.com/Tour01-MistraSelmunMalta.php

And this page, with a 4.01 Transitional doctype, shows 141 errors in the HTML.

Many of them are because of the use of <font> tag (depreciated). As I said, the code originated from a template which was heavy modified and mutilated! I am fixing them.

Please do you know how can I move the left box slightly to the right. Margin-left cannot be used.

Ronpat, now the errors should have been reduced by 95% (ignoring depreciation alerts). The graphical table were using the tag moduleid which is not further used.

You could increase the left padding a bit instead.

.introtxt_l {padding: 10px 10px 10px [COLOR="#FF0000"]20[/COLOR]px;

I am working up an example for you. I’m not very fast, so it will take a few minutes. :slight_smile:

I do not advocate using properties that are still under development, especially if there is no graceful fallback for non-supporting devices; therefore, my suggested code does not use the box-sizing property. That’s my preference and is no reflection on anyone else (call me a slow adopter, if you wish).

I suggest making the following change to the HTML:

from:


<td class="toptext">
    <a name="top"></a>
    <h1>MALTA NATURE TOURS</h1>
    [color=red]<br>[/color]
    <div class="introtxt_l">
        text here
    </div>
    <div class="introtxt_r">
        text here
    </div>
</td>

to:


<td class="toptext">
    <a name="top"></a>
    <h1>MALTA NATURE TOURS</h1>
    [color=blue]<div class="intro">[/color]
        <div class="introtxt_l">
            [color=blue]<p><span>introduction</span>... text here...</p>[/color]
        </div>
        <div class="introtxt_r">
            [color=blue]<p><span>mission</span>... text here...</p>[/color]
        </div>
    [color=blue]</div>[/color]
</td>

In the above change, we add <div class=“intro”> and delete the <br> tag. Remember that <div class=“intro”> has a closing tag, too.
The text is changed by putting it in <p> tags and changing the <font> tags to <span> tags.

The new HTML with text should look like this:


<td class="toptext">
    <a name="top"></a>
    <h1>MALTA NATURE TOURS</h1>
    <div class="intro">
        <div class="introtxt_l">
            <p><span>introduction:</span> Backed with 7 years of experience, MaltaNatureTours.com specializes in organising <b>guided nature walks</b> in Malta and Gozo. Identification and information about the <b>biodiversity</b> encountered is supplied in all tours. <b>Small groups</b> guarantee personal attention and a unique experience of the fantastic Maltese natural habitats. During these years, experience made these tours having the right balance of fun, education, non-exhaustive exercise, site seeing, plenty of magnificent views and with a touch of history in the <b>safest and best countryside areas</b> in the Maltese islands.</p>
        </div>
        <div class="introtxt_r">
            <p><span>mission:</span> Our mission is to promote educatione, awarness and protection to the Maltese biodiversity and natural habiats. For several decades along the development of the country, these habitats were endangered; partly due to lack of awerness and partly due to the growing urbanisation. Major profits from these tours are directed to financially support environmental activities and websites needed to araise awareness and educate younger generations about the importance of our environment. For example, a part of the sister website - <a href="http://www.maltawildplants.com">Maltawildplants.com</a> is supported from such funds.</p>
        </div>
    </div>
</td>

The above HTML is supported by the following CSS. This CSS replaces whatever is currently in place. If in doubt, you can insert it at the bottom of the stylesheet or at top of the HTML page.


.intro {
    overflow:hidden;    /* because contents are floated */
    padding-top:20px;    /* space between intro boxes and "Malta Nature Tours" text */
    padding-left:40px;   /* space to the left of the intro boxes */
}

.introtxt_l,
.introtxt_r {
    background-color:#ddd;
    float:left;
    margin-bottom:25px;
    width:49%;
}

.introtxt_r {
    margin-left:2%;    /* The total width of the contents of div.intro must not exceed 100%;  {width:49%} + {width:49%} + {margin-left:2%} = 100%. */
}

.introtxt_l p,
.introtxt_r p {
    color:#000;
    font-size:1em;
    line-height:1.5;
    margin:.5em 1em;
}

.introtxt_l p span,
.introtxt_r p span {
    font-size:1.5em;
    text-transform:uppercase;
}

This is a screenshot showing how it appears on my PC.

Everyone is entitled to their own view on things which is fine, however IE7 is dead with less than a 1% global footprint which means box-sizing is completely safe to use since IE8 supports border-box without any major issues.

The prefixes are only used to support legacy versions of Chrome, Firefox etc…

Thanks, Chris. Truly, I’m slow to pick up on these things as they become accepted. If properties work in IE8+ and all other major browsers (which are automatically updated) I’m usually happy with them. Thanks again for the input (I appreciate it!).

EDIT: I just checked http://caniuse.com/#feat=css3-boxsizing and they seem a bit less convinced that it’s fully accepted, so maybe I’ll still take the conservative road for a while longer. Can’t hurt. I’ll keep my eye on it, though.

In the notes, they say—

Partial support refers to supporting only the “border-box” value, not “padding-box” (which was added to the spec later).

That means that all of the browsers listed there support border-box, which is good enough for me. :slight_smile:

Steve, the number of validation errors went down from 141 to 135.

The reason I mentioned the Transitional doctype in post #6 is because there are NO errors for deprecated elements included in the error tally. Most of these are legit errors. I imagine most are fixable. I tinkered with it and got it down to 26 errors before I stopped. My tinkering will probably remain unfinished. It’s a long page and requires a few hours of work to clean up.

I’ll stop nagging about validating the HTML, but it’s important for you to realize that CSS cannot work its magic reliably if the HTML that it styles contains certain types of errors. That’s just the way it is.

I’m slightly hesitant because FF still requires the -moz- prefix, and apparently plans to do so for the next few versions. Anyway, I’m overly cautious as a defense mechanism because I find it difficult to keep up with the incremental evolution of the properties. Plus, apparently I didn’t understand what that note was saying, either; I interpreted it to mean exactly the opposite. :rolleyes: (“… my native tongue”)

Hi RonPat and Chris. Thanks for your help. I went with Ronpat css as I was more understanding what is happening in the code and also relearing the css which I forgot most of it. The test page is here: www.maltanaturetours.com/index.php From this post, now I have created another block (green col) beneath the two intro text boxes. I tried to apply the cascading effect like in the example above for having the last two lines (those not in <ul>) to be smaller. I tried many ways but it does not want to get smaller



.maintxt  {
    background-color:#ded;
    float:left;
    margin-bottom:0px;
    width:90%;
    padding: 15px;
    border:double;
	 font-size:12pt;
	 margin-left:4%;
	 margin-right:6%;
}

.maintext p {
	font-size:10pt;
}



<div class="maintxt">
<ul>
<li>Prices...</li>
<li>For a quote...</li>
<li>Please note... </li>
</ul>

<p class="maintxt"> Full details about selected tours are provided below.... </p>
</div>


I tried using span and also giving a different classname to the small font, but it seems nothing makes effect.
What I am doing wrong ?

Oh no! lol! What validator are you using. I did mine with the tool on Dreamweaver V8. I have table cleaned one page (this) and was really a brain damage experience. If there are still 135 errors on this page, than its amazing.!

I placed the reported faults [here]

are you passing the partially validated file some way to see what are these errors. ?

Delete the class from


<p [color=red]class="maintxt"[/color]>

so you just have <p> without a class.

See what happens.

What web browser do you use?

I just noticed that I misdiagnosed the problem in message #17.


.maintxt  {
    background-color:#ded;
    margin-bottom:0px;
    width:90%;
    padding: 15px;
    border:3px double #000;
    font-size:12pt;
    margin-left:4%;
}
.maintxt p {    /* maintxt was spelled wrong "maintext" */
    font-size:10pt;
}


<div class="maintxt">
    <ul>
        <li>Prices...</li>
        <li>For a quote...</li>
        <li>Please note... </li>
    </ul>
    <p> Full details about selected tours are provided below.... </p>
</div>

That should do it.