Steps to center the background color and text on screen:1. Determine the dimensions of

I've tried many different approaches, but I'm struggling to figure it out. In my code below, the h1 and h2 elements have a gold and black background respectively. Although I've set their width to 500px, they are stuck at the top left corner of the page. Even with text-align: center, I can't get them to move to the center.

h1, h2 {    
    width:500px;
    text-align: center;
   }
   
h1 {
    background-color: goldenrod;
    margin: 5px;
}
h2 {
    margin: 5px;
    color: goldenrod;
    background-color: black;
   }

.content-table {
    font-family: Helvetica;
    border-collapse:collapse;
    font-size: 0.9em;
    min-width: 400px;
    border-radius: 5px 5px 5px 5px;
    overflow: hidden;
    box-shadow: -5px 5px 14px black;
    margin: auto;    
}

.content-table thead tr {
    background-color: goldenrod;
    font-size: 1.5em;
    color: black;
    text-align: left;
    font-weight: bold;
}
    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>
    <meta http-equiv="X-UA-Compatible" content="IE-edge">
    <meta name="viewport" content="width=device-width, initial-scale=10">
    <title>HTML Table Reference Cheat Sheet</title>
    <link href="style.css" rel="stylesheet">
</head>
<body>
    <h1>HTML Table Reference</h1>
    <h2>Table Tags</h2>

    <table class="content-table">
        <thead>
            <tr>
              <th>Tag</th>
              <th>Name</th>
              <th>Description</th>
            </tr>
            
        </thead>

        <tbody>
            <tr>
                <td><span class="code"&gt;&amp;lttable&gt</span></td> 
                <td>Table</td>
                <td>The wrapper element for all HTML tables.</td>
            </tr>

            <tr>
                <td><span class="code">&lthead&gt</span></td>
                <td>Table Head</td>
                <td>The set of rows defining the column headers in a table.</td>
            </tr>

            <tr>
                <td><span class="code">&ltbody&gt</span></td>
                <td>Table Body</td>
                <td>The set of rows containing actual table data.</td>
            </tr>

            <tr>
                <td><span class="code">&lttr&gt</span></td>
                <td>Table Row</td>
                <td>The table row container.</td>
            </tr>

            <tr>
                <td><span class="code">&lttd&gt</span></td>
                <td>Table Data</td>
                <td>The table data container.</td>
            </tr>

            <tr>
                <td><span class="code">&ltfoot&gt</span></td>
                <td>Table Foot</td>
                <td>The set of rows defining the footer in a table.</td>
            </tr>
        </tbody>




    </table>


    
</body>
</html>

Answer №1

To create a centered header, I added a div with the class "header" and applied flexbox properties to it. This ensures that the content is aligned in the center vertically and horizontally.

