Как выровнять элемент кнопки под меткой в ​​bootstrap - PullRequest
0 голосов
/ 20 апреля 2020

Итак, я создаю форму, в которой у нас есть кнопка загрузки на карточку. В результате <label> и изображение были выровнены правильно, но элемент <button> не отображался под меткой. Я даже пытался использовать bootstrap классы, такие как ml-5 и такие, но все же это не работает. Я хочу, чтобы кнопка находилась под надписью, но проблема в том, что она находится на той же строке, как показано на рисунке:

enter image description here

Я хочу это будет выглядеть примерно так:

enter image description here

Есть ли способ выровнять кнопку под меткой. Это должно выглядеть как на картинке выше.

Код:

.img-wrapper {
    display: block;
    width: 6.4rem;
    line-height: 15px;
    margin-left: 6rem;
    float: right;
}

.img-wrapper img {
    display: inline;
    border-radius: 50%;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"        crossorigin="anonymous">
</head>
<body>

<div class="card bg-light mb-3">
                                <div class="card-body">
                                    <label for="profile">Upload a Profile image:</label>
                                    <button type="button" class="btn btn-primary d-inline" id="upload">Upload</button>
                                    <input type="file" name="profile" id="profile" hidden=""/>
                                    <div class="img-wrapper">
                                        <img class="float-right" src="https://i.imgur.com/yPOQLQh.png" alt="Profile" width="100" height="100"/>
                                    </div>
                                </div>
                            </div>

<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<!-- Bootstrap.js -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

</body>
</html>

Ответы [ 5 ]

2 голосов
/ 20 апреля 2020

Вы можете использовать этот код :

.img-wrapper {
    display: block;
    width: 6.4rem;
    line-height: 15px;
    margin-left: 6rem;
    float: right;
}

.img-wrapper img {
    display: inline;
    border-radius: 50%;
}
.card-body{
    display: flex;
    justify-content: space-between;
}
.card-content{
    display: flex;
    flex-direction: column;
    align-items: flex-start;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"        crossorigin="anonymous">
</head>
<body>

<div class="card bg-light mb-3">
                                <div class="card-body">
                                    <div class="card-content">
                                    <label for="profile">Upload a Profile image:</label>
                                    <button type="button" class="btn btn-primary d-inline" id="upload">Upload</button>
                                        </div>
                                    <input type="file" name="profile" id="profile" hidden=""/>
                                    <div class="img-wrapper">
                                        <img class="float-right" src="https://i.imgur.com/yPOQLQh.png" alt="Profile" width="100" height="100"/>
                                    </div>
                                </div>
                            </div>

<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<!-- Bootstrap.js -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

</body>
</html>
0 голосов
/ 20 апреля 2020

, поскольку вы используете bootstrap, так что вам будет очень легко это сделать. позвольте мне показать вам, как это будет работать:

      <div class="row">
        <div class="col-8 col-md-8 col-sm-8 col-lg-8"> // here you can specify cols on your own
          <div class="row">
              //your label and ll go here
          </div>
          <div class="row">
            //button ll go here
            //it ll create a new line but within this parent div
          </div>
        </div>
        <div class="col-4 col-md-4 col-sm-4 col-lg-4">
          // your image code ll go here
        </div>
      </div>

это даст вам кнопку внизу надписи, а также изображение будет размещено, как и раньше, с использованием только стандартных bootstrap 4 классов ...

Спасибо! Удачного кодирования !!

0 голосов
/ 20 апреля 2020

Вы можете изменить класс кнопки на d-block с d-inline и поставить <div class="img-wrapper"> перед ним.

<label for="profile">Upload a Profile image:</label>
<div class="img-wrapper">
    <img class="float-right" src="https://i.imgur.com/yPOQLQh.png" alt="Profile" width="100" height="100"/>
</div>  
<button type="button" class="btn btn-primary d-block" id="upload">Upload</button>
<input type="file" name="profile" id="profile" hidden=""/>
0 голосов
/ 20 апреля 2020

Оберните эти элементы в div:

<div style="display: inline-block">
  <label for="profile">Upload a Profile image:</label>
  <button type="button" class="btn btn-primary d-inline" id="upload">Upload</button>
</div>

и добавьте display: inline-block к вашей кнопке.

.d-inline {
  display: inline-block;
}
0 голосов
/ 20 апреля 2020

Просто добавьте тег <br/> после <label>

.img-wrapper {
  display: block;
  width: 6.4rem;
  line-height: 15px;
  margin-left: 6rem;
  float: right;
}

.img-wrapper img {
  display: inline;
  border-radius: 50%;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

<div class="card bg-light mb-3">
  <div class="card-body">
    <label for="profile">Upload a Profile image:</label><br/>
    <button type="button" class="btn btn-primary d-inline" id="upload">Upload</button>
    <input type="file" name="profile" id="profile" hidden="" />
    <div class="img-wrapper">
      <img class="float-right" src="https://i.imgur.com/yPOQLQh.png" alt="Profile" width="100" height="100" />
    </div>
  </div>
</div>

<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...