Centering multiple items in flexbox not working as expected [duplicate] - css

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 2 years ago.
I am trying to vertically content within a div with flexbox, so that it always remains centered.
However it is not working as expected. Interested to see where I'm going wrong.
Just to confirm, the 'left div' should display the contents in regular vertical order, not horizontally as is currently happening.
section {
display:flex;
}
div {
padding:12px;
width:400px;
height:300px;
display:flex;
align-items:center;
}
.left {background:#bada55}
.right {background:red}
<section>
<div class="left">
<h2>Very long title</h2>
<p>Caption goes here</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque at magni est facilis error! Perspiciatis ab tempore qui mollitia, nihil itaque praesentium deleniti hic asperiores soluta, rem natus. Quidem, necessitatibus?</p>
</div>
<div class="right">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsa distinctio, labore, quod ex blanditiis totam esse eligendi consequatur inventore reprehenderit temporibus architecto molestias ratione, ullam deserunt tempora nesciunt corrupti! Tempora!</p>
</div>
</section>
Link to codepen

You can use flex-direction on .left to make it center vertically:
section {
display: flex;
}
div {
padding: 12px;
width: 400px;
height: 300px;
display: flex;
}
.left {
background: #bada55;
flex-direction: column;
justify-content: center;
}
.right {
background: red;
align-items: center;
}
<section>
<div class="left">
<h2>Very long title</h2>
<p>Caption goes here</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque at magni est facilis error! Perspiciatis ab tempore qui mollitia, nihil itaque praesentium deleniti hic asperiores soluta, rem natus. Quidem, necessitatibus?</p>
</div>
<div class="right">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsa distinctio, labore, quod ex blanditiis totam esse eligendi consequatur inventore reprehenderit temporibus architecto molestias ratione, ullam deserunt tempora nesciunt corrupti! Tempora!</p>
</div>
</section>

EDIT based on your comment, I believe this works:
section {
display: flex;
align-items: center;
height: 300px;
}
div {
width: 400px;
padding: 12px;
}
div.right {
display: flex;
align-items: center;
}
<section>
<div class="left">
<h2>Very long title</h2>
<p>Caption goes here</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque at magni est facilis error! Perspiciatis ab tempore qui mollitia, nihil itaque praesentium deleniti hic asperiores soluta, rem natus. Quidem, necessitatibus?</p>
</div>
<div class="right">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsa distinctio, labore, quod ex blanditiis totam esse eligendi consequatur inventore reprehenderit temporibus architecto molestias ratione, ullam deserunt tempora nesciunt corrupti! Tempora!</p>
</div>
</section>

Related

Why the text doesnt appear aligned?

I have this css and its working fine the text appears at right and is aligned.
However the same code of that fiddle in a pdf, the text is apeparing like this (like the example at right, at left is how it should display):
Do you know what can be the issue?
.container {
display: flex;
}
.span {
margin-right: 5px;
}
<div class="container">
<span class="span">1.</span>
<span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sint vel aut quidem consequatur quaerat optio ab! Iste fugit nostrum odio dolorum sequi, odit ratione omnis, atque sunt perferendis commodi, iure.</span>
</div>
You can solve this problem by using text-align
.container {
display: flex;
text-align: justify;
text-justify: inter-word;
}
.span {
margin-right: 5px;
}
<div class="container">
<span class="span">1.</span>
<span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sint vel aut quidem consequatur quaerat optio ab! Iste fugit nostrum odio dolorum sequi, odit ratione omnis, atque sunt perferendis commodi, iure.</span>
</div>
#mehran text-justify:inter-word; is not a valid CSS property

when overflow: auto, padding-bottom is not seen

<div class="container">
<div class="content">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Pariatur amet, vitae fuga provident et quae aut minus voluptate quidem maiores at recusandae sit deleniti quia dolore, illum reiciendis! Hic, optio Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fugit accusantium, obcaecati dicta unde repellat illo maxime! Magni officiis, culpa nihil, sequi aliquid vel voluptas quidem laboriosam, omnis nam fuga veniam.</div>
</div>
.content {
width: 200px;
height: 150px;
padding: 20px;
background: yellow;
overflow: scroll;
box-sizing: border-box;
}
http://jsfiddle.net/v6yjLdnp/
Why doesn't the padding-bottom work in this case when scrolling? And how do I make this possible?
I suppose you mean the padding at the bottom of the scrolled content. This seems to be a browser issue - see the comments. But with the following code it should work properly in all browsers.
Transfer some of the settings to the container, then it works as desired:
body {
margin: 0;
}
.container {
width: 200px;
height: 150px;
overflow: scroll;
}
.content {
padding: 20px;
background: yellow;
}
<div class="container">
<div class="content">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Pariatur amet, vitae fuga provident et quae aut minus voluptate quidem maiores at recusandae sit deleniti quia dolore, illum reiciendis! Hic, optio Lorem ipsum dolor sit amet, consectetur adipisicing
elit. Fugit accusantium, obcaecati dicta unde repellat illo maxime! Magni officiis, culpa nihil, sequi aliquid vel voluptas quidem laboriosam, omnis nam fuga veniam.</div>
</div>
The problem is probably be caused by the scroll bars.
This is a workaround with using pseudo elements in css, but it might solve your problem:
.content::after { content: ''; display: block; width: 100%; height: 20px; }
EDIT: Remove the padding bottom of the content element to make it work in all browsers:
.content { padding-bottom: 0px; }

Center text in column/vertically, CSS Grid

I have a simple grid (https://jsfiddle.net/3f5oLjxu/1/), how can I get the links on the left side of the grid to center vertically, instead of being positioned at the top.
css:
.grid {
display: grid;
grid-template-columns: 1fr 3fr;
margin: 0px 12%;
}
.grid>* {
border: 1px solid lightgray;
padding: 15px;
}
<div class="grid">
<nav>
<ul>
<li>Home</li>
<li>Interior Painting</li>
<li>Exterior Painting</li>
<li>Deck Painting</li>
<li>Power Wash</li>
<li>Wallpaper Remvoal</li>
<li>Contact</li>
</ul>
</nav>
<section>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Placeat dignissimos error expedita debitis, eligendi a, dolorum velit, doloremque est repellat perferendis consectetur non similique mollitia maiores officiis totam voluptatibus libero.Lorem ipsum
dolor sit amet, consectetur adipisicing elit. Distinctio non provident sunt, voluptatem omnis. Porro nisi architecto expedita quae odit distinctio sint illo, tempora possimus dolore at, reprehenderit corrupti voluptatibus.
</section>
</div>
<!-- close grid -->
I have tried all sorts of combinations of justify-content, justify-items, align-self, margin: auto;, turning the nav into a flex item, the ul into a flex item, etc... Thanks in advance for any help.
You could do this using flexbox:
https://yoksel.github.io/flex-cheatsheet/
It is easy to learn and is very common tool in nowadays websites.
Make .grid a flex item with flex-basis: 100%; and flex-flow: row nowrap;
Make .nav a flex container, and align it's items in the center with align-items: center;
OR if you just want to use flex on your nav element and nowhere else, doing
nav{
display: flex;
align-items: center;
}
would be totally sufficient, If you don't want your whole layout to have a flexy manner
.grid {
display: flex;
flex-flow: row nowrap;
flex-basis: 100%;
margin: 0px 12%;
}
.nav{
display: flex;
align-items: center;
}
.grid>* {
border: 1px solid lightgray;
padding: 15px;
}
<div class="grid">
<div class="nav">
<nav>
<ul>
<li>Home</li>
<li>Interior Painting</li>
<li>Exterior Painting</li>
<li>Deck Painting</li>
<li>Power Wash</li>
<li>Wallpaper Remvoal</li>
<li>Contact</li>
</ul>
</nav>
</div>
<section>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Placeat dignissimos error expedita debitis, eligendi a, dolorum velit, doloremque est repellat perferendis consectetur non similique mollitia maiores officiis totam voluptatibus libero.Lorem ipsum
dolor sit amet, consectetur adipisicing elit. Distinctio non provident sunt, voluptatem omnis. Porro nisi architecto expedita quae odit distinctio sint illo, tempora possimus dolore at, reprehenderit corrupti voluptatibus.
dignissimos error expedita debitis, eligendi a, dolorum velit, doloremque est repellat perferendis consectetur non similique mollitia maiores officiis totam voluptatibus libero.Lorem ipsum
dolor sit amet, consectetur adipisicing elit. Distinctio non provident sunt, voluptatem omnis. Porro nisi architecto expedita quae odit distinctio sint illo, tempora possimus dolore at, reprehenderit corrupti voluptatibus.
</section>
</div>
<!-- close grid -->
To change the position of a child of a grid, you can use the *-self property. Vertical will be your column access, use nav {align-self: center;} to vertically center the nav in this scenario.
You can use the flexbox layout module. It seems like you were on the track there to begin with. Just attach this to the nav style.
nav {
display: flex;
alignt-items: center;
}

Flexbox - Same height columns inside row

Is it possible to make the columns inside each row the same height as each other? For example to make both the blue headings the same size and so on
Apologies if this has been asked before but I couldn't see an answer to this when searching.
<div class="row">
<div class="column">
<h2>heading</h2>
<div class="block-1">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Debitis, nesciunt.</div>
<div class="block-2">Lorem ipsum dolor sit amet.</div>
</div>
<div class="column">
<h2>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quod, maiores!</h2>
<div class="block-1">Lorem ipsum</div>
<div class="block-2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. In quam praesentium suscipit laudantium illo voluptatibus eligendi, est exercitationem commodi reiciendis.</div>
</div>
</div>
.row {
display: flex;
}
.column {
display: flex;
flex-direction: column;
* {
flex: 1;
}
}
https://jsfiddle.net/vdLaq7t1/
You can do it with align-items: stretch (which is the default value for align-items), but you should redesign your HTML structure.
.column {
background: silver;
}
h2 {
background: cornflowerblue;
margin: 0;
}
.block-1 {
background: tomato;
}
.block-2 {
background: brown;
}
.row {
display: flex;
/* Not required because it's already the default value */
align-items: stretch;
}
.row > *{
flex: 1;
}
<div class="row">
<h2>heading</h2>
<h2>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quod, maiores!</h2>
</div>
<div class="row">
<div class="block-1">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Debitis, nesciunt.</div>
<div class="block-1">Lorem ipsum</div>
</div>
<div class="row">
<div class="block-2">Lorem ipsum dolor sit amet.</div>
<div class="block-2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. In quam praesentium suscipit laudantium illo voluptatibus eligendi, est exercitationem commodi reiciendis.</div>
</div>
Flexbox works in both X and Y axis. When you use flex-direction: column you are changing the main axis from X to Y. You can manage the main axis with the justify-content property, and the secondary axis with the align-items property.
The thing is that align-items work with the stretch value, but justify-content cannot.
So if you want the height to be the same (Y axis), you need to stretch it with align-items: stretch, but align-items only works in the secondary axis, so the main axis needs to be the X axis in this case, and that means that you cannot use flex-direction: column in order to make them the same height.
It can be done with many technics.
For example, you can place 6 columns in 1 row.
.row {
display: flex;
flex-wrap: wrap;
}
.column {
flex-basis: 70%;
}
.column.sm {
flex-basis: 30%;
}
.blue {
background: blue;
}
.silver {
background: silver;
}
.cornflowerblue {
background: cornflowerblue;
}
<div class="row">
<div class="column blue sm">heading</div>
<div class="column blue">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quod, maiores!</div>
<div class="column silver sm"><div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Inventore quaerat, facilis qui placeat! Voluptatem reprehenderit similique rerum officia iste error ab, animi nobis quaerat culpa possimus, nisi laboriosam aliquid hic.</div>
<div>Tempora eius eaque harum, temporibus sequi porro, minima quia, necessitatibus amet nisi unde reiciendis iure ipsa, facilis rerum qui dolores doloribus sed voluptatum! Dicta at, qui, exercitationem molestiae voluptas natus.</div></div>
<div class="column silver">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iusto nemo ipsum voluptates mollitia eius enim, esse voluptatibus eaque doloremque vel asperiores quos unde similique rerum perspiciatis iure, ipsam eum soluta.</div>
<div class="column cornflowerblue sm">lorem</div>
<div class="column cornflowerblue"><div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore enim recusandae veniam optio delectus doloremque maiores quidem, impedit vel reprehenderit quam vitae, fugit atque assumenda molestiae debitis laboriosam blanditiis fugiat.</div>
<div>Excepturi ea minima accusantium delectus totam quae fugiat, ex eos inventore deleniti odit, commodi eveniet, eum ullam consectetur ipsa quasi odio similique. Doloribus tempore accusantium soluta, id deserunt maxime accusamus.</div></div>
</div>

CSS: Trouble with internal links and equal column heights [jsfiddle]

I am using a CSS only solution to get the appearance of equal height columns:
.equal-height-row {
overflow: hidden;
}
.equal-height-col {
padding-bottom: 99999px;
margin-bottom: -99999px;
}
So that when I do this:
<div class="equal-height-row">
<div class="left equal-height-col">
<p>This is the first column.</p>
</div>
<div class="right equal-height-col">
<p>This is the second column.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae optio, tenetur ipsa. Veritatis, consequatur? Optio totam, hic voluptate eos magnam aspernatur. Accusamus similique aperiam, soluta, omnis corporis reprehenderit illum quasi.</p>
</div>
</div>
...both columns appear to be the same height. The problem I am running into is when an internal anchor link is visited, everything above it is being cut off of the page.
Here is a jsfiddle: https://jsfiddle.net/7ctkL4yx/
Notice just scrolling the HTML, it looks great. Now, try clicking a link and notice all the content above it gets chopped off.
How can I correct this?
.left {
float: left;
width: 50%;
background: red;
}
.right {
float: left;
width: 50%;
background: blue;
}
.equal-height-row {
overflow: hidden;
}
.equal-height-col {
padding-bottom: 99999px;
margin-bottom: -99999px;
}
<p>
First<br />
Second
</p>
<div class="equal-height-row">
<div class="left equal-height-col">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae optio, tenetur ipsa. Veritatis, consequatur? Optio totam, hic voluptate eos magnam aspernatur. Accusamus similique aperiam, soluta, omnis corporis reprehenderit illum quasi.
</p>
</div>
<div class="right equal-height-col">
<p id="first">FIRST ANCHOR<br />
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae optio, tenetur ipsa. Veritatis, consequatur? Optio totam, hic voluptate eos magnam aspernatur. Accusamus similique aperiam, soluta, omnis corporis reprehenderit illum quasi.
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae optio, tenetur ipsa. Veritatis, consequatur? Optio totam, hic voluptate eos magnam aspernatur. Accusamus similique aperiam, soluta, omnis corporis reprehenderit illum quasi.
</p>
<p id="second">SECOND ANCHOR<br />
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae optio, tenetur ipsa. Veritatis, consequatur? Optio totam, hic voluptate eos magnam aspernatur. Accusamus similique aperiam, soluta, omnis corporis reprehenderit illum quasi.
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae optio, tenetur ipsa. Veritatis, consequatur? Optio totam, hic voluptate eos magnam aspernatur. Accusamus similique aperiam, soluta, omnis corporis reprehenderit illum quasi.
</p>
</div>
</div><!--/.equal-height-row-->
overflow: hidden cuts off anything beyond the certain part of the element, and when you jump to a link, you jump to that line of the text. There's no natively CSS solution for the default link behavior to change, alas. Instead, you want overflow: scroll instead on your .equal-height-row class to preserve the height without any content being cut off.
Or you could just use flexbox, and give the .equal-height-row a display property of flex to establish it as your flex container. With flexbox, you won't need the padding-bottom: 99999px;margin-bottom: -99999px; hack for establishing equal column height, so your CSS would be:
.left {
background: red;
}
.right {
background: blue;
}
.equal-height-row {
display: flex;
}

Resources