align text of flex item to center [duplicate] - html

This question already has answers here:
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Closed 2 years ago.
I have a flexbox container with 3 flex items
the text of the two outer items should be at the center point of each item
so far the text appears on the top of
using align-items: center;
recommended in How to vertically align text inside a flexbox?
on the container just squished the divs to one line, ignoring the heigth of the container.
how to center the text at the center of each div ?
body {
margin: 0;
}
.file_upload-container {
display: flex;
margin: 20px;
height: 200px;
/*align-items: center;*/
justify-content: center;
}
.drop-zone_main {
color: #cccccc;
border: 4px dashed #009578;
border-radius: 10px;
}
.upload_spacer {
background-color: #00ffb7;
flex-basis: 200px;
}
.drop-zone_second {
color: #cccccc;
border: 4px dashed #009578;
border-radius: 10px;
flex-basis: 300px;
}
input {
display: none;
}
<div class="file_upload-container">
<div class="drop-zone_main">Drop MainFile here or click to upload</div>
<input class="input_main" type="file" name="upload_mainfile">
<div class="upload_spacer"></div>
<div class="drop-zone_second">Drop second file here or click to upload</div>
<input class="input_second" type="file" name="upload_secondfile">
</div>

You can wrap your input in a div, so it gets aligned with its siblings, and use display: flex to align the inner content:
body {
margin: 0;
}
.file_upload-container {
display: flex;
margin: 20px;
height: 200px;
/*align-items: center;*/
justify-content: center;
}
.drop-zone_main {
color: #cccccc;
border: 4px dashed #009578;
border-radius: 10px;
}
.upload_spacer {
background-color: #00ffb7;
flex-basis: 200px;
}
.drop-zone_second {
color: #cccccc;
border: 4px dashed #009578;
border-radius: 10px;
flex-basis: 300px;
}
.input-wrap {
display: flex;
align-items: center;
}
<div class="file_upload-container">
<div class="drop-zone_main">Drop MainFile here or click to upload</div>
<div class="input-wrap">
<input class="input_main" type="file" name="upload_mainfile">
</div>
<div class="upload_spacer"></div>
<div class="drop-zone_second">Drop second file here or click to upload</div>
<div class="input-wrap">
<input class="input_second" type="file" name="upload_secondfile">
</div>
</div>

You need to correct the css rules :
.drop-zone_main {
color: #cccccc;
border: 4px dashed #009578;
border-radius: 10px;
justify-content: center;
align-items: center;
display: flex;
}
.drop-zone_second {
color: #cccccc;
border: 4px dashed #009578;
border-radius: 10px;
flex-basis: 300px;
justify-content: center;
align-items: center;
display: flex;
}

Related

Align text centrally to the adjacent box [duplicate]

This question already has answers here:
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Flexbox: center horizontally and vertically
(14 answers)
How can I vertically align elements in a div?
(28 answers)
Closed 4 months ago.
I have an inline-block element and would like to centre the text next to it. I had already tried negative margin on the span (box) element but this changed the whole content (box and text).
MY code
fieldset {
max-width: 350px;
margin: 0 auto;
padding:1px;
display: flex;
justify-content: space-around;
align-items: center;
}
.legend-item-box {
display: inline-block;
height: 16px;
width: 30px;
margin-right: 5px;
margin-left: 0;
border: 1px solid #999;
}
.a {
background:#8DD3C7;
}
.b {
background:#FFFFB3;
}
<fieldset>
<legend>Legend:</legend>
<div class="legend-item-container">
<span class="a legend-item-box"></span>Legend a
</div>
<div>
<span class="b legend-item-box"></span> Legend b
</div>
</fieldset>
Many thanks in advance! Max
Is this what you want?
fieldset {
max-width: 350px;
margin: 0 auto;
padding:1px;
display: flex;
justify-content: space-around;
align-items: center;
}
.legend-item-container {
display: flex;
align-items: center;
}
.legend-item-box {
display: inline-block;
height: 16px;
width: 30px;
margin-right: 5px;
margin-left: 0;
border: 1px solid #999;
}
.a {
background:#8DD3C7;
}
.b {
background:#FFFFB3;
}
<fieldset>
<legend>Legend:</legend>
<div class="legend-item-container">
<span class="a legend-item-box"></span>Legend a
</div>
<div>
<span class="b legend-item-box"></span> Legend b
</div>
</fieldset>
You can use vertical-align: middle; to archive this. https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align?retiredLocale=en
fieldset {
max-width: 350px;
margin: 0 auto;
padding:10px;
display: flex;
justify-content: space-around;
align-items: center;
}
.legend-item-box {
vertical-align: middle; /* <--- added */
display: inline-block;
height: 16px;
width: 30px;
margin-right: 5px;
margin-left: 0;
border: 1px solid #999;
}
.legend-item-container {}
.legend-text { /* <--- added */
font-size: 0.6rem;
}
.a {
background:#8DD3C7;
}
.b {
background:#FFFFB3;
}
<fieldset>
<legend>Legend:</legend>
<div class="legend-item-container">
<span class="a legend-item-box"></span><span class="legend-text">Legend a</span>
</div>
<div class="legend-item-container"><!-- added class -->
<span class="b legend-item-box"></span><span class="legend-text">Legend b</span>
</div>
</fieldset>

