how to center div in the middle of the page? - html

After googling for hours, I am still stuck at this problem. Since I used flex-direction:column, to align the child divs hence, I used justify-content:center but the child divs look aligned to the left.
<div class=container>
<div class= "wrapper">
<div class = "image-float">
<img class = "profile-picture" src = "{% static 'images/image.jpg'%}">
</div>
<div>
<p>Welcome to Sparison...</p>
</div>
<div class="intro">
<h1>Copy code below and share with friends</h1>
</div>
<div class="url-container input-group">
<input type="text" id="random" class="url input-border form-control" value="">
<div class="input-group-append">
<span class="input-border input-group-append input-group-text">
<i class="far fa-copy url-copy-icon"></i></span>
</div>
</div>
</div>
</div>
The relevant CSS is below:
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
font-family: Montserrat;
font-size: 50px;
font-weight: bold;
background-color: var(--very-pale-blue);
width:100%;
height:100vh;
}
.container{
max-width: 100%;
height: 90vh;
margin-top: 0;
}
.wrapper{
display: flex;
flex-wrap: wrap;
max-width: 100%;
height: 90vh;
justify-content: center;
flex-direction: column;
}
.url-container .input-group {
border-radius: 10px;
width: 50%;
}
.input-group {
position: relative;
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
-ms-flex-align: stretch;
align-items: stretch;
width: 50%;
/* background-color: #1DB954; */
border-radius: 2px;
}
Above is how the page renders. every element under the wrapper div should be in the middle of the page centered vertically.

You can find more information about flex here:
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Aligning_Items_in_a_Flex_Container
.wrapper{
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
max-width: 100%;
height: 90vh;
}
Link to jsfiddle:
https://jsfiddle.net/y4qra692/

Change the container width
.container {
width: 100%;
}
Then for the wrapper as you have changed the flex-direction to column you now need align-items rather than justify-content as (confusingly) when you change the direction these properties swap around:
.wrapper {
width: 100%;
display: flex;
align-items: center;
}

To make div centered you can add this code to .wrapper
.wrapper{
width: 50%;
margin:auto;
}

To make a div centered:
<section class="middle">
<div class="div">
<p>Hi</p>
</div>
</section>
Style.css
.middle {
height: 100vh; /* or height you want */
width: 100%;
position: relative;
}
.div {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}

Related

Vertical scrolling for multiple div tags inside a div tag

I am trying to create a scrollable file tree. Minimal code can be found below.
https://jsfiddle.net/3kLmchot/1/
Problem is when I try to add overflow-y: scroll;, to the filebrowse-outer div, the first few items are not visible at all. This problem gets worse when I add more divs.
It seems like items are added about the center of the filebrowse-outer div, so the items that are added at the bottom can be found via scrolling, but the items that are added at the top are invisible. (see the visual aid below)
item 1
item 2
center of the div
item 3
item 4
Scroll bar forms at around item 2, which makes item 1 invisible. Does anyone know why this is happening, and potential solutions?
I got it to work with the following styles, specifically removing justify-content: center; from your .flex-container that is being used by multiple divs and only applying it to the one that is for .flex-container.filebrowse-inner
.side-bar {
position: fixed;
width: 20%;
height: 200px;
display: flex;
flex-direction: column;
}
.flex-container {
display: flex;
/* justify-content: center; <-- THIS LINE IS CAUSING THE ISSUE */
align-items: center;
}
.flex-container.filebrowse-outer {
flex-direction: column;
border: 1px solid #f00;
overflow-y: scroll;
align-items: stretch;
}
.flex-container.filebrowse-inner {
flex-direction: row;
justify-content: center;
width: 100%;
padding-bottom: 15px;
text-align: left;
position: relative;
}
/* .display-container {
width: 70%;
overflow: auto;
} */
I think that you meant to have scroll bar in the external div with class 'side-bar', if yes:
.side-bar {
position: fixed;
width: 20%;
height: 200px;
display: flex;
flex-direction: column;
overflow-y:scroll;
}
.flex-container {
display: flex;
justify-content: center;
align-items: center;
}
.flex-container.filebrowse-outer {
flex-direction: column;
border: solid;
min-height: max-content; /* For safari*/
/* overflow-y: scroll; */
align-items: stretch;
}
.flex-container.filebrowse-inner {
flex-direction: row;
width: 100%;
min-height: max-content; /* For safari*/
padding-bottom: 15px;
text-align: left;
position: relative;
}
.display-container {
width: 70%;
overflow: auto;
}
<div class="side-bar">
<div class="flex-container filebrowse-outer">
<div class="flex-container filebrowse-inner">
<div class="display">
<p>Click Me 1!</p>
</div>
</div>
<div class="flex-container filebrowse-inner">
<div class="display">
<p>Click Me 2!</p>
</div>
</div>
<div class="flex-container filebrowse-inner">
<div class="display">
<p>Click Me 3!</p>
</div>
</div>
<div class="flex-container filebrowse-inner">
<div class="display">
<p>Click Me 4!</p>
</div>
</div>
</div>
</div>
You can then move the border to 'side-bar' class.

