Home > Blockchain >  How to center a button?
How to center a button?

Time:11-18

i am trying to make the add button on the middle of the box not just be in the center but be on the middle of it.

.gallery {
  background-color: #fbfbfb;
  border-radius: 5px;
  border-style: solid;
  border: 1px solid #bbbbbb;
  height: 85px;
  line-height: 1;
  box-sizing: border-box;
  margin: 12px;
  height: auto;
}

input[type="file"] {
  display: none;
}

.images-upload {
  background-color: #ffffff;
  border-radius: 5px;
  border: 1px dashed #ccc;
  display: inline-block;
  padding: 3px;
  cursor: pointer;
  width: 165px;
  height: 85px;
}

.images-preview {
  border-radius: 5px;
  border: 1px solid #ccc;
  display: inline-block;
  width: 165px;
  height: 85px;
  padding-top: -14px;
}

.button-container {
  height: 90px;
  display: inline-block;
}

.image-container {
  height: 90px;
  width: 15%;
  display: inline-block;
}

.custum-icon {
  color: #00afca;
}

.close-btn {
  background: none;
  color: white;
  border: none;
  padding: 0;
  font: inherit;
  cursor: pointer;
  outline: inherit;
  position: relative;
  left: -160px;
  top: -15px;
  box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
  width: 0px;
}

.close-btn:hover {
  color: red;
  box-shadow: red 0px 7px 29px 0px;
}
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  <link rel="stylesheet" href="./assets/css/style.css" class="href">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta2/css/all.min.css" integrity="sha512-YWzhKL2whUzgiheMoBFwW8CKV4qpHQAEuvilg9FAn5VJUDwKZZxkJNuGM4XkWuk94WCrrwslk8yWNGmY1EduTA==" crossorigin="anonymous" referrerpolicy="no-referrer"
  />
</head>

<body>
  <div class="container">
    <div class="gallery w-100 m-2">
      <div class="p-3">
        <div class="button-container my-1">
          <div for="images-upload" class="images-upload">
            <i class=" fas fa-plus-circle fa-3x custum-icon"></i>
          </div>
          
          <input id="images-upload" type="file" name="images" multiple="multiple">
        </div>

        <div class="image-container d-inline my-1">
          <img src="https://via.placeholder.com/200x150" alt="" class="images-preview">
          <button class="close-btn"> <i class="fas  fa-2x fa-times"></i></button>
        </div>
      </div>
    </div>
  </div>
</body>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You need to set the parent to position absolute, then the child to position relative, and put the button 50% away from the edge and that will move the left top corner to the center, so you move it back with transform 50% of the child width and height.

I Hope that makes sense

.gallery {
  background-color: #fbfbfb;
  border-radius: 5px;
  border-style: solid;
  border: 1px solid #bbbbbb;
  height: 85px;
  line-height: 1;
  box-sizing: border-box;
  margin: 12px;
  height: auto;
}

input[type="file"] {
  display: none;
}

.images-upload {
  background-color: #ffffff;
  border-radius: 5px;
  border: 1px dashed #ccc;
  display: inline-block;
  padding: 3px;
  cursor: pointer;
  width: 165px;
  height: 85px;
}

.images-preview {
  border-radius: 5px;
  border: 1px solid #ccc;
  display: inline-block;
  width: 165px;
  height: 85px;
  padding-top: -14px;
}

.button-container {
  height: 90px;
  display: inline-block;
 position: absolute;

}

.image-container {
  height: 90px;
  width: 15%;
  display: inline-block;
}

.custum-icon {
 color: #00afca;
 position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

.close-btn {
  background: none;
  color: white;
  border: none;
  padding: 0;
  font: inherit;
  cursor: pointer;
  outline: inherit;
  position: relative;
  left: -160px;
  top: -15px;
  box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
  width: 0px;
}

.close-btn:hover {
  color: red;
  box-shadow: red 0px 7px 29px 0px;
}
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  <link rel="stylesheet" href="./assets/css/style.css" class="href">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta2/css/all.min.css" integrity="sha512-YWzhKL2whUzgiheMoBFwW8CKV4qpHQAEuvilg9FAn5VJUDwKZZxkJNuGM4XkWuk94WCrrwslk8yWNGmY1EduTA==" crossorigin="anonymous" referrerpolicy="no-referrer"
  />
</head>

<body>
  <div class="container">
    <div class="gallery w-100 m-2">
      <div class="p-3">
        <div class="button-container my-1">
          <div for="images-upload" class="images-upload">
            <i class=" fas fa-plus-circle fa-3x custum-icon"></i>
          </div>
          
          <input id="images-upload" type="file" name="images" multiple="multiple">
        </div>

        <div class="image-container d-inline my-1">
          <img src="https://via.placeholder.com/200x150" alt="" class="images-preview">
          <button class="close-btn"> <i class="fas  fa-2x fa-times"></i></button>
        </div>
      </div>
    </div>
  </div>
</body>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

put the button in a div and style that div like this:

.divClass {
  display: flex;
  align-items: center;
  justify-content: center;
}
<div class="divClass">
  <button>add</button>
</div>
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

the button should be centered in the div

  • Related