The title in my div class doesn't seem to be properly centered, even though I have set the margin to auto. What steps can I take to correct this issue?

My div is set to have a margin:auto, but the h1 inside is not centered as expected. I've tried various solutions, but none seem to work. Can someone please help me get it centered properly?

    body {
        background: #5D6D7E;
        color: white;
        font-family: "Varela Round", sans-serif;
        margin: 0;
        padding: 0;
    }

    header {
        background: white;
        color: #3498DB;
    }

    ul {
        list-style-type: none;
    }

    li {
        display: inline-block;
        margin-right: 20px;
    }

    a {
        color: #D7DBDD;
        font-weight: bold;
    }

    a:hover {
        color: white;
    }

    section {
        display: flex;
        flex-direction: row;
    }

    div {
        width: 100px;
        margin: auto;
    }

    .title {
        /* Isn't centered  */
        font-size: 50px;
    }

    header,
    section {
        background: #2E86C1;
    }

    header,
    section,
    footer {
        padding: 20px;
    }
<section>
    <div class="title">
        Brainstorm
    </div>
</section>

Answer №1

In the current answers, they all show how to center text inside a div, but none mention the absence of any heading in your Markup.

It's important to note that divs lack semantic meaning, so if you want to include a heading, be sure to use appropriate heading tags:

<section>
  <h1 class="title"> Brainstorm </h1>
</section>

You can style the heading using the same CSS as for the div:

.title {text-align: center}

Answer №2

The issue arises from the rule you implemented div { width: 100px; }, which confines the specific element to a width of only 100px. While your element is centered correctly, it exceeds the constraints of the 100px limit.

Once this rule is removed, the element centers as intended:

body {
  background: #5D6D7E;
  color: white;
  font-family: "Varela Round", sans-serif;
  margin: 0;
  padding: 0;
}

header {
  background: white;
  color: #3498DB;
}

ul {
  list-style-type: none;
}

li {
  display: inline-block;
  margin-right: 20px;
}

a {
  color: #D7DBDD;
  font-weight: bold;
}

a:hover {
  color: white;
}

section {
  display: flex;
  flex-direction: row;
}

div {
  /*width: 100px;*/
  margin: auto;
}

.title {
  /* Doesn't appear to be center */
  font-size: 50px;
}

header,
section {
  background: #2E86C1;
}

header,
section,
footer {
  padding: 20px;
}
<section>
  <div class="title">
    Brainstorm
  </div>
</section>

If you prefer not to eliminate this rule entirely for all <div> elements (although recommended), another solution is to set width: auto specifically on .title.

Additionally, there were some invalid HTML and an extra leading } in your CSS within your inquiry—both have been rectified in the snippet above.

Answer №3

Include the CSS snippet below to align the text in the h1 element to the center:

h1{
text-align:center;
}

Answer №4

It's a Success!

Markup Language

<article>
  <div class="heading">
    Innovative Ideas
  </div>
</article>

Styling Rules

.heading{
  text-align:center;
}

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

Tips for duplicating specific div elements

