What's the best way to use CSS to center an HTML element on a page?

My goal is to perfectly center an HTML element on a page, ensuring that the center of the element aligns with the center of the page.

The current code I am using is as follows:

#associate {
    position:absolute;
    color:white;
    background-color:red;
    margin-top:55px;
    margin-left:15%;
    margin-right:15%;
    max-width:70%;
    text-align:center;
    padding:2.5%;
    max-height:17.5%;
    border-radius:25px;
    border-style:outset;
    border-color:white;
    border-width:5px;
}

When the content inside the element exceeds the max-width:70%;, it centers perfectly on the page. However, if the content falls short of this width limit, the element shifts to the left with a 15% margin.

In a previous project where I needed to rotate an element, I could specify the rotation point with:

transform-origin:0% 50%;
transform:rotate(90deg);

Is there a similar method to define the central point of an element so that it aligns with the center of the page? I aim to place the center of the element at the center of the page and allow the sides to expand as needed to accommodate more text until reaching the max-width:70%;.

Answer №1

To efficiently center an element (rather than just the text inside it), you can utilize CSS flex properties on its container.

This code snippet places your element in a container that spans the entire viewport and utilizes justify-content and align-items to centrally position it both horizontally and vertically.

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100vw;
}

#associate {
  position: absolute;
  color: white;
  background-color: red;
  margin-top: 55px;
  margin-left: 15%;
  margin-right: 15%;
  max-width: 70%;
  text-align: center;
  padding: 2.5%;
  max-height: 17.5%;
  border-radius: 25px;
  border-style: outset;
  border-color: white;
  border-width: 5px;
}
<div class="container">
  <div id="associate">
    Here is some content<br> And another line of it.
  </div>
</div>

Answer №2

To center the element, you have a couple of options. You can either utilize text-align:center; in CSS or employ the <center></center> tag in 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

Issue encountered when attempting to print a Bootstrap table using Google Chrome

Encountering a strange issue while attempting to print my webpage in Chrome. Using Bootstrap, I have a table that adjusts perfectly to the screen size but gets cropped when trying to print in Chrome, unlike Firefox where it prints perfectly. Here is a lin ...

Retrieving information from a JSON file and displaying it in an HTML document using Bootstrap, AJAX requests, and a Mustache

I have a website that is built using bootstrap. I am looking to create a div element with data stored in a JSON file and render it using mustache. The script for the mustache template is included within the HTML: <script id="plutos" type="text/template ...

Is it feasible to display a message for a certain duration without using the alert() function upon clicking a button?

I'm working on a NEXT.JS project and I need to display a <p> element under my button for a specific duration before it disappears. I don't want to use the alert() function. Any suggestions on how to achieve this? ...

Ways to boost an array index in JavaScript

I recently developed a JavaScript function that involves defining an array and then appending the values of that array to an HTML table. However, I am facing an issue with increasing the array index dynamically. <script src="https://cdnjs.cloudflare. ...

I am encountering difficulties displaying the image and CSS files from another folder in my HTML with Node.js

I am facing difficulty in establishing a connection between my front-end and back-end using node.js. As a result, the website only displays the HTML code without linking to the CSS or image files. Here is the folder structure: root -src •pi ...

The true meaning of CSS3 transition is often misconstr

I'm trying to create a simple linear transition effect, but for some reason it's not working. Any suggestions on what might be causing the issue? HTML <div class="button-modern" style="background-color:#e73032"> <span style="color: ...

Issue with Bootstrap Modal failing to display the dialog box

I have been attempting to incorporate a Bootstrap Modal for quite some time now. Despite following the guidelines in the bootstrap documentation, I am still unable to get it to work properly. Any ideas why this jsfiddle link is not functioning as expected? ...

What causes my form submission to redirect to a php file instead of staying on the

I'm currently using PHP mailer to send emails with data. The form submission is working perfectly on XAMPP locally, as I receive the email with the specified data after submitting the form. However, when I try running my app on Vercel or Netlify, it ...

Could someone offer me some guidance on the proper placement and presentation of CSS elements?

Just arrived here for the first time. Feeling a bit nervous, so let's dive right in. Currently delving into CSS and experimenting with various positioning and display examples on different websites. To practice and set a goal for myself, I decided t ...

Retrieving data from an API after the onClick event is activated

I'm facing a bit of a challenge with an HTML & JavaScript issue, which I believe should be straightforward to resolve. Since frontend development is not really my forte, I am hoping to find some assistance here. Task: A friend who owns a website buil ...

What is the best way to make a DIV box span the entire webpage without overflowing to the left?

I am currently working on a client's website and running into some issues with the layout of the booking page. The address is supposed to be on the left side, the contact form on the right, and both should be contained within a white box that fills th ...

Automatically adapt the height of a bootstrap button based on the dimensions of other bootstrap buttons

First and foremost, my objective is to centrally align a glyphicon both vertically and horizontally within a button. Despite attempting CSS attributes like position: relative; and position: absolute;, my efforts have been in vain. You can check out the sam ...

Dynamic stacking of buttons in response to user navigation choices

Is it possible to create a navigation bar with 5 buttons using CSS and JS? Here is what I am looking to achieve: Display 5 nav/button options When a user clicks on a button, the other buttons should transition or hide, leaving only the selected button vi ...

What causes an image background to take precedence over the background color of Bootstrap buttons?

Why is the CSS overriding the bootstrap button background styling and making it transparent instead of the default color? For example, if I have a button with the following styling in my document that sets the background-image of the entire body to an ima ...

Spring form submission with HTML

I am currently working on building a straightforward login page using Spring framework. I have followed the steps outlined in the tutorial [here][1] to create a form. However, I am facing an issue where the request is not being forwarded to the control ...

What methods are available in JavaScript for determining the width of a section and implementing proper spacing for a menu?

Is it possible to use Javascript to dynamically adjust the size of each <li> element within the menuArea so that they fill the entire space correctly? // Could we achieve this with the following code... var menuSpace = Math.floor((menuAreaWidth ...

Tips for displaying a fallback image when the desired image is not found in HTML 5

Is there a way to display a default image indicating that the image is not found when one of the images on the page cannot be loaded? If so, how can it be implemented? ...

The loading speed is being impacted by the jQuery in the <head> section and <scripts> in my code

My website includes Slick JS on the index page and Colcade on another page. Before launching my site, I want to ensure that the loading times are optimized. Can someone review the order of appearance for these scripts and confirm if it's correct? Than ...

Adding a stylish underline to text using HTML and CSS

Looking for some assistance with my Html and CSS website project. I am currently facing a challenge trying to place a border below the text as shown in the provided image. Below is the snippet of my Html code: <div class="Sectionheading"> ...

Guide on combining vendor CSS files in a React application using Webpack

Incorporating third-party libraries is an essential part of my project. For example, I have Mapbox GL installed via npm, which comes with CSS files needed for its functionality. The Mapbox GL CSS file can be found at mapbox-gl/dist/mapbox-gl.css in the no ...