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

Display an image overlay upon hovering over a div

I've developed an add-on for Elementor that includes a card with the following HTML markup: <div class="card"> <div class="header"> <img src="img.jpg"> < ...

Correcting the reference to "/" (root) for assets after relocating the site to a subdirectory

I currently have a website located in the public_html directory, where all assets (images, css, js, etc) are referenced using /asset_folder/asset. The "/" at the beginning ensures that the browser starts from the root and navigates through the directories. ...

Tooltips envelope around themselves

Several paragraphs contain tooltips, but some of them break onto a new line instead of staying within the text. <div class="row"> ... </div> Fiddle: https://jsfiddle.net/LhjmLw8k/29/ The issue is that certain words like "Trasimeno" and ...

Update dynamically generated CSS automatically

Is there a way to dynamically change the CSS? The problem I'm facing is that the CSS is generated by the framework itself, making it impossible for me to declare or modify it. Here's the scenario at runtime: I am looking to target the swiper-pa ...

Display a specific paragraph inside a div using jQuery

I am having trouble getting my jQuery code to display a specific paragraph when a particular div is clicked on. My goal is for the content "v" to be displayed if the user clicks on the ".Virus" div, and for the contents of ".screenInfo" to be shown if the ...

Steps to implement a text border in Raphael.js

I have a website dedicated to creating customized football jersey designs. I'm utilizing Raphael.js for customization, and I am looking for a way to add an outline to text using Raphael. My research led me to the suggestion of using the stroke proper ...

Bizarre actions with jQuery append in Internet Explorer 8

Issue with adding elements to a div in IE8: The element is not added until the button is clicked twice, resulting in two new elements being added at once. Here's the code snippet in question: $(options.addButton).click(function(event) { var proto ...

Arrange a line of images in the center

I've been working on aligning images in a row and centering them using flex, but I'm having trouble figuring it out. I managed to center all the images with flex and justify-content center, but setting flex-direction: row on the container doesn&a ...

Designing a search form to browse through the database by entering a specific title

I am currently working on designing a form to perform database searches for specific titles. However, I have encountered an issue where nothing happens when I try to search, possibly due to a problem with connecting to the database. The link to the appli ...

Improving Java Performance by frequently replacing one String with another

Currently, I am developing an HttpServlet extension (plugin) for a CMS that is responsible for filtering the HTML response. Within my method filterResponse, I retrieve the requested text/html as a String, which is one of three parameters. The main task a ...

What is the best way to prevent 2 divs from overlapping on a webpage?

I'm having trouble getting the smaller div to display right below the larger red div. I tried using the display block css property, but it doesn't seem to be working as expected. Unfortunately, I can't provide an image here, but you can vie ...

Having Trouble with the Background Video Scaling on My HTML Bootstrap Page for Mobile Devices

I have been working on an HTML background video page, and everything seems to be working perfectly on desktop. However, when it comes to mobile, the video does not cover the full screen. Only half of the video is visible on the mobile screen. Here is the ...

Is there a way to display the back and forward buttons on a popup window opened through window.open or self.open in Chrome browser?

When I try to open a popup window using the code snippet below, I am facing some issues. self.open('myJSPPage','ServicePopUp','height=600,width=800,resizable=yes,scrollbars=yes,toolbar=yes,menubar=yes,location=yes'); Afte ...

What are some methods for preventing JavaScript function calls from the browser console?

In the process of developing a web application using HTML and JavaScript, I'm looking for a way to prevent users from accessing functions through their browser console in order to maintain fairness and avoid cheating. The functions I want to protect a ...

The input field will be in a read-only state if it contains a value from the database. However, it will be editable if the value

Hello everyone, I am a newcomer to this community and would greatly appreciate your help. I am encountering an issue with the following lines of code: <input type="text" class="form-control" id="fines" name="fines&quo ...

Develop a responsive component on the right side

I am encountering an issue with creating a responsive div that matches the height of the image on the left side. I want to include text in this div, however, when I do so, the paragraph expands in height along with the div element when I attempt to change ...

Having trouble reaching the upload file in PHP

I am attempting to transfer files to a remote server using JavaScript, with PHP as the backend. The JavaScript code seems to be functioning properly, but on the PHP side, the $_FILES and $_POST arrays are empty. However, the $_SERVER array contains some da ...

Position elements to the right side of a flex container

I'm utilizing Bootstrap 4 along with this CSS class to vertically align my elements: .vertical-align { display: flex; flex-direction: row; } .vertical-align > [class^="col-"], .vertical-align > [class*=" col-"] { display: flex; align-i ...

Utilize the Sed command to manipulate the input stream by substituting all occurrences of HTML <i> tags with <em> tags

I am attempting to create a regex with the sed command in order to process the input stream and replace all HTML tags with em tags. For example: This is <i id="x">emphasized text</i> and <i>so is this</i>. should be replaced by Thi ...

Troubles with modal functionality in Ionic application involving ion-slide-box

Utilizing ion-slider to display images has been a seamless experience, except for one hiccup. If I navigate directly from the first full image back to the home screen, the slider ceases to function properly. To address this challenge, I have employed spec ...