CSS/HTML background image fixed issue [duplicate]

This question already has answers here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 1 year ago.
CSS
.as {
width: 300px;
height: 300px;
border: 5px solid red;
position: absolute;
left: 25%;
display: flex;
flex-direction: column;
}
.ab {
width: 60px;
height: 60px;
border: 2px solid green;
background-color: green;
}
.cd {
width: 60px;
height: 60px;
border: 2px solid blue;
background-color: blue;
}
HTML
$div class="as"$
$div class="ab"$123$/div$
$div class="cd"$123$/div$
$/div$
When I run it, I have green and blue boxes in a big red box. The green and blue are on the left corner.
since the parent container, which is "as" has display: flex and flex-direction: column
I can't go to the left bottom corner.
Is there a way to go to the left bottom? besides make it position: relative and bottom: %.
Thank you
Hi, I am practicing CSS
Do you need use property the justify-content with value flex-end to the left bottom corner
Look this example bellow
.as {
width: 300px;
height: 300px;
border: 5px solid red;
display: flex;
flex-direction: column;
margin-bottom: 1rem;
}
.bottom-left {
justify-content: flex-end;
}
.bottom-right {
align-items: flex-end;
justify-content: flex-end;
}
.ab {
width: 60px;
height: 60px;
border: 2px solid green;
background-color: green;
}
.cd {
width: 60px;
height: 60px;
border: 2px solid blue;
background-color: blue;
}
<p>Bottom left</p>
<div class="as bottom-left">
<div class="ab">123</div>
<div class="cd">123</div>
</div>
<p>Bottom right</p>
<div class="as bottom-right">
<div class="ab">123</div>
<div class="cd">123</div>
</div>

How can I center a flex-element with unknown width that is not a button-element with CSS? [duplicate]

This question already has answers here:
Centering the link as the button
(3 answers)
Closed 2 years ago.
There's a style for a button, that can be applied to a-elements and button-elements:
.button {
appearance: none;
display: flex;
justify-content: center;
align-items: center;
width: auto;
min-width: 150px;
min-height: 50px;
margin: 0 auto 20px auto;
padding: 0;
border: 1px solid black;
background: white;
font-size: 1.4rem;
}
<a class="button">a-element</a>
<button class="button" type="button">a-element</button>
While this works fine, there's a difference in rendering:
the a-element has 100 % width of the parent container
the button-element doesn't stretch and is centered
I'm not sure which browser default style property for buttons causes this.
How can I make the a-element to behave like the button?
So, the element is centered but not stretched.
Use inline-flex
.button {
appearance: none;
display: flex;
justify-content: center;
align-items: center;
width: auto;
min-width: 150px;
min-height: 50px;
margin: 0 auto 20px auto;
padding: 0;
border: 1px solid black;
background: white;
font-size: 1.4rem;
}
a.button {
display: inline-flex;
background: lightblue;
}
body {
text-align:Center;
}
<a class="button" href="#">a-element</a>
<button class="button" type="button">a-element</button>
It's just because of display : flex, can be solved with inline flex
EDITED
.button {
appearance: none;
display: inline-flex;
justify-content: center;
align-items: center;
min-width: 150px;
min-height: 50px;
width: auto;
margin: 0 auto 20px auto;
padding: 0;
border: 1px solid black;
background: white;
font-size: 1.4rem;
}
.btn-container{
text-align:center;
}
<div class="btn-container">
<a class="button">a-element</a>
<button class="button" type="button">a-element</button>
</div>

How to center a image next to an input field using flexbox

