Guide to horizontally aligning a div with a dynamic width in the center

After extensive research, I discovered that setting a width allows you to center a div using margin: 0 auto along with left: 0;, right: 0, and position: absolute. However, all the examples I found had a specified width.

In my specific case, I need to center a button with a cursor: pointer;, so I can't set a width because that would change the cursor. Moreover, since it's a link, setting a width would further complicate things.

So, my question is - how can you center a div with an absolute value without defining a width?

.blue-section {
  background-color: #9BD0D2;
  height: 500px;
  width: 100%;
  position: relative;
}
#blue-info-container {
  top: 20%;
  position: absolute;
  left: 0;
  right: 0;
  width: 70%;
  margin: 0 auto;
  text-align: center;
}
#blue-section-title {
  color: #FFF;
  font-size: 1.4em;
  padding-bottom: 75px;
}
#blue-section-description {
  color: #FFF;
  font-size: 1.2em;
}
#blue-section-button {
  position: absolute;
  bottom: 20%;
  left: 0;
  right: 0;
  width: 300px;
  margin: 0 auto;
  cursor: pointer;
}
#blue-section-button span {
  border: 1px solid #FFF;
  text-align: center;
  color: #FFF;
  padding: 20px 20px;
}
<div class="blue-section">
  <div id="blue-info-container">
    <div id="blue-section-title">fdsfdsafsda</div>
    <div id="blue-section-description">fnderjfgnreopn nfdewjfn wreo fnewjif njkfkew nji fn jekwf njfedww nfdefnewdi fewjq nffemdwkom fdmkwf mfewmkqoffewkqo fnfew klf</div>
  </div>
  <div id="blue-section-button"><span>MORE ABOUT</span>
  </div>
</div>

Answer №1

take a look at this. I have made some adjustments to your css

.blue-section {
                background-color: #9BD0D2;
                height: 500px;
                width: 100%;
                position: relative;
            }
            #blue-info-container {
                top: 20%;
                position: absolute;
                left: 0;
                right: 0;
                width: 70%;
                margin: 0 auto;
                text-align: center;
            }
            #blue-section-title {
                color: #FFF;
                font-size: 1.4em;
                padding-bottom: 75px;
            }
            #blue-section-description {
                color: #FFF;
                font-size: 1.2em;
            }
            #blue-section-button {
                position: absolute;
                bottom: 20%;
                left: 0;
                right: 0;
                text-align: center;
            }
            #blue-section-button span {
                border: 1px solid #FFF;
                text-align: center;
                color: #FFF;
                padding: 20px 20px;
                display: inline-block;
                cursor: pointer;
            }
<div class="blue-section">
            <div id="blue-info-container">
                <div id="blue-section-title">fdsfdsafsda</div>
                <div id="blue-section-description">fnderjfgnreopn nfdewjfn wreo fnewjif njkfkew nji fn jekwf njfedww nfdefnewdi fewjq nffemdwkom fdmkwf mfewmkqoffewkqo fnfew klf</div>
            </div>
            <div id="blue-section-button"><span>MORE ABOUT</span>
            </div>
        </div>

Answer №2

<html lang="en">

<head>
  <meta charset="UTF-8" />
  <title>Document</title>




</head>

<body>

  <div class="wrapper">

  <div class="blue-section">
  <div id="blue-info-container">
    <div id="blue-section-title">fdsfdsafsda</div>
    <div id="blue-section-description">fnderjfgnreopn nfdewjfn wreo fnewjif njkfkew nji fn jekwf njfedww nfdefnewdi fewjq nffemdwkom fdmkwf mfewmkqoffewkqo fnfew klf</div>
  </div>
  <div id="blue-section-button"><span>MORE ABOUT</span>
  </div>
</div>


</div>

</body>

</html>
















.blue-section {
    position: absolute;
    left: 0;
    right: 0;
    margin: auto;
    text-align: center;
    height: 250px;
    top: 0;
    bottom: 0;
}


#blue-info-container {

}
#blue-section-title {
  color: #FFF;
  font-size: 1.4em;
  padding-bottom: 75px;
}
#blue-section-description {
  color: #FFF;
  font-size: 1.2em;
}
#blue-section-button {

}
#blue-section-button span {
  border: 1px solid #FFF;
  display:inline-block;
  text-align: center;
  color: #FFF;
  padding: 20px 20px;
}

.wrapper
{  background-color: #9BD0D2;
  height: 500px;
  width: 100%;
  position: relative;

    }

Answer №3

Give this a shot

#red-section-button{display:flex; justify-content:center;}

Answer №4

To demonstrate the desired functionality, please refer to the following code snippet and accessible JSFiddle link.

Essentially, include the attribute align='center' within the div containing the button, and eliminate width: 300px; margin: 0 auto; from the CSS styling of said button.

HTML Code:

<div class="blue-section">
     <div id="blue-info-container">
         <div id="blue-section-title">Example Title</div>
         <div id="blue-section-description">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam vehicula a ligula at varius.</div>
    </div>
    <div id="blue-section-button" align='center'>
        <span>MORE DETAILS</span>
     </div>
</div>

CSS Stylings:

