Make all grid cells have the width of the widest cell - css

My first question here and your help would be much appreciated
I have a ul width display: grid inside a div with overflow: hidden.
The ul's purpose is to function as a slidable element, so its width is not explicitly specified, but rather, defined by its contents. Each li element inside the list contains text of variable length, which should not be truncated nor break on a 2nd line.
The li count inside the list may vary, from 4 to up to 12 elements.
My question is how can I create a grid that all its cells have the width of the widest li? (which, in turn, is defined by the longest text)
Please keep in mind that the grid is always going to have 2 rows
Here is a simplified version of what I am trying to accomplish:
.container {
overflow: hidden;
background: #fafafa;
width: 760px;
margin: 0 auto;
padding: 24px;
}
ul {
overflow: auto;
list-style: none;
padding: 0;
display: grid;
grid-gap: 8px;
grid-auto-flow: column;
grid-auto-columns: 1fr;
grid-template-rows: repeat(2, 1fr);
}
li {
border: 1px solid #eee;
border-radius: 20px;
padding-inline: 12px;
display: flex;
gap: 4px;
align-items: center;
}
a {
white-space: nowrap;
}
<div class='container'>
<ul>
<li>
<img src='https://unsplash.it/50/50'>
<a>text 1</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 2</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 3</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Fourth text</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Test 5 is is a huge text and all other cells should follow its size</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 6</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>This is also a large text</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 8</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 9</a>
</li>
</ul>
</div>

.container {
overflow: hidden;
background: #fafafa;
width: 760px;
margin: 0 auto;
padding: 24px;
}
ul {
overflow: auto;
list-style: none;
padding: 0;
display: grid;
grid-gap: 8px;
grid-auto-flow: column;
grid-templates-column: auto auto auto auto;
grid-template-rows: auto auto;
}
li {
border: 1px solid #eee;
border-radius: 20px;
padding-inline: 12px;
display: flex;
gap: 4px;
align-items: center;
}
a {
white-space: nowrap;
}
<div class='container'>
<ul>
<li>
<img src='https://unsplash.it/50/50'>
<a>text 1</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 2</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 3</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Fourth text</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Test 5 is is a huge text and all other cells should follow its size</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 6</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>This is also a large text</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 8</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 9</a>
</li>
</ul>
</div>
.container {
overflow: hidden;
background: #fafafa;
width: 760px;
margin: 0 auto;
padding: 24px;
}
ul {
overflow: auto;
list-style: none;
padding: 0;
display: grid;
grid-gap: 8px;
grid-auto-flow: column;
grid-auto-columns: 1fr;
grid-template-rows: repeat(2, 1fr);
}
li {
border: 1px solid #eee;
border-radius: 20px;
padding-inline: 12px;
display: flex;
gap: 4px;
align-items: center;
}
a {
white-space: nowrap;
}
<div class='container'>
<ul>
<li>
<img src='https://unsplash.it/50/50'>
<a>text 1</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 2</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 3</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Fourth text</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Test 5 is is a huge text and all other cells should follow its size</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 6</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>This is also a large text</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 8</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 9</a>
</li>
</ul>
</div>

Related

adding columns using flex

