What is the best way to ensure that the button's left margin matches the left margin of the table at all times?

https://i.sstatic.net/qJwvE.png

Hello there,

I am currently working with HTML/CSS and I have managed to center a table on my page using the CSS class

.tablecenter    { margin-left:auto; margin-right:auto; }

Following the table, I have placed a button:

<input type = "button" value = "Create Listening GIFT" style="???">

However, as shown in the image, the button is aligned directly to the left.

What I aim for: My goal is to position the button starting from where the first column of the table begins. Additionally, when resizing the browser window, causing changes in the left margin of the table, I want the button to move along with the table adjusting its left margin accordingly. In simpler terms, regardless of how the browser window size changes, I want the button to always be right below the table.

How can I achieve this?

CODE:

<div>
        <table class = "tablecenter">
            <thead>
                <tr>
                    <th style="height:80px">
                        <span class="tableheader">Listening - Part 1</span>
                    </th>
                    <th style="height:80px">
                        <span class="tableheader">Listening - Part 2</span>
                    </th>
                    <th style="height:80px">
                        <span class="tableheader">Listening - Part 3</span>
                    </th>
                </tr>
            </thead>

            <tbody>
            <tr>
                <td id = "col1">
                    <label> <span class = "spc7">Speaker A:</span>
                        <select name = "speakerA">
                            <option>Statement 1</option>
                            <option>Statement 2</option>
                            <option>Statement 3</option>
                            <option>Statement 4</option>
                            <option>Statement 5</option>
                            <option>Statement 6</option>
                            <option>Statement 7</option>
                        </select>
                    </label>
                    <p></p>

                    <!-- Other data omitted for brevity -->

                </td>
            </tr>
            </tbody>
        </table>
        <br>
            <input type = "button" value = "Create Listening GIFT">
        </div>

Answer №1

To ensure the button is aligned with the first column of the table, it's important to remove the margin-left:auto; margin-right:auto; rule from the table and instead apply these css styles on the parent div while also including display:table.

div {
      margin: auto;
      display: table;
    }

By following these steps, you can guarantee that the button will always be positioned correctly at the beginning of the first column.

Answer №2

To center the table, try setting the width to 98% in the .tablecenter class:

.tablecenter    { margin-left:auto; margin-right:auto;width:98% }

You can then wrap the button in a div with the same class or create a new class with the same attributes as above:

<div class="tablecenter">
    <input type = "button" value = "Create Listening GIFT">
</div>

Answer №3

My approach was slightly different from the norm. Here is my CSS code:

    .tablecenter {
        margin-left: auto;
        margin-right: auto;

        border-bottom:none;
    }
   tbody td, thead th {
        border:solid black thin
    }

As for the bottom of the table...

        </tbody>
        <tfoot>
            <tr>
                <td colspan="3"><input type="button" value="Create Listening GIFT"></td>
            </tr>
        </tfoot>
    </table>

Answer №4

To implement this code, enclose your code in a div element with a width of 900px and set margin-left and margin-right to auto:

div{
    margin-left:auto;
    margin-right:auto;
    width:900px;
}
table{
  
}
<div>
      <table class="tablecenter">
        <thead>
          <tr>
            <th style="height:80px">
              <span class="tableheader">Listening - Part 1</span>
            </th>
            <th style="height:80px">
              <span class="tableheader">Listening - Part 2</span>
            </th>
            <th style="height:80px">
              <span class="tableheader">Listening - Part 3</span>
            </th>
          </tr>
        </thead>

        <!-- Listening - Part 1-->
        <tbody>
          <tr>
            <td id="col1">
              <label> <span class="spc7">Speaker A:</span>
                <select name="speakerA">
                  <option>Statement 1</option>
                  <option>Statement 2</option>
                  <option>Statement 3</option>
                  <option>Statement 4</option>
                  <option>Statement 5</option>
                  <option>Statement 6</option>
                  <option>Statement 7</option>
                </select>
              </label>
              <p></p>

              <label> <span class="spc7">Speaker B:</span>
                <select name="speakerB">
                  <option>Statement 1</option>
                  <option>Statement 2</option>
                  <option>Statement 3</option>
                  <option>Statement 4</option>
                  <option>Statement 5</option>
                  <option>Statement 6</option>
                  <option>Statement 7</option>
                </select>
              </label>

              <p></p>
              <label> <span class="spc7">Speaker C:</span>
                <select name="speakerC">
                  <option>Statement 1</option>
                  <option>Statement 2</option>
                  <option>Statement 3</option>
                  <option>Statement 4</option>
                  <option>Statement 5</option>
                  <option>Statement 6</option>
                  <option>Statement 7</option>
                </select>
              </label>

              <p></p>
              <label> <span class="spc7">Speaker D:</span>
                <select name="speakerD">
                  <option>Statement 1</option>
                  <option>Statement 2</option>
                  <option>Statement 3</option>
                  <option>Statement 4</option>
                  <option>Statement 5</option>
                  <option>Statement 6</option>
                  <option>Statement 7</option>
                </select>
              </label>
              <p></p>

              <label> <span class="spc7">Speaker E:</span>
                <select name="speakerE">
                  <option>Statement 1</option>
                  <option>Statement 2</option>
                  <option>Statement 3</option>
                  <option>Statement 4</option>
                  <option>Statement 5</option>
                  <option>Statement 6</option>
                  <option>Statement 7</option>
                </select>
              </label>

              <p></p>
              <label> <span class="spc7">Speaker F:</span>
                <select name="speakerF">
                  <option>Statement 1</option>
                  <option>Statement 2</option>
                  <option>Statement 3</option>
                  <option>Statement 4</option>
                  <option>Statement 5</option>
                  <option>Statement 6</option>
                  <option>Statement 7</option>
                </select>
              </label>
            </td>

            <!-- Listening - Part 2-->
            <td id="col2">
              // Statement details here
            </td>

            <!-- Listening - Part 3-->
            <td id="col3">
              // Question details here
            </td>
          </tr>
        </tbody>
      </table>
      <br>
      <input type="button" value="Create Listening GIFT">
    </div>

