I'm starting to play with CSS and I'm struggling to center my "tasks". To debug I used a colored border and it's centered, but the elements aren't.
.App {
display: block;
text-align: center;
align-items: center;
}
.done {
color: red;
}
.task {
text-align: center;
align-items: center;
display: flex;
margin: auto;
width: 50%;
border: 3px solid green;
padding: 10px;
}
Here is where the CSS is targetting:
import React, { Component } from "react";
class Task extends Component {
render() {
return (
<div className={`task ${this.getClass()}`}>
<div>{this.props.task.text}</div>
<button className="task-element" onClick={() => this.props.onDone(this.props.task.id)}>
<span className="material-symbols-outlined">check_box</span>
</button>
<button className="task-element" onClick={() => this.props.onDelete(this.props.task.id)}>
<span className="material-symbols-outlined">
delete
</span>
</button>
</div>
);
}
getClass() {
return this.props.task.isDone ? 'done' : 'notDone';
}
}
export default Task;
Here is the Output
Tryed to center elements and can't.
You need to use the justify-content property to center the elements inside the flexbox
Try this
.task {
align-items: center;
justify-content: center;
display: flex;
margin: auto;
width: 50%;
border: 3px solid green;
padding: 10px;
}
You would need to justifyContent to centen, as your flex main axis is horizontal, and you want to center the element horizontally. Justify content manage content along main Axis, Here you can give a CSS a try, and let me know whether it works
.task {
text-align: center;
align-items: center;
justify-content: center; /*This will align the content to center in horizontal direction which is main axis if flex direction is row*/
display: flex;
margin: auto;
width: 50%;
border: 3px solid green;
padding: 10px;
}
If you can provide whole code in a codesandbox, I can help more on that
Following are good articles on Flex box
An Interguide to Flex box by Josh W Comeau
Css Trick article on Flex box
Related
I'm trying to make a header that UX/UI has made on an inhouse project. Above the header there's a closeable bar. I want to have it sticky so that it's above the header and not on the header. I'm having trouble getting the CSS right. I can't seem to move the X mark to the right while keeping the rest of the text centered.
Here is my code (heavily modified and probably a bit to much trying to get it right.
JSX:
<>
{!bar && (
""
)}
{bar && (<div className="barClass">
<div className="pClass">
<p className="somethingP"> Something very important!</p>
<p className="readMoreLink">Read more</p>
</div>
<div className="buttonClass">
<button className="barButton" type="button" onClick={removeBar} >X </button>
</div>
</div>)}
</>
CSS:
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.barClass {
background-color: rgb(196,49,196);
position: sticky;
flex-direction: row;
display: flex;
justify-content: center;
align-items: center;
height: 40px;
color: white;
}
.somethingP {
margin-right: 10px;
}
.readMoreLink {
text-decoration: underline;
}
.pClass {
display: flex;
justify-content: center;
align-items: center;
}
.buttonClass {
display: flex;
flex-direction: row;
justify-content: flex-end;
align-items: flex-end;
}
.barButton {
align-self: flex-end;
background-color:rgb(196,49,196) ;
color: white;
border: none;
}
As I get it, your concern here is to position the close button to the right. One way to do it would be like that :
.barButton {
position: absolute; /* allow us to position it where we want on the bar*/
top: 50%; /* this will put the top of the close button to the center of
the bar */
right: 20px; /* Arbitrary, it's up to you*/
transform: translateY(-50%); /* trick to make the button to go up by 50% of its proper height, so it will be perfectly vertically centered*/
background-color:rgb(196,49,196) ;
color: white;
border: none;
}
I'm currently trying to align the number 238,741 and the letter N in Number. Currently I have a padding between the Prize Pool div and the Number Active div. They're all contained within a parent with display set to flex and flex-direction: column. They're then within another parent container with display flex and direction set to row. I tried setting different paddings between the divs but when I zoom in or out they don't maintain their position.
I cannot figure out how to make them align and stay aligned when zooming in and out or going into mobile view. I've spent 7 hours on it and made little progress by going through CSS tricks.
Code:
<Wrapper>
<HeaderWrapper row alignItems="center">
<PrizeText variant={TextVariant.subhead}>Prize Pool</PrizeText>
<Text color={TextColor.primary} fontWeight="600" variant={TextVariant.subhead}>Number Active</Text>
<GoldText style={{'padding-right': '26%'}} variant={TextVariant.display}>$7,162,245</GoldText>
<GoldText color={TextColor.primary} variant={TextVariant.display}>238,741</GoldText>
</HeaderWrapper>
</Wrapper>
const HeaderWrapper = styled(Box)`
height: auto;
align-items: flex-start;
/* border: 2px solid white; */
flex-wrap: wrap;
margin-top: 45px;
margin-left: 32px;
`;
const MoneyWrapper = styled(Box)`
height: auto;
align-items: flex-start;
/* border: 2px solid white; */
flex-wrap: wrap;
margin-left: 32px;
`;
const GoldText = styled(Text)`
background-image: linear-gradient(98.84deg, #C9882B 0%, #E7CA66 47.71%, #C69935 100%);
-webkit-text-fill-color: transparent;
-webkit-background-clip: text;
white-space: nowrap;
`;
const PrizeText = styled(Text)`
padding-right: 152px;
color: ${props => props.theme.palette.text.primary};
font-weight: 600;
white-space: nowrap;
`;
const Wrapper = styled(Box)`
position: relative;
border: 2px solid white;
width: 100%;
/* justify-content: center; */
align-items: center;
`;
A Box is just a div, with display set to flex and can take the prop col or row to change the flex direction.
It might help to rethink the structure of your components. If you wrap <PrizeText> and <GoldText> in a container and <Text> and <GoldText> in another container then things will fall into place for you.
Kind of like this:
.wrapper {
display: flex;
justify-content: space-around;
padding: 1rem;
}
.missing-wrapper {
width: 50%;
}
h1, h5, div {
border: 1px solid red;
}
<div class="wrapper">
<div class="missing-wrapper">
<h5>Prize Pool</h5>
<h1>$7,162,245</h1>
</div>
<div class="missing-wrapper">
<h5>Number Active</h5>
<h1>238,741</h1>
</div>
</div>
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 can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 3 years ago.
I'm really struggling to vertically centre my div, which is currently serving as a container for two text areas. I have two text areas within my div, positioned side by side how I want it, and I would like them to retain their relative position to eachother, but, be vertically in the very middle of the screen. How can I do this?
App.js
class App extends React.Component {
state = {
character: null
};
render() {
return (
<div className="Centre">
<div className="Left">
<TextBox
/>
</div>
<div className="Right">
<textarea
className="Box"
placeholder={"English translation"}
value={this.state.english}
/>
</div>
</div>
);
}
}
export default App;
App.css
.Box {
height: 100px;
width: 98%;
padding: 1%;
border: solid 1px orange;
}
.Centre {
width: 800px;
height: 400px;
margin: 0 auto;
}
.Left {
width: 300px;
height: 200px;
float: left;
border: red;
}
.Right {
width: 300px;
height: 200px;
float: Right;
border: red;
}
textbox.jsx
class TextBox extends React.Component {
render() {
return (
<form>
<textarea
className="Box"
placeholder="Type in Spanish"
value={this.props.equation}
type="text"
name="equation"
/>
</form>
);
}
Change your css code
.Box {
height: 100px;
width: 98%;
padding: 1%;
border: solid 1px orange;
}
.Centre {
height:100vh;
display: flex;
align-items: center;
justify-content: center;
}
.Left {
width: 300px;
border: red;
}
.Right {
width: 300px;
border: red;
}
Live Demo
If i got your question right, you can use flex and center them in the middle of the div:
.Centre {
display: flex;
align-items: center;
}
You can use flexbox with align-items on parent class Centre:
display: flex;
align-items: center.
<div id="container">
<div class="textarea-wrapper">
<textarea>text 1</textarea>
<textarea>text 2</textarea>
</div>
</div>
#container {
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
}
.textarea-wrapper{
display: flex;
flex-direction: row;
justify-content: center;
}
I've researched this and tried all the solutions and yet the text is not vertically aligned.
jsfiddle: http://jsfiddle.net/9a6pdfbt/
html:
<a>משטרה</a>
css:
a {
font-size: 2rem;
border: 2px solid black;
padding: 10px;
display: flex;
align-items: center;
vertical-align: middle;
}
If you use ctrl+shift+c to examine the a tag, you can see that the text is aligned to the bottom of the a tag and not in the exact middle
If you're trying to align your text in the center, vertically, there are multiple ways you can do it.
One way is to use absolute positioning like so (won't work for Bootstrap Columns):
.parent-class {
position: relative;
}
.parent-class a {
position: absolute;
top: 50%;
width: 100%;
transform: translateY(-50%);
}
And in your HTML:
<div class="parent-class">
<a>משטרה</a>
</div>
If you're using Flexbox, all you need is this in your CSS:
a.flexbox {
display: flex;
align-items: center;
justify-content: center;
}
And in your HTML:
<a class="flexbox">משטרה</a>
Unless you specify a height, the text should always be vertically aligned.
a {
font-size: 2rem;
border: 2px solid black;
padding: 20px;
display: flex;
align-items: center;
vertical-align: middle;
background-color: red;
}
<a>משטרה</a>
If you do specify a height, the flexbox should do it all for you. What would break your code there would be the display: inline-block;. I fixed the Fiddle for you: http://jsfiddle.net/3L5d7awj/
I have markup like so:
HTML:
<ListElementRoot>
<ListElementText>
{children}
</ListElementText>
<ListElementDescription>
{description}
</ListElementDescription>
</ListElementRoot>
//platform.web.js
export const ListElementRoot = div(style.root)
export const ListElementIcon = div(style.icon)
export const ListElementText = div(style.text)
export const ListElementDescription = div(style.description)
//CSS
.root {
display: flex;
width: 100%;
height: 56px;
align-items: center;
border-top: 1px solid var(--color-gray2);
padding: 0;
}
.icon {
position: absolute;
width: 28px;
height: 28px;
align-items: center;
padding-left: 18px;
}
.text {
color: #000;
font-size: calc(var(--font-size-body) / 10)rem;
padding-left: 64px;
}
.description {
color: #000;
font-size: calc(var(--font-size-body) / 12.3)rem;
padding-left: 64px;
}
I'm new to flexbox.
How can I make element just under ?
I'm trying to find a solution but it is heavily twisted in flexbox for me (for now). Any ideas?
EDIT:
Without flex-direction: column;
With flex-direction: column;
You need to have a flex-direction property set to column for your flex container (.root).
Here you can find a jsfiddle example.
EDIT:
Change align-items: center to be align-items: flex-start in order to have the elements align to the left