Is there a way to create copies of selected divs within the same panel using a Javascript report designer? I attempted to achieve this by using the following code snippet: function DesignerClone() { $(".ui-selected").each(function () { va ...

The layout of my website appears distorted on mobile devices

When I view my website, http://www.healthot.com, on an iPhone or other mobile device, the page doesn't display correctly. It scales the window, requiring me to zoom out to see the entire page. To better understand the issue, please refer to this photo ...

Utilize LESS Bootstrap as a guide for incorporating content

I have been struggling to properly integrate Bootstrap into my LESS file. Currently, I am adding Bootstrap to my project using NPM. If I simply use the following import statement in my LESS file: @import (reference) 'node_modules/bootstrap/less/boot ...

I am facing an issue in my Vue Project where the CSS does not recognize URLs enclosed in quotes

When attempting to pass over a quoted URL to background-image or cursor, I'm facing an issue where the file simply doesn't load. I am currently working with Vue and have installed the following libraries: Vuetify, SASS, SASS-Loader, and Node-S ...

Variety of formatting options for menu items when the menu is in its collapsed state

I am working with a Bootstrap nav-bar that collapses into a button by default. Within my nav-bar, I have opted for images instead of text links. However, when the nav-bar is collapsed, I would like these images to be smaller in size. Is there a way to cu ...

The Django base template that utilizes Bootstrap3 completely replaces the index.html file

I have set up a base.html file where I plan to house my bootstrap3 navbar and footer, which will be consistent across all pages of my website. However, the base.html and its corresponding CSS file seem to override all the content in the index.html file an ...

Header displayed on each page. (WordPress)

(my website: ) I am using Wordpress.org and I am trying to create a button that will redirect the user back to the front page, but I am having trouble figuring out how to do it. The button should be located in the top left corner of the site and should not ...

Customize the text displayed on the select box button in a React application

My task involves working with an array of JSON objects structured like this - [ { "country_code": "AF", "country_name": "Afghanistan", "country_flag": " ...

Tips for aligning numbers in a monospaced ordered list

Is there a way to align the numbers in a numbered list within a table using monospace font so that they are at the same level as bullets on a bullet list? Simplest example: ol { font-family: monospace; } ul, ol { margin-top: 10px; margin-bottom: ...

The z-index of the fixed header in FullPageJS is causing the section's content to be hidden

For my current project, I am incorporating FullPageJS and using a fixed top menu that adjusts its height based on the viewport size. On smaller screens, the menu items will be stacked on top of each other, while on wider screens, the menu items will be po ...

Selenium - pausing for scroll completion

I am currently working on automating downloads from a webpage using the selenium tool. My approach involves creating a firefox driver that opens the page and clicks the download button. However, I have encountered an issue where the button needs to be vis ...

Multiple Ajax calls interacting with each other are causing issues

Within my Index.php file, there is an ajax call that loads a php page containing an HTML form. I am attempting to trigger another ajax call from Index.php whenever a select box is changed. Everything functions properly except for the ajax on change event ...

Concealing subcategories within a responsive menu using jQuery

My website's responsive menu changes based on screen resolution, but I encountered an issue. On mobile viewports, the sub items in the menu are displayed by default. I want them to only appear when the parent item is clicked. Currently, clicking the p ...

Prevent dragging functionality after being dropped onto a droppable area

I am currently working on a drag and drop project and have encountered a minor issue. My goal is to disable the draggable item after it has been dropped onto the droppable area. I have created a function called disableDrag, but I am receiving an error in t ...

Every file downloaded through the iframe is automatically stored in a designated server folder

Is it possible for my website to have an iframe that enables users to browse the web, and when they click on a download button on any external website, the file can be saved directly to a specific folder on my server instead of their own computer? I'm ...

Is it possible to achieve seamless image transitions in Firefox like it does in Chrome?

To achieve the desired effect, you may need to use some javascript. Visit aditagarwal.com for more information. Styling with CSS: .images-wrapper{ position: fixed; left: 0; top: 80px; bottom: 0; width: 100%; height: 100vh; an ...

Utilizing JSON and select for dependency management

Let's say we have a JSON object: { "A": { "1": "1", "2": "2", "3": "3" }, "B": { "4": "4", "5": "5", "6": "6" }, "C": { "7": "7", "8": "8" } } And we also have ...

Getting the checkbox count value in a treeview upon successful completion of an AJAX request

After successful JSON Ajax response, a treeview structure with checkboxes is displayed. I need to capture the number of checkboxes checked when the save button is clicked. @model MedeilMVC_CLOUD.Models.UserView <script type="text/javascript"> ...

Converting a Powershell array into an HTML format

I have three arrays that I need to display on an HTML page: foreach($item in $array1){ // write array data to HTML } foreach($element in $array2){ // write array data to HTML } foreach($value in $array3){ // write array data to HTML } What is the ...

Is your Jquery validation malfunctioning?

I am currently facing a challenge with required validation on an asp.net page. In order to validate the inputs, I have implemented multiple controls which can be hidden or displayed based on certain conditions. These controls include checkboxlists, dropdow ...