0

This probably is the most frequently asked question however I am not really able to find anything that seems to work for me - I am a newbie @ styling/css.

I would like a cross browser compliant solution (IE9 or higher btw) where I can have as a header (first row) 3 columns:

Name_____DOB______Contact

underneath, there will be content. a row of data and I want the row of data (which is contained in a div) to be aligned correctly with the column headings.

Currently I have this:

<div style="width: 100%">
   <div style="width: 300px; float:left">Name</div>
   <div style="width: 200px; float:left">DOB</div>
   <div style="width: 100px; float:left">Contact</div>
</div>

in terms of the data, I am using MVC4 with Razor so I am just using a for loop to go through a collection of data and spitting it out in a div i.e:

[for loop here]

   <div id="refitem_@counter">
      [data here]
   </div>
[end for loop]
6
  • 3
    If you're creating a table use <table> Commented Sep 10, 2013 at 19:58
  • 1
    although using <table> is bad practice for creating layout. its the best practice for creating tables. so don't try to invent the wheel, for a table use a <table>. Commented Sep 10, 2013 at 20:04
  • I was trying to avoid using table because its old/outdated etc... and want to move forward with CSS3 and HTML5 Commented Sep 10, 2013 at 20:50
  • 2
    Tables are only old and outdated for page layout. Tables serve a very useful purpose for displaying repeating data in rows and columns. I don't see tables ever disappearing from html. Commented Sep 10, 2013 at 20:52
  • Well, can anyone give a solution using the div's? Commented Sep 11, 2013 at 9:00

1 Answer 1

1

You can continue with the same approach.

<div>
    <div style="width: 100%">
       <div style="width: 300px; float:left">Name</div>
       <div style="width: 200px; float:left">DOB</div>
       <div style="width: 100px; float:left">Contact</div>
    </div>
    <div style="width: 100%">
       <div style="width: 300px; float:left">Name 1</div>
       <div style="width: 200px; float:left">DOB 1</div>
       <div style="width: 100px; float:left">Contact 1</div>
    </div>
    <div style="width: 100%">
       <div style="width: 300px; float:left">Name 2</div>
       <div style="width: 200px; float:left">DOB 2</div>
       <div style="width: 100px; float:left">Contact 2</div>
    </div>
    ..
</div>

I'm strongly dis-advise you to use that. use a table instead. also (as a BTY): don't mix your markup and CSS, use something like that.

HTML:

<div>
    <div>
       <div class="column col1">Name</div>
       <div class="column col2">DOB</div>
       <div class="column col3">Contact</div>
    </div>
    <div>
       <div class="column col1">Name 1</div>
       <div class="column col2">DOB 1</div>
       <div class="column col3">Contact 1</div>
    </div>
    <div>
       <div class="column col1">Name 2</div>
       <div class="column col2">DOB 2</div>
       <div class="column col3">Contact 2</div>
    </div>
    ..
</div>

CSS:

.column
{
    float: left;
}
.col1
{
    width: 300px;
}
.col2
{
    width: 200px;
}
.col3
{
    width: 100px;
}

width: 100%; is the default behaviour for a block element (like a div) so you should omit it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.