What is the best way to vertically and horizontally center an element?

Is it possible to center an object in the middle of the screen using margin: 0 auto, but how can you center it vertically too? Additionally, is there a way to make the border in h2 surround only the text and not the whole width?

html

    body {
      padding: 0;
      margin: 0;
    }
    #section1 {
      background: #FEC56B;
      height: 100vh;
    }
    #section2 {
      background: #51C5D4;
      height: 100vh;
    }
    #section3 {
      background: #80D4DF;
      height: 100vh;
    }
    #section1 h2 {
      margin-top: 0px;
      border: 1px solid black;
      font-size: 60px;
      color: white;
      padding: 0px;
    }
<div id="section1">
  <h2>Be awesome!</h2>
</div>

<div id="section2">
</div>

<div id="section3">
</div>

Answer №1

#mainContent h2{

  border: 1px solid black;
  font-size: 60px;
  color: white;
  padding: 0px;
    margin: auto;
    position:absolute;
    top:0;
    bottom:0;
    right:0;left:0;height:60px;width:350px;
}

Answer №2

Give this a try, it's what you're looking for

To achieve the desired effect, simply add text-align: center; to the CSS rule #section1 h2{. See the code snippet below:

body{
  padding: 0;
  margin: 0;
}

#section1{
  background: #FEC56B;
  height: 100vh;
}

#section2{
  background: #51C5D4;
  height:100vh;
}

#section3{
  background: #80D4DF; 
  height:100vh;
}

#section1 h2{
  text-align: center;
  margin-top: 0px;
  border: 1px solid black;
  font-size: 60px;
  color: white;
  padding: 0px;
}
<div id ="section1">
    <h2>Be awesome!</h2>
</div>

<div id ="section2">
</div>

<div id ="section3">
</div>

Answer №3

Give this a shot

body{
  padding: 0;
  margin: 0;
}

#section1{
  background: #FEC56B;
  height: 100vh;
  text-align:center;
}

#section2{
  background: #51C5D4;
  height:100vh;
}

#section3{
  background: #80D4DF; 
  height:100vh;
}

#section1 h2{
  margin-top: 0px;
  border: 1px solid black;
  font-size: 60px;
  color: white;
  padding: 0px;
}
<div id ="section1">
    <h2>Be amazing!</h2>
</div>

<div id ="section2">
</div>

<div id ="section3">
</div>

Answer №4

<div class="snippet" data-lang="js" data-hide="true">
<div class="snippet-code">
<pre class="snippet-code-css lang-css prettyprint-custom"><code>          body{
  padding: 0;
  margin: 0;

}

#section1{
  background: #FEC56B;
  height: 100vh;
}

#section2{
  
  background: #51C5D4;
  height:100vh;
}

#section3{
  background: #80D4DF; 
  height:100vh;
}

#section1 h2 {
    border: 1px solid black;
    color: white;
    font-size: 60px;
    line-height: 100vh;
    margin: 0;
    padding: 0;
    position: relative;
    text-align: center;
    vertical-align: middle;
}
<div id ="section1">
    <h2>Stay amazing!</h2>
</div>

<div id ="section2">
</div>

<div id ="section3">
</div>

Answer №5

<div class="snippet" data-lang="js" data-hide="false">
<div class="snippet-code">
<pre class="snippet-code-css lang-css prettyprint-override"><code>            body{
  padding: 0;
  margin: 0;

}

#section1 {
    background: none repeat scroll 0 0 #fec56b;
    float: left;
    height: 100vh;
    margin: 0;
    padding: 0;
    position: relative;
    width: 100%;
}
#section2{
  
  background: #51C5D4;
  height:100vh;
}

#section3{
  background: #80D4DF; 
  height:100vh;
}


#section1 h2 {
    border: 1px solid black;
    color: white;
    font-size: 60px;
    left: 50%;
    line-height: 100vh;
    margin: -394px 0 0 -157px;
    position: absolute;
    text-align: center;
    top: 50%;
    vertical-align: middle;
}
    <div id ="section1">
        <h2>Be fabulous!</h2>
    </div>

    <div id ="section2">
    </div>

    <div id ="section3">
    </div>

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

The password-protected HTML blog remains hidden until the correct entry is entered into the password box, causing

After successfully creating a password redirect page where entering the correct password redirects you to another URL (psswrdtest.tumblr.com with the password as correctpsswrd, redirecting to google.com*), I attempted to improve it by making the password p ...

Encountering an Issue with Incorporating Multiple SCSS files in Jekyll

Currently, I am in the process of developing a website using Jekyll. My goal is to incorporate multiple SCSS files into the project. Although I successfully added a main file, I have encountered some difficulties with additional files. Here is my director ...