I'm trying to learn more about flex.
I want to add more columns to a flex layout but I can only get two columns.
When I try to add a third column, the div's just stack.
Here's the two col version and my attempt to get a third column...
http://brinsterinc.com/footer.html
http://brinsterinc.com/footer1.html
Here's the CSS...
footer[is=footer] {
background: hsl(var(--tertiary));
display: grid;
gap: 3rem;
grid:
"menu"
"menu1"
"menu2"
"policies"
"copyright";
padding: clamp(4rem,8vw,8rem) max((100% - 80rem) / 2,4vw);
}
footer[is=footer]>div>ul {
all: unset;
display: flex;
flex-flow: column wrap;
gap: 1rem;
}
footer[is=footer]>div:nth-of-type(1) {
display: flex;
flex-direction: column;
gap: 2rem 4rem;
grid-area: menu;
}
footer[is=footer]>div:nth-of-type(2) {
display: flex;
flex-direction: column;
gap: 2rem 4rem;
grid-area: menu1;
}
footer[is=footer]>div:nth-of-type(3) {
display: flex;
flex-direction: column;
gap: 2rem 4rem;
grid-area: menu2;
}
footer[is=footer]>div:nth-of-type(4) {
grid-area: policies;
}
footer[is=footer]>p {
all: unset;
color: hsl(var(--on-tertiary));
grid-area: copyright;
}
#media (min-width: 50em) {
footer[is=footer] {
grid: "menu menu1 menu2" "copyright policies" / -webkit-max-content 1fr;
grid:
"menu menu1 menu2"
"copyright policies" / max-content 1fr;
place-items: start;
}
footer[is=footer]>div:nth-of-type(4)>ul {
-moz-column-gap: clamp(1rem,4vw,2rem);
column-gap: clamp(1rem,4vw,2rem);
flex-direction: row;
}
}
Put a flex wrapper round the items you want to put in a column. I think the grid markup is causing some problems though, you're allocating them to different grid areas and without setting grid-template-rows or grid-auto-rows, the child divs will stack on top of each other.
footer[is=footer] {
background: hsl(var(--tertiary));
/* display: grid;
gap: 3rem;
grid:
"menu"
"menu1"
"menu2"
"policies"
"copyright";
padding: clamp(4rem, 8vw, 8rem) max((100% - 80rem) / 2, 4vw); */
}
.flexContainer {
display: flex;
}
.flexContainer div {
flex: 1 0 auto;
}
footer[is=footer]>div>ul {
/* all: unset;
display: flex;
flex-flow: column wrap;
gap: 1rem; */
}
footer[is=footer]>div:nth-of-type(1) {
/* display: flex;
flex-direction: column;
gap: 2rem 4rem;
grid-area: menu; */
}
footer[is=footer]>div:nth-of-type(2) {
/* display: flex;
flex-direction: column; */
/* gap: 2rem 4rem;
grid-area: menu1; */
}
footer[is=footer]>div:nth-of-type(3) {
/* display: flex;
flex-direction: column; */
/* gap: 2rem 4rem;
grid-area: menu2; */
}
footer[is=footer]>div:nth-of-type(4) {
/* grid-area: policies; */
}
footer[is=footer]>p {
/* all: unset;
color: hsl(var(--on-tertiary));
grid-area: copyright; */
}
/* #media (min-width: 50em) {
footer[is=footer] {
grid: "menu menu1 menu2""copyright policies" / -webkit-max-content 1fr;
grid:
"menu menu1 menu2"
"copyright policies" / max-content 1fr;
place-items: start;
}
footer[is=footer]>div:nth-of-type(4)>ul {
-moz-column-gap: clamp(1rem, 4vw, 2rem);
column-gap: clamp(1rem, 4vw, 2rem);
flex-direction: row;
}
} */
<footer is="footer">
<div class="flexContainer">
<div>
<ul>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
</ul>
</div>
<div class="oakhide">
<ul>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
</ul>
</div>
<div>
<ul>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
</ul>
</div>
</div>
<div>
<ul>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
</ul>
</div>
<p><small>Copyright © 2022</small></p>
</footer>

gap borders' right and left

When I applied padding to 'a' elements, there will be gaps top-borders' left and right.I want the top border covers entire line until navbar's right border. How can I solve this problem.
* {
margin: 0;
padding: 0;
font-family: 'Lato', sans-serif;
}
.wrapper {
display: grid;
grid-template-columns: 1fr 6fr;
}
.navbar {
border-right: gray 3px solid;
padding: 5%;
position: sticky;
top: 0;
height: 100vh;
box-sizing: border-box;
min-width: 300px;
}
#navbar-header {
text-align: center;
margin-bottom: 20px;
}
#navbar-content a {
text-decoration: none;
color: black;
}
#navbar-content a li {
list-style: none;
padding: 5%;
padding-left: 10%;
border-top: gray 1px solid;
}
<div class="wrapper">
<div class="navbar">
<h2 id="navbar-header">JS Documentation</h2>
<ul id="navbar-content">
<a href="#intro">
<li>Introduction</li>
</a>
<a href="#know">
<li>What you should already know</li>
</a>
<a href="#js-java">
<li>JavaScript and Java</li>
</a>
<a href="#hello">
<li>Hello World</li>
</a>
<a href="#var">
<li>Variables</li>
</a>
<a href="#dec-var">
<li>Declaring Variables</li>
</a>
<a href="#var-sco">
<li>Variable Scope</li>
</a>
<a href="#glo-var">
<li>Global Variables</li>
</a>
<a href="#cons">
<li>Constants</li>
</a>
<a href="#data">
<li>Data Types</li>
</a>
<a href="#if-else">
<li>if...else statement</li>
</a>
<a href="#while">
<li>while statement</li>
</a>
<a href="#func">
<li>Function declarations</li>
</a>
<a href="#ref">
<li>Reference</li>
</a>
</ul>
</div>
<div class="contents">
</div>
</div>
When I applied padding to 'a' elements, there will be gaps top-borders' left and right.I want the top border covers entire line until navbar's right border. How can I solve this problem.
Remove the padding-left/right from the navbar and increase the one of the li:
* {
margin: 0;
padding: 0;
font-family: 'Lato', sans-serif;
}
.wrapper {
display: grid;
grid-template-columns: 1fr 6fr;
}
.navbar {
border-right: gray 3px solid;
padding: 5% 0; /*updated*/
position: sticky;
top: 0;
height: 100vh;
box-sizing: border-box;
min-width: 300px;
}
#navbar-header {
text-align: center;
margin-bottom: 20px;
}
#navbar-content a {
text-decoration: none;
color: black;
}
#navbar-content a li {
list-style: none;
padding: 5% 10%; /*updated*/
border-top: gray 1px solid;
}
<div class="wrapper">
<div class="navbar">
<h2 id="navbar-header">JS Documentation</h2>
<ul id="navbar-content">
<a href="#intro">
<li>Introduction</li>
</a>
<a href="#know">
<li>What you should already know</li>
</a>
<a href="#js-java">
<li>JavaScript and Java</li>
</a>
<a href="#hello">
<li>Hello World</li>
</a>
<a href="#var">
<li>Variables</li>
</a>
<a href="#dec-var">
<li>Declaring Variables</li>
</a>
<a href="#var-sco">
<li>Variable Scope</li>
</a>
<a href="#glo-var">
<li>Global Variables</li>
</a>
<a href="#cons">
<li>Constants</li>
</a>
<a href="#data">
<li>Data Types</li>
</a>
<a href="#if-else">
<li>if...else statement</li>
</a>
<a href="#while">
<li>while statement</li>
</a>
<a href="#func">
<li>Function declarations</li>
</a>
<a href="#ref">
<li>Reference</li>
</a>
</ul>
</div>
<div class="contents">
</div>
</div>

