Tips for using flexbox to center a div within another div?

I am looking to create a header image with a semi-circular bottom. The challenge I'm facing is centering the div containing the title and button using flexbox or bootstrap. While mx-auto (margin x auto) works, my-auto doesn't have the same effect. Additionally, the align-self-center and align-items-center classes do not seem to work either - I have to resort to using mx-auto for centering. To center the circular bottom properly, I end up having to apply margin-left: -25vw. What would be the most effective method to achieve centered divs in this case?

Here is the code snippet:

.image-wrapper {
 position: relative;
 width: 100vw;
 height: 100%;
 overflow: hidden;
}

.circular-bottom {
  width: 150vw;
  height: 90vh;
  margin-left: -25vw;
  border-bottom-left-radius: 50%;
  border-bottom-right-radius: 50%;
  overflow: hidden;
}

.circular-bottom img {
  z-index: -1;
}

.title-box {
  position: absolute;
  top: 0;
  height: 100%;
  width: 100%;
}

.title-text {
  text-align: center;
  width: 40%;
 }
<div class="image-wrapper">
  <div class="circular-bottom d-flex justify-content-center">
    <img src="url/image" draggable="false">
   </div>
   <div class="title-box d-flex">
    <div class="title-text align-self-center">
      <h1>Title</h1>
      <a class="btn" href="#">button text</a>
    </div>
  </div>
</div>

The image-wrapper prevents horizontal scrolling. The circular-bottom element helps create the circular bottom by utilizing overflow hidden and extending its width for the shape of the circle. This also explains why the image cannot be set as a background image, as it needs to be cropped using overflow:hidden.

Answer №1

To center the content within the flex container (in this case, .title-box), you can add the justify-content: center property like so:

.image-wrapper {
 position: relative;
 width: 100vw;
 height: 100%;
 overflow: hidden;
}

.circular-bottom {
  width: 150vw;
  height: 90vh;
  margin-left: -25vw;
  border-bottom-left-radius: 50%;
  border-bottom-right-radius: 50%;
  overflow: hidden;
}

.circular-bottom img {
  z-index: -1;
}

.title-box {
  justify-content: center;
  position: absolute;
  top: 0;
  height: 100%;
  width: 100%;
}

.title-text {
  text-align: center;
  width: 40%;
 }
<div class="image-wrapper">
  <div class="circular-bottom d-flex justify-content-center">
    <img src="url/image" draggable="false">
   </div>
   <div class="title-box d-flex">
    <div class="title-text align-self-center">
      <h1>Title</h1>
      <a class="btn" href="#">button text</a>
    </div>
  </div>
</div>

I personally tested this approach and it worked perfectly for me.

You can view the functioning codepen example here: https://codepen.io/anon/pen/YLOwxX

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

How to trigger a forced download of a .csv file instead of displaying it in the browser using HTML

After clicking the download button, it should redirect to a new "thank you" webpage and then automatically start downloading a .csv file from the SD card after 2 seconds. However, instead of downloading the file, it is being displayed in the browser. Belo ...

Ways to identify and differentiate various checkboxes using HTML and CSS

I am currently working on implementing a thumbs-up and thumbs-down feature, where clicking on the thumb-up icon will change the color to green, and clicking on the thumb-down icon will change it to red. However, I am facing some difficulties understanding ...

Stop the webpage from scrolling when clicking on a ui-grid field

Is there a way to prevent page scrolling when clicking on a row field in ui-grid? I'm working with a page that has ui-grid, and each row includes an anchor tag with a URL value linked and target="_blank" to open in a new tab like the example below: ...

I am having difficulty with my ajax code and I am unsure of how to resolve it

Here is the code snippet. I am encountering an issue where pressing the button does not trigger any action, while I simply want to ensure that the AJAX functionality is working properly. The expected behavior is for an alert message to be displayed when th ...

Tips on how to postpone the loading of an image when utilizing a CSS animation on a different element

