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>
` tag and check the `margin:0 auto;` remove top bottom margin also nothing else
– Ismail Farooq Jan 31 '18 at 07:15element because,if i did not added a width to element
means which takes the whole width of it's parent.
– Afsal Khan Jan 31 '18 at 07:15