1

I would like to align contents of a class button into the middle on both horizontally and vertically.I used margin: auto property for that but which aligns the text vertically but not horizontal. I know it's possible to make it effective by using either text-align property or display:flex.but I just need a reason here about why the margin: auto property did not worked.I'm adding the snippet below.Thanks in advance.

Note here it's working fine vertically the problem not working horizontally .so how it can be duplicate of not working vertically?

#main-body{
    width: 100%;
    height: 95%;
    box-sizing: border-box;
    background: yellow;
}

#title-container{
    width:100%;
    height: 10%;
    background: red;
}

#button-container{
    width:100%;
    height: 10%;
    background-color: blue;
}

#data-container{
    padding: 5px;
    box-sizing: border-box;
    background-color: blueviolet;
}

.btn{
    width:25%;
    height: 100%;
    border: 1px solid black;
    /*margin:auto;*/
    float:left;
    box-sizing: border-box;
    /*margin:0px;*/
}
.btn-text{
    margin:auto;
    /*float:none;*/
    color: #ffffff;
}
<body>
  <div id="main-body">
    <div id="title-container"></div>
    <div id="button-container">
      <div id="home-button" class="btn">
        <p class="btn-txt">Home</p>
      </div>
      <div id="update-button" class="btn">
        <p class="btn-txt">Update</p>
      </div>
      <div id="delete-button" class="btn">
        <p class="btn-txt">Create New</p>
      </div>
      <div id="logout-button" class="btn">
        <p class="btn-txt">Log Out</p>
      </div>
    </div>
    <div id="data-container"></div>
  </div>
</body>

i know it is possible to done using text-align:center;

    #main-body{
        width: 100%;
        height: 95%;
        box-sizing: border-box;
        background: yellow;
    }

    #title-container{
        width:100%;
        height: 10%;
        background: red;
    }

    #button-container{
        width:100%;
        height: 10%;
        background-color: blue;
        text-align:center;
        
    }

    #data-container{
        padding: 5px;
        box-sizing: border-box;
        background-color: blueviolet;
        
    }

    .btn{
        width:25%;
        height: 100%;
        border: 1px solid black;
        /*margin:auto;*/
        float:left;
        box-sizing: border-box;
        /*margin:0px;*/
    }
    .btn-text{
        margin:auto;
        /*float:none;*/
        color: #ffffff;
    }
<body>
  <div id="main-body">
    <div id="title-container"></div>
    <div id="button-container">
      <div id="home-button" class="btn">
        <p class="btn-txt">Home</p>
      </div>
      <div id="update-button" class="btn">
        <p class="btn-txt">Update</p>
      </div>
      <div id="delete-button" class="btn">
        <p class="btn-txt">Create New</p>
      </div>
      <div id="logout-button" class="btn">
        <p class="btn-txt">Log Out</p>
      </div>
    </div>
    <div id="data-container"></div>
  </div>
</body>

it's also working fine with display:flex;

#main-body{
    width: 100%;
    height: 95%;
    box-sizing: border-box;
    background: yellow;
}

#title-container{
    width:100%;
    height: 10%;
    background: red;
}

#button-container{
    width:100%;
    height: 10%;
    background-color: blue;
    display:flex;
    align-content:center;
    justify-content:center;
}

#data-container{
    padding: 5px;
    box-sizing: border-box;
    background-color: blueviolet;
}

.btn{
    width:25%;
    height: 100%;
    border: 1px solid black;
    /*margin:auto;*/
    float:left;
    box-sizing: border-box;
    /*margin:0px;*/
    display:flex;
    align-content:center;
    justify-content:center;
}
.btn-text{
    margin:auto;
    /*float:none;*/
    color: #ffffff;
}
<body>
  <div id="main-body">
    <div id="title-container"></div>
    <div id="button-container">
      <div id="home-button" class="btn">
        <p class="btn-txt">Home</p>
      </div>
      <div id="update-button" class="btn">
        <p class="btn-txt">Update</p>
      </div>
      <div id="delete-button" class="btn">
        <p class="btn-txt">Create New</p>
      </div>
      <div id="logout-button" class="btn">
        <p class="btn-txt">Log Out</p>
      </div>
    </div>
    <div id="data-container"></div>
  </div>
</body>
Tomasz Mularczyk
  • 34,501
  • 19
  • 112
  • 166
Afsal Khan
  • 37
  • 6

3 Answers3

0

As I know that margin affects to element not content. If you set margin: 0 auto; it will place element to center of parent element.

0

There are few things you should note here.

  1. content cannot be aligned vertically using margin: auto;
  2. to align content align vertically you need to use some other ways, such as vertical alignment or using flexbox model etc.
  3. if you want to align content horizontally center using margin: auto? then the width of the element should not be more than the content width.
  4. if the content inside the element needs o be horizontally center aligned then simply use text-align: center;

for more reference on how CSS margin works please read the following description.

The margin CSS property sets the margin area on all four sides of an element. It is a shorthand that sets all individual margins at once: margin-top, margin-right, margin-bottom, and margin-left.

for more in detail description: https://developer.mozilla.org/en-US/docs/Web/CSS/margin

Just to solve your problem with your existing code, check out following solution.

CSS

class name changed from .btn-text to .btn-txt

.btn-txt{
  text-align:center;
}
Nilesh Mahajan
  • 3,506
  • 21
  • 34
0

checkout this answer https://stackoverflow.com/a/35817140/2724173 you child must have the width to work margin:auto

try to give width to .btn-txt i.e 40px also there is the spelling mistake in your class is btn-txt and your css selector is .btn-text

#main-body{
    width: 100%;
    height: 95%;
    box-sizing: border-box;
    background: yellow;
}

#title-container{
    width:100%;
    height: 10%;
    background: red;
}

#button-container{
    width:100%;
    height: 10%;
    background-color: blue;
}

#data-container{
    padding: 5px;
    box-sizing: border-box;
    background-color: blueviolet;
}

.btn{
    width:25%;
    height: 100%;
    border: 1px solid black;
    /*margin:auto;*/
    float:left;
    box-sizing: border-box;
    /*margin:0px;*/
}
.btn-txt{
    margin:10px auto;
    /*float:none;*/
    color: #ffffff;
    width:40px;
}
<body>
  <div id="main-body">
    <div id="title-container"></div>
    <div id="button-container">
      <div id="home-button" class="btn">
        <p class="btn-txt">Home</p>
      </div>
      <div id="update-button" class="btn">
        <p class="btn-txt">Update</p>
      </div>
      <div id="delete-button" class="btn">
        <p class="btn-txt">Create New</p>
      </div>
      <div id="logout-button" class="btn">
        <p class="btn-txt">Log Out</p>
      </div>
    </div>
    <div id="data-container"></div>
  </div>
</body>
Ismail Farooq
  • 6,309
  • 1
  • 27
  • 47