What is the best way to horizontally align a DIV element in the center?

Having trouble centering an entire DIV on the page? Just started learning HTML, CSS, JS, and BOOTSTRAP.

My question is how can I align all rows in the middle of the page. Check out my current code below and see how I want it to look: https://i.sstatic.net/QSpbJ.jpg

<div style="text-align: center">
  <span class="dot" onclick="currentSlide(1)"></span>
  <span class="dot" onclick="currentSlide(2)"></span>
  <span class="dot" onclick="currentSlide(3)"></span>
  <h4>
    Be inspired by our talented hair stylists who will create a look that enhances your natural beauty, accentuates your features, and evolves with you!
  </h4>

  <p>
    Our salon stylists are dedicated to using their skills and experience in hair styling to craft a unique style just for you. Personalization is at the core of what we do: tailoring each beauty service we offer after a thorough consultation and diagnosis. We ask that you spare the 15 minutes for a YO consultation, even for express services, so we can understand your desires and accurately diagnose your hair's needs to provide treatments that suit you best. We've created the perfect environment for you to build a long-lasting relationship with your favorite stylist, which is why we encourage you to reach out if any recommended home care products don't seem to fit - our policy is simple: 100% satisfaction or money back immediately!
  </p>
</div>

This is my current code but I'm aiming for this layout:

https://i.sstatic.net/aoOaC.png

Answer №1

To achieve a centered inner div within a full-width wrapper, you can set the inner div to 75% of its parent width and apply margin: auto. This will automatically center the inner div by adding equal margins on both sides.

<div style="width: 100%; background-color: lightblue">
  <div style="text-align: left; width: 75%; margin: auto">
    <span class="dot" onclick="currentSlide(1)"></span>
    <span class="dot" onclick="currentSlide(2)"></span>
    <span class="dot" onclick="currentSlide(3)"></span>
    <h4>
      Let yourself be inspired by our talented hair stylists who will create a look that enhances your natural beauty, accentuates your features, and evolves with you!
    </h4>

    <p>
      The stylists at our beauty salon will use their knowledge and experience in hair styling to create a unique style just for you. This is what defines us: personalizing every beauty service we offer after a thorough consultation and diagnosis. That's why we ask you to dedicate 15 minutes to the YO consultation, even for express services, so that we can accurately determine your desires and diagnose your hair strand to provide suitable treatments. We have created the best environment for you to develop a long-term relationship with your favorite stylist, and we encourage you to let us know if any of the recommended home care products do not suit you, as our policy is: 100% satisfaction or money back, on the spot!
    </p>
  </div>
</div>

Answer №2

To horizontally align in the center, simply apply margin: auto

Check out MDN for more info

#wrapper {
  width: 500px;
  height: 500px;
  background: #eee;
}

#dot {
  width: 50px;
  height: 50px;
  margin: auto;
  border-radius: 100%;
  background: #ddd;
}
<div id="wrapper">
  <div id="dot"></div>
</div>

If you want to virtually center elements, use align-items.

MDN is a great resource!

#wrapper {
  width: 500px;
  height: 500px;
  background: #eee;
  display: flex;
  align-items: center;
}

#dot {
  width: 50px;
  height: 50px;
  border-radius: 100%;
  background: #ddd;
}
<div id="wrapper">
  <div id="dot></div>
</div>

You can also achieve this using both methods simultaneously.

#wrapper {
  width: 500px;
  height: 500px;
  background: #eee;
  display: flex;
  flex-directon: column;
}

#dot {
  width: 50px;
  height: 50px;
  margin: auto;
  border-radius: 100%;
  background: #ddd;
}
<div id="wrapper">
  <div id="dot"></div>
</div>

Answer №3

If you want to center the content of a div, I suggest adding a class to the div and using some CSS styling.

.centered-content {
  align-content: center;
  overflow: auto;
}
<div class="centered-content">
<span class="dot" onclick="currentSlide(1)"></span>
      <span class="dot" onclick="currentSlide(2)"></span>
      <span class="dot" onclick="currentSlide(3)"></span>
      <h4>
        Get inspired by our talented hair stylists who will create a look that enhances your natural beauty, accentuates your features, and grows with you!
      </h4>

<div>

I hope this information helps you. Best wishes!

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

execute the function based on the given ID

I've been attempting to dynamically add content using a function. My goal is for the content with the same anchor ID as the clicked anchor to be added, but it seems like nothing is happening. Is there a more effective approach to achieve this? $(&a ...

Issue with Bootstrap 4 navigation tabs: dropdown items do not cycle through active tab panels

Whenever I utilize nav-tabs to toggle between tabpanels, it functions properly. However, there seems to be an issue when attempting to switch the active tabpanel using the links in the dropdown menu. Below is the HTML for my nav-tabs and dropdown menu, alo ...

Is it possible to generate a basic HTML page using Express JS without utilizing Ejs or any other templating engine

