Pixel-Sized Gap Between Inner and Outer Div - css

Is this a rendering bug in Chrome?
Using Google Chrome 107.0.5304.32 (Official Build) beta (64-bit)
.meters {
display: grid;
grid-template-columns: 4rem 1fr 3rem;
grid-auto-rows: 1.3rem;
align-items: center;
gap: 0.8rem;
}
.meter {
display: flex;
height: 100%;
border: solid 3px black;
}
.fill {
background: blue;
}
<div class="meters">
<p>0 Sterne</p>
<div class="meter">
<div class="fill" style="width: 100%;"></div>
</div>
<p>27%</p>
<p>1 Sterne</p>
<div class="meter">
<div class="fill" style="width: 10.989%;"></div>
</div>
<p>11%</p>
<p>2 Sterne</p>
<div class="meter">
<div class="fill" style="width: 32.4176%;"></div>
</div>
<p>32%</p>
<p>3 Sterne</p>
<div class="meter">
<div class="fill" style="width: 11.5385%;"></div>
</div>
<p>12%</p>
<p>4 Sterne</p>
<div class="meter">
<div class="fill" style="width: 17.5824%;"></div>
</div>
<p>18%</p>
</div>
I see white around the blue divs:
If I set the border to 4px or more, the white is gone. For 1px, it also works fine. With 2px I see only white at the bottom and with 3px white on top and bottom.
Amaury Hanser in the comments mentioned zoom, so I checked and when zooming in to 110%, the white also disappears.
I even tried setting overflow: hidden; on .meter and height: 150%; on fill, so I can see in the inspector that the fill is larger than the container, but it still shows the white border:

Iam not sure what is wrong but you can use outline instead
.meters {
display: grid;
grid-template-columns: 4rem 1fr 3rem;
grid-auto-rows: 1.3rem;
align-items: center;
gap: 0.8rem;
}
.meter {
display: flex;
height: 100%;
outline: 3px solid black !important;
}
.fill {
background: blue;
}
<div class="meters">
<p>0 Sterne</p>
<div class="meter">
<div class="fill" style="width: 100%;"></div>
</div>
<p>27%</p>
<p>1 Sterne</p>
<div class="meter">
<div class="fill" style="width: 10.989%;"></div>
</div>
<p>11%</p>
<p>2 Sterne</p>
<div class="meter">
<div class="fill" style="width: 32.4176%;"></div>
</div>
<p>32%</p>
<p>3 Sterne</p>
<div class="meter">
<div class="fill" style="width: 11.5385%;"></div>
</div>
<p>12%</p>
<p>4 Sterne</p>
<div class="meter">
<div class="fill" style="width: 17.5824%;"></div>
</div>
<p>18%</p>
</div>

Related

Keep left margin of a div inline with another div when resizing

the problem that I'm having is that when I resize the browser the left margins of both the and my card divs aren't aligning. Is there some sort of css property or maybe some JS that will make them stay aligned when resizing?
here's my sandbox
https://codesandbox.io/s/stupefied-christian-tosys?file=/src/styles.css:0-
Add the .filter margin margin: 0 80px; to your .row. and then remove justify-content: center;. It will loose the centered. But will be align on the left. To aviod the overflow from the body, you can set width: calc(100% - 160px);to your .row:
/* ADDED BELOW */
margin: 0 80px;
justify-content: unset;
width: calc(100% - 160px);
After if you want to keep your card more center, either you use justify-content: space-between; on row, either you use margin: 10px auto; on your card.
DEMO:
body {
margin: 0 auto;
max-width: 1200px;
}
.row {
display: flex;
justify-content: center;
width: 100%;
flex-wrap: wrap;
/*ADDED BELOW */
margin: 0 80px;
justify-content: unset;
width: calc(100% - 160px);
}
.filter {
display: flex;
margin: 0 80px;
}
.filter select {
display: inline;
width: 15rem;
margin: 10px;
border-radius: 5px;
}
.card {
width: 15rem;
margin: 10px;
border-radius: 5px;
border: 1px solid rgba(0, 0, 0, 0.125);
}
.info {
padding: 15px;
}
.img-container {
padding-top: 0;
position: relative;
}
.card img {
width: 100%;
object-fit: cover;
}
.price {
font-weight: bold;
}
<div id="root">
<div class="App">
<div class="filter-container">
<div class="product-listing-wrap">
<div class="filter">
<select class="custom-select" id="priceGroup">
<option value="1">Under $50</option>
<option value="2">$50 to $100</option>
<option value="3">$100 to $250</option>
<option value="4">Over $250</option>
</select>
</div>
<div class="row" style="
margin: 0 80px;
padding: 10px 0;
width: calc(100% - 160px);
">
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="1"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="2"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="3"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="4"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="5"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="6"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="7"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="8"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
try it:
.row {
display: flex;
justify-content: space-between;
margin: 0 80px;
max-width: 1040px;
flex-flow: wrap;
}

