Flex items aren't centering within container? - css

I'm attempting to center the flex items, however I must be doing something wrong because it isn't working. The basic outline is that Timeline-Container holds 3 smaller containers (TL-1,TL-2, etc). Everything is working fine between the parent container and the children container (meaning that all the TL-# containers are centered correctly with flex), however none of the items within the TL-# containers are centering.
I've tried "justify-content: center;" and it isn't affecting anything.
<div id="Timeline-Container">
<div id="TL-1">
<img src="Photos/2018 SB Photos/Lighted_Stadium_1.JPG">
<p>Caption 1</p>
</div>
<div id="TL-2">
<img src="Photos/2018 SB Photos/Lighted_Stadium_2.JPG">
<p>Caption 2</p>
</div>
<div id="TL-3">
<img src="Photos/2018 SB Photos/Lighted_Stadium_3.JPG">
<p>Caption 3</p>
</div>
</div>
#Timeline-Container {
display: flex;
align-items: center;
justify-content: center;
flex-wrap: nowrap;
}
#Timeline-Container img {
height: 35%;
width: 35%;
border: 5px solid #cccccc;
}
#TL-1 {
/*Parent Flex Code*/
display: flex;
flex-direction: column;
justify-content: center;
align-items; center;
/*Child Flex Code*/
order: 1;
}
#TL-2 {
/*Parent Flex Code*/
display: flex;
flex-direction: column;
justify-content: center;
align-items; center;
/*Child Flex Code*/
order: 2;
}
#TL-3 {
/*Parent Flex Code*/
display: flex;
flex-direction: column;
justify-content: center;
align-items; center;
/*Child Flex Code*/
order: 3;
}

You have a typo align-items; center; which has a semi-colon instead of align-items: center; which has a colon. Here is a code pen: https://codepen.io/the_legitTDM/pen/NQXPWv
FYI: There is added margin for better viewing

The justify-content rule only works on actual flex items. Flex items are elements whose immediate parent has display: flex; set. So if you want to center the content in your #TL-? items usinf flexbox rules, you will need to add display: flex to them as well.
My favorite flexbox resource, helps the concepts just make sense: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Related

Why does a flex container shrink when applying display: flex to body?

I'm making an observation more so than stating a problem, if only to help anyone who's also noticed. I'm following a tutorial on flexbox when I accidently comment out display: flex on body{}. The container expands from 120px (flex-direction: column) wide to 100% of the viewport, horizontally. Display: grid also expands to 100% of the horizontal viewport. This observation is enough for me to post the question because I struggle working with flexbox and I end up applying padding and margin manually. I wonder if anyone else has any observations that are not explicitly stated in flexbox documentation.
<div class="flex-container">
<div class="item">item 1</div>
<div class="item">item 2</div>
<div class="item">item 3</div>
</div>
.flex-container {
display: flex;
flex-direction: column;
align-items: center;
}
.item {
background-color: blue;
color: white;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100px;
width: 100px;
margin: 10px;
}
Why does display: flex shrink the container?
When an element has display: flex applied—making it a flex container—certain default settings come into play. Two of these settings are flex-direction: row on the container and flex-basis: auto on the items.
With display: flex on the body element, the flex item (.flex-container) is automatically set to flex-basis: auto, which is the width of the content. That's why the nested container "shrinks".
When display: flex is removed, the body element reverts back to display: block. Its child (.flex-container) is no longer a flex item and takes the default width: 100% of block elements.
If you want a flex item to expand to 100% width, just give it flex-grow: 1 or flex: 1.
body {
display: flex;
}
.flex-container {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
border: 1px solid red;
}
.item {
background-color: blue;
color: white;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100px;
width: 100px;
margin: 10px;
}
<div class="flex-container">
<div class="item">item 1</div>
<div class="item">item 2</div>
<div class="item">item 3</div>
</div>

Equal width flex columns when the container has fluid width