I have a basic HTML file with a Bootstrap form, along with a simple API coded within. In my server.js file, I've specified that when I request /about, it should redirect me to the about.html page. However, instead of redirecting, I'm encountering ...

Display an additional div when hovering over form input

I am currently in the process of designing a search bar animation. Specifically, I want the search history to appear when hovering over the search bar. However, I am facing an issue where the code doesn't seem to work when I use .search-bar-input:hove ...

Click to alternate video sources

Take this scenario for instance: In Section 1 HOME, there is a video loop with "video source 1". My goal is to change the video source to "video source 2" by clicking on the video in section 1, however, video2 should only play once and then switch back to ...

Attaching a Stylesheet (CSS) without being inside the HEAD Tag

After seeing numerous inquiries about this topic, I have yet to find the answer I seek. My main question is whether it is feasible to connect an external stylesheet in a location other than the HEAD tag. I am facing this issue because I need to use Conflu ...

How can you showcase a background on both the top and bottom of a div at the same time? #dual

Trying to add a gray rectangle (indicated by the red circle) at both the top and bottom of a div that contains thumbnails. This div may have 2 or 3 rows of thumbnails. Image: Current code snippet: .jq_thumbnails { background: url("/site_media/static ...

Scrapy and Python data gets corrupted by <br> tags causing issues

As I attempt to extract the text from Amazon reviews using scrapy, I encounter an issue. Reviews with multiple lines have their text separated by "< br >" tags within a span element. So, when I try to scrape the first review, I use this line of code: ...

Styling a span within a row in Bootstrap to automatically indent using CSS

I have integrated Bootstrap into my project and I am utilizing its grid system. I start by creating a row, then adding a span inside the row which functions as a column (col-12). The text inside this span is underlined, with <br> elements used to se ...

Why won't my second div with the unordered list show up?

I am currently working on developing a To-Do list application, and I have encountered an issue with the second div that contains an unordered list. The list is not displaying, and I am unsure of the reason behind this. I attempted to remove the div and a ...

Elements with absolute positioning become distorted when the browser is resized

As I work on creating dropdown menus for my website, I noticed that they appear very nice and organized on laptop screens with a resolution of 1280 x 800. However, when these menus are scaled to larger sizes, they tend to separate from each other even thou ...

Enhance the appearance of div reflection with additional flair

Attempting to enhance a div with a reflection using the following CSS: CSS :: div.reflection { -webkit-transform: scaleY(-1); -moz-transform: scaleY(-1); -o-transform: scaleY(-1); -ms-transform: scaleY(-1); transform: scaleY(-1); ...

Can you explain how to pause the .setInterval() function when hovering over it and clicking on the interior content simultaneously?

I have been working on a slideshow that involves building elements into an array, modifying the DOM, and using items from the array to fade in and out on a timer. While I have made progress, there are still a few things I would like to accomplish but I am ...

Email in HTML format: content rendering poorly in Outlook

Below is the code I am using to display text with an image. <table width="220" align="right" style=""> <tr> <td> <table align="left" width="100" style="padding: 0; Margin: 0; Margin-top:15px;"> <tr& ...

Adjusting the position of navbar links to the right in Bootstrap 4 using a toggle button

Attempting to shift the links from the left of the navbar to the right side proved to be challenging. I tried floating it to the right, using position:relative; right: 200px;, and padding-right: -200px;, but none of it worked. If anyone has any other sug ...

vue - When using v-bind:class, how can you interact with a nested computed property object value?

Looking to assign a class to an inbox item in Vue when the value is equal to 'null'. I have almost achieved this, but there's one aspect I can't figure out. I've been following examples like https://v2.vuejs.org/v2/guide/class-and- ...

Changing the size of a div using jQuery

I'm attempting to adjust the size of a div element. You can view my progress in this code fiddle: https://jsfiddle.net/bj3m24ks/31/ Kindly refrain from sharing links to other tutorials as I have already explored them all. I attempted to utilize the $ ...

I have configured my CSS styles in the module.css file of my Next.js application, but unfortunately, they are not being applied

I recently developed a nextjs with electron template application: Here is the link to my code on CodeSandbox: https://codesandbox.io/s/sx6qfm In my code, I have defined CSS classes in VscodeLayout.module.css: .container { width: '100%'; he ...

Problem with rendering mailer templates in Outlook

I am experiencing an issue with my mailer template that is constructed using divs, with a width of 650px. While it displays perfectly in browsers, Outlook is not recognizing the specified width and instead displaying it at 100% width. Additionally, setting ...

Incorporate additional attributes within ASP.NET's HTML Helpers for enhanced labeling functionality

Is it achievable to create the following label tag using HTML Helpers? <label class="myClass" attr1="attr1Value" attr2="attr2Value" attr3="attr3Value">labelText</label> This is essentially a regular label tag with 3 additional attributes. ...