Fine-tuning the design of the Box Model layout

I can't seem to get the table in my code to center properly and not be stuck against the left border. Despite trying various solutions, the table in column 2 is floating off to the left and touching the border. I need it to be more centered within the box. Can anyone offer assistance on how to make the table look like this:

   header {
    text-align: center;
    font-size: 30px;
    font-style: bold;
    font-style: italic;
  }
  
  #column-1 {
    float: left;
    background-color: lightblue;
    width: 25%;
    border: 5px solid red;
    padding: 20px;
    margin: 2px;
    height: 400px
  }
  
  #column-2 {
    background-color: lightblue;
    width: 100%;
    border: 5px solid red;
    padding: 20px;
    margin: 2px;
    text-align: center;
    height: 400px
<!DOCTYPE html>
<html>

  <head>
    <title>Logans Dinner</title>
  </head>

  <body>
    <header>Welcome to Logans Dinner</header>
    <div id='column-1'>
      <nav>
        <ul>
          <li><a href='/Menu.html'>Menu</a></li>
          <li><a href='/About.html'>About Us</a></li>
          <li><a href='/Contact.html'>Contact Us</a></li>
        </ul>
      </nav>
  </header>
  </div>
  <div id='column-2'>
    <section>
      <p>Our Menu</p>
      <table>
        <thead>
          <tr>
            <th>Food</th>
            <th>Description</th>
            <th>Price</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Burger</td>
            <td>1/2 lb Angus Beef</td>
            <td>$10.99</td>
          </tr>
        </tbody>
      </table>
  </div>

  </section>
  </body>

</html>

Answer №1

To center your table, you can utilize the CSS rule margin: 0 auto. Essentially, this shorthand is equivalent to setting

margin-top: 0; margin-bottom: 0; margin-left: auto; margin-right: auto;

Furthermore, I've observed that there are some HTML tags mismatched in your code (such as an early </body> and an extra </header>). It's crucial to ensure that your opening and closing tags are correctly paired and positioned.

header {
  text-align: center;
  font-size: 30px;
  font-style: bold;
  font-style: italic;
}

#column-1 {
  float: left;
  background-color: lightblue;
  width: 25%;
  border: 5px solid red;
  padding: 20px;
  margin: 2px;
  height: 400px
}

#column-2 {
  background-color: lightblue;
  width: 100%;
  border: 5px solid red;
  padding: 20px;
  margin: 2px;
  text-align: center;
  height: 400px
}

table {
  margin: 0 auto;
}
<header>Welcome to Logans Dinner</header>
<div id='column-1'>
  <nav>
    <ul>
      <li><a href='/Menu.html'>Menu</a></li>
      <li><a href='/About.html'>About Us</a></li>
      <li><a href='/Contact.html'>Contact Us</a></li>
    </ul>
  </nav>
</div>
<div id='column-2'>
  <section>
    <p>Our Menu</p>
    <table>
      <thead>
        <tr>
          <th>Food</th>
          <th>Description</th>
          <th>Price</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Burger</td>
          <td>1/2 lb Angus Beef</td>
          <td>$10.99</td>
        </tr>
      </tbody>
    </table>
  </div>

</section>

Answer №2

      header {
        text-align: center;
        font-size: 30px;
        font-style: bold;
        font-style: italic;
        height: 50px
      }

      #column-1 {
        float: left;
        background-color: lightblue;
        width: 25%;
        border: 5px solid red;
        padding: 20px;
        margin: 2px;
        height: 400px
      }

      #column-2 {
        background-color: lightblue;
        width: 100%;
        border: 5px solid red;
        padding: 20px;
        margin: 2px;
        text-align: center;
        height: 400px 
      }
      
      p {
        font-size: 150%;
        font-weight: bold;
      }

      table {
        margin: 0 auto;
      }

      table, th, td {
        border: 2px solid black;
        border-collapse: collapse;
      }
      td {
        padding: 15px;
      }

      /*The following CSS classes add styling to the table*/

      .table-striped thead tr:first-child th {
        background-color: #FF0000;
      }
      
      .table-striped tbody tr:nth-child(odd) td {
        background-color: #81DAF5;
      }

      .table-striped tbody tr:nth-child(even) td {
        background-color: #CEECF5;
      }