Flexbox make columns equal heights

I am trying to get 3 columns in a row and be the same height.
I am using the HTML/CSS below. I can't figure out why the boxes arent the same height.
.container {
text-align: center;
display: block;
width: 100%;
}
.grid {
width: 100%;
height: 100%;
}
.row {
display: flex;
height: 100%;
flex-direction: row;
}
.col {
background: #444;
padding: 2em;
flex: 1;
height: 100%;
border: 1px solid blue;
}
<div class="container">
<div class="data">
<div class="grid">
<div class="row">
<div class="col">
<p>col 1</p>
</div>
<div class="col">
<p>col </br>2</p>
</div>
<div class="col">
<p>col 3</p>
</div>
</div>
</div>
</div>
</div>
How can I make the boxes all the same height. thanks
Simply, remove the height: 100% from .col.
flex: 1; will do the job.
.row {
display: flex;
}
.col {
flex: 1;
background: #444;
padding: 2em;
border: 1px solid blue;
text-align: center;
}
<div class="container">
<div class="data">
<div class="grid">
<div class="row">
<div class="col">
<p>col 1</p>
</div>
<div class="col">
<p>col </br>2</p>
</div>
<div class="col">
<p>col 3</p>
</div>
</div>
</div>
</div>
</div>

Setting width relatively to the size of the grandparent div

