0

I am trying to center the text in the rectangle but I am not successful. I want to write the code for every kind of fonts.

here is my code snippet

.button {
display: block;
color: rgb(181, 1, 88);
background-color: rgb(248, 224, 237);
width: 232px;
height: 200px;
border-radius: 8px;
margin-left: 368px;
box-shadow: 5px 4px rgb(181, 1, 88);
text-align: center;
}
nathancy
  • 42,661
  • 14
  • 115
  • 137

5 Answers5

3

You can use css flex layout.

.button {
  display: flex;
  align-items: center;
  justify-content: center;
  color: rgb(181, 1, 88);
  background-color: rgb(248, 224, 237);
  width: 232px;
  height: 200px;
  border-radius: 8px;
  margin-left: 30px;
  box-shadow: 5px 4px rgb(181, 1, 88);
  text-align: center;
}
<a class="button" href="#">Some<br>text</a>
Nidhin Joseph
  • 9,981
  • 4
  • 26
  • 48
0

In your case the simplest solution is to replace your defined height with padding, like so:

.button {
display: block;
color: rgb(181, 1, 88);
background-color: rgb(248, 224, 237);
width: 232px;
// height: 200px; ——replace with padding below
padding: 100px 0;
border-radius: 8px;
margin-left: 368px;
box-shadow: 5px 4px rgb(181, 1, 88);
text-align: center;
}

Here is an updated fiddle:

https://jsfiddle.net/54jqrfuo/

Claire
  • 3,146
  • 6
  • 22
  • 37
  • This is incorrect. When I replace height: 200px with padding: 100px top and bottom, the real height will be different (more than 200px) because the text has some height. – Milen Grigorov Jun 27 '19 at 11:34
  • @MilenGrigorov this is not incorrect at all. Your question doesn’t state that you need to maintain a 200px height. It just says you need to center the text in the button. If you need it to be 200px in height then subtract the height of the text from the padding. Easy. – Claire Jun 27 '19 at 15:18
0

One way to achieve this is to wrap the text in a <p> tag and align with top: 50% and transform.

<a class="button" href="#"><p>Some<br>text</p></a>
.button {
    color: rgb(181, 1, 88);
    background-color: rgb(248, 224, 237);
    width: 232px;
    height: 200px;
    border-radius: 8px;
    margin-left: 368px;
    box-shadow: 5px 4px rgb(181, 1, 88);
    text-align: center;
}
.button p{
    position: relative;
    top: 50%; #positions 50% from top of parent.
    transform: translateY(-50%); #positions -50% in relation to itself
}
0

You can use the following 3-properties for aligning an element to center.

display: flex;
justify-content: center;
align-items: center;

So here is the full source code for positioning the element to center.

.button {
   display: flex;
   justify-content: center;
   align-items: center;
   color: rgb(181, 1, 88);
   background-color: rgb(248, 224, 237);
   width: 232px;
   height: 200px;
   border-radius: 8px;
   margin-left: 368px;
   box-shadow: 5px 4px rgb(181, 1, 88);
   text-align: center;
 }
<a class="button" href="#">Some<br>text</a>
Makdia Hussain
  • 744
  • 3
  • 11
-1

Try This

<div class="button">
   <a href="#">Some<br>text</a>
</div>

.button {
        display: table;
        color: rgb(181, 1, 88);
        background-color: rgb(248, 224, 237);
        width: 232px;
        height: 200px;
        border-radius: 8px;
        margin-left: 368px;
        box-shadow: 5px 4px rgb(181, 1, 88);
        text-align: center;
        vertical-align: middle;
     }
     .button a{
        display: table-cell;
        vertical-align: middle;
     }