0

I have three div elements having the same styles.the content inside the div is dynamic as you can see below

Current

enter image description here

how to make the text center inside each div like below

Expected

enter image description here

.sample{
     padding:15px;
    width:150px;
    height:70px;
    float: left;
    color:#fff;
    background-color:#3498db;
    margin-bottom:20px;
    text-align:left;
    text-align:center;
    line-height:normal;
    margin-right:10px;
}
<div class="sample"> Some text </div>
<div class="sample"> A little more text is added </div>
<div class="sample"> This text is large compared to other sample divs </div>
yasarui
  • 6,209
  • 8
  • 41
  • 75
  • Your answer should be found in a previous thread: https://stackoverflow.com/questions/19461521/how-to-center-an-element-horizontally-and-vertically. – JCJ Oct 31 '17 at 14:57
  • using `display:flex;` . but why you use `text-align:left` and also `text-align:center` ? – Mihai T Oct 31 '17 at 14:57

1 Answers1

0

Using flex is very simple. All you have to do is add: display: flex, align-items: center and justify-content: center

.sample{
     padding:15px;
    width:150px;
    height:70px;
    float: left;
    color:#fff;
    background-color:#3498db;
    margin-bottom:20px;
    text-align:left;
    text-align:center;
    line-height:normal;
    margin-right:10px;
    display: flex;
    align-items: center;
    justify-content: center;
}
<div class="sample"> Some text </div>
<div class="sample"> A little more text is added </div>
<div class="sample"> This text is large compared to other sample divs </div>
Kushtrim
  • 1,899
  • 6
  • 14