<!DOCTYPE html>
<html>
  <head>
    <title>Logans Dinner</title>
  </head>
  
  <body>
    <header>Welcome to Logans Dinner</header>
    <div id='column-1'>
      <nav>
        <ul>
          <li><a href='/Menu.html'>Menu</a></li>
          <li><a href='/About.html'>About Us</a></li>
          <li><a href='/Contact.html'>Contact Us</a></li>
        </ul>
      </nav>
    </div>
    <div id='column-2'>
      <section>
        <p>Our Menu</p>
        <table class="table-striped">
           <thead>
            <tr>
              <th>Food</th>
              <th>Description</th>
              <th>Price</th>
            </tr>
          </thead>
          
          <tbody>
            <tr>
              <td>Burger</td>
              <td>1/2 lb Angus Beef</td>
              <td>$10.99</td>
            </tr>
            <tr>
              <td>Cheese Burger</td>
              <td>1/2 lb Angus Beef</td>
              <td>$12.99</td>
            </tr>
            <tr>
              <td>Burger with fries</td>
              <td>1/2 lb Angus Beef</td>
              <td>$15.89</td>
            </tr>
            <tr>
              <td>Double Burger</td>
              <td>1/2 lb Angus Beef</td>
              <td>$19.89</td>
            </tr>
            <tr>
              <td>Chicken Burger</td>
              <td>1/2 lb Angus Beef</td>
              <td>$11.75</td>
            </tr>
            <tr>
              <td>Black Burger</td>
              <td>1/2 lb Angus Beef</td>
              <td>$13.50</td>
            </tr>
           
          </tbody>
          
        </table>
        </div>
      </section>
  </body>

</html>

I have created a sample code based on the image you provided for your reference. The CSS code is included within the HTML document, but you can move it to a separate CSS file if needed.

<!DOCTYPE html>
<html>
  <head>
    <title>Logans Dinner</title>

    <style>

      /* This is the CSS code for styling the table and other elements */
      header {
        text-align: center;
        font-size: 30px;
        font-style: bold;
        font-style: italic;
        height: 50px
      }

      #column-1 {
        float: left;
        background-color: lightblue;
        width: 25%;
        border: 5px solid red;
        padding: 20px;
        margin: 2px;
        height: 400px
      }

      #column-2 {
        background-color: lightblue;
        width: 100%;
        border: 5px solid red;
        padding: 20px;
        margin: 2px;
        text-align: center;
        height: 400px 
      }

      p {
        font-size: 150%;
        font-weight: bold;
      }

      table {
        margin: 0 auto;
      }

      table, th, td {
        border: 2px solid black;
        border-collapse: collapse;
      }
      td {
        padding: 15px;
      }

      /*This CSS class provide you striped rows on the table*/ 

      .table-striped thead tr:first-child th {
        background-color: #FF0000;
      }

      .table-striped tbody tr:nth-child(odd) td {
        background-color: #81DAF5;
      }

      .table-striped tbody tr:nth-child(even) td {
        background-color: #CEECF5;
      }

    </style>

  </head>

  <body>
    <header>Welcome to Logans Dinner</header>
    <div id='column-1'>
      <nav>
        <ul>
          <li><a href='/Menu.html'>Menu</a></li>
          <li><a href='/About.html'>About Us</a></li>
          <li><a href='/Contact.html'>Contact Us</a></li>
        </ul>
      </nav>
    </div>
    <div id='column-2'>
      <section>
        <p>Our Menu</p>
        <table class="table-striped">
           <thead>
            <tr>
              <th>Food</th>
              <th>Description</th>
              <th>Price</th>
            </tr>
          </thead>

          <tbody>
            <tr>
              <td>Burger</td>
              <td>1/2 lb Angus Beef</td>
              <td>$10.99</td>
            </tr>
            <tr>
              <td>Cheese Burger</td>
              <td>1/2 lb Angus Beef</td>
              <td>$12.99</td>
            </tr>
            <tr>
              <td>Burger with fries</td>
              <td>1/2 lb Angus Beef</td>
              <td>$15.89</td>
            </tr>
            <tr>
              <td>Double Burger</td>
              <td>1/2 lb Angus Beef</td>
              <td>$19.89</td>
            </tr>
            <tr>
              <td>Chicken Burger</td>
              <td>1/2 lb Angus Beef</td>
              <td>$11.75</td>
            </tr>
            <tr>
              <td>Black Burger</td>
              <td>1/2 lb Angus Beef</td>
              <td>$13.50</td>
            </tr>

          </tbody>

        </table>
        </div>
      </section>
  </body>

</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Customize your Bootstrap navbar dropdown to showcase menu items in a horizontal layout

I have been developing a new project that requires a navbar to be positioned at the center of the page, at the top. The current HTML code I am using is as follows... <div class="navbar navbar-inverse navbar-fixed-top center"> <div class="con ...

"Internet Explorer: Unleashing the Magic of Card Fl

I'm encountering a problem that I can't seem to solve. Everything works perfectly in Chrome, FF, Safari, etc., but in Internet Explorer, I see the image on the front side instead of the text on the backside. Can anyone please assist me with this ...

