This question already has answers here:
Why are flex items not wrapping?
(2 answers)
Closed 2 years ago.
I have two boxes I want to be displayed side-by-side. I had them displayed side-by-side fine but the left box was not vertically centered.
I followed a guide online on how to vertically align the boxes and it worked, except now the responsiveness is all messed up, if I resize the window to be very thin, the boxes dont wrap or overflow neatly.
https://jsfiddle.net/martinradio/adqLpxn5/85/
<div>
<style>
#container {
display: flex;
flex-wrap: wrap;
flex-direction: row;
align-items: center;
width:100%;
}
.box {
width: 50%;
box-sizing:border-box;
margin: 5px;
text-align: center;
}
</style>
<div id="container" class='w-100' >
<!-- flex container -->
<div class="box" style='background:red'>
<!-- flex item -->
<div class="form-group">
<div id='taggerErrDisplay'></div>
<input size="22" type="text" placeholder='Discogs URL' name="url" id="urlInput">
<button style="cursor: pointer;" id='urlInputButton' name="data" type="button"
onclick="submitDiscogsURL(document.getElementById('urlInput').value)">Submit</button>
</div>
</div>
<div class=" box" style='background:blue'>
<!-- flex item -->
<div id="drop-area">
<form class="my-form">
<h5>Upload multiple files with the file dialog or by dragging and dropping files here.</h5>
<br>
<input style="cursor: pointer;" type="file" id="file" multiple="multiple" />
</form>
</div>
</div>
</div>
<style>
#drop-area {
/* border: 2px dashed #ccc; */
border-radius: 20px;
width: 480px;
padding: 20px;
}
/*
#drop-area.highlight {
border-color: purple;
}
*/
p {
margin-top: 0;
}
.my-form {
margin-bottom: 10px;
}
#gallery {
margin-top: 10px;
}
#gallery img {
width: 150px;
margin-bottom: 10px;
margin-right: 10px;
vertical-align: middle;
}
.button {
display: inline-block;
padding: 10px;
background: #ccc;
cursor: pointer;
border-radius: 5px;
border: 1px solid #ccc;
}
.button:hover {
background: #ddd;
}
#fileElem {
display: none;
}
</style>
</div>
I tried changing the flex properties but it causes both elements to remain permanently overflowed:
I don't know but you can try this.
Change the css as below:
#container {
display: flex;
flex-direction: row;
align-items: center;
width:100%;
}
.box {
width: 50%;
box-sizing:border-box;
margin: 5px;
text-align: center;
Related
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>
I'm currently designing a header bar for a site I'm working on. I would like the header to show a logo at the left, the title in the center, and the user account on the right.
This mockup is what I'm envisioning. Note that the dotted boxes denote a div.
I've made some progress on creating it in HTML/CSS, but I can't seem to get the title to center to the viewport.
As you can see, the title is centering itself between the logo and the account info divs, instead of centering itself on the viewport. This ends up making the page just look a little bit off, and so I would really like to center the title to the viewport.
Here's my code:
.headerBarArea {
background-color: #7785A2;
border: 0;
padding: 0;
width: 100%;
display: inline-block;
text-align: center;
}
.logoArea {
display: inline;
text-align: center;
float: left;
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.minesLogo {
width: 96px;
}
.titleArea {
display: inline-block;
text-align: center;
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.siteTitle {
color: white;
}
.pageTitle {
color: white;
}
.userAccountArea {
display: inline;
text-align: center;
float: right;
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.userAccountIcon {
float: left;
width: 35px;
}
.userAccountText {
float: right;
}
<div className="headerBarArea">
<div className="logoArea">
<img src="assets/mines_logo_stacked.png" className="minesLogo" />
</div>
<div className="titleArea">
<h2 className="siteTitle">This is my site Title</h2>
<h3 className="pageTitle">Page Title</h3>
</div>
<div className="userAccountArea">
<img src="assets/user_account.png" className="userAccountIcon" />
<span className="UserAccountText">John Smith (Student)</span>
</div>
</div>
Any ideas on what I could do to make the title div centered to the viewport, instead of centering between the two divs?
html code
<div class="flex-container">
<div class="flex-item">1</div>
<div class="align-self-center">
<span class="siteTitle">This is my site Title</span>
<br>
<div class="text-center ">Page Title</div>
</div>
<div class="flex-item align-self-end third-item">3</div>
</div>
CSS code
.flex-container {
/* We first create a flex layout context */
display: flex;
/* Then we define the flow direction
and if we allow the items to wrap
* Remember this is the same as:
* flex-direction: row;
* flex-wrap: wrap;
*/
flex-flow: row wrap;
/* Then we define how is distributed the remaining space */
justify-content: space-between;
}
.flex-item {
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin-top: 10px;
line-height: 150px;
color: white;
font-weight: bold;
text-align: center;
}
.third-item {
height: 100px;
}
.text-center{
text-align: center;
}
.align-self-end{
align-self:end;
}
.align-self-center{
align-self:center;
}
code output
code solution
used flex to place the items used .flex-container as parent div where flex items are placed in .justify-content: space-between; is used to place space in between the items. align-self:center; is used to place Page Title at center
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
codepen
.headerBarArea{
display:flex;
justify-content: space-between;
align-items: center
}
It is an easy way to the layout.
you can try it.
Try adding margin-left:auto; and margin-right:auto; to the .titleArea class .
I would suggest using a flex-box though.
Replace your css code with this to make title div centered in all the viewport.
.headerBarArea {
background-color: #7785A2;
border: 0;
padding: 0;
width: 100%;
display:flex;
justify-content: space-between;
}
.logoArea , .titleArea , .userAccountArea {
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.minesLogo {
width: 96px;
}
.titleArea {
text-align: center;
}
.siteTitle , .pageTitle{
color: white;
}
.userAccountIcon {
float: left;
width: 35px;
}
.userAccountText {
float: right;
}
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;
}
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>
This question already has answers here:
Why don't flex items shrink past content size?
(5 answers)
Closed 4 years ago.
I have a problem with the display flex, but this only happens in firefox, the idea is not to place absolute position, so I put everything in flex.
Is there a possible solution without using absolute positions?
This error occurs when the browser is a small window
Google Chrome:
Firefox:
CSS:
* {
padding: 0;
margin: 0;
}
.w-100 {
width: 100%;
}
.flex {
display: flex;
align-items: center;
}
.special {
border-bottom: 1px solid #CCC;
padding: 10px;
}
input {
width: 100%;
padding: 10px;
border: none;
background: gray;
}
.flex-2 {
padding-left: 10px;
font-size: 28px;
}
.content {
white-space: nowrap;
}
HTML:
<div class="flex">
<div class="flex w-100 special">
<input type="" class="flex-1" />
<div class="flex-2">
<i class="fa fa-address-book" aria-hidden="true"></i>
</div>
</div>
<div class="content">
content info box content
</div>
</div>
URL: https://codepen.io/anon/pen/RJmWmy
Width and flex layouts don't really work well together. Also you requested the input be 100% of the width which means it would push out the address book element. Also, input fields have a minimum width which you would likely need to reset. Maybe something like this...
Note: I replaced the address book icon with a blue background div so I didn't have to get the css url working.
* {
padding: 0;
margin: 0;
}
.flex {
display: flex;
align-items: center;
}
.flex > .form-fields {
flex: 1 1 0px;
display: flex;
}
.flex > .content {
flex: 0 0 auto;
display: flex;
white-space: nowrap;
}
.flex > .form-fields > .text-input {
flex: 1 1 0px;
border-bottom: 1px solid #CCC;
padding: 10px;
padding: 10px;
border: none;
background: gray;
min-width: 0px;
}
.flex > .form-fields > .address-book-wrapper {
flex: 0 0 28px;
padding: 0px 8px;
background: blue;
}
<div class="flex">
<div class="form-fields">
<input type="" class="text-input" />
<div class="address-book-wrapper">
<i class="fa fa-address-book" aria-hidden="true"></i>
</div>
</div>
<div class="content">
content info box content
</div>
</div>