What is the best way to center align an element while having another element situated directly below it?

I want to create a grid of "clock" elements, with five clocks in each row. Below each clock, I'd like to display the DAY and DATE centered with the clock (day on top line, date below). Since I'm new to html/css, I appreciate your patience. Here's what I have so far for the first line of 5 clocks:

<div class="clock">

<h4><canvas id="piechart0" width="150" height="150"></canvas></h4>
<h4><canvas id="piechart1" width="150" height="150"></canvas></h4>
<h4><canvas id="piechart2" width="150" height="150"></canvas></h4>
<h4><canvas id="piechart3" width="150" height="150"></canvas></h4>
<h4><canvas id="piechart4" width="150" height="150"></canvas></h4>

<div class="date" id="day0"></div>
<div class="date" id="day1"></div>
<div class="date" id="day2"></div>
<div class="date" id="day3"></div>
<div class="date" id="day4"></div>

<div class="date1" id="date0"></div>
<div class="date1" id="date1"></div>
<div class="date1" id="date2"></div>
<div class="date1" id="date3"></div>
<div class="date1" id="date4"></div>
<script>
    Clock.getDates();
</script>

The Clock.getDates() function populates the dates for the "day" and "date" id's. Here is the CSS styling for these elements:

h4 {
    padding-left: 70px;
    padding-top: 0px;
    float: left;
    margin: 15px 0px 0px 0px;
}

.clock {
   text-align: left;
   padding: 0px 0px 0px 0px;
}

.date {
    display: inline;
    padding-left: 0px;
    color: white;
    margin-left: 120px;
    font-weight: bold;
}
.date1 {
    display: inline;
    padding-left: 10px;
    color: white;
}
canvas {
    padding-top: 0px;
    display: inline;
    margin-left: auto;
    margin-right: auto;
    left: 25%
}
  • Goal 1: Create rows of 5 clocks where the two lines of text beneath each clock are centered.

  • Goal 2: Ensure the five clocks (each 150px wide and tall) maintain a distance of 70px between them.

What would be the best approach to achieve these objectives?

Answer №1

1.) Enclose each clock component in a wrapping element:

<clock>

<div class="clockwrapper">
  <h4><canvas id="piechart0" width="150" height="150"></canvas></h4>
  <div class="date" id="day0"></div>
  <div class="date1" id="date0"></div>
</div>
<div class="clockwrapper">
  <h4><canvas id="piechart1" width="150" height="150"></canvas></h4>
  <div class="date" id="day1"></div>
  <div class="date1" id="date1"></div>
</div>
<div class="clockwrapper">
  <h4><canvas id="piechart2" width="150" height="150"></canvas></h4>
  <div class="date" id="day2"></div>
  <div class="date1" id="date2"></div>
</div>
<div class="clockwrapper">
<h4><canvas id="piechart3" width="150" height="150"></canvas></h4>
  <div class="date" id="day3"></div>
  <div class="date1" id="date3"></div>
  </div>
<div class="clockwrapper">
  <h4><canvas id="piechart4" width="150" height="150"></canvas></h4>
  <div class="date" id="day4"></div>
  <div class="date1" id="date4"></div>
</div>

</div>

2.) Styling with CSS:

Set the wrapper elements as inline-blocks and define the desired margins for spacing. Ensure all child elements are blocks that are centered within the wrapper (and horizontally aligned with one another) using margin: 0 auto:

.clockwrapper {
  display: inline-block;
  margin: 35px 0;
  width: 150px;
  height: 150px;
}
.clockwrapper > * {
  display: block;
  margin: 0 auto;
}

Remove unnecessary styling from other CSS rules.

Answer №2

To Achieve Goal 1: Ensure each clock is enclosed within its own block, similar to components. Then, center align the text with their paragraphs by utilizing 100% width of the container (default setting).

To Achieve Goal 2: Make your clocks float (using float: left;) as long as they have a specified width set.

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

Unable to utilize Stats.js with @angular/cli version 1.4.4

Attempting to utilize @types/stats with @angular/cli following the guidance at https://github.com/angular/angular-cli/wiki/stories-third-party-lib. However, encountering a tslint error when trying to import * as STATS from 'stats.js'. [ts] Modul ...

Utilizing Selenium with Java, you can hover over a button and choose to click on any of the available options

Currently, I am utilizing Selenium to automate the user interface using Java. Within the UI, there is an action button that, when hovered over by the User, displays two clickable options - Create and Edit. For ease of use, I have stored CSS locators as E ...

Directing a controller assignment in AngularJS 1.2 via a directive