CSS Flexbox annoying slight offset of row elements [duplicate]

This question already has answers here:
White space under image [duplicate]
(5 answers)
Remove white space from image
(3 answers)
Closed 5 years ago.
So I have two divs in a full width container that I want to give variable sizing with flexbox, but no matter what I do, there is an annoying offset at the bottom. Using margins I can come close to fixing the problem, but it's never perfect.
If you run the code snippet below and scroll to the bottom you can see it, the image and the black content container are not aligned at the bottom.
What's going on?
#container {
width: 100%;
display: inline-flex;
flex-direction: row;
}
#image-wrapper {
flex-grow: 3;
max-width: 1000px;
position: relative;
/*background-color: black;*/
}
#menu {
flex-grow: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 50px;
background-color: #101010;
color: #fefefe;
align-items: stretch;
display: flex;
margin-bottom:7px;
}
#form {
width: 100px;
}
#image {
width: 100%;
}
<div id="container">
<div id="image-wrapper">
<img id="image" src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/sample1_l.jpg"/>
</div>
<div id="menu">
<div id="form">
CONTENT<br>CONTENT<br>
</div>
</div>
</container>
There is some space below the image since the image is an inline-element and as such there is some space reserved below the (invisble) baseline that the image is aligned to vertically. To avoid that, there are two possible solutions:
1.) Apply display: block; to the image (see first snippet)
or
2.) Apply font-size: 0 to the image container (see second snippet)
#container {
width: 100%;
display: inline-flex;
flex-direction: row;
}
#image-wrapper {
flex-grow: 3;
max-width: 1000px;
position: relative;
/*background-color: black;*/
}
img {
display: block;
}
#menu {
flex-grow: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 50px;
background-color: #101010;
color: #fefefe;
align-items: stretch;
display: flex;
}
#form {
width: 100px;
}
#image {
width: 100%;
}
<div id="container">
<div id="image-wrapper">
<img id="image" src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/sample1_l.jpg" />
</div>
<div id="menu">
<div id="form">
CONTENT<br>CONTENT<br>
</div>
</div>
</div>
SECOND SOLUTION:
#container {
width: 100%;
display: inline-flex;
flex-direction: row;
}
#image-wrapper {
flex-grow: 3;
max-width: 1000px;
position: relative;
/*background-color: black;*/
font-size: 0;
}
#menu {
flex-grow: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 50px;
background-color: #101010;
color: #fefefe;
align-items: stretch;
display: flex;
}
#form {
width: 100px;
}
#image {
width: 100%;
}
<div id="container">
<div id="image-wrapper">
<img id="image" src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/sample1_l.jpg" />
</div>
<div id="menu">
<div id="form">
CONTENT<br>CONTENT<br>
</div>
</div>
</div>
#container {
width: 100%;
display: inline-flex;
flex-direction: row;
}
#image-wrapper {
flex-grow: 3;
max-width: 1000px;
position: relative;
/*background-color: black;*/
}
#menu {
flex-grow: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 50px;
background-color: #101010;
color: #fefefe;
align-items: stretch;
display: flex;
margin-bottom:4px;
}
#form {
width: 100px;
}
#image {
width: 100%;
}
<div id="container">
<div id="image-wrapper">
<img id="image" src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/sample1_l.jpg"/>
</div>
<div id="menu">
<div id="form">
CONTENT<br>CONTENT<br>
</div>
</div>
</container>
Looks like the margin is just a bit off

