This question already has answers here:
Targeting flex items on the last or specific row
(10 answers)
Closed 3 years ago.
I am trying to make products catalog with React and CSS. Everything seems to be okay except last row of products.
The last row has only 1 element and since the flex-grow: 1 is set, it takes up all the available width.
Is it possible to set the same width for all elements in the row?
ul {
display: flex;
flex-wrap: wrap;
list-style: none;
}
.products__product {
flex: 1 0 20%;
margin: 1.5em 0.75em 0 0.75em;
min-height: 250px;
background:grey;
}
<div class="products">
<div class="container">
<div class="products__content">
<ul>
<a class="products__product" href="/products/21/stone-in-the-night"
><li><h1>Stone in the Night</h1></li></a
><a class="products__product" href="/products/21/the-dying-of-the-spirits"
><li><h1>The Dying of the Spirits</h1></li></a
><a class="products__product" href="/products/21/the-beginnings-guardian"
><li><h1>The Beginning’s Guardian</h1></li></a
><a class="products__product" href="/products/21/death-of-light"
><li><h1>Death of Light</h1></li></a
><a class="products__product" href="/products/21/the-lost-soul"
><li><h1>The Lost Soul</h1></li></a
><a class="products__product" href="/products/21/first-husband"
><li><h1>First Husband</h1></li></a
><a class="products__product" href="/products/21/verzliaraktis"
><li><h1>Veržliaraktis</h1></li></a
><a class="products__product" href="/products/21/raktas"
><li><h1>Raktas</h1></li></a
>
</ul>
</div>
</div>
</div>
You can always set the initial width to a percentage:
.products__product {
...
flex: 0 0 20%
...
}
The above says, do not shrink nor grow the items, but set it to 20% of the width of the container. So even if you have a single element in the container, it will only occupy 20%. You can adjust based on the number of elements you have. Unfortunately, you can't use fractional notation.
You could just set the width of the products__product class if you want to keep them uniform.
ul {
display: flex;
flex-wrap: wrap;
list-style: none;
.products__product {
width: 30%; // 3 per row
margin: 1.5em 0.75em 0 0.75em;
min-height: 250px;
}
}
How about this?
ul {
display: flex;
flex-wrap: wrap;
list-style: none;
}
.products__product {
flex: 0 0 20%;
min-width: 20%;
margin: 1.5em 0.75em 0 0.75em;
min-height: 250px;
background: red;
}
<div class="products">
<div class="container">
<div class="products__content">
<ul>
<a class="products__product" href="/products/21/stone-in-the-night">
<li>
<h1>Stone in the Night</h1>
</li>
</a>
<a class="products__product" href="/products/21/the-dying-of-the-spirits">
<li>
<h1>The Dying of the Spirits</h1>
</li>
</a>
<a class="products__product" href="/products/21/the-beginnings-guardian">
<li>
<h1>The Beginning’s Guardian</h1>
</li>
</a>
<a class="products__product" href="/products/21/death-of-light">
<li>
<h1>Death of Light</h1>
</li>
</a>
<a class="products__product" href="/products/21/the-lost-soul">
<li>
<h1>The Lost Soul</h1>
</li>
</a>
<a class="products__product" href="/products/21/first-husband">
<li>
<h1>First Husband</h1>
</li>
</a>
<a class="products__product" href="/products/21/verzliaraktis">
<li>
<h1>Veržliaraktis</h1>
</li>
</a>
<a class="products__product" href="/products/21/raktas">
<li>
<h1>Raktas</h1>
</li>
</a>
</ul>
</div>
</div>
</div>
Related
I'm not sure if these are empty cells, most likely they are stretched because of the largest element, please tell me how I can remove these indentations without javascript. I tried to use grid-auto-flow: colunm, but it doesn't works.
picture with problem
what i'm trying to do
<nav class="header__submenu" id="header__submenu-resources" data-opened="1">
<ul class="header__submenu-list" id="header__submenu-list-resources">
<li class="header__submenu-item">
<a href="#" class="header__submenu-link">
Webinars
<p class="header__submenu-description">Get to know Tines and our use cases, live and on-demand.</p>
</a>
</li>
<li class="header__submenu-item">
<a href="#" class="header__submenu-link">
Blog
<p class="header__submenu-description">Read articles by team members, from company updates to tutorials.</p>
</a>
</li>
<li class="header__submenu-item">
<a href="#" class="header__submenu-link">
Prodcast
<p class="header__submenu-description">Listen to the latest episodes of our podcast, 'The Future of Security Operations.'</p>
</a>
</li>
<li class="header__submenu-item">
<a href="#" class="header__submenu-link">
Customers stories
<p class="header__submenu-description">Learn how the world’s best security teams automate their work.</p>
</a>
</li>
<li class="header__submenu-item">
<a href="#" class="header__submenu-link">
Story library
<p class="header__submenu-description">Discover helpful example Stories, connect them to your own tools and start customizing instantly.</p>
</a>
</li>
<li class="header__submenu-item">
<a href="#" class="header__submenu-link">
Docs
<p class="header__submenu-description">Get to know the features and concepts of Tines, in detail.</p>
</a>
</li>
<li class="header__submenu-item" id="header__submenu-item-block">
<a href="#" class="header__submenu-link">
<div class="submenu-block">
<div class="submenu-block__top">
<img src="images/tines-icon-galaxy-gate-150w.png" alt="" class="submenu-block__top-image" />
</div>
<div class="submenu-block__text">
<div class="submenu-block__top-text">
<h3 class="submenu-block__title">Tines</h3>
<span class="submenu-block__mark">Hub</span>
</div>
<p class="submenu-block__description">Learn how to automate your workflows, troubleshoot any issues, or get help from our support team.</p>
</div>
</div>
</a>
</li>
</ul>
</nav>
CSS:
.header__submenu-list {
margin: 0;
padding: 40px 0;
list-style: none;
display: grid;
grid-template-columns: repeat(4, 1fr);
row-gap: 30px;
background-color: var(--color-light);
width: auto;
border-radius: 0 0 28px 28px;
}
#header__submenu-list-resources {
grid-template-columns: repeat(3, 1fr);
}
.header__submenu-item {
padding: 0 20px 0 20px;
align-self: flex-start;
}
#header__submenu-item-block {
grid-column: 4 / 5;
grid-row: 1 / 2;
align-items: flex-start;
/* grid-area: block; */
}
The whole code won't fit here, please see the codepen
is this what you want ?
I updated the following code to make it work
#header__submenu-item-block {
grid-column: 4;
grid-row: 1 / 3;
}
check below the working sample.
https://codepen.io/shahilparichay/pen/LYdpJGX
learn about Grid from Here
I'm pretty new to css and my navbar looks really bad. I'm trying to play with the css but i get nothing better than the below picture. If i remove the .menu, icons get verticaly aligned;
Of course i've seen a lot of stackoverflow post on this topic and tryied lot of stuff, but again, could not get better than the below picture.
Here my react html code.
<ul className="menu">
<li>
<a href="https://discord.com/channels/#me" id="menu-item-41">
<img src={Discord} alt="discord" className="discord-logo"/>
</a>
</li>
<li>
<a href="www.twitter.com" className="page-scroll" id="menu-item-42">
<img src={Twitter} alt="twitter" className="twitter-logo"/>
</a>
</li>
<li>
<a href="#" className="page-scroll" id="menu-item-44">
TAG
</a>
</li>
<li>
<a href="#roadmap-title" className="page-scroll" id="menu-item-43">
ROAD MAP
</a>
</li>
<li>
<a href="#team" className="page-scroll" id="menu-item-45">
TAG2
</a>
</li>
<li>
<button onClick={() => this.connect() }
id="menu-item-47"
className="connect-button">CONNECT
</button>
</li>
</ul>
Here my css :
li.menu-item {
margin-bottom: 40px;
vertical-align: middle;
text-align: center;
}
.menu{
position: left;
display: inline-flex;
text-align: center;
vertical-align: middle;
float: left;
margin: 40;
}
Eny suggestions or observation would be super welcomed !
Try changing your css to this
.menu {
display: flex;
justify-content: space-between;
align-items: center;
}
I have a menu, with one of the menu items being a logo. I would like to center the menu so the logo item stays in the center of the page. The goal is to have a menu, split by a logo, that stays centered on the logo. The location of the logo is calculated by number of menu items / 2 + 1 and round down if needed. Example: with 6 menu items, logo is in spot 4; with 5 menu items, logo is in spot 3.
Complications: 1) menu items do not have a set width - this is meant to be responsive, and I am avoiding set pixel widths at every juncture, 2) I do not want to use JavaScript
EDIT: HTML
<ul class="nav-menu">
<li class="menu-item">
<a href="/link-1">
<span>Link 1</span>
</a>
</li>
<li class="menu-item-logo">
<div id="logo">
<a href="http://example.com/">
<img src="https://pbs.twimg.com/profile_images/581134293931548673/8R33fIjl.png"/>
</a>
</div>
</li>
<li class="menu-item">
<a href="/link-2">
<span>Link 2</span>
</a>
</li>
<li class="menu-item">
<a href="/link-3">
<span>Link 3</span>
</a>
</li>
</ul>
CSS
<style>
.nav-menu {
display: flex;
justify-content: space-between;
width: 85%;
}
.nav-menu .menu-item {
display: inline-block;
margin: .25rem 2rem;
text-align: center;
}
#logo {
max-width: 15vw;
}
#logo img {
width: 100%;
}
</style>
jsfiddle # https://jsfiddle.net/szamxv2u/
I am trying to indent the text from an inline block on safari. It only indents in chrome.
I have tried using margin left. This works in chrome but in safari i have to adjust the margin-left to another number for them to look the same.
I tried the text-indent and the inline-block is not wrapping properly now.
html
<nav>
<ul>
<li><a href="#" >Home</a></li>
<li type="checkbox" class="sub">
<input type="checkbox" />
<a href="#" >Profile</a>
<!-- Begin-->
<ul class="submenu">
<li >
<a target = "box">Profiles</a>
</li>
</ul>
</li>
<!-- end-->
<li class="sub">
<input type="checkbox" />
<a href="#" >Logs</a>
<!-- Begin-->
<ul class="submenu">
<li >
<a target = "box" >View Logs</a>
</li>
<li >
<a target = "box" >Testing a long test</a>
</li>
</ul>
</li>
</li>
</ul>
</nav>
CSS in chrome
nav .submenu a {
*zoom: 1;
*display: inline;
display: inline-block;
max-width: 122px;
margin-left: 55px;
padding: 3px 1px 3px 0px;
}
CSS in safari
nav .submenu a {
*zoom: 1;
*display: inline;
display: inline-block;
max-width: 122px;
margin-left: 65px;
padding: 3px 1px 3px 0px;
}
I tried putting important on the variables to see anything overrides it too and that didn't help them look the same.
I also tried
text-indent: 50px;
Make a table with no content and put it ahead of your text.
<table>
<tbody>
<tr>
<td width="65px"> <!--this is the indent, don't forget to style it so it becomes invisible.--></td>
</tr>
</tbody>
</table>
I have
<ul id="list">
<li>
<a href=''>1</a>
</li>
<li>
<a href=''>2</a>
</li>
<li>
<a href=''>3</a>
</li>
<li>
<a href=''>4</a>
</li>
...
<li>
<a href=''>10</a>
</li>
<li>
<a href=''>11</a>
</li>
</ul>
And with style looks like:
I wish to have double digit numers centred like single digits are. I've tried (separately):
#list li{
text-align: center;
align-items: stretch; // and center
margin-left: auto; //with margin right the same
left: 0;
}
But nothing seem's to do the trick
Set text-align to a:
#list li a{
text-align: center;
}