Consider the following HTML code:
<div class="ungrouped-ordered-item">
<div class="information-container">
<div class="originating-order-id"> #00019405 </div>
<div class="placed-by"> Placed by: 18175 </div>
</div>
<div class="indicator-container">
<div class="indicator"> Unpaid </div>
<div class="indicator" >Pending </div>
</div>
</div>
with the following css:
.ungrouped-ordered-item {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.information-container {
display: flex;
flex-direction: column;
}
.indicator-container {
display: flex;
flex-direction: row;
align-items: center;
}
.indicator-container .indicator {
display: flex;
justify-content: space-between;
align-items: center;
flex: 1;
padding: 0 10px;
border: 1px solid #2e2240;
}
The design I am aiming for, is:
to let the outermost flex containers (.ungrouped-ordered-item) children, grow as they need, and leave space between them, so that they are aligned to the left and right respectively of their containers
to make the children of the inner flex container (.indicator-container .indicator) be equal-width, by taking up the width of the wider element (in this case, it being the element with the text "Pending")
My first goal is achieved, but it seems, that even if adding flex: 1 to the .indicator containers, the browser will not correctly calculate the width of the two elements, and they will have uneven widths. I am presuming that this is because that their container, .indicator-container, has a fluid width. Am I right in this? How can I achieve my desired effect with all fluid width containers? (preferably without javascript).
Here's a fiddle also!
What is the argument against a width:50% for the .indicatorbox?
.ungrouped-ordered-item {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.information-container {
display: flex;
flex-direction: column;
}
.indicator-container {
display: flex;
flex-direction: row;
align-items: center;
gap: 10px;
}
.indicator-container > * {
border: 1px solid #2e2240;
padding: 0 10px;
flex-wrap: wrap;
}
.indicator-container .indicator {
width: 50%;
text-align: center;
}
<div class="ungrouped-ordered-item">
<div class="information-container">
<div class="originating-order-id"> #00019405 </div>
<div class="placed-by"> Placed by: 18175 </div>
</div>
<div class="indicator-container">
<div class="indicator"> Unpaid </div>
<div class="indicator" >Pending long long</div>
</div>
</div>
Changing the .indicator-container class from flex to grid and using automatic columns solves my problem. Although, it doesn't have as much coverage as flex, for my needs, this works:
.indicator-container {
display: grid;
grid-auto-columns: minmax(0, 1fr);
grid-auto-flow: column;
}
Here's the updated fiddle also.

Why is my nested div content shrinking and space I did not declare appearing between the div containers?

Here is my html file:
<header >
<div id="ballo">
<div id="balloresize"> <img src="images/BAL.png" alt="" id="balloicresize"> <h2>Black Anthem LTD</h2> </div>
</div>
<div id="menus">
<div class="icir">
<img src="images/bmenu icon.png" alt="menu" class="icimg">
</div>
<div id="home">
<h3>HOME</h3>
</div>
<div id="aut">
<h3>ABOUT</h3>
</div>
<div id="serv">
<h3>SERVICES</h3>
</div>
<div id="proj">
<h3>PROJECTS</h3>
</div>
<div id="gal">
<h3>GALLERY</h3>
</div>
<div id="raq">
<h3>REQUEST A QUOTE</h3>
</div>
</div>
</header>
and the css:
*{
font-family: Arial, Helvetica, sans-serif;
font-size: medium;
text-decoration: none;
}
html,body{
width:100%;
height:100%;
margin: 0;
padding: 0;
overflow-x: hidden;
}
header{
display: grid;
grid-template-columns: repeat(2, auto);
grid-template-rows: auto;
width: 100%;
column-gap: 0%;
background: #fff;
}
#ballo{
grid-column: 1/2;
}
#balloresize{
width: 50%;
height: 30%;
display: flex;
flex-flow: row;
flex-wrap: nowrap;
justify-content: space-between;
}
#balloicresize{
max-width: 100%;
box-sizing: border-box;
}
#menus{
grid-column: 2/3;
display: flex;
flex-flow: row nowrap;
width: 50%;
justify-content: space-evenly;
}
this the output I'm getting:
image of the undesired output I'm getting
I don't know why there's space between the two parent divs under the header tag #fsect and #menus and the contents are also unable to properly get spaced. Please help me solve this, I have tried all that I know.
No matter what I try the space between the two divs just don't go and the contents also stays shrinked together. The html is properly rendered it's the css that is not working the way I want it to.
This is the way I want the header to be arranged:
The header of this picture is the design I want to get on my css
Please help!
I would recommend changing your header to be a flex-box, unless you have a specific reason you need to use grid. The following style changes:
header {
width: 100%;
background: #fff;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
#ballo, #menus {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
display: flex says that this element will be a flex-container.
flex-direction specifies if we're laying out children in a row or column. your header is a row.
justify-content specifies how the flex-container will allocate space for its children along its main-axis (its main-axis is a row when you specify 'row' for flex-drection and a column when you specific 'column'). space-between will maximize the amount of whitespace between the elements. You might prefer space-around.
align-items specifies how the flex-container will align items along it's opposite axis (whatever the opposite of the axis above is). If you want to place your items roughly in the 'center' vertically of your header, which by your example looks to be the case, then you want a value of 'center' here.
Move the outside of that inner div:
<div id="ballo">
<img src="images/BAL.png" alt="" id="balloicresize">
<h2>Black Anthem LTD</h2>
</div>
The spacing between things like individual menu items will still not be ideal, you can fix that by using a padding value of your choosing.
I would delete or comment out your other css classes temporarily, to see what just the above gets you. Then add stuff as you need it.

Align-items: center overriding justify-content: space-around

I want to have a centered flex column layout with some items inside in a row with space between them.
html:
<div class="flex">
<h1>Welcome</h1>
<p>
Before you start there's a short introduction
</p>
<p>
<b>Do you want to see it?</b>
</p>
<div class="flex-row">
<span><b>Skip</b></span>
<button>Start</button>
</div>
</div>
css:
.flex {
display: flex;
flex-direction: column;
align-items: center;
}
.flex-row {
display: flex;
flex-direction: row;
justify-content: space-between;
}
The problem is the flex-row space-between doesn't work while the parent container has align-items: center and I can't figure out to how ignore it for the flex-row.
Try switching
align-items: center;
with
text-align: center;
but since
justify-content: space-between;
gives you the max space you can have between two items try adjusting the width of the site.

Flexbox item does not vertically align as expected when container has a fixed height in iE11

I have a flexbox vertical align problem in IE11.
I have a flexbox container an a child. I have an image inside the item div.The image is bigger than the flex container, so I want to show the middle part of the image, using justify-content: center; This works as I expect in all browsers but IE11. The attached illustration should show the issue. Any help is appreciated.
<div class="container">
<div class="item">
<img src="image.jpg" />
</div>
</div>
.container {
display: flex;
flex-direction: column;
height: 200px;
overflow: hidden;
}
IE11 is buggy when it comes to Flexbox, and in this case it doesn't what other browsers does.
When in a flex column direction, use transform: translate() to make it behave.
.container {
display: flex;
flex-direction: column;
justify-content: center;
height: 200px;
overflow: hidden;
border: 1px dashed gray;
}
/* IE11 only */
_:-ms-fullscreen, :root .IE11Fix {
position: relative;
top: 50%;
transform: translateY(-50%);
}
<div class="container">
<div class="item IE11Fix">
<img src="http://placehold.it/100x300/f00" />
</div>
</div>
I think you are mistakenly using justify-content: center and flex-direction: column;
In a flex column, The X axis alignment is controlled by align-items, and the Y axis alignment is controlled by justify-content
If you remove flex-direction: column and set your container to be align-items: center and justify-content: center, both work in the same way with the result you expected:
.container {
display: flex;
height: 200px;
justify-content: center;
align-items: center;
}
https://jsfiddle.net/s3Lmqndu/1/

Resources