I have the structure that currently looks approximately like the snippet below; how can I make div2 to take the width 50% of its grandparent div, rather than 50% of its immediate parent?
<div id="grandParent" style="width: 100%; display: flex; flex-direction: row; flex-wrap: nowrap; background-color: yellow; border-color: yellow">
<div id="div1" style="width: 100px; background-color: aqua">
div1
</div>
<div id="parent" style="width: 100%; background-color: green">
parent<br>
<div id="div2" style="width: 50%; background-color: fuchsia; margin: 0 auto;">
div2
</div>
</div>
</div>
We can do some maths. The width of the parent is P - 100px and you need to have P/2 so if you do X/2 + 50px where X = P - 100px you will have P/2. Also better use flex-grow:1 instead of width:100% to avoid the shrink effect and have the first div always at 100px
console.log($('#div2').width())
console.log($('#grandParent').width())
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="grandParent" style=" display: flex; flex-direction: row; flex-wrap: nowrap; background-color: yellow; border-color: yellow">
<div id="div1" style="width: 100px; background-color: aqua">
div1
</div>
<div id="parent" style="flex-grow:1; background-color: green">
parent<br>
<div id="div2" style="width: calc(50% + 50px); background-color: fuchsia; margin: 0 auto;">
div2
</div>
</div>
</div>
You can consider the use of CSS variable to make this easier:
console.log($('#div2').width())
console.log($('#grandParent').width())
:root {
--w:100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="grandParent" style="display: flex; flex-direction: row; flex-wrap: nowrap; background-color: yellow; border-color: yellow">
<div id="div1" style="width: var(--w); background-color: aqua">
div1
</div>
<div id="parent" style="flex-grow:1;background-color: green">
parent<br>
<div id="div2" style="width: calc(50% + var(--w)/2); background-color: fuchsia; margin: 0 auto;">
div2
</div>
</div>
</div>

How to style space between items?

Is it possible to style spaces between items in flex?
Case Scenario:
I have a kind of table/grid
I don't want items to grow (flex-grow: 0)
I don't want space-between items
If there is not enough remaining space, next item falls to next line
Example:
Fiddle:
https://jsfiddle.net/t245o0vr/21/
Desired effect:
I would like to add a border-bottom in those spacing in the end. You think it's possible? Any ideas?
.container {
display: flex;
flex-wrap: wrap;
border: 1px solid #ccc;
border-bottom: 0;
max-width: 470px; /* for the sake of example */
}
.item {
display: inline-flex;
padding: 10px;
border-bottom: 1px solid #ccc;
}
<div class="container">
<div class="item">
itemA
</div>
<div class="item">
itemBitemB
</div>
<div class="item">
itemCCCC
</div>
<div class="item">
itemA
</div>
<div class="item">
itemBitemB
</div>
<div class="item">
itemCCCC
</div>
<div class="item">
itemA
</div>
<div class="item">
itemBitemB
</div>
<div class="item">
itemCCCC
</div>
</div>
This solutions is not based in flex (I also would like to know how to solve it with any flex'ish rule) but it works.
Instead of adding border to elements add and absolute positioned &:after pseudo-element on every item and hide the overflow in parent
.container {
display: flex;
flex-wrap: wrap;
border: 1px solid #ccc;
border-bottom: 0;
max-width: 470px; /* for the sake of example */
overflow: hidden; /* <------- new */
}
.item {
display: inline-flex;
padding: 10px;
/* border-bottom: 1px solid #ccc; <---- removed */
position: relative; /* <------- new */
}
.item:after {
content: "";
position: absolute;
left: 0;
bottom: 0;
width: 100vw; /* Bigger enough :P */
border-bottom: 1px solid #ccc;
}
<div class="container">
<div class="item">
itemA
</div>
<div class="item">
itemBitemB
</div>
<div class="item">
itemCCCC
</div>
<div class="item">
itemA
</div>
<div class="item">
itemBitemB
</div>
<div class="item">
itemCCCC
</div>
<div class="item">
itemA
</div>
<div class="item">
itemBitemB
</div>
<div class="item">
itemCCCC
</div>
</div>
I would consider another way using gradient. The trick is to have a gradient that will get repeated each line to cover each new line that will appear. You simply need to know the height of your line which is based on the line-height and padding
.container {
display: flex;
flex-wrap: wrap;
border: 1px solid #ccc;
line-height:1.2em;
background:
repeating-linear-gradient(to bottom,
transparent 0,transparent calc(1.2em + 20px),
#ccc calc(1.2em + 20px),#ccc calc(1.2em + 21px));
max-width: 470px; /* for the sake of example */
}
.item {
display: inline-flex;
padding: 10px;
}
<div class="container">
<div class="item">
itemA
</div>
<div class="item">
itemBitemB
</div>
<div class="item">
itemCCCC
</div>
<div class="item">
itemA
</div>
<div class="item">
itemBitemB
</div>
<div class="item">
itemCCCC
</div>
<div class="item">
itemA
</div>
<div class="item">
itemBitemB
</div>
<div class="item">
itemCCCC
</div>
</div>

line out images to the left side among each other

I have some images where I put a trach bin image on with z-index. This works. But when I resize the browser to a smaller screen the pictures slide over each other. But the intention is that they retain their original format and instead next to each other they line out to the left side among each other.
Below the code I use.
<style type="text/css">
.test {
position:absolute;
top: 0px;
left: 0px;
z-index: 1;
display: inline-block;
min-width: 160px;
min-height: 120px;
width: 160 px;
height: 120px;
}
.trashbin {
position:absolute;
top: 65px;
left: 115px;
z-index: 3;
}
</style>
<fieldset>
<div class="form-group">
<div class="col-xs-4">
<img src="/img/l16001.jpg" class="test" >
<img src="/img/trash.png" class="trashbin">
</div>
<div class="col-xs-4 test2">
<img src="/img/l16001.jpg" class="test" >
<img src="/img/trash.png" class="trashbin">
</div>
<div class="col-xs-4 test2">
<img src="/img/l16001.jpg" class="test" >
<img src="/img/trash.png" class="trashbin">
</div>
</div>
</fieldset>
Using flexboxes for layout. Will wrap when needed.
.images {
display: flex;
justify-content: space-evenly;
flex-wrap: wrap;
}
.test2 {
display: flex;
flex-wrap: no-wrap;
align-items: center;
}
.test2 img {
margin-right: .5em;
}
<fieldset>
<div class="form-group images">
<div class="col-xs-4 test2">
<img src="http://placehold.it/100">
<img src="http://placehold.it/10" class="trashbin">
</div>
<div class="col-xs-4 test2">
<img src="http://placehold.it/100">
<img src="http://placehold.it/10" class="trashbin">
</div>
<div class="col-xs-4 test2">
<img src="http://placehold.it/100">
<img src="http://placehold.it/10" class="trashbin">
</div>
</div>
</fieldset>

Resources