chat web app: flex-boxes inside div position:fixed

I have a chat web app with a bottom bar consisting of: button, text entry, button:
<div id="bottom-bar">
<div class="button-left">
<img src="https://placeholdit.co//i/40x40">
</div>
<div class="textentry">
<input type="text" id="chatmsg" name="chatmsg">
</div>
<div class="button-right">
<img src="https://placeholdit.co//i/40x40">
</div>
</div>
#bottom-bar {
position: fixed;
bottom: 0px;
height: 40px;
width: 100%;
}
What I want to achieve: (1) place the left button to the left of the bottom-bar, (2) place the right button to the right of the bottom-bar and (3) have an input field that stretches in the middle using all the space that is available.
I tried:
#bottom-bar {
...the css above and additionally ...
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content:space-between;
align-items: center;
}
#chatmsg{
width: auto;
}
But this got me nowhere. Any help is much appreciated.
the textentry has to grow and take all the place left:
#bottom-bar {
position: fixed;
bottom: 0px;
height: 40px;
width: 100%;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content:space-between;
align-items: center;
}
.button-left,
.button-right {
width: 40px;
}
.textentry{
flex-grow: 1;
}
#chatmsg {
width: 100%;
}
check this: https://jsfiddle.net/43Lqzznt/3/
Use calc(100% - 80px); as the width for .textentry (i.e. full width minus the width of the two images) and make the input 100% wide inside of that. If you do just that, the input overflows its container to the right, so use padding on .textentry to compensate that and adjust the distance between the elements as desired, as shown in my snippet.
html,
body {
margin: 0;
}
#bottom-bar {
position: fixed;
bottom: 0px;
height: 40px;
width: 100%;
}
#bottom-bar {
display: flex;
justify-content: space-between;
align-items: center;
}
.textentry {
width: calc(100% - 80px);
padding: 0 10px 0 2px;
}
input#chatmsg {
width: 100%;
}
<div id="bottom-bar">
<div class="button-left">
<img src="https://placeholdit.co//i/40x40">
</div>
<div class="textentry"><input type="text" id="chatmsg" name="chatmsg"></div>
<div class="button-right">
<img src="https://placeholdit.co//i/40x40">
</div>
</div>
Using display:flex on the container and by adjusting the input div's flex:1 or setting it's width to 100% should do it. Check output in the console.
FLEX
The input box will stretch between the images as the container div shrinks or expands in size.
#bottom-bar {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content:space-between;
align-items: center;
}
#chatmsg{
width: 100%;
}
.textentry {
/* width: 100% */
flex: 1;
}
<div id="bottom-bar">
<div class="button-left">
<img src="https://placeholdit.co//i/40x40">
</div>
<div class="textentry">
<input type="text" id="chatmsg" name="chatmsg">
</div>
<div class="button-right">
<img src="https://placeholdit.co//i/40x40">
</div>
</div>

flexbox vertical center with 100% height

Is it possible to use flexbox to center vertically and horizontally for background color purposes?
.wrapper {
display: flex;
width: 100vw;
height: 100vh;
text-align: center;
}
.item {
flex: 1;
background-color: green;
align-self: center;
justify-content: center;
}
<div class='wrapper'>
<div class='item'>
sdafsdfs
</div>
</div>
If I add a height: 100% the text moves back to the top.
Add display: flex for your items. Then only it can align items inside it as per your requirements. Please have a look at the updated code.
.wrapper {
display: flex;
width: 100vw;
height: 100vh;
text-align: center;
}
.item {
display: flex;
background-color: green;
align-items: center;
justify-content: center;
height: 100%;
width: 100%;
}
<div class='wrapper'>
<div class='item'>
sdafsdfs
</div>
</div>
Hope this help

Incorrect image sizing in IE11 in flexbox

