Trouble centering a list within a parent div using CSS

Even though this issue seems straightforward and many others have asked similar questions on the site, I find myself stuck after reading through several solutions and attempting various fixes. Why is this happening?

My problem involves centering a navigation list for pagination on my Django site. Here is the code I am using:

{% if concerts.has_other_pages %}
  <div class="center">
    <ul class="paginator">
      {% if concerts.has_previous %}
        <li><a href="?page={{ concerts.previous_page_number }}">&laquo;</a></li>
      {% else %}
        <li class="disabled"><span>&laquo;</span></li>
      {% endif %}
      {% for i in concerts.paginator.page_range %}
        {% if concerts.number == i %}
        <li class="active"><span>{{ i }}</span></li>
        {% else %}
          <li><a href="?page={{ i }}">{{ i }}</a></li>
        {% endif %}
      {% endfor %}
      {% if concerts.has_next %}
        <li><a href="?page={{ concerts.next_page_number }}">&raquo;</a></li>
      {% else %}
        <li class="disabled"><span>&raquo;</span></li>
      {% endif %}
    </ul>
  </div>
  {% endif %}

Additionally, here is the CSS I am using:

.center{
  text-align: center;
}

.center .paginator{
  width: 100%;
  margin: 1em 0;
  padding: 0;
  list-style-type: none;
}

.paginator li{
  display: inline-block;
  float: left;
  padding: 8px 16px;
  border-radius: 5px;
  font-size: 1.3em;
  text-align: center;
}

.paginator li a{
  padding: 8px 16px;
  border-radius: 5px;
}

.paginator li.active{
  background-color: #444;
}

.paginator li a:hover{
  background-color: #111;
  border-radius: 5px;
  transition: background-color .5s;
}

Despite trying various approaches, I cannot get the navigation buttons to center on the screen. Can anyone provide insight on what might be causing this issue? I have spent a significant amount of time troubleshooting what seems like a simple problem.

Answer №1

It is not possible to have both the float and inline-block display properties at the same time:

.paginator li{
  display: inline-block;
  /* float: left; /* This forces you to put it to left align */
  padding: 8px 16px;
  border-radius: 5px;
  font-size: 1.3em;
  text-align: center;
}

If you remove the rule mentioned above, your code should work correctly.

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

JavaScript code using the Jquery library to retrieve the following article from 'cached memory'

I am aware of the easier CSS options for my question, but I am determined to learn JS (Jquery), so please bear with me. My plan: Menu-items are connected to articles within my content division. Initially, only the first article (#intro) is displayed. All ...

Switching Unicode icon when element is clicked

My form has two inputs - one for text input and the other for submitting, like a button. I have added an icon to the submit button and want it to change when clicked. <input class="searchBtn" id="submit" name="submit" type="submit" value="&#xf002"& ...

Tips for utilizing background-position and background-size in CSS while also positioning an image at a specific location

My current layout is achieved using background-position: center and background-size: cover. Take a look at the image here: https://i.sstatic.net/552RA.png, originally sourced from this link. I am aiming to modify the appearance of the layout to resemble t ...

"Using Bootstrap's toast feature repositions every element on the

I am working on a website project and encountering an issue with a bootstrap toast. The toast currently appears in the top right corner, but I would like it to display above the carousel without affecting the layout. Instead, when the toast appears, it shi ...

Email was not converted within a Celery Task

I am currently working on a celery task that handles sending emails asynchronously. from djcelery.common import respects_language @task(ignore_result=True) @respects_language def async_send_activation_email(registration_profile): registration_profile ...

Trigger SVG stroke-dashoffset animation with a click event once more

Recently, I created a simple SVG stroke animation using CSS and jQuery. The animation triggers on a click event, but I'm struggling to figure out how to restart it on a second click, or subsequent clicks. I've tried various methods like using .ad ...

The dropdown menu in the navigation bar is not properly lined up with its corresponding menu item

I've been struggling to align my submenu with the navbar, and I suspect there is an issue with the CSS that I can't seem to figure out. CodePEN: https://codepen.io/wowasdi/pen/xxKzdQq Even after trying to adjust the navbar CSS settings by changi ...

What's the reason for the element not inheriting margins from its parent?

I am facing an issue with centering an h1 header within a .banner element. When I try to apply margin to the header, it appears to indent not from the top border of the banner, but rather from the nav element located above the banner. After inspecting the ...

Remove all HTML tags except for those containing a specific class

Looking for a regex that removes all HTML tags except the "a" tags with the "classmark" class For example, given this HTML string: <b>this</b> <a href="#">not match</a> <a href="#" target="_blank" ...

Django continues to throw errors despite successful database connection

For some reason, the code works perfectly fine on my local PC, but when I transferred it to CPanel and connected it to the database, I encountered a dbError when loading a page: This code functions flawlessly in localhost (on my PC), but once I uploaded i ...

extract the text content from an object

I am trying to add an object to the shopping cart. The item contains a key/value pair as shown in the following image: https://i.stack.imgur.com/5inwR.png Instead of adding the title with its innerText using p and style, I would like to find another ...

Enabling Multi-Row Form Submission in jQuery: Ensure Submit Button is Disabled Until Every Row Has a Checked

I am dealing with a multi-row form that contains single choice radio buttons in each row. My goal is to have the final <button> tag disabled until a radio button has been checked in each row. Although I have found a solution from a previous question ...

Guide to center-aligning ElementUI transfer components

Has anyone successfully incorporated ElementUI's transfer feature inside a card element and centered it? https://jsfiddle.net/ca1wmjLx/ Any suggestions on how to achieve this? HTML <script src="//unpkg.com/vue/dist/vue.js"></ ...

Angular onscroll event creating a parallax effect

I attempted to create a parallax effect using Angular and the OnScroll event, however, while scrolling, the text seems to be flickering. Is there a way to make the smooth rendering onscroll? Maybe through CSS alone? Here is the script I used: https://sta ...

Unable to establish a 'through' relationship between two models that have a common base class in Django

My project has a 'set' model that has a many-to-many relationship with the 'Item' model. The problem arises because 'set' is a subclass of Item (where everything is considered an Item). Everything works well until I attempt t ...

Use jQuery to trigger a click event when an element is in focus, unless it was clicked to

I am currently developing a website using the MDL framework. One issue I encountered is that there is no default select form element provided. After some research, I found a solution by utilizing a menu component that displays when the input is clicked. Th ...

The marriage of a sticky and floating navigation bar using the power of Bootstrap 4

I am currently designing a navigation bar inspired by the layout on the EA GAMES website. While it functions perfectly in Chrome, I am encountering significant display issues in Internet Explorer. Any suggestions on how to troubleshoot and resolve this pro ...

The transition in Vue Js is only effective when elements are entering, not when they are

Attempting to implement a vue js transition from bottom to top and top to bottom. The transition works smoothly from bottom to top when entering, but there is no transition effect when leaving. Below is the CSS code for the transition: .slide-details-mob ...

Interacting with Cassandra using PHP

I am attempting to execute a SELECT query from the Cassandra Database using a PHP script. The connection is successfully established through PHP. Here is the query: $session = $cluster->connect($keyspace); $result = $session->execute("SELEC ...

Utilize Django models to effectively filter data, group items as needed, and calculate the

I am trying to filter data based on a provider and display the total cost for each circuit type associated with that provider. Here is the query I have been working on: query: model_provider_costs = CircuitInfoData.objects.filter(provider = "BT").values( ...