Transitioning from angularJS 1.0 to 1.2 has presented a challenge for me when it comes to assigning a controller to a directive with a distinct scope, without explicitly defining the controller in my HTML using ng-controller. Let's look at this scena ...

JavaScript functionality is not operational when accessed from a network drive

I'm having an issue running the following file. It works perfectly when run from a local drive but fails to execute when placed on a network drive. Any insights on why this may be happening? The code snippet below is what I am attempting to run, util ...

Tips for accurately obtaining row counts from a dynamic table when the `<tr>` numbers are constantly fluctuating

One issue that I encountered as a tester involved verifying the total number of rows on a dynamic table. Despite having 50 rows in the table, the HTML only displayed a maximum of 22 <tr>. This discrepancy caused my automation code to return an incorr ...

Setting up package.json to relocate node_modules to a different directory outside of the web application:

My web app is currently located in C:\Google-drive\vue-app. When I run the command yarn build, it installs a node_modules folder within C:\Google-drive\vue-app. However, since I am using Google Drive to sync my web app source code to Go ...

jQuery: event not firing for dynamically loaded elements via AJAX

In my jQuery/HTML5 front-end setup (with backend-generated code omitted), I am currently using version 1.8.3 of jQuery with no version conflicts. The front-end calls the following functions: detailAjaxCall("\/client\/orders\/detailsLoad&bso ...

execute a rigorous compilation during the ng build angular process

I am currently working on a project using angular-cli and I have configured my package.json with the following scripts: "scripts": { "ng": "ng", "build": "ng build --base-href /test/", "prod": "ng build --prod --base-href /test/" } According to the ...

Tips for determining if a key is present in local storage:

I need to set a key value, but only if it doesn't already exist. In my component1.ts file, I am assigning the key and value in the constructor. However, I want to include a condition that this action should only be taken if the key is not already pre ...

Retrieve the red, green, and blue components of a color in the RGB format

When I retrieve "rgb(18, 115, 224)" from a DOM element, I need to convert this color to hexadecimal in order to assign it to a span element. To get the hexadecimal equivalent of the color, I can use the following code snippet: "#" + componentToHex(r) + co ...

The tag <li> is not allowing enough room for the content to expand as needed

I am trying to create a list using ul li elements. When the content inside the li exceeds the width, a scrollbar appears. However, I am encountering an issue where the li tag does not cover the entire width and spills outside. .container{ b ...

What happens when a browser can support multiple versions of a font - which one does it choose

If I have two versions of a font, customFont1.woff2 and customFont1.woff, and I list the woff2 version first followed by the woff version in the font declaration file, what happens when the browser supports both formats? Will it download both fonts or ju ...

Can we display various React components based on the screen size?

I am looking to implement various components that will display at different breakpoints. I attempted this using Material-UI import withWidth, { isWidthUp } from '@material-ui/core/withWidth'; function MyComponent(props) { if (isWidthUp('s ...

Puppeteer encounters difficulty when attempting to click on a particular div element within Gmail

I am attempting to click on the three dots in Gmail by following this link: 1 After that, I want to click on the 'mark all as read' option: 2 Clicking on the three dots works without any issues. However, I am encountering difficulty when tryin ...

Frontend utilizing the Next-auth Github Provider for Profile Consumption

After following the official documentation for implementing SSO with the Next-auth Github provider in my App, I encountered an issue where the Client API documentation suggested using useSession() to retrieve session information, but it was not returning t ...

Executing a script on a different HTML page without the need to reload it

I'm currently working on incorporating a notification system inspired by Facebook's design. This system will feature an icon in the header that, when clicked, reveals a drop-down containing a table of sorted notifications for the user. My goal is ...

What steps can I take to resolve the TypeError issue with tailwindcss-cli where it is throwing an error stating that Object.fromEntries is

I've been diligently following tutorials by Tailwind and have encountered an error while trying to run the command npx tailwindcss-cli build css/tailwind.css -o build/tailwind.css. The error message I received is related to Object.fromEntries not bein ...

What is the best way to trigger an event in VueJS?

I recently implemented a table using Vuetify in my project. The table is now split into two components - the Table component and the Row component. My challenge is how to handle the same function, this.selected = !this.selected!, when dealing with 2 differ ...

When the parent div overflows, the child div remains hidden within the boundaries of the parent div

<section class="margin-top-30"> <div class="row" style="position: relative;"> <div class="col-sm-4"> <div class="lightbgblock" style="overflow-x:visible;overflow-y:scroll;"> ...

The Vue v-on:click event listener seems to be unresponsive when applied to a

I've been attempting to utilize the on-click directive within a component, but for some reason, it doesn't seem to be functioning. Whenever I click on the component, nothing happens, even though I should see 'test clicked' in the consol ...