Vertically centring a div containing two text areas [duplicate] - css

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;
}

Related

CSS Issue on React project

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

center all div tags using flex [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 1 year ago.
I am trying to center all my div tags in the center of the screen, but even after doing text-align: center, I can't center all the nested div tags. Below is the screen that I am trying to center :
I want "Please upload your file", choose file and drag and drop box all in the center of the screen, but all the elements are not aligned properly. Below is my stylesheet:
.dropArea {
border: 2px dashed steelblue;
padding: 10px;
display: flex;
align-items: center;
justify-content: center;
background-color: lightblue;
font-size: 1.5rem;
cursor: pointer;
position: relative;
min-height: 200px;
}
.dropArea:hover {
background-color: lightskyblue;
color: #333;
}
.dropArea input[type=file] {
position: absolute;
width: 100%;
height: 100%;
opacity: 0;
cursor: pointer;
}
.dropAreaDrug {
background-color: lightseagreen;
}
.container{
display:flex;
justify-content:center;
}
.child {
margin: 0 auto;
text-align:center;
}
below is my html file:
<p>Please <b>upload</b> your file</p>
<div class="container">
<div class="inputArea ">
<InputFile id="inputDefault"
OnChange="OnInputFileChange"
accept="image/png,image/gif,image/jpeg" />
</div>
<div class="dropArea #dropClass">
Drag and drop your files here or click to open file loading dialogue...
<InputFile id="inputDrop"
OnChange="OnInputFileChange"
#ondragenter="HandleDragEnter"
#ondragleave="HandleDragLeave"
multiple />
</div>
#if (files != null && files.Count > 1)
{
<div>
<ul>
#foreach (var file in files)
{
<li>#file.Name</li>
}
</ul>
</div>
}
#if (urls.Count > 0)
{
foreach (var url in urls)
{
<br />
<a href="#url" download>#url</a>
}
}
</div>
any help will be appreciated.
You can align the flex items in your CSS Flexbox along the main axis using justify-content: center and vertically align them along the cross axis with align-items: center. Specify min-height: 100vh on the .container element if you want the flex items to be centered vertically on the page along the cross axis.
Also, your going to want to nest the topmost <p> tag inside .container or .inputArea if you want it to be a flex item and aligned in the same row format. After reading your comments below, you can make the <p> text and <input> in a column layout by making .inputArea a flex container with flex-direction: column. Then if you want to restrict the width of the drag/drop container just give it a max-width declaration.
.container{
display:flex;
justify-content:center;
align-items: center;
min-height: 100vh;
}
.inputArea {
display: flex;
flex-direction: column;
}
.dropArea {
max-width: 500px; /* vary this as you see fit */
border: 2px dashed steelblue;
padding: 10px;
display: flex;
align-items: center;
justify-content: center;
background-color: lightblue;
font-size: 1.5rem;
cursor: pointer;
position: relative;
min-height: 200px;
}
.dropArea:hover {
background-color: lightskyblue;
color: #333;
}
.dropArea input[type=file] {
position: absolute;
width: 100%;
height: 100%;
opacity: 0;
cursor: pointer;
}
.dropAreaDrug {
background-color: lightseagreen;
}
.child {
margin: 0 auto;
text-align:center;
}
<div class="container">
<div class="inputArea">
<p>Please <b>upload</b> your file</p>
<input type="file" />
<!--<InputFile id="inputDefault"
OnChange="OnInputFileChange"
accept="image/png,image/gif,image/jpeg" />-->
</div>
<div class="dropArea #dropClass">
Drag and drop your files here or click to open file loading dialogue...
<InputFile id="inputDrop"
OnChange="OnInputFileChange"
#ondragenter="HandleDragEnter"
#ondragleave="HandleDragLeave"
multiple />
</div>
<!--
#if (files != null && files.Count > 1)
{
<div>
<ul>
#foreach (var file in files)
{
<li>#file.Name</li>
}
</ul>
</div>
}
#if (urls.Count > 0)
{
foreach (var url in urls)
{
<br />
<a href="#url" download>#url</a>
}
}-->
</div>

Wrapped flex row items start at top [duplicate]

This question already has answers here:
What's the difference between align-content and align-items?
(15 answers)
CSS Flexbox: difference between align-items and align-content [duplicate]
(2 answers)
Closed 5 years ago.
Whilst trying to answer this question, I was trying to come up with a flex solution. The closest I could get was this:
.container {
height: 500px;
width: 500px;
background-color: red;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content:space-between; /* puts spacing between left and right column */
}
.headerTitle {
width:100%;
height: 24px;
margin: 24px 24px 0;
padding: 0;
line-height: 24px;
}
.sectionClass {
width: 249px;
height: 200px;
background-color: yellow;
}
.rightSideDiv {
width: 249px;
height: 200px;
border: 4px solid green;
box-sizing:border-box; /* need this otherwise border will take an extra 8px width in some browsers */
}
<aside>
<div class="container">
<header class="headerTitle"> Header Title </header>
<section class="sectionClass"> . </section>
<div class="rightSideDiv"> </div>
</div>
</aside>
However, I couldn't make the 2 lower boxes start flush to the top heading. Is there a way of doing this using flex? I tried align-items and align-self but that didn't seem to do anything.
I also tried adding a pseudo element to the container with flex-grow:1; but it didn't grow in the required manner.
It would be interesting to see if flex can handle this as I'm still trying to learn the intricacies of it
Just add align-content: flex-start to the .container div:
.container {
height: 500px;
width: 500px;
background-color: red;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content:space-between; /* puts spacing between left and right column */
align-content: flex-start;
}
.headerTitle {
width:100%;
height: 24px;
margin: 24px 24px 0;
padding: 0;
line-height: 24px;
}
.sectionClass {
width: 249px;
height: 200px;
background-color: yellow;
}
.rightSideDiv {
width: 249px;
height: 200px;
border: 4px solid green;
box-sizing:border-box; /* need this otherwise border will take an extra 8px width in some browsers */
}
<aside>
<div class="container">
<header class="headerTitle"> Header Title </header>
<section class="sectionClass"> . </section>
<div class="rightSideDiv"> </div>
</div>
</aside>

write a text on the side of a centered text [duplicate]

This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 5 years ago.
I want to add a text beside a centered text without moving the centered text.
Example: C is a centered text and s is a side text:
+++++
sC
+++++
ssC
+++++
sCCC
Is this possible with CSS?
Sure, use flexbloxes like this. This is gross, but at least it works.
.container {
display: flex;
justify-content: center;
align-items: stretch;
}
.container > * {
border: 1px solid #ccc;
padding: 6px 12px;
box-sizing: border-box;
}
.side {
flex: 1 1 50%;
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.side:nth-child(1) {
justify-content: flex-end;
}
.content {
width: 200px;
text-align: center;
}
<div class="container">
<div class="side"><p>Side text</p><p>Side text</p></div>
<p class="content">Content text</p>
<div class="side"></div>
</div>
It sounds like if you are trying to display text before or after the element in which case you may want to read over https://developer.mozilla.org/en-US/docs/Web/CSS/::before
See the example below.
.container {
width: 100%;
background: #eee;
}
.center-text {
display: table;
margin: auto;
}
.center-text:before {
content: "s";
font-size: smaller;
}
.center-text:after {
content: "s";
font-size: smaller;
}
<div class="container">
<div class="center-text">CCC</div>
</div>
Is this possible with CSS? Yes it is. If you're just exploring and playing around with CSS, you can do it by using :before or :after pseudo element. Set the positioning of the parent element to relative then set the pseudo element's position to absolute so you can control its positing inside the centered element div.
However it is a bad practice if you will use this in your work.
div.centered-element{
text-align: center;
width: 100%;
background-color: #f0f0f0;
position: relative;
}
div.centered-element:before{
content: "sss";
color: red;
position: absolute;
left: 30%;
}
<div class="centered-element">C</div>

How to center only a child element from a container using Flexbox? [duplicate]

This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 5 years ago.
I have a container with 2 children. First child is an icon and second is text. I want to align the icon left and the title center using Flexbox only.
The HTML code and the CSS code:
.container {
display: flex;
background-color: #7D8F99;
width: 100%;
height: 50px;
}
.title {
display: flex;
background-color: #9AADB8;
}
.icon {
display: flex;
background-color: #1AADA9;
}
<div class="container">
<span class="icon">icon</span>
<span class="title">text</span>
</div>
https://jsfiddle.net/eajosjoh/
A combination of position: absolute and left: 0 on the .icon would do the trick.
.container {
display: flex;
justify-content: space-around;
background-color: #7D8F99;
width: 100%;
height: 50px;
position: relative;
}
.title {
background-color: #9AADB8;
}
.icon {
background-color: #1AADA9;
position: absolute;
left: 0;
}
<div class="container">
<span class="icon">icon</span>
<span class="title">text</span>
</div>
Or, if you wanted to use only flex, you could add an empty spacer element on the right side. The spacer element would need to have the same width as the .icon element. And honestly, adding an empty element for the sake of layout isn't a great option. So I'd recommend the first method.
.container {
display: flex;
justify-content: space-between;
background-color: #7D8F99;
width: 100%;
height: 50px;
}
.title {
background-color: #9AADB8;
}
.icon {
background-color: #1AADA9;
width: 30px;
}
.spacer {
/* width must match icon width */
width: 30px;
}
<div class="container">
<span class="icon">icon</span>
<span class="title">text</span>
<span class="spacer"></span>
</div>
Adding a margin: 0 auto; to the .title will solve the problem
.container {
display: flex;
background-color: #7D8F99;
width: 100%;
height: 50px;
}
.title {
display: flex;
background-color: #9AADB8;
margin: 0 auto;
}
.icon {
display: flex;
background-color: #1AADA9;
}
<div class="container">
<span class="icon">icon</span>
<span class="title">text</span>
</div>

Resources