Alternatively, you can use flexbox by setting the display property to flex, direction to column, and align-content to center:

Check out this code snippet:

div {
  display: flex;
  flex-direction: column;
  align-items: center;
}
input[type="button"] {
  flex: 0 1;
}
<div>
  // Your table content here
  <input type="button" value="Create Listening GIFT">
</div>
html css

I hope this information helps!

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

Creating a Circular Design with a Centered Line and Text Above and Below using Qualtrics (HTML/CSS/Java)

My knowledge of creating shapes and using these programming languages is limited. I am currently working on a survey in Qualtrics and I need to insert text into circular shapes. Creating one circle was straightforward, thanks to some helpful discussions ...

What is the process for determining the area of the section?

Take a look at this page as an example - scrolling down on responsive devices reveals related navigation areas which I aim to change with scrollspy (affix fixed navigation). Having a bootstrap navbar, when on the corresponding section of the navbar while u ...

Guide on adding a personalized table or Div section in a datatable

I am looking to add a custom table above a datatable, specifically I want the custom table to be displayed above the header columns. Here is the code I have tried: columns: [ { "data": "WorkFlowType" }, { "data": "WorkflowInstanceId" } ...

Tips for activating scrolling on a background element even with a modal window currently displayed

Encountering an issue with Angular and Material for Angular - my application contains multiple modals that disable background scrolling when opened. However, there is one notification modal that should not block the background scroll. Despite not having a ...

Mediawiki needs to be able to respond effectively to constantly changing and evolving content

My extension generates content dynamically for certain pages. For example, I create headlines using <html> <h1>, <h2> and <h3> tags. I want my mediawiki to respond to these headlines and generate a directory dynamically. I attempte ...

Show only a cropped section of a resized image within a div

I have a script that calculates the best region of an image to display within a specific div size. This calculation is done in PHP and generates a JSON output like this: {"scale":1.34,"x1":502,"x2":822,"y1":178,"y2":578} The image in question has dimensi ...

AngularJS: The dynamic setting for the stylesheet link tag initiates the request prematurely

I'm encountering a problem that is somewhat similar (although not exactly the same, so please be patient) to the one discussed in Conditionally-rendering css in html head I am dynamically loading a stylesheet using a scope variable defined at the sta ...

The on-screen keyboard using JQuery and CSS is not working properly on an HTML redesign

UPDATE - Check out the modified version here: https://codepen.io/anon/pen/wZQjQy Here is the original for reference: https://codepen.io/anon/pen/YMRLBN (had to utilize CodePen, as simply adding a snippet here doesn't link the CSS) I essentially copie ...

Is it necessary for CSS Media query to load all the referenced CSS files?

When utilizing a CSS Media Query to assign two different stylesheets, such as desktop.css and mobile.css based on the max-width, how does this process function? Is it necessary for both stylesheets to be loaded by the browser every time, and then the appr ...

The footer seems to be malfunctioning

Let's start with the code: CSS: *{ margin:0; padding:0; } body,html{ height:100%; } body{ background:url('../images/bg.png'); background-position:center; background-repeat:no-repeat; background-attachment:fixed; height:100%; ...

Finding JPG images using Jquery selector

I am looking to utilize jQuery to specifically target <a href=""> elements that have 'href' attributes with file extensions '.JPG' or '.jpg' For instance: To only select these 'links' <a target=& ...

Intersecting table columns with a different data table

My task is to create a table with a column that contains another table, where I want the colors of each alternating row to be different. Please refer to the image below (ignore the "CharacterAgain" column). Below is an example of AngularJS code for the ta ...

What might be the reason behind the failure to implement my color class on the text within the <p> element?

I'm looking to streamline my stylesheet to easily change the color of different text elements. I've created a list of general colors that can be applied using classes, like (class="blue") . Here are some examples: .dark-grey { color: #232323; } ...

Restoring the background color to its original shade following alterations made by jQuery

In my table, I have multiple rows with alternating white and grey backgrounds achieved through CSS. .profile tr:nth-child(odd) { background:#eee; } .profile tr:nth-child(even) { background:none; } However, I now want to allow users to select a row ...

CSS: elements that are only visible when positioned above specific elements

Can we create an element that is only visible above specific other elements? For instance, in the following code snippet, I want the .reflection element to be visible only above the .reflective elements (located at the top and bottom) and not visible on t ...

Using CSS to create a strikethrough effect on prices with spacing

I have a woocommerce setup and I want to showcase the original prices by striking them out like a price tag when products are on sale. However, there seems to be an issue with the space that woocommerce automatically adds between the currency and the pric ...

Why Jquery's nth-child selection and conditional formatting are failing to work

I am working with a table and need to format specific data fields based on their content. For instance, any field less than 95% should be highlighted in red. Below is the jQuery code I am using: $(function(){ $('#ConditionalTable td:nth-child(2n ...

Failure to receive results from $.getJSON request

I am currently working on developing a web page that allows users to search and retrieve Wikipedia pages based on their search results. Below is the relevant HTML code for the search feature: <form class='search-form' onsubmit='return f ...

Can you tell me which BBC channel is airing this animation?

Is anyone familiar with the animation on the login screen of BBC when entering the day, month, and year? Check it out here: ...

Google Fonts are displaying in a bold style even when the bold property is not set

I need assistance with an issue involving emails going from Salesforce to Outlook 365. Here is the HTML code snippet: <link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet" type="text/css"> <style> body ...