How to center content with dynamic CSS dimensions?

Asked

Viewed 35 times

1

I am using Bootstrap 4, which is based on flexbox(dynamic properties/dimensions). I created a Datapicker, with an input and icon on the right side.

<div class="col-md-3">
   <div class="input-group date" data-provide="datepicker" style="flex: 0 0 auto;">
      <input type="text" class="form-control" class="datepicker">
      <div class="input-group-addon" style="justify-content: center;align-items: center;flex: 0 0 25px;">
         <i class="fa fa-md fa-calendar" aria-hidden="true"></i>
      </div>
   </div>
</div>

When I try to center the icon, with

style="justify-content: center;align-items: center;flex: 0 0 25px;"

the image looks like this

imagem

console.log($date = "data");
$('.datepicker').datepicker({
		format: 'dd/mm/yyyy'
}).on('changeDate', function(e) {
$date = $('.datepicker').datepicker('getDate');
		console.log($date);
});
<!-- Bootstrap CND -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
	<!-- Datapicker css -->
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/css/bootstrap-datepicker.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/js/bootstrap-datepicker.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/locales/bootstrap-datepicker.pt.min.js"></script>
  <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

<div class="container-fluid">
  <div class="row">
    <div class="col-md-3">
	    <div class="input-group date" data-provide="datepicker" id="sandbox-container" style="flex: 0 0 auto;">
			<input type="text" class="form-control" class="datepicker" data-date-format="dd/mm/yyyy" data-date-autoclose="true" data-date-today-highlight="true">
			<div class="input-group-addon" style="justify-content: center;align-items: center;flex: 0 0 25px;">
				<i class="fa fa-calendar" aria-hidden="true" style="align-self: stretch"></i>
			</div>
		</div>
	  </div>
  </div>
</div>

[update] 02-03-2019

Replace

<div class="input-group-addon" style="justify-content: center;align-items: center;flex: 0 0 25px;">

for

<div class="input-group-addon d-flex  align-items-center justify-content-center" style="flex: 0 0 25px">

1 answer

1


It is not working because these classes of flex you have to use on the father you have display:flex. Also, there is no need to use the styles in hand, as Bootstrap itself has classes of flex to make this alignment correctly using the classes d-flex and align-items-center as you can see here https://getbootstrap.com/docs/4.0/utilities/flex/

<link rel="stylesheet" type="text/css" media="screen" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" media="screen" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />

<div class="col-md-3">
    <div class="input-group date" data-provide="datepicker" style="flex: 0 0 auto;">
        <input type="text" class="form-control" class="datepicker">
        <div class="input-group-addon d-flex  align-items-center" style="">
            <i class="fa fa-md fa-calendar ml-2" aria-hidden="true"></i>
        </div>
    </div>
</div>

  • Good help. For my code to have the expected result, I had to add the xx axis alignment, so I added the "Justify-content-center" class. In addition, I need the container to have 25px, so I added the "flex: 0 0 25px" style. Edit your answer in this line: <div class="input-group-addon d-flex align-items-center Justify-content-center" style="flex: 0 0 25px"> So I can validate. Thanks for the help, now I understand better how bootstrap works.

  • 1

    @Pedrappeared Got it, in Sso these fine adjustments sometimes we need to create some extra classes even. If you believe that the problem has been solved and the answer of helped consider mark it as Accepted in this icon below the arrows on the left side of the answer :) So the site does not get open questions pending no answer accepted. You can remove the acceptance if another answer comes up that suits you better. Abs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.