How can I convert Css to React-native

I want to do the exact same thing of what this CSS styling suggests in react native
So how can I convert this CSS to React -native
.grid {
display: flex;
flex-direction: row;
list-style: none;
padding-left: 0;
flex-wrap: wrap;
margin-top: 0;
margin-bottom: 20px;
width: 400px;
}
.grid-item {
flex-grow: 0;
border-right: 1px solid #000;
border-bottom: 1px solid #000;
height: 100px;
background: rgba(0,0,0,0.1);
width: 33%;
}
.grid-item:nth-last-child(-n+3) {
border-bottom: none;
}
.grid-item:nth-child(3n+0) {
border-right: none;
}
.list {
display: flex;
flex-direction: column;
list-style: none;
padding-left: 0;
width: 400px;
}
.list-item {
flex-grow: 0;
border-bottom: 1px solid #000;
height: 60px;
background: rgba(0,0,0,0.1);
flex: 1;
}
.list-item:last-child {
border-bottom: none;
}
<ul class="grid">
<li class="grid-item">
</li>
<li class="grid-item">
</li>
<li class="grid-item">
</li>
<li class="grid-item">
</li>
<li class="grid-item">
</li>
<li class="grid-item">
</li>
<li class="grid-item">
</li>
<li class="grid-item">
</li>
<li class="grid-item">
</li>
</ul>
<ul class="list">
<li class="list-item">
</li>
<li class="list-item">
</li>
<li class="list-item">
</li>
<li class="list-item">
</li>
</ul>
I want to make a Modal which looks like this:
<Modal>
<BrandsGrid />
</Modal>
<Modal>
<ModelsList />
</Modal>
I need to define BrandsGrid and ModelsList and do something exactly similar to that CSS styling in React-native
So example codes will be more helpful for understanding I have referred the docs but couldn't get much help from that
You can use different modules for converting css components to react native. For example:
https://www.npmjs.com/package/css-to-react-native-transform
Another great example:
https://github.com/kristerkari/react-native-css-transformer

Flexbox css unordered list custom styling

I want to style my unordered list like this one below
Link to what i want to achive
My structure looks like:
<div class="tabs">
<ul class="tabs__labels">
<li class="0">
<a class="active" href="#">
<img/>
<div>something</div>
</a>
</li>
<li class="1">
<a class="" href="#">
<img/>
<div>something1</div>
</a>
</li>
...
</ul>
</div>
Is it a good idea to name the list classes like 0,1 etc. and style them separately to somehow achive this effect? How to use flexbox to get similar effect? Should i group them into rows?
Would this be a start?
.tabs__labels {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
list-style: none;
margin: 0;
padding: 0;
}
.tabs__labels li {
flex-basis: 45%;
margin: 20px 0;
padding: 0;
}
.tabs__labels li:nth-child(odd) {
display: flex;
justify-content: flex-end;
}
.tabs__labels li:nth-child(even) {
display: flex;
}
.tabs__labels li:nth-child(3),
.tabs__labels li:nth-child(4) {
flex-basis: 35%;
}
.tabs__labels li a {
display: flex;
justify-content: center;
align-items: center;
height: 40px;
width: 40px;
border-radius: 50%;
border: 1px solid red;
text-decoration: none;
}
.tabs__labels li:nth-child(1)::before {
content: 'some text';
text-decoration: underline;
margin-right: 10px;
}
.tabs__labels li:nth-child(2)::after {
content: 'some text';
text-decoration: underline;
margin-left: 10px;
}
<div class="tabs">
<ul class="tabs__labels">
<li>
<a class="active" href="#">S
</a>
</li>
<li>
<a class="" href="#">S
</a>
</li>
<li>
<a class="" href="#">S
</a>
</li>
<li>
<a class="" href="#">S
</a>
</li>
<li>
<a class="" href="#">S
</a>
</li>
<li>
<a class="" href="#">S
</a>
</li>
</ul>
</div>