Can anyone provide guidance on how to create this particular shadow effect using CSS?

Is there a method to use CSS to create a green shadow in that particular shape? ...

Steer your keyboard attention towards the parent element that embodies a list

My implementation focuses on making drop down menus accessible via keyboard input using HTML/CSS and JS/jQuery events. The goal of keyboard accessibility includes: Tab key to navigate the menu elements. Pressing the down arrow key opens a focused menu. ...

When using Vue.js, the class binding feature may not function properly if it is referencing another data property that has variants

I am currently developing a basic TODO application. Within my index.html file, I have a main div with the id #app. Inside this div, there is another div with the class .todobox where I intend to display different tasks stored in my main.js file. Each task ...

I need to adjust my navbar in Bootstrap 5 so that one <li> item floats to the left while the remaining items float to the right

Currently embarking on my inaugural html/css venture while utilizing bootstrap. I do have some previous html experience from using Blogger in the past. Issue I'm encountering is that when expanding my navbar, I aim to have the first li item (a button ...

How can I retrieve and showcase the size of each file, including images, CSS, and JS, present on a website?

Currently, my goal is to create a dashboard capable of retrieving the file size for all resources (images, javascript, css, etc.) loaded on a webpage. I aim to then further filter these resources by file type and file size in order to identify which ones c ...

Modify the scrolling functionality to ensure that fixed elements remain visible even on smaller screens

I am facing an issue with a fixed div on my webpage. When the height of the div exceeds the viewport height of the browser, I am unable to scroll within the div itself as the background scrolls instead (due to the position being fixed). For example: < ...

The Bootstrap datepicker functions properly when used in plain HTML, but encounters issues when implemented within the .html()

Using HTML: <input class="m-ctrl-medium date-picker" size="16" type="text" id='lateETD1' name="dateRecv" value=""/> Not working with script: var ETD = $("#ETD"); ETD.html("<input class='m-ctrl-medium date-picker' id='la ...

"Utilize JavaScript to extract data from JSON and dynamically generate a

I'm currently facing an issue with reading JSON data and populating it in my HTML table. The function to load the JSON data is not working as expected, although the data typing function is functioning properly. I have shared my complete HTML code alo ...

"Trouble arose when I tried to incorporate additional functions into my JavaScript code; my prompt feature is not

As a beginner in HTML and JS coding, I am working on creating a page that prompts user input for a name and then executes animations using radio buttons. However, after adding functions for radio button changes, my prompt is no longer functioning properly. ...

Sliding Toggle: Panel Revealed with Button Slide

Currently, I am working on implementing a jquery slide tab feature. The functionality is working fine on button click. However, I want the button to stick with the panel and slide along with it. At the moment, the button remains fixed while the panel slide ...

Changing CSS module variables through JavaScript programming

I'm currently utilizing an older version of react-boilerplate that includes CSS Modules, allowing for the creation of variables and the ability to import them into other CSS files. Below is my colors.css file: :root { /* Status colors */ --error ...

"Enhance your audio experience across all browsers with the revolutionary

I am looking for a way to play an mp3 file endlessly when the user opens my website. I need a lightweight crossbrowser solution that works in all browsers, including IE. Any suggestions? ...

iOS is not receiving JSON data in the response headers

I am currently utilizing the CocoaHTTPServer for establishing my server connection. However, I encountered an issue with retrieving JSON data in the header file of my HTML page (web client). Here is the code snippet : HTML Page : endAPSession: funct ...

Centralized and secure masthead

Currently, I am focusing on showcasing my portfolio at www.bbellmedia.com/mywork The specific requirement I have is to center the text 'Bryan Bell' and ensure that it remains fixed even when the user scrolls. Can anyone suggest the most effecti ...

identify when the bottom of a container is reached during scrolling through a window

On my website, I have a section with products displayed in the center of an HTML page. I am looking to implement an AJAX call that will load additional products when the user reaches the bottom of this product container by scrolling down. How can I detec ...

Display advertisement upon clicking

I am looking to enhance my HTML page by incorporating Google Adsense adverts that load up when a visitor clicks on video links. After 10 seconds, the page should automatically redirect to the video file. Any suggestions on how to achieve this? Thanks! ...

Open the HTML document and access the file contents

I'm having trouble reading a text file from my computer onto a website. It works fine in Internet Explorer, but won't work in Chrome. I don't have much experience with HTML, so any help would be much appreciated! <html> <body> ...

Change the background image when hovering over an element with vanilla JavaScript by utilizing data attributes

I would like to be able to change the background image of a <div> when hovering over a link. Initially, the <div> should not have a background image when the page loads for the first time. This is my current setup: <div class="background"& ...