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>
Related
Example:
* {
margin: 0;
box-sizing: border-box;
}
.row{
width: 100%;
height: 90px;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
padding: 0 50px;
background-color: grey;
}
a{
background-color: black;
color: white;
width: 8rem;
text-align: center;
line-height: 40px;
}
<div class="row">
<p>Other content</p>
<a>Test Button</a>
</div>
<a>Test Button</a>
As you can see, the style of the two anchor tags are not the same. Inside the flex container, the link has the desired width and line-height, but outside it only changes its background color. I guess that has something to do with the container, but I can't tell what. Why is that happening?
From the specification:
The display value of a flex item is blockified: if the specified display of an in-flow child of an element generating a flex container is an inline-level value, it computes to its block-level equivalent.
Your link is an inline element but inside a flexbox container it becomes a flex item and behave differently.
Many properties can do the same like for example using float
a {
background-color: black;
color: white;
width: 8rem;
text-align: center;
line-height: 40px;
}
<a>Test Button</a>
<hr>
<a style="float:left">Test Button</a>
width and line-height don't behave the same with inline element or "block-like" elements which explain the difference.
10.3.1 Inline, non-replaced elements
The 'width' property does not apply ref
and
On a non-replaced inline element, 'line-height' specifies the height that is used in the calculation of the line box height. ref
So the width doesn't apply and the line-height won't change the height of the element but is only considered to calculate the line box height.
The trivial fix is to use inline-block
* {
margin: 0;
box-sizing: border-box;
}
.row {
width: 100%;
height: 90px;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
padding: 0 50px;
background-color: grey;
}
a {
background-color: black;
color: white;
width: 8rem;
text-align: center;
line-height: 40px;
display: inline-block;
}
<div class="row">
<p>Other content</p>
<a>Test Button</a>
</div>
<a>Test Button</a>
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:
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
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/
Here's what I've got so far:
http://codepen.io/anon/pen/jPQjMZ
HTML:
<div class="container">
<div class="info">
This Is Info
</div>
</div>
<div class="container">
<div class="info">
This Is More Info
</div>
</div>
CSS: (LESS)
body {
background: #000;
}
.container {
background: #eee;
width: 150px;
height: 200px;
display: inline-block;
vertical-align: top;
}
.info {
background: #404040;
color: #fff;
display: flex;
display: -ms-flexbox;
align-items: center;
-ms-align-items: center;
font-size: 1.4em;
padding: 1em;
text-align: center;
height: 20px;
//overflow: hidden;
}
This works great in Chrome and Firefox. But in IE10, the text fails to wrap:
What's going on and how do I fix this? I don't necessarily have to use flexbox, but it's the only way I could figure out how to center the text perfectly within the div whether it's one line or two.
If you wrap your info text in an extra <span>, you can use the double line-height trick from this other question to vertically center the text (without using flexbox):
How do I vertically align something inside a span tag?
Note: I had to remove the vertical padding and instead set the total height to 3em
.info {
background: #404040;
color: #fff;
font-size: 1.4em;
padding: 0 1em;
/*height: 3em;*/
line-height: 3em;
text-align: center;
}
.info span {
display: inline-block;
line-height: 1em;
vertical-align: middle;
}
Updated Pen: http://codepen.io/anon/pen/aOQegy