why is my grid content not wrapping text, or showing ellipsis? - css

I am having trouble getting long-string text to wrap &/or show ellipsis. It's kind of like a table, but really it's 2 columns with rows.
I have tried messing around with the .line-flex and the .long-text-maybe classes, but no success.
Is there some sort of conflict between the grid wrapper & flex contents?
.box {
width: 50%;
}
.gridwrapp {
display: grid;
grid-gap: 8px;
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.line-flex {
display: flex;
align-items: center;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
<div class="box">
<div class="gridwrapp">
<div class="line-flex">
<p>Reservation ID: </p>
<p class="long-text-maybe">982398</p>
</div>
<div class="line-flex">
<p>Item Name: </p>
2020 Ram Promaster 1500 HR 136 WB Custom
</div>
<div class="line-flex">
<p>Name: </p>
<p class="long-text-maybe">Kim Bob</p>
</div>
<div class="line-flex">
<p>Location: </p>
<p class="long-text-maybe">Really long name in a place far far away</p>
</div>
</div>
</div>

You have to call directly to the children containing the text elements for ellipsis to take effect. Check out the CSS changes I made.
.box {
width: 50%;
}
.gridwrapp {
display: grid;
grid-gap: 8px;
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.line-flex {
display: flex;
align-items: center;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.line-flex > p, a {
text-overflow: ellipsis;
overflow: hidden;
}
<div class="box">
<div class="gridwrapp">
<div class="line-flex">
<p>Reservation ID: </p>
<p class="long-text-maybe">982398</p>
</div>
<div class="line-flex">
<p>Item Name: </p>
2020 Ram Promaster 1500 HR 136 WB Custom
</div>
<div class="line-flex">
<p>Name: </p>
<p class="long-text-maybe">Kim Bob</p>
</div>
<div class="line-flex">
<p>Location: </p>
<p class="long-text-maybe">Really long name in a place far far away</p>
</div>
</div>
</div>
You will have to use flex-wrap: wrap; to get your text to wrap automatically. This includes getting rid of the white-space: nowrap; on your line-flex so that the text can wrap.
See here:
.box {
width: 50%;
}
.gridwrapp {
display: grid;
grid-gap: 8px;
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.line-flex {
display: flex;
align-items: center;
overflow: hidden;
flex-wrap: wrap;
}
<div class="box">
<div class="gridwrapp">
<div class="line-flex">
<p>Reservation ID: </p>
<p class="long-text-maybe">982398</p>
</div>
<div class="line-flex">
<p>Item Name: </p>
2020 Ram Promaster 1500 HR 136 WB Custom
</div>
<div class="line-flex">
<p>Name: </p>
<p class="long-text-maybe">Kim Bob</p>
</div>
<div class="line-flex">
<p>Location: </p>
<p class="long-text-maybe">Really long name in a place far far away</p>
</div>
</div>
</div>

Related

Alternating rows using css grids and nth-child

I am working on a product grid with an alternating row pattern like this.
The color containers represent what I think the rows should look like.
Basically there are two products on the first row, and one product on the second row, repeating infinitely. I've been trying to do it with css grids + nth child, but I can't seem to get it right. Here's what I have so far:
.container {
display: grid;
grid-template-columns: 1fr 2fr;
grid-column-gap: 0;
width: 70vw;
margin: 0 auto;
}
.item:nth-child(3n+3) {
grid-column: auto / span 2;
background-color: #e2a7de;
}
/*just for debugging*/
.container{grid-gap:5px;}
.item{background-color: #ffa900;padding: 10px;text-align:center;}
<div class="container">
<div class="item">
X
</div>
<div class="item">
X
</div>
<div class="item">
X
</div>
<div class="item">
X
</div>
<div class="item">
X
</div>
</div>
My brain can't wrap around combining grids and nth-child to create this layout. I'm also open to a better way of creating this 2-1-2 pattern if anyone has other suggestions. Thank you!
You can simplify your code like below:
.container {
display: grid;
grid-auto-columns: 1fr; /* all columns equal */
width: 70vw;
margin: 0 auto;
}
.item:nth-child(3n) {
grid-column: span 2; /* span and create two columns*/
background-color: #e2a7de;
}
/*just for debugging*/
.container{grid-gap:5px;}
.item{background-color: #ffa900;padding: 10px;text-align:center;}
<div class="container">
<div class="item">
X
</div>
<div class="item">
X
</div>
<div class="item">
X
</div>
<div class="item">
X
</div>
<div class="item">
X
</div>
</div>
Update the grid-template-columns to be 1fr 1fr
.container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-column-gap: 0;
width: 70vw;
margin: 0 auto;
}
.item:nth-child(3n+3) {
grid-column: auto / span 2;
background-color: #e2a7de;
}
/*just for debugging*/
.container{grid-gap: 5px;}.item {background-color: #ffa900;padding: 10px;text-align: center;}
<div class="container">
<div class="item">
X
</div>
<div class="item">
X
</div>
<div class="item">
X
</div>
<div class="item">
X
</div>
<div class="item">
X
</div>
</div>

CSS Flexbox align 1 item horizontally and 2 vertically

I have the following HTML:
<div class="icon-box">
<div class="icon">
<svg>...</svg>
</div>
<h3>Title here</h3>
<p>Lorem ipsum dolor...</p>
</div>
My CSS:
.icon-box {
display: flex;
}
The result:
How can i get the following using flexbox without changing the HTML:
I am trying to align the icon to left and the heading and paragraph to its right on the same column. Without changing/adding new HTML.
Try using flex-direction: column; on main parent and regular display: flex; (flex-direction: row;) on the child of parent.
.icon-box {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.icon {
display: flex;
gap: 10px;
}
<div class="icon-box">
<div class="icon">
<img src="https://dummyimage.com/50/000000/f2009d&text=SVG">
<h3>Title here</h3>
</div>
<p>Lorem ipsum dolor...</p>
</div>

Is there a way to have repeating columns that shrink to fit content in CSS Grid?

Is there a CSS Grid configuration that will repeat (wrap) cells but dynamically shrink columns based on the size of the content, instead of using uniformly sized columns?
For example, here is a configuration using the following rules:
display: grid;
grid-template-columns: repeat(auto-fit, 186px);
grid-auto-columns: min-content;
grid example 1
notice that though the last column has narrower content, it uses the same width as the other columns (186px)
I'd like the columns to shrink to the width of the content, while still wrapping as screen size collapses.
grid example 2
notice the last column is now narrower that the others.
It seems grid-template-columns: repeat(); takes two arguments. The first of which can be auto-fit or auto-fill, but the second must be a specific width. Is there any way to allow this to be automatically computed based on content width?
Here is the codepen I've been playing with to try to achieve this.
Here is the real world scenario as well:
real world example
notice the first column should be wider to fill the content, and the second column should shrink to the content.
I'm familiar with flexbox, but I'm not super familiar with grid yet. Any guidance here would be much appreciated.
You can create repeating columns that to fit content, but for fixed a amount columns. When you use auto-fit, auto-fill grid generates all cells the same width.
In your case in look like 3x3.
display: grid;
grid-template-columns: repeat(3, min-content);
*,
::after,
::before {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
display: grid;
place-items: center;
background-color: hsl(215, 100%, 98%);
position: relative;
overflow-x: hidden;
}
.container {
background-color: lightgray;
width: 760px;
display: grid;
grid-template-columns: repeat(3, min-content);
resize: horizontal;
overflow: hidden;
gap: 5px;
justify-items: center;
align-items: center;
}
.child {
background-color: gray;
display: flex;
flex-direction: column;
justify-content: center;
}
p {
text-align: center;
}
<div class="container">
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/80x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/120x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/160x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/80x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/120x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/160x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/80x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/120x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/160x75" alt="" />
</div>
</div>
It seems grid-template-columns: repeat(); takes two arguments. The first of which can be auto-fit or auto-fill, but the second must be a specific width. Is there any way to allow this to be automatically computed based on content width?
In repeat() function you can use another function minmax() as the second property. But this will not solve your problem, because the first parametr defines minimal width, example 186px and the second how large it will extend.
display: grid;
grid-template-columns: repeat(auto-fit, minmax(186px, 1fr));
*,
::after,
::before {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
display: grid;
place-items: center;
background-color: hsl(215, 100%, 98%);
position: relative;
overflow-x: hidden;
}
.container {
background-color: lightgray;
width: 760px;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(186px, 1fr));
resize: horizontal;
overflow: hidden;
gap: 5px;
justify-items: center;
align-items: center;
}
.child {
background-color: gray;
display: flex;
flex-direction: column;
justify-content: center;
}
p {
text-align: center;
}
<div class="container">
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/80x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/120x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/160x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/80x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/120x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/160x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/80x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/120x75" alt="" />
</div>
<div class="child">
<p>title</p>
<img src="https://via.placeholder.com/160x75" alt="" />
</div>
</div>

How set dynamic margin?

i have one div and three child div, check out the code below
<div class="parent">
<div class="child">
<icontag />
<texttag />
</div>
<div class="child">
<icontag />
<texttag />
</div>
<div class="child">
<icontag />
<texttag />
</div>
</div>
each child tag composed of icontag and texttag and text tag is of dynamic height.
but, each icon tag margin must be same, even if text tag height is different.
for more explanation, refer to the picture.
as the above picture, the last text is multiline. so, last child tag height is diffent from other tags.
how can I do that?
Here is an example with grid and flexbox.
Try this, it might fit your needs
.child {
display: flex;
align-items: center;
}
.parent {
display: grid;
grid-template-rows: 33% 33% 33%;
}
.child .icon {
border-radius: 50%;
border: 1px solid blue;
height: 40px;
width: 40px;
display: flex;
justify-content: center;
align-items: center;
margin-right: 20px;
}
.child p {
max-width: 100px;
}
.wrapper {
display: flex;
align-items: center;
}
.same {
margin-right: 30px;
}
<div class="wrapper">
<p class="same">Same height</p>
<div class="parent">
<div class="child">
<div class="icon">1</div>
<p>Bla bla bla bla bla bla bla bal dsfafda af afas fa faf af</p>
</div>
<div class="child">
<div class="icon">1</div>
<p>Bla bla bla</p>
</div>
<div class="child">
<div class="icon">1</div>
<p>Bla bla bla bla bla bla bla bal dsfafda af afas fa faf af</p>
</div>
</div>
</div>
Try adding max-height to all child div..
.child {
--
--
max-height: 300px; // give max height as per your design
overflow-y: auto;
}
You can use display: flex on child items and align-items: center to vertically center the content.
Then use padding property (or margin could work as well) to have some space between .child items as in the following snippet.
.child{
display: flex;
flex-direction: row;
align-items: center;
width: 500px;
padding: 15px 0;
}
.icon{
width: 30px;
height: 30px;
border-radius: 100%;
border: 2px solid #0000ff;
display: flex;
align-items:center;
justify-content: center;
margin-right: 15px;
}
.text{
flex: 1;
}
.child.third .icon{
width: 50px;
height: 50px;
}
<div class="parent">
<div class="child first">
<div class="icon">1</div>
<div class="text">Foo bar baz</div>
</div>
<div class="child second">
<div class="icon">2</div>
<div class="text">Foo bar baz</div>
</div>
<div class="child third">
<div class="icon">3</div>
<div class="text">Foo bar baz<br/>Foo bar baz</div>
</div>
<div class="child fourth">
<div class="icon">4</div>
<div class="text">Foo bar baz<br/>Foo bar baz</div>
</div>
</div>
But note that, if the texts are much longer than the icons, it could break the layout and you need to give static height to child items. But this way texts may overlap.
Try this :
<div class="parent">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 child">
<icontag />
<texttag />
</div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 child">
<icontag />
<texttag />
</div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 child">
<icontag />
<texttag />
</div>
</div>
You can set padding for child class.
padding: 10px;

Vertical align text in Bootstrap 4 divs (with btn class) with no fixed height when using CSS Grid (and no flexbox)

I have a CSS Grid with Bootstrap 4 divs with btn tags in it. The divs widen (stretch) vertically. I just need to vertical align text in the middle.
I know I can use flexbox but in this project I can't.
Here is an example: https://jsfiddle.net/fredhors/0sw2uLay/5/
.grid-wrapper {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(20vmax, 1fr));
grid-gap: 0.5rem;
height: 100vh;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="grid-wrapper">
<div class="btn btn-primary">Content A</div>
<div class="btn btn-warning">Content B</div>
<div class="btn btn-danger">Content C</div>
<div class="btn btn-success">Content D</div>
</div>
If you have to center the whole btn, just use align-items: center on the grid-wrapper - see demo below:
.grid-wrapper {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(20vmax, 1fr));
grid-gap: 0.5rem;
height: 100vh;
align-items: center; /* center vertically */
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="grid-wrapper">
<div class="btn btn-primary">Content A</div>
<div class="btn btn-warning">Content B</div>
<div class="btn btn-danger">Content C</div>
<div class="btn btn-success">Content D</div>
</div>
If you want the background stretched and have the vertical alignment, its easier if you should use flexbox for btn - see demo below:
.grid-wrapper {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(20vmax, 1fr));
grid-gap: 0.5rem;
height: 100vh;
}
.grid-wrapper > .btn {
display: flex;
align-items: center;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="grid-wrapper">
<div class="btn btn-primary">Content A</div>
<div class="btn btn-warning">Content B</div>
<div class="btn btn-danger">Content C</div>
<div class="btn btn-success">Content D</div>
</div>
I know I can use flexbox but in this project I can't.
If you can't use flexbox for this, you can use a pseudo element that will center the text in the grid items - see demo below:
.grid-wrapper {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(20vmax, 1fr));
grid-gap: 0.5rem;
height: 100vh;
}
.grid-wrapper .btn:after {
display: inline-block;
vertical-align: middle;
content: '';
height: 100%;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="grid-wrapper">
<div class="btn btn-primary">Content A</div>
<div class="btn btn-warning">Content B</div>
<div class="btn btn-danger">Content C</div>
<div class="btn btn-success">Content D</div>
</div>

Resources