Centering a button using flexbox

Joined
Feb 5, 2023
Messages
1
Reaction score
0
I'm trying to center a button inside of a div that has a background img. I was able to position my navbar at the top and spaced evenly using flexbox. How would I center a button in this same div with background img and navbar. My code is below. I prefer it be done using flexbox, but I'm open to other suggestions.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>RachelRodgersHair</title>
<style>
* {
box-sizing: border-box;
}
body {
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
/* Header BG Img */
.bgimg {
background-image: url(images/2.jpeg);
min-height: 380px;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
/* NAVBAR */
.navbar {
display: flex;
justify-content: space-evenly;
}
.navbar a {
text-decoration: none;
font-size: 20px;
padding: 14px 20px;
}
.navbar a:hover {
background-color: black;
color: white;
}

</style>
</head>
<body>
<!-- H E A D E R -->
<!-- BG IMG -->
<!-- NAVBAR -->
<div class="bgimg">

<div class="navbar">
<a href="">BOOK NOW</a>
<a href="">PRICING</a>
<a href="">CONTACT</a>
</div>

</div>

</body>
</html>
 
Joined
Nov 13, 2020
Messages
302
Reaction score
36
  1. We use the property of display set to flex i.e. display: flex;
  2. Align items to center using align-items: center;
  3. The last step is to set justify-content to center i.e. justify-content: center;
 
Joined
Jan 30, 2023
Messages
108
Reaction score
13
I'm trying to center a button inside of a div that has a background img. I was able to position my navbar at the top and spaced evenly using flexbox. How would I center a button in this same div with background img and navbar. My code is below. I prefer it be done using flexbox, but I'm open to other suggestions.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>RachelRodgersHair</title>
<style>
* {
box-sizing: border-box;
}
body {
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
/* Header BG Img */
.bgimg {
background-image: url(images/2.jpeg);
min-height: 380px;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
/* NAVBAR */
.navbar {
display: flex;
justify-content: space-evenly;
}
.navbar a {
text-decoration: none;
font-size: 20px;
padding: 14px 20px;
}
.navbar a:hover {
background-color: black;
color: white;
}

</style>
</head>
<body>
<!-- H E A D E R -->
<!-- BG IMG -->
<!-- NAVBAR -->
<div class="bgimg">

<div class="navbar">
<a href="">BOOK NOW</a>
<a href="">PRICING</a>
<a href="">CONTACT</a>
</div>

</div>

</body>
</html>
PHP:
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>RachelRodgersHair</title>
  <style>
    * {
      box-sizing: border-box;
    }
    body {
      text-align: center;
      font-family: Arial, Helvetica, sans-serif;
    }
    /* Header BG Img */
    .bgimg {
      background-image: url(images/2.jpeg);
      min-height: 380px;
      background-position: center;
      background-repeat: no-repeat;
      background-size: cover;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
    }
    /* NAVBAR */
    .navbar {
      display: flex;
      justify-content: space-evenly;
    }
    .navbar a {
      text-decoration: none;
      font-size: 20px;
      padding: 14px 20px;
    }
    .navbar a:hover {
      background-color: black;
      color: white;
    }
    .btn {
      margin-top: 20px; /* adjust as needed */
    }
  </style>
</head>
<body>
  <!-- H E A D E R -->
  <!-- BG IMG -->
  <!-- NAVBAR -->
  <div class="bgimg">
    <div class="navbar">
      <a href="">BOOK NOW</a>
      <a href="">PRICING</a>
      <a href="">CONTACT</a>
    </div>
    <button class="btn">My Button</button>
  </div>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,703
Messages
2,569,328
Members
44,612
Latest member
Billysak

Latest Threads

Top