I'm currently working on a webpage where I have integrated CSS animations to move images around. When you click on these images, a larger version of the image along with a related paragraph is supposed to appear in its place. Initially, I had set up a ...

HighCharts display/hide data points

My goal is to create a HighCharts report based on survey data for a panel discussion. Participants will use their smartphones to vote on 10 questions, and a moderator will discuss the results one by one in a non-sequential order. Objective... When the us ...

What is the reason for being unable to remove the event listener from a sibling element?

function show1() { console.log("ok1"); document.getElementById("a2").removeEventListener("click", delegate); } function show2() { console.log("ok2"); } function show3() { console.log("ok3"); } function delegate(event) { var flag = event.target ...

Difficulty with setting a border for text spanning multiple lines

I am trying to style a header text with a border around it. The issue arises when the larger text spans over two lines, causing the border to break in between. In an attempt to fix this, I applied the CSS property box-decoration-break: clone;. However, thi ...

Encountered an error when attempting to submit with Node.js and Express.js connected to MySql - "Cannot POST /login

I am currently working on creating a basic login page using node.js with the express.js and mysql packages. The goal is to redirect users to the layout.html page if their username and password exist in the mysql database. For this project, I have set up a ...

Both radio buttons are being chosen simultaneously

<div class="container"> <div class="row"> <form> <div class="form-group"> <label>username</label> <br /> <input type="text" class=" ...

Clicking on a React component will result in highlighting

What's the best way to display the selected item after a user clicks on it? <Box py={2}> <Grid container width="330px"> <Grid container direction="column" item xs align="left"> ...

The CSS dropdown menu ends up being positioned underneath the slideshow

Currently, I am working on creating a CSS dropdown menu using CSS3. However, I have encountered an issue where the dropdown menus are going under the slideshow on the page when hovered, making the dropdown items invisible. I am unsure whether I should fix ...

Guide on retrieving JavaScript variables in a JSON response and showcasing their values on a web page

I need to show the data from a JSON response: item.php $ItemList = array(itemcode=>EATERY, itemname=>'Popcorn') ; $ItemDisplay = " '<div>' + document.write(this.itemname) + ' - ' + ...

Mailchimp "Error Encountered: Text Instead of a New Tab Created"

I have implemented a basic mailchimp form that has the following structure <!DOCTYPE html> <html> <head> <title></title> </head> <body> <!-- Begin MailChimp Signup Form --> <div id="mc_embed_signup"& ...

Using a global variable to change the class of the <body> element in AngularJS

I've recently developed an angularJS application. Below is the index.html <html ng-app="MyApp"> <head> <!-- Import CSS files --> </head> <body class="{{bodylayout}}"> <div ng-view></div> < ...

"Clicking on one item in the Bootstrap submenu doesn't close the other items,

I have this Javascript code snippet that deals with expanding and collapsing submenu items: <script type="text/javascript> jQuery(function(){ jQuery(".dropdown-menu > li > a.trigger").on("click",function(e){ var current ...

No search results found on Dropbox. PHP

I've utilized this code in various projects I have created, but for some reason it is not functioning correctly now. To me, everything seems fine and it should work, but I am hopeful that someone will be able to identify the mistake that I am overlook ...

Automating the process of choosing a dropdown option on a website with a Python script

When navigating the website, I found that I needed to choose a specific time duration from a drop-down menu, as shown in the image. My goal is to automate this process using a Python script. I already have a script in place, but it requires an additional c ...

User account details displayed in the website's header for Prestashop users

I am currently using prestashop 8.1.5 with the Megafox theme installed. The issue I am facing is in the header section where user information is displayed as a drop down menu, but unfortunately, the username is not visible when a user is logged in. I have ...

Display the icon beneath the list item label

How can I display the ion-edit icon below the list names? The icons are currently appearing on the right side of each list name, but I want them to be aligned below with some spacing. When clicking on "Data Source Connections," a sublist will be shown whe ...