.header{
display:flex;
justify-content:center;
align-items:center;
flex-flow:column;

  .header h1, h2 {    
        width:500px;
        text-align: center;
       }
       
   .header h1 {
        background-color: goldenrod;
        margin: 5px;
    }
   .header h2 {
        margin: 5px;
        color: goldenrod;
        background-color: black;
       }

    .content-table {
        font-family: Helvetica;
        border-collapse:collapse;
        font-size: 0.9em;
        min-width: 400px;
        border-radius: 5px 5px 5px 5px;
        overflow: hidden;
        box-shadow: -5px 5px 14px black;
        margin: auto;    
    }

    .content-table thead tr {
        background-color: goldenrod;
        font-size: 1.5em;
        color: black;
        text-align: left;
        font-weight: bold;
    }
        <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>HTML Table Reference Cheat Sheet</title>
        <link href="style.css" rel="stylesheet">
    </head>
    <body>
<div class="header">
        <h1>HTML Table Reference</h1>
        <h2>Table Tags</h2>

        <table class="content-table">
            <thead>
                <tr>
                  <th>Tag</th>
                  <th>Name</th>
                  <th>Description</th>
                </tr>
                
            </thead>

            <tbody>
                <tr>
                    <td><span class="code">&lttable&gt</span></td> 
                    <td>Table</td>
                    <td>The wrapper element for all HTML tables.</td>
                </tr>

                <tr>
                    <td><span class="code">&lthead&gt</span></td>
                    <td>Table Head</td>
                    <td>The set of rows defining the column headers in a table.</td>
                </tr>

                <tr>
                    <td><span class="code">&ltbody&gt</span></td>
                    <td>Table Body</td>
                    <td>The set of rows containing actual table data.</td>
                </tr>

                <tr>
                    <td><span class="code">&lttr&gt</span></td>
                    <td>Table Row</td>
                    <td>The table row container.</td>
                </tr>

                <tr>
                    <td><span class="code">&lttd&gt</span></td>
                    <td>Table Data</td>
                    <td>The table data container.</td>
                </tr>

                <tr>
                    <td><span class="code">&ltfoot&gt</span></td>
                    <td>Table Foot</td>
                    <td>The set of rows defining the footer in a table.</td>
                </tr>
            </tbody>




        </table>

</div>
        
    </body>
    </html>

Answer №2

text-align: center is functioning as intended by centering the text within the H1 and H2 elements. To also center the elements themselves, adjust the left and right margins to auto:

h1,
h2 {
  width: 500px;
  text-align: center;
}

h1 {
  background-color: goldenrod;
  margin: 5px auto;
}

h2 {
  margin: 5px auto;
  color: goldenrod;
  background-color: black;
}

.content-table {
  font-family: Helvetica;
  border-collapse: collapse;
  font-size: 0.9em;
  min-width: 400px;
  border-radius: 5px 5px 5px 5px;
  overflow: hidden;
  box-shadow: -5px 5px 14px black;
  margin: auto;
}

.content-table thead tr {
  background-color: goldenrod;
  font-size: 1.5em;
  color: black;
  text-align: left;
  font-weight: bold;
}
<h1>HTML Table Reference</h1>
<h2>Table Tags</h2>
<table class="content-table">
  <thead>
    <tr>
      <th>Tag</th>
      <th>Name</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><span class="code">&lttable&gt</span></td>
      <td>Table</td>
      <td>The wrapper element for all HTML tables.</td>
    </tr>
    <tr>
      <td><span class="code">&lthead&gt</span></td>
      <td>Table Head</td>
      <td>The set of rows defining the column headers in a table.</td>
    </tr>
    <tr>
      <td><span class="code">&ltbody&gt</span></td>
      <td>Table Body</td>
      <td>The set of rows containing actual table data.</td>
    </tr>
    <tr>
      <td><span class="code">&lttr&gt</span></td>
      <td>Table Row</td>
      <td>The table row container.</td>
    </tr>
    <tr>
      <td><span class="code">&lttd&gt</span></td>
      <td>Table Data</td>
      <td>The table data container.</td>
    </tr>
    <tr>
      <td><span class="code">&ltfoot&gt</span></td>
      <td>Table Foot</td>
      <td>The set of rows defining the footer in a table.</td>
    </tr>
  </tbody>
</table>

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

PHP data insertion using filters and validation

I have created a database and implemented a validation system to ensure that fields are not left empty. If the fields are filled and pass through the sanitization function, I then proceed to save the data in the database. <?php // Define variables as ...

Can you explain the steps to implement this feature in a modal window using jQuery?

My HTML list contains a bunch of li elements. ul li img .det li I am looking to create a modal popup that appears when I click on something. I want this modal popup to have previous and next buttons, preferably implemented using jQuery. I have alr ...

Having trouble with the functionality of Bootstrap 5 carousel?

I've been attempting to integrate this carousel into my Bootstrap 5 webpage. I found it on Codepen - https://codepen.io/sahil-verma/pen/wxXryx Thus far, I've copied the code into my webpage and linked the corresponding CSS within style tags, sim ...

What is the best way to create spacing between images in a CSS image slider?

UPDATE: I've made significant progress by modifying the code provided in the link below. I am very close to achieving my desired result, but there are parts of it that still puzzle me. The transition now has spacing on both sides and the hidden space ...

Encountering Error 404 1668 in CMD preventing connection to my CSS file within my HTML document

In the base.html file, my script is focused on the tree depicted in the image. I have verified that the href is correct and have loaded static at the very top of the HTML file. Despite this, I am still unable to connect the CSS file. Can anyone provide ass ...

Update your ASP.NET application to replace checkboxes with images

I have attempted the following methods, and I believe the question is pretty clear. Attempted Solution Using CSS: (Unfortunately, this approach did not work as expected and I had to resort to using a jsfiddle) .star + label{ background:url('imag ...

What is the best way to center align tabs in a Bootstrap 4 list?

I am working with tabs structured like this: <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/> <ul class="nav nav-pills mb-3" id="pills-tab" role="tablist"> <li class="nav-item"> ...

How can PHP be utilized to dynamically add classes to <li> elements and alternate them after every third item

I'm trying to find a way to add a class to an unordered list and alternate that class after every third item. Here's the code I have so far: <?php $i=1; foreach($this->items as $item) : ?> <li class="<?php if ($i % 3 == 0) : ...

Having trouble inserting an item into a SQL database using SQLAlchemy for a Flask web application in Python. What steps should I take to diagnose the issue and find out why it is not functioning properly?

Running into a bit of trouble here. New to Flask and web development in general. Attempting to insert a new item into a database with SQLAlchemy, but it's proving to be quite challenging. This is my initial project and it's extremely basic. I&apo ...

The radio button default selection is checked, but the desired styles are not being applied

My webpage features two radio buttons, "No" and "Yes," with the default selection being "No." I have implemented CSS styles for the checked elements, but they only work once physically selected. My goal is to apply these styles immediately on page load wit ...

IE has trouble with CSS inline-block and width-auto styling, leading to incorrect rendering

Recently, I've noticed a strange behavior in Internet Explorer 9-11 and Edge (Spartan). In all browsers except for IE, my example displays as desired: However, in Internet Explorer, it looks different: I used to encounter this issue on multiple bro ...

Creating a button with a water-inspired design using CSS that works seamlessly across

Looking to create a stylish aqua-themed button that works seamlessly across different browsers. I'm new to CSS, so any guidance is appreciated. I found an online example that looks great in Chrome but fails to display properly in IE... Check out the ...

Server issues causing Laravel 6 CSS and JS to fail loading

I compressed my project folder and uploaded it to the Document Root for the subdomain. All of my CSS and JS files are located inside the Public folder. The project works perfectly fine on XAMPP, but when I transfer it to my shared hosting, the CSS and JS f ...

How can we save the final composite image created with image layers, Cufon, jQuery,

I'm currently exploring the possibility of a new idea, but I'm not entirely certain if it can be achieved. My progress so far involves loading an image from a URL and using jQuery UI draggable feature to enable users to drag HTML text (which has ...

Tips for positioning label/input box/button on Internet Explorer 7

Here is a Fiddle example you can check out, along with the corresponding code : <div class="menu-alto"> <ul> <li> <a title="Link" href="#">Link</a> </li> <li class="newsletter ...

Utilizing CSS position sticky in Bootstrap 4 to maintain visibility of a sidebar

I currently have a layout with two columns structured like this: <div class="row"> <div class="col-xs-8 content"> </div> <div class="col-xs-4"> </div> </div> By applying the position:sticky to the si ...

Is there a way to dynamically alter the content depending on the index of the current slide using swiper.js?

Hi, I am new to utilizing the Swiper framework and so far, it has been one of the best sliders I have ever experienced. Currently, I am trying to establish a connection between 2 div tags - one tag holds the content of each slide while the other tag contro ...

Instructions for adding up all the values in each column of an HTML table and showing the total in the footer of that specific column

Can someone help me with my HTML table setup? I have a table structure as shown below, and I am looking to add a "Total" row at the footer for each specific "quarter". "quarter1" "quarter2" "quarter3" "quarter4" 250000 115000 ...

When an SVG image is embedded, its color may not change even after being converted to an inline SVG

I've inserted an SVG using an img tag. When hovering over it, I want the fill color of the SVG to change. I attempted to convert the SVG to inline SVG following this method, but it doesn't seem to be working as expected. No console errors are b ...

Content is cleared from the leaflet popup upon first closure

In my application, I've been working extensively with Leaflet and have a good grasp on the "leaflet way" of doing things. Since I have a single static layout for all my markers, I decided to create a simple Mustache template to insert variable data a ...