This question already has answers here:
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
What is the default padding and/or margin for a p element (reset css)?
(5 answers)
Align inline-block DIVs to top of container element
(5 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I don't understand why there is a gap between the paragraph and the div element, below is my code. I am new to CSS.
.center {
line-height: 200px;
height: 200px;
border: 3px solid green;
text-align: center;
}
.center p {
display: inline-block;
vertical-align: middle;
background-color: green;
}
<div class="center">
<p>I am vertically and horizontally centered.
</p>
</div>
Your paragraph has a margin-top by browser defaults. Set it to margin-top: 0; and there won't be gap.
Related
This question already has answers here:
How can I horizontally center an element?
(133 answers)
How to center a "position: absolute" element
(31 answers)
How can I center an absolutely positioned element in a div?
(37 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed last year.
I'm trying to center a button on my website, so I tried left : 50% to get it to the middle of the screen, but for some reason it brings it all the way to the right side of the screen. Here's the code:
#ContactButton {
font-size: 20px;
border: none;
width : 120px;
position : relative;
height: 30px;
font-weight: 500;
border-radius: 30px;
left : 50%;
}
There are many ways. On the button itself, you can just use margin: 0 auto;.
Or you could maybe just use text-align: center on the button's container.
Or, for example, you could use flexbox on the buttons container:
.container{
display: flex;
justify-content: center;
}
And there are several other possibilities. Check the link #cloned gave you on his comment.
This question already has answers here:
inline-block div with and without text not vertically aligned
(1 answer)
Align inline-block DIVs to top of container element
(5 answers)
CSS vertical alignment of inline/inline-block elements
(4 answers)
Closed 4 years ago.
How to put these red boxes in line? One in the middle jumps down for no reason.
nav label {
width: 50px;
height: 50px;
display: inline-block;
background-color: red;
}
<nav>
<label></label>
<label>2</label>
<label></label>
</nav>
I need to preserve empty labels empty.
You need to set the vertical-align and change its default value of baseline, which is the reason behind the unwanted result, to e.g. top:
nav label {
width: 50px;
height: 50px;
display: inline-block;
vertical-align: top; /* or "middle", "bottom" */
background-color: red;
}
<nav>
<label></label>
<label>2</label>
<label></label>
</nav>
This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
Flexbox: center horizontally and vertically
(14 answers)
How do I vertically center text with CSS? [duplicate]
(37 answers)
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Closed 4 years ago.
I am trying to do something I think is pretty easy but clearly making a pigs ears of it. Here is my fiddle
I have a button and a label on display. I want to centre the label in the middle of the div, so horizontally and vertically centred.
I can centre it horizontally but not vertically. So I have tried a few things but nothing seems to centre the label vertically, even the vertical-align I use below. Not really sure why though?
.headerLbl {
text-align: center;
font-weight: bold;
color: white;
font-size: 14pt;
display: block;
vertical-align: central;
}
You need to add following styles into your code:
.menu {
background-color: #9FACEC; /* Medium blue */
position: fixed;
width: 100%;
margin: 0;
z-index: 1;
display:flex;
justify-content: center;
}
.headerLbl {
align-self: center;
width: 90%;
text-align: center;
}
This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
How can I vertically align elements in a div?
(28 answers)
How do I vertically center text with CSS? [duplicate]
(37 answers)
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Closed 4 years ago.
Thats not a div I want I just cant get to post it because it does not meet some requirement. I have never used anything like that before, just need help for a college project that requires programming.
I have this: enter image description here
I just want the text to be centered.
jsfiddle .net /etr1ok47/#&togetherjs=VfasppUOxh
using flex in this-p class:
display: flex;
flex-direction: column;
justify-content: center;
Try applying the following to your containing div (.parent) and the wrapper around your text (.child).
.parent {
height: 300px;
line-height: 300px;
text-align: center;
}
.child {
line-height: normal;
display: inline-block;
vertical-align: middle;
}
You can add padding to the top of your p element in css if you are needing the words in the circle to be vertically better centered.
p.this-p{
vertical-align: middle;
}
If the problem is the p itself not centering in the li element then define the parent with display: table and the element itself with vertical-align: middle and display: table-cell
p.this-p{ vertical-align: middle; align: center}
Above is when you use p element with padding. Same is applicable for other element
This question already has answers here:
Best way to center a <div> on a page vertically and horizontally? [duplicate]
(30 answers)
How can I horizontally center an element?
(133 answers)
Closed 6 years ago.
What is the best way to center div in the middle of another element?
I have an container with with set to 100% and height to 130% of the screen but i fail to centr it int the middle ( of the container ).
i tried using
div {
margin: auto;
top: 0;
bottom: 0;
right: 0;
left: 0;
position: absolute;
text-align: center;
vertical-align: middle;
}
But this will just set the the content of div in the middle horizontally only , and set the width and height of the inner element to container element.
What is the best trick to achieve such thing? Here is demo