I am attempting to center a image next to an input field although when attempting to do so with vertical-align: bottom; nothing happens and my image is not centered where I want it to be.
I have tried to use position:relative and then move my image using top,bottom etc but I want to do it in a way that uses flexbox.
How would I go about doing this and what divs would I have to change to get the layout I want.
Layout I am trying to achieve:
Jsfiddle
HTML
<div class="container">
<h1>Inputs</h1>
<p class="spacing">multiple inputs...</p>
<div class="searchinput">
<input type="text" name="search" id="google-input" placeholder="Google search...">
<img src="logos/google.png" alt="youtube" class="logos">
</div>
</div>
CSS
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.logos{
width: 90px;
vertical-align: bottom;
}
.container {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 90vh;
}
.searchinput {
margin-top: 20px;
}
input {
padding: 20px;
height: 30px;
background-color: transparent;
border: 2px solid black;
border-radius: 5px;
color: black;
font-size: 20px;
vertical-align: bottom;
}
You need to have the direct parent of the items that you want to have flex properties to have the display:flex property. So in your case it would be the .searchinput. So your .searchinput css should be the following:
.searchinput {
margin-top: 20px;
display:flex;
align-items: center;
}
So here is a snippet of the whole thing:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.logos{
width: 90px;
vertical-align: bottom;
}
.container {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 90vh;
}
.searchinput {
margin-top: 20px;
display:flex;
align-items: center;
}
input {
padding: 20px;
height: 30px;
background-color: transparent;
border: 2px solid black;
border-radius: 5px;
color: black;
font-size: 20px;
vertical-align: bottom;
}
<div class="container">
<h1>Inputs</h1>
<p class="spacing">multiple inputs...</p>
<div class="searchinput">
<input type="text" name="search" id="google-input" placeholder="Google search...">
<img src="https://i.imgur.com/dpkbGqu.png" alt="youtube" class="logos">
</div>
</div>

How can i center the content in a flex-item? [duplicate]

This question already has answers here:
Flex items not centering vertically
(2 answers)
How do I vertically center text with CSS? [duplicate]
(37 answers)
How can I horizontally center an element?
(133 answers)
Closed 4 years ago.
i am trying like crazy but i cannot get the text inside this table (i built with flex) to be centered. All the content should be vertically centered. Any ideas how i can achieve this? Any kind of help is really appreciated. I tried now for several hours.
Thanks a lot!
.wrapper {
display: flex;
flex-wrap: nowrap;
text-align: center;
background-color: silver;
border-top: 2px solid #000;
}
.wrapper>div {
height: 250px;
}
.ct-title {
flex: 1 40%;
background: deepskyblue;
text-align: left;
}
.ct-content-1 {
flex: 1 20%;
background: gold;
}
.ct-content-2 {
flex: 1 20%;
background: hotpink;
border-left: 4px solid #000;
border-right: 4px solid #000;
}
.ct-content-3 {
flex: 1 20%;
background: lime;
}
<div class="wrapper">
<div class="ct-title">Title
<p>This is some text in a paragraph.</p>
</div>
<div class="ct-content-1">6</div>
<div class="ct-content-2">8</div>
<div class="ct-content-3">12</div>
</div>
Try this. you need to apply flex on the child also.
.wrapper {
display: flex;
flex-wrap: nowrap;
text-align: center;
background-color: silver;
border-top: 2px solid #000;
}
.wrapper>div {
height: 250px;
display: flex;
align-items: center;
flex-direction: column;
justify-content: center;
}
.ct-title {
flex: 1 40%;
background: deepskyblue;
text-align: left;
}
.ct-content-1 {
flex: 1 20%;
background: gold;
}
.ct-content-2 {
flex: 1 20%;
background: hotpink;
border-left: 4px solid #000;
border-right: 4px solid #000;
}
.ct-content-3 {
flex: 1 20%;
background: lime;
}
<div class="wrapper">
<div class="ct-title">Title
<p>This is some text in a paragraph.</p>
</div>
<div class="ct-content-1">6</div>
<div class="ct-content-2">8</div>
<div class="ct-content-3">12</div>
</div>
Please try below code
.wrapper {
display: flex;
flex-wrap: nowrap;
text-align: center;
background-color: silver;
border-top: 2px solid #000;
}
.wrapper>div {
height: 250px;
}
.ct-title {
flex: 1 40%;
background: deepskyblue;
text-align: left;
display: flex;
justify-content: center;
flex-direction: column;
text-align: center;
}
.ct-content-1 {
flex: 1 20%;
background: gold;
display: flex;
justify-content: center;
flex-direction: column;
text-align: center;
}
.ct-content-2 {
flex: 1 20%;
background: hotpink;
border-left: 4px solid #000;
border-right: 4px solid #000;
display: flex;
justify-content: center;
flex-direction: column;
text-align: center;
}
.ct-content-3 {
flex: 1 20%;
align-items: center;
display: flex;
justify-content: center;
flex-direction: column;
text-align: center;
}
<div class="wrapper">
<div class="ct-title">Title
<p>This is some text in a paragraph.</p>
</div>
<div class="ct-content-1">6</div>
<div class="ct-content-2">8</div>
<div class="ct-content-3">12</div>
</div>