I am trying to position an image in a div. It should be centered. The div should have a minimum width and it should grow only if text below the image requires it.
The following code demonstrates what I want in Chrome:
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
body {
background-color: aliceblue;
}
.loading-spinner-overlay-1 {
left: 0;
top: 0;
right: 0;
bottom: calc(100% - 300px);
position: absolute;
z-index: 9999;
display: flex;
align-items: center;
justify-content: center;
}
.loading-spinner-overlay-2 {
left: 0;
top: 300px;
right: 0;
bottom: calc(100% - 600px);
position: absolute;
z-index: 9999;
display: flex;
align-items: center;
justify-content: center;
}
.loading-spinner-background {
min-width: 100px;
min-height: 100px;
max-width: 50%;
background-color: white;
border-radius: 10px;
display: flex;
flex-direction: column;
z-index: 1;
padding: 20px;
}
.loading-spinner-container {
display: flex;
flex-direction: column;
flex-grow: 1;
}
.loading-spinner-container > img {
margin: auto;
}
.loading-spinner-container > p {
text-align: center;
margin: 0;
}
<img src="http://placehold.it/40x40">
<div class="loading-spinner-overlay-1">
<div class="loading-spinner-background">
<div class="loading-spinner-container">
<img src="http://placehold.it/40x40">
</div>
</div>
</div>
<div class="loading-spinner-overlay-2">
<div class="loading-spinner-background">
<div class="loading-spinner-container">
<img src="http://placehold.it/40x40">
<p>
Some long text
</p>
</div>
</div>
</div>
However, in IE11, it looks like this:
Am I doing something wrong? Or is this a bug in IE11?
What can I do to fix this?
I have tried setting max-width: 100% and flex-shrink:0 on the img tag as some google results suggest, but this didn't help.
Updated
Adding align-items: flex-start to the loading-spinner-container fixes the issue, which kind of make sense, since align-items default is stretch and works cross axis (in this case horizontal) for flex column direction.
Updated, 2nd revision
Additionally, to fix the vertical centering, and since IE11 again has some issues when it comes to min-height, remove flex-direction: column from the loading-spinner-background and move min-height: 100px to loading-spinner-container.
Stack snippet
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
body {
background-color: aliceblue;
}
.loading-spinner-overlay-1 {
left: 0;
top: 0;
right: 0;
bottom: calc(100% - 300px);
position: absolute;
z-index: 9999;
display: flex;
align-items: center;
justify-content: center;
}
.loading-spinner-overlay-2 {
left: 0;
top: 300px;
right: 0;
bottom: calc(100% - 600px);
position: absolute;
z-index: 9999;
display: flex;
align-items: center;
justify-content: center;
}
.loading-spinner-background {
min-width: 100px;
max-width: 50%;
background-color: white;
border-radius: 10px;
display: flex;
/* flex-direction: column; removed */
z-index: 1;
padding: 20px;
}
.loading-spinner-container {
min-height: 60px; /* added/value changed (moved from *-background class) */
display: flex;
flex-direction: column;
flex-grow: 1;
align-items: flex-start; /* added */
}
.loading-spinner-container > img {
margin: auto;
}
.loading-spinner-container > p {
text-align: center;
margin: 0;
}
<img src="http://placehold.it/40x40">
<div class="loading-spinner-overlay-1">
<div class="loading-spinner-background">
<div class="loading-spinner-container">
<img src="http://placehold.it/40x40">
</div>
</div>
</div>
<div class="loading-spinner-overlay-2">
<div class="loading-spinner-background">
<div class="loading-spinner-container">
<img src="http://placehold.it/40x40">
<p>
Some long text
</p>
</div>
</div>
</div>
Also align-items: center can be used, and if combined with justify-content: center, you can drop the margin: auto on the img.
Fiddle demo
Updated, 3rd revision, based on a comment
Longer text appears to not wrap on IE.
As shown in this post, IE need to have the width set explicit on the flex item, here p, and since also loading-spinner-container is a flex column item (for row item flex-grow is enough), it needs one too (or overflow: hidden).
Fiddle demo
Try this:
<div class="loading-spinner-overlay-2">
<div class="loading-spinner-background">
<div class="loading-spinner-container">
<div class="img-container"> <!-- added this -->
<img src="http://placehold.it/40x40">
<p>
Some long textwrwerwer
</p>
</div>
</div>
</div>
</div>
Then added new class:
.loading-spinner-container .img-container {
clear:both;
text-align:center;
}
Basically I added a wrapping div.
Hope it helps.