Need a basic submenu for my CSS navigation

So disregarding styling and everything else, all I would like is for a sub menu to appear for some of my navigation selections.
Right now I have a basic list that I made on my own using an online tutorial. Afterwards though, after searching through the web, I still have not found a submenu navigation that is close to my code or understanding.
This is my html code:
<div class="navigation">
<li>
<h2>Services</h2>
</li>
<li>
<h2>Store</h2> </l
<li>Photoshop</li>
<li>Illustrator</li>
<li>Web Design</li>
</li>
</nav>
</div>
<div class="navigation blue">
<li>
<a href="https://www.facebook.com/pages/Argano-Computer-Services/268377233227887?fref=ts">
<h2>Facebook</h2>
</a></li></div><a href="https://www.facebook.com/pages/Argano-Computer-Services/268377233227887?fref=ts">
</a>
<div class="navigation orange">
<li>
<a href="http://www.bing.com/maps/default.aspx?v=2&pc=FACEBK&mid=8100&rtp=adr.~pos.35.195859_-106.672877_Argano+Computer+Services_10009+Clearwater+Court%2C+Albuquerque%2C+New+Mexico&cp=35.195859~-106.672877&lvl=16&sty=r&rtop=0~0~0~&mode=D&FORM=FBKPL1&mkt=en-US" target="_blank">
<h2>Locate</h2>
</a>
</li>
</div>
<div class="navigation green">
<li>
<h2>Feedback</h2>
</li>
</div>
<div class="navigation purple">
<li>
<h2>About</h2>
</li>
</div>
</div>
</div>
this is my css:
<div class="navigation">
<li>
<h2>Services</h2>
</li>
<li>
<h2>Store</h2> </l
<li>Photoshop</li>
<li>Illustrator</li>
<li>Web Design</li>
</li>
</nav>
</div>
<div class="navigation blue">
<li>
<a href="https://www.facebook.com/pages/Argano-Computer-Services/268377233227887?fref=ts">
<h2>Facebook</h2>
</a></li></div><a href="https://www.facebook.com/pages/Argano-Computer-Services/268377233227887?fref=ts">
</a>
<div class="navigation orange">
<li>
<a href="http://www.bing.com/maps/default.aspx?v=2&pc=FACEBK&mid=8100&rtp=adr.~pos.35.195859_-106.672877_Argano+Computer+Services_10009+Clearwater+Court%2C+Albuquerque%2C+New+Mexico&cp=35.195859~-106.672877&lvl=16&sty=r&rtop=0~0~0~&mode=D&FORM=FBKPL1&mkt=en-US" target="_blank">
<h2>Locate</h2>
</a>
</li>
</div>
<div class="navigation green">
<li>
<h2>Feedback</h2>
</li>
</div>
<div class="navigation purple">
<li>
<h2>About</h2>
</li>
</div>
</div>
</div>
any suggestions?
Simple submenu for you:
<ul id="submenu">
<li>Home</li>
<li>About</li>
<li>Team</li>
</ul>
<style>
*, *:before, *:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
zoom: 1;
}
#submenu {
width: 200px;
background: #eee;
padding: 10px;
}
#submenu li{
margin: 0;
margin-bottom: 10px;
position: relative;
text-align: center;
font-weight: bold;
color: #fff !important;
line-height: 1 !important;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
white-space: nowrap;
border: none;
cursor: pointer;
display: inline-block;
vertical-align: middle;
zoom: 1;
border-radius: 3px;
-webkit-font-smoothing: antialiased;
background-color: #77cc6d;
text-shadow: 0 1px 0 rgba(0,0,0,0.15);
font-size: 15px;
padding: 12px 12px 12px 12px;
box-shadow: 0 3px 0 0 #53994b;
width: 100%;
}
#submenu li:active, #submenu li:focus{
box-shadow: 0 1px 0 0 #53994b;
top: 2px;
}
#submenu li:last-child{
margin-bottom: 0px;
}
</style>
JSFiddle: http://jsfiddle.net/aLggP/1/

Resources