.blue-section {
  background-color: #9BD0D2;
  height: 500px;
  width: 100%;
  position: relative;
}
#blue-info-container {
  top: 20%;
  position: absolute;
  left: 0;
  right: 0;
  width: 70%;
  margin: 0 auto;
  text-align: center;
}
#blue-section-title {
  color: #FFF;
  font-size: 1.4em;
  padding-bottom: 75px;
}
#blue-section-description {
  color: #FFF;
  font-size: 1.2em;
}
#blue-section-button {
  position: absolute;
  bottom: 20%;
  left: 0;
  right: 0;
  cursor: pointer;
}
#blue-section-button span {
  border: 1px solid #FFF;
  text-align: center;
  color: #FFF;
  padding: 20px 20px;
}

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 is not processing the HTML form I submitted

HTML: <form method="post" action="create.php"> Job #: <input type="text" name="jobnum"> <br> Program: <input type="text" name="program"><br /> Ship Date: <input type="text" name="shipdate"><br /> Description: < ...

Get rid of any fonts that are causing rendering delays on amp pages

Encountering issues with Google Lighthouse and AMP Pages I have attempted various solutions but none seem to be working <link rel="preload" href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,600,700,900&display=swap ...

Unable to view a basic bottle webpage on Raspberry Pi from a PC

After referencing the instructions found at , I successfully tested them on a Raspberry Pi using the following code snippet: from bottle import route, run @route('/hello') def hello(): return "Hello World!" run(host='0.0.0.0', port ...

Determine the elapsed time in seconds between two specified moments

I am trying to implement two input fields in my HTML, one for a starting point and another for an end point. The user will enter two times like this: For example: [8:15] - [14:30] alert("XXXXX seconds") I want to calculate the number of seconds between 8 ...

Angular 2: The *ngFor directive is unable to locate a suitable differing framework

Below is the code for client.service.ts clients: Client[]; getClientList() { let headers = new Headers(); headers.append('Content-Type', 'application/json'); let authToken = localStorage.getItem('auth_token&apo ...

Error in line 36: firebase.auth function is undefined

Snippet of index.html code <html> <head> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" ...

What steps can I take to resolve the TypeError in next js where I am unable to set properties of undefined for 'className'?

I've been working on a Next.js project and I've copied some code from CodePen that is used for designing product layouts on an e-commerce website. However, I'm encountering a problem with the following error message: TypeError: Cannot set pr ...

Concealing tooltip when button is clicked using jQuery

I am facing an issue where the tooltip does not hide on button click. Below is the code for the button in question: <button class="btn btn-outline-inverse ajax_addtocart additems ...

Show the extracted data on the following web page once it has been cleaned

I am looking to connect a python file with an HTML page using Flask application. On the first page (upload.html), the user is required to select the job field and job title from two dropdown menus. Afterwards, the user needs to upload a file containing a l ...

Difficulty encountered when trying to update a re-updated HTML DOM using Flask

I have a flask dropzone for file uploads, and after each upload I want to display a log text on the website. While it currently works, there is an issue where the log text does not update after the second upload. The website continues to show the text from ...

What is the best way to present a Python list in individual lines on an HTML webpage?

This might be a simple question, and I apologize if I haven't explained it clearly. This is my first time working with HTML, and I'm using a given program that I don't fully comprehend. In the python program, app.py, I read a list of string ...

Minification of CSS - Vuetify

While working on my Nuxt project with Vuetify, I noticed that the page source is quite lengthy with 26k lines of code, most of which is generated by Vuetify's CSS. On comparing it with other pages, I see shorter code with difficult-to-read CSS. Is the ...

Materialize.css | managing spaces, indentation, and 'overriding' in a span element

I recently started using a new framework called Materialize CSS for my project and I'm still getting the hang of it. One issue I've encountered is that long texts in my sidebar are creating large spaces between line breaks and strange indents. D ...

The symbol "#" appears in my URL whenever the link is clicked

Seeking guidance on a URL issue that I am facing. Whenever I click the source link, it adds a pound sign to the URL. How can I prevent this from happening? Can someone assist me in identifying the necessary changes required in my jQuery or HTML code? Bel ...

Error with AngularJS: IE not showing dropdown options for MultiSelect

My application features a multiselect dropdown menu that shows a list of countries. While this dropdown functions correctly in Chrome, it displays options differently in IE as shown below: https://i.stack.imgur.com/xhOmV.png I have attempted to adjust th ...

CSS3PIE is failing to function properly in Internet Explorer

Looking for a solution to achieve rounded corners in Internet Explorer? Consider using . In my CSS file named template.css, I have included the following code which is loaded when the page loads on Joomla 1.5. .form_field {color:#222 !important; border:2p ...

CommentsController#new encountered a NoMethodError due to an undefined method called 'text' for the Comment with the following attributes: id: nil, author: nil, created_at: nil, updated_at: nil

I'm currently integrating rails with react to include a comments section in the app I am developing. While the /comments page is functional, I encountered an error when attempting to create a new comment. Despite consulting the guide I am following, t ...

Most effective strategy for optimizing web font loading considering caching and load speeds

When it comes to embedding fonts in your web app, there are a few different methods you can use. Method A) One way is to utilize the Google Fonts CDN by inserting this code into your HTML: <link href="https://fonts.googleapis.com/css2?family=Roboto:wg ...

Arrange a div beneath another div that is positioned absolutely

Within my code, I have a div with the property of absolute positioning referred to as "abs". Right after this div, there is another div called "footer" which does not have any specified position value assigned to it. Even though I've experimented with ...

What is causing my divs not to center with justify-content?

I've been working on centering two divs inside a parent div horizontally. The parent div is set to flex-direction: column so that the child divs stack one below the other, but I want them centered in the middle of the page. Although justify-content: ...