This question already has answers here:
How to center an element horizontally and vertically
(27 answers)
Closed 4 years ago.
I want to make <h1> centered.
Problem:
When I center the text horizontally, I can't center it vertically, and if I center it vertically, then I can't do it horizontally .
My code :
h1 {
font-weight: 400;
text-transform: uppercase;
font-size: 28px;
}
.main-text {
text-align: center;
}
<header>
<div class="main-text">
<h1>This is Centered Text.</h1>
</div>
</header>
What I Tried :
Here are the some CSS that I tried .
.main-text {
text-align: center;
vertical-align: middle;
}
The below code works but then the text isn't responsive .
.main-text {
position: absolute;
top: 30%;
left: 30%;
}
How I want it to look:
Solution 1: Use CSS 2D transforms
You can use the good old trick of positioning the <h1> element absolutely, use 50% for left and top cardinal coordinates, and then translate it to the top and left by half of its own dimensions. Note that you must declare an explicit height on the parent, otherwise the trick will not work.
h1 {
font-weight: 400;
text-transform: uppercase;
font-size: 28px;
}
.main-text {
position: relative;
width: 100%;
/* You need to define an explicit height! */
height: 100vh;
}
.main-text h1 {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100%;
text-align: center;
}
<header>
<div class="main-text">
<h1>This is Centered Text.</h1>
</div>
</header>
Solution 2: Use flexbox
Since flexbox is very widely supported, you can also use it. This can be done by simply declaring the following rules on the parent wrapping element:
.main-text {
display: flex;
align-items: center;
justify-content: center;
}
h1 {
font-weight: 400;
text-transform: uppercase;
font-size: 28px;
}
.main-text {
display: flex;
align-items: center;
justify-content: center;
text-align: center;
width: 100%;
/* You need to define an explicit height! */
height: 100vh;
}
<header>
<div class="main-text">
<h1>This is Centered Text.</h1>
</div>
</header>
body{
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
width: 100%;
min-height: 100vh;
}
<div class="main-text">
<h1>This is Centered Text.</h1>
</div>
If you can use the flex, try to use the flex.
If you want to get more information about the flex,
visit here https://codepen.io/enxaneta/pen/adLPwv?q=flex&limit=all&type=type-pens
Related
So, I am building a banner, the content needs to be responsive.
I am using flex with center. But the issue is the dollar amount needs to be centered and not the $+dollar amount.
I have tackled this with a negative margin, but it is clunky. I was wondering is the was a way to mark the $ as not included in the center calculation.
.flex-center {
padding: 3rem;
border: 1px solid black;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: 2rem;
}
.offer {
font-size: 3rem;
font-weight: bold;
}
.offer sup {
font-size: 2rem;
}
<div class="flex-center">
<div>Some normal text</div>
<div class="offer"><sup>$</sup>250</div>
<div>Text</div>
</div>
If you meant to exclude the $ sign away from the document flow so that only the price tag width was considered for centering, one solution could be:
Remove the <sup> element entirely
Make the container position:relative
make a new css rule adding the pseudo element ::before positioned absolute to show the $ sign using the content css property
EDIT: As #TemaniAfif suggested in comments, I replaced the fixed left
offset with the right offset set as 100%
This is a demo:
.flex-center {
padding: 3rem;
border: 1px solid black;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: 2rem;
}
.offer {
position: relative;
font-size: 3rem;
font-weight: bold;
}
.offer::before {
content: '$';
font-size: 2rem;
position: absolute;
/*left: -1.25rem;*/
right: 100%;
}
<div class="flex-center">
<div>Some normal text</div>
<div class="offer">250</div>
<div>Text</div>
</div>
Text Not perfectly centring within the Flex Div despite setting, align-items, justify-content, flex-direction.
It's particularly evident on F and d, the bottom and top should be equidistance from their respective boundaries
CSS:
.fbubble{
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
&.feat {
width: 25%;
margin-top: 3em;
background: $pink;
height: 32px;
h2 {
height: 100%;
font-size: 20;
font-weight: 500;
color: $white;
}
}
}
HTML:
<div class="fbubble feat">
<h2>Featured</h2>
</div>
Edit:
This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 1 year ago.
I have 3 elements in a div, I want them to be in a block (vertical) and be aligned horizontally and vertically. Here is how it looks right now:
Here is my code:
<div className="parent">
<p className="heading">TITLE</p>
<p className="description"><strong>DISCLAIMER</strong>: The site contains offensive, vulgar langauge which may not be<br/>suitable for many, so enter with caution and do not reveal any personal details</p>
<Link to="/school"><button className="enterButton">enter the website</button></Link>
</div>
----------------------
stylesheet.css
----------------------
.parent {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.heading {
font-size: 76px;
text-align: center;
}
.description {
font-size: 18px;
opacity: .7;
text-align: center;
line-height: 26px;
}
.enterButton {
width: 360px;
height: 100px;
background-color: #E1E8ED;
color: #15202B;
font-size: 32px;
font-weight: bolder;
text-align: center;
border: none
}
I want them align and below each other like:
TITLE
Disclaimer
Enter the website
Flex-direction column to set up the alignment of your elements within the flex parent element. You already have the center alignment both axis with align-items: center and justify-content: center.
.parent {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
Will do the trick...
I'm trying to align text and button vertically as well as horizontally and cannot make it...
What I want ultimately is that alignment was responsive.
My code:
HTML:
<div id="stage">
<div id="stage-caption">
<h1 class="display-3">Gallery Nellija</h1>
<p>Some description</p>
Sign up now
</div>
</div>
CSS:
#stage {
background: url('img/stage.jpg') center center no-repeat;
background-size: cover;
color: white;
height: 100%;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
#stage-caption {
font-size: 1.4rem;
font-weight: 200;
max-width: 60rem;
margin: 0 auto;
}
#stage-caption h1 {
color: white;
font-weight: bold;
text-transform: uppercase;
}
For now it looks like that:
Thanks in advance!
Add text-align:center to #stage
I'm pretty new to CSS and I'm trying to vertical-align: middle some text inside a div. This has been asked before, but I can't get any of the solutions to work for me, so I must be missing something obvious.
I've tried:
Having the text in a <p> tag and in CSS adding vertical-align: middle; to the <p> tag.
Having the text in a <p> tag and in CSS adding vertical-align: middle; to the parent div.
Having the text in a <div class="flex-container"> and in CSS adding
.flex-container {
display: flex;
justify-content: center;
align-items: center;
}
As here: https://jsfiddle.net/dt3kvmdm/
The parent div doesn't have a fix height in px. Instead it's a percentage. One solution to a similar question suggested that this could be a problem, but I'm not clear on it. It would be helpful to me to be able to keep it as a percentage.
I'll be very happy to hear any suggestions!
Thanks a lot!
Nick.
You need to use display: flex on parent element or set height: 100% on child element Fiddle
.ProjectTitle {
background-color: green;
position: absolute;
width: 100%;
height: 20%;
bottom: 0;
display: flex;
justify-content: center;
align-items: center;
}
.flex-container {
font-family: "Taviraj", serif;
color: #000;
letter-spacing: 0.11em;
}
<div class="ProjectTitle">
<div class="flex-container">
Project Title
</div>
</div>
I'm not sure if I completely understand what you're wanting, but see if this helps:
HTML:
<div class="ProjectTitle">
<div class="flex-container">
<p>Project Title</p>
</div>
</div>
CSS:
.ProjectTitle {
background-color: green;
position: absolute;
width: 100%;
height: 20%;
bottom: 0;
}
.flex-container {
display: flex;
justify-content: center;
align-items: center;
font-family: "Taviraj", serif;
color: #000;
letter-spacing: 0.11em;
line-height: 20%;
height: 100%;
}
.flex-container p {
color: #ffffff;
vertical-align: middle;
}
Fiddle: https://jsfiddle.net/dt3kvmdm/1/