This question already has answers here:
CSS-only masonry layout
(4 answers)
Closed 1 year ago.
My posts are currently arranged like this:
.
HTML looks like this:
<div class="center">
<ul>
<li>
<p>[User] posted: [content] at [date] (likes: [likes]) </p>
<button>like</button>
<button>dislike</button>
</li>
</ul>
</div>
Here's the css:
.center {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
min-height: 100vh;
}
li {
width: 20%;
display: inline-block;
list-style-type: none;
align-items: center;
align-content: center;
line-break: strict;
margin: 10px;
border-radius: 7px;
}
li p {
margin: 20px;
overflow-wrap: break-word;
}
Inspired by tumblr's design, how do I make it look more like this?
Considering how you re-ordered elements between your current result and the desired layout, CSS columns seem to be exactly what you're after.
For that to work, you column-containing element (which I assume is your ul) needs a limited height. Given you have applied a (min-)height, use this:
ul { columns: 4; column-gap: 20px; }
The kind of layout you're after is commonly called masonry layout. CSS already has a specification achieving this (see https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout/Masonry_Layout) but so far Firefox is the only browser that has support for it behind a flag. If you want to enable this experimental support, go to about:config in Firefox, and set layout.css.grid-template-masonry-value.enabled to true.
If you want to try that out, use
ul {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: masonry;
gap: 20px;
}
Also compare https://caniuse.com/mdn-css_properties_masonry and https://caniuse.com/mdn-css_properties_grid-template-rows_masonry.
Related
https://codepen.io/fluark/pen/VwxGawr
.header {
display: flex;
font-family: monospace;
background: papayawhip;
align-items: center;
justify-content: space-between;
margin-left: auto;
}
ul {
display: flex;
background: papayawhip;
gap: 10em;
list-style-type: none;
flex-direction: row;
margin: 0;
padding: 0;
}
Desired Outcome
Visually my header looks fairly close to the desired outcome, however when I shrink down the page, the right links/ul (child items) spill out of the header (parent).
I am pretty sure this is a matter of not having the proper flex settings. Is the error maybe in the flex-basis? Or potentially the relationship between flex-shrink and flex-basis?
I have looked up flex settings and tried separately adding “flex: 1;” on both the parent .header as well as the div.right-links and ul.
I have also tried creating a separate div... div.header and then adding flex: 1 to that with the intention of making it so the parent is able to grow when the window is resized. That didn't seem to do anything.
I am a little confused because with “display: flex” on both the .header element and the ul, that means the flex-shrink is 1 (flex = 0, 1, auto), so shouldn’t the links be shrinking when the parent element is resized, not spilling out?
I’m looking for some guidance/talk throughs because I am at the point where I am just adding to the code to “see what happens”, and that’s when I know I need help.
Thanks in advance!
The ul element has a default padding of 26px and when you narrow the viewport it's that padding that's pushing the div to the right. If you set padding-left: to 0 then it removes it. I've also set the li display type to inline-block so that the padding on the a element does not overflow the logo on small screen sizes. At really small screen sizes (below 612px or so), the logo, a tags, gaps and padding will all be the lowest they can be so if you want to restyle it any further then I'd use a media query. See below
.header {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
font-family: monospace;
background: papayawhip;
padding: 5px;
}
.logo {
font-size: 48px;
font-weight: 900;
color: tomato;
background: white;
padding: 4px 32px;
}
ul {
padding-left: 0;
display: flex;
gap: 1em;
flex-direction: row;
/* this removes the dots on the list items*/
list-style-type: none;
}
li {
display: inline-block;
}
a {
font-size: 22px;
background: white;
/* this removes the line under the links */
padding: 8px;
text-decoration: none;
}
<div class="header">
<div class="left-links">
<ul>
<li>ONE</li>
<li>TWO</li>
<li>THREE</li>
</ul>
</div>
<div class="logo">LOGO</div>
<div class="right-links">
<ul>
<li>FOUR</li>
<li>FIVE</li>
<li>SIX</li>
</ul>
</div>
</div>
This question already has answers here:
Centering in CSS Grid
(9 answers)
Closed 1 year ago.
How can I center a grid column? This is my current code
.example-grid {
display: grid;
justify-content: center;
align-items: center;
grid-template-columns: repeat(5, 1fr);
}
However, all the grid items are aligned to the left.
section {
width: 400px;
height: 400px;
border:1px solid #ccc;
display: grid;
justify-content: center;
align-content: center;
gap: 4px;
grid-auto-flow: column;
}
div{
background:#777;
color:#fff;
padding: 30px;
}
<section>
<div>1</div>
<div>2</div>
<div>3</div>
</section>
Instead of align-items: center;, you have to use align-content: center;.
Everything else looks fine :)
align-items property is a Flexbox property not a CSS-Grid property
Your display property must be inline-grid instead of grid.
I tried to move the grid at center of the page, I dont know why it doesn't work
I looked a lot of example, I see people using this:
justify-content: center;
Even if I tried it and it doesn't work.
Here's my code
ul {
list-style-type: none;
background-color: silver;
padding: 10px;
text-align: center;
margin: none;
}
p {
text-align: center;
padding: none;
}
li {
display: inline;
padding: 10px;
}
a {
color: yellow;
}
.testing {
padding: 150px;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-gap: 10px;
/* next comment was fixed to meet syntax in a plain style sheet */
justify-content: center;
/* its not moving to the center of the page*/
}
.testing div {
width: 225px;
height: 225px;
background-color: blue;
}
<ul>
<li><a href=" ">Home</a ></li>
<li><a href="#news">News</a ></li>
<li><a href="#contact">Contact</a ></li>
</ul>
<p>hellow world</p >
<div class = "testing">
<div></div>
Just change padding: 150px; to padding: 50%;, on .testing ('testing' class).
This should work.
Best regards,
Brhaka
The grid is centered, you just gave it 3 equal columns but only one child element, so the div is showing in the left-most column of the grid. If you change the CSS to grid-template-columns: 1fr or define a template area in the center it will display as you expected, but having only one column begs the question of why you are using a grid layout to begin with. What kind of layout are you trying to build? Maybe a flexbox solution would better fit your needs?
This is a nicely centered grid, but I want the 4 columns to go down to 2 for mobile. What should I add?
body {
display: grid;
align-items: center;
justify-items: center;
background: grey;
max-width: 56em;
}
ul {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 3em;
margin: 0 auto;
max-width: 64em;
}
li {
display: grid;
align-items: center;
justify-content: center;
list-style: none;
}
You have to add changes of css when the screen is narrow. Using:
#media screen and (max-width: ???px){}
And in the block you add rules like in normal css which should change when width of screen is less than given value. I suppose it will be enough for you to deal with this. If you need more information (e.g. new values) write comments and I will try to answer them.
Examples
This question already has answers here:
flex container min-height ignored in IE
(3 answers)
Closed 5 years ago.
I am trying to vertical align some text inside a flexbox container that has a min height and it works well except for in ie11, where the content is not centred.
.banner {
background:#ccc;
padding: 10px 20px;
display: flex;
align-items: center;
justify-content: center;
min-height: 60px;
flex-direction:column;
}
<div id="section-title-1" class="banner banner--theme-1 js-section-title" data-title="Section Title">
<span>IE is bobbins</span>
</div>
I have had a look around and people who have had similar problems are suggesting that adding a height will fix the issue but obviously this negates the min-height so it's not very useful.
Does anyone know a way to get the vertical centring to work in ie with a min-height?
If you are open to detecting and applying different CSS to ie11, you could do something like the following:
fiddle
.banner {
background: #ccc;
padding: 10px 20px;
display: flex;
align-items: center;
justify-content: center;
min-height: 60px;
flex-direction: column;
}
#media screen and (-ms-high-contrast: active),
(-ms-high-contrast: none) {
.banner span {
display: table-cell;
min-height: 60px;
vertical-align: middle;
}
}
<div id="section-title-1" class="banner banner--theme-1 js-section-title" data-title="Section Title">
<span>IE is bobbins</span>
</div>
This fix by Chris Wright might be usefull:
https://codepen.io/chriswrightdesign/pen/emQNGZ/
It's based on the idea to wrap flex-direction:column inside a flex-direction:row parent:
.l-flex-parent {
display:flex;
flex-direction:row;
}