What are the benefits of utilizing the useStyles MaterialUI function instead of traditional CSS?

As I develop my MERN application, I am utilizing Material UI components along with inline attributes. However, I find myself questioning the necessity of creating a JavaScript file just to import the makeStyles function, which essentially outputs an obje ...

Insert between the top navigation bar and the sidebar menu

I am currently in the process of designing my very first website and I have encountered a bit of trouble. I would like to add a page between a navbar and a vertical bar, and I want this page to be displayed when a button on the vertical bar is clicked. You ...

Press the Concealed Button Enclosed in a div Using Selenium with Python

Currently, I am in the process of automating the login on the Workday portal by utilizing Chrome WebDriver in Selenium. My main challenge lies in figuring out how to click on a hidden sign-in button with the following properties: <div class="css-1s ...

What is the best way to modify a constant array in Angular?

Hello team, I'm fresh to working with angular and I have a TypeScript file that contains a list of heroes: export const HEROES: Hero[] = [ { id: 11, name: 'Dr Nice' }, { id: 12, name: 'Narco' }, { id: 13, name: 'Bombas ...

Creating a standalone static page in Wordpress showcasing the header, sidebar, and footer of the twenty fourteen template

I have created a static blank page containing my website's header, sidebar, and footer. However, I am trying to remove the 'style' that is being enforced by the CSS of my WordPress template on the page. Below is the code I am using: <?p ...

Creating subpages using IDs can be accomplished by following these simple steps

Currently, I am in the process of developing a website that contains a plethora of information, specifically news articles. Each news article on my site features an introduction and a header. Upon clicking on a particular news article, the full content is ...

AngularJS enables box to smoothly slide up and down when hovered over

I am attempting to create a div that will overlay an image on hover with a slide up and slide down effect. Can anyone provide guidance on how to achieve this without relying on jQuery? I would prefer to utilize only AngularJS in my application. Here is a ...

Saving data values for utilization in jQuery

Recently, I've come across a series of links structured like this: <a class="colorBox" href="#" title="#0676B3"></a> <a ... <a ... these links all have color values in the title attribute <a ... What I'm trying to do is uti ...

"Modify hyperlink attributes to enhance security and optimize user experience

$("a[href*='youtube']").attr('rel', 'prettyPhoto'); Having some trouble targeting links on a page containing "youtube" in the URL. I'm trying to set their rel attribute to "prettyPhoto" so they open in a lightbox window. ...

jQuery and CSS animation implemented on website performs flawlessly on all browsers, with the exception

I have recently started learning jQuery and I am facing an issue with the website linked below. It works perfectly in Firefox, Chrome, and Opera but not in Safari. <head> <meta charset="UTF-8"> <meta name="viewport" content="width=devic ...

Steps for selecting a radio button based on a MySQL table value:

My table has two fields, name and gender. When I retrieve the data and attempt to display it in input fields, everything works fine for the Name field. However, I am curious if there is a way to automatically select the radio button based on the gender d ...

"Using Flexbox to Align Child Elements of Varying

After learning about flexbox, a pressing question arose: How can I create a layout of columns similar to the one shown in this image? Check out MyCodeFiddle for more details! (http://jsfiddle.net/1s3bo913/) Click here to view the layout project: Layout Pr ...

Positioning of the dropdown in Material UI AutoComplete menus

Is there a way to turn off autocomplete auto position? I would like the autocomplete options to always appear at the bottom. Check out this link for more information! ...

Confused by navigating with php

I've been attempting to convert this link into a PHP foreach loop without any success, and I'm new to Stack Overflow. I've used an array to generate the code, but I am uncertain of how to transform this code into a foreach loop. <ul class ...

Managing the combination of <a href/> and <br/> tags simultaneously

I'm working on a website that features details similar to those shown in the image: Below is the HTML content: <div class="tableholder"> <br> <br> Open <a href="javascript:popWin1('/ASCNA/showTemplate.do? TEMPLATE_TY ...

Adjust the GTK3 tooltip color exclusively for Eclipse when it is operating on Ubuntu

I am using Eclipse Mars on Ubuntu 15.04 and looking to customize the GTK3 tooltip fg/bg color specifically for Eclipse without impacting other applications. I have come across instructions for changing this color system-wide, but I need guidance on how t ...

Revamping the Look: Refreshing Background of Div

I'm attempting to change the background image of the body element on a webpage when I hover over links with data-* attributes. It's working perfectly, but I can't seem to figure out how to create a smooth fade between the images when a link ...

Connect CSS Transition to a click action

Below is the code snippet. When you click on the div, it creates a folding effect to the left. You can view it here. I want to link this effect to the arrows I use for sliding back and forth. For example: The left arrow should move the slide to the l ...