Why are my inline divs at different heights? - html

I am using a grid container on an html page; inside two adjacent divs in the grid I have some little divs. I want these little divs to appear at the same height so they are aligned across the page. I think that I've given them the same relevant properties, but they are sitting at slightly different heights, and the line spacing is different. Why this is happening and how to remedy it?
Relevant info: I'm looking at my HTML in Chrome.
Image of my uneven blocks:
image of divs at uneven heights
Relevant code:
The container is defined in CSS like this:
.container {
width: 100%;
display: grid;
grid-template-columns: 1fr 2fr 2fr 4fr 1fr;
height: 400px;
background-color: lightgreen;
}
The two columns of the container where the problem arises are columns 3 and 4 (2fr and 4fr columns). Those divs are defined as:
.wordDisplay {
padding-top: 100px;
text-align: center;
}
.display {
padding-top: 100px;
}
And the little blue-box divs that I want to appear at the same height are:
.a {
display: inline-block;
width: 50px;
height: 50px;
padding-top: 20px;
margin: 1%;
border: 1px solid blue;
background-color: lightgreen;
font-size: 20px;
text-align: center;
}
.a:hover {
background-color: yellow !important;
}
.b {
display: inline-block;
width: 150px;
height: 50px;
padding-top: 20px;
margin: 1%;
border: 1px solid blue;
background-color: lightgreen;
font-size: 20px;
text-align: center;
}
.b:hover {
background-color: yellow;
}

Remove margins to get full height, The problem is, you're using margin for the wrong purpose
margin has different behavior, you have to be careful while using it.
Ask yourself why I want to use it here.
Your use of it as a value does not mean the expected result because it depends on your code scenario.
I recommend you to read this article
to ensure that you understand margins clearly, you will know what issue is happened
it's something dirty call margin collapse
there are two fixes I will recommend for your situation:
If your HTML like that:
<div class="container">
<div class="wordDisplay">
<div class="b">
band
</div>
<div class="b">
band
</div>
</div>
<div class="display">
<div class="a">
b
</div>
<div class="a">
a
</div>
<div class="a">
n
</div>
<div class="a">
d
</div>
<div class="a">
b
</div>
<div class="a">
a
</div>
<div class="a">
n
</div>
<div class="a">
d
</div>
</div>
</div>
Change margin for class "a" to half of margin in class "b" so if you add margin:1% in class "b" change the value of margin in class "a" to 0.5% so that will solve your problem.
Change grid template columns for column 3 and 4 to two equals fractions like that
.container {
width: 100%;
display: grid;
grid-template-columns: 1fr 2fr 4fr 4fr 1fr;
height: 400px;
background-color: lightgreen;
}

Related

Vertically center text in a grid layout with grid and items 100% height [duplicate]

This question already has answers here:
How to vertically align text inside a flexbox?
(13 answers)
Closed last year.
Here's an SO question about exactly this, and I've found several others. Many of them have a dozen answers, using many (many!) combinations of align and justify styles, transforms, margins, references to table cells, etc.
I've tried maybe 200 combinations of ideas from references. Is it possible to center the text vertically? Is it possible to do it while maintaining 100% height without adding divs around or inside the boxes?
My main finding after all this is that height: 100% thwarts every other style that can succeed. For example, margin-top and margin-bottom set to auto works, but not with 100% height.
section {
height: 400px;
}
#boxes {
background-color: green;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
gap: 6px;
height: 100%;
min-height: 10em;
min-width: 12em;
}
#boxes>div {
background-color: #8ca0ff;
text-align: center;
border: 1px solid red;
height: 100%;
}
<section>
<div id="boxes">
<div id="00">I'm not</div>
<div id="01">vertically</div>
<div id="02">centered.</div>
<div id="10">I</div>
<div id="11">am not</div>
<div id="12">either!</div>
<div id="20">Was CSS designed</div>
<div id="21">by a psychopath?</div>
<div id="22">Seems like it!</div>
</div>
</section>
I am astonished at how non-simple this simple-seeming thing is.
Put flex on the boxes, just like in an answer on the post you linked.
https://css-tricks.com/snippets/css/a-guide-to-flexbox
section {
height: 400px;
}
#boxes {
background-color: green;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
gap: 6px;
height: 100%;
min-height: 10em;
min-width: 12em;
}
#boxes>div {
background-color: #8ca0ff;
text-align: center;
border: 1px solid red;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
}
<section>
<div id="boxes">
<div id="00">I'm not</div>
<div id="01">vertically</div>
<div id="02">centered.</div>
<div id="10">I</div>
<div id="11">am not</div>
<div id="12">either!</div>
<div id="20">Was CSS designed</div>
<div id="21">by a psychopath?</div>
<div id="22">Seems like it!</div>
</div>
</section>

Why is row with column cards layout not working?

I'm building a website in which I'm trying to create a row of 2 column cards. I'd also like it to become just one row cards when the screen size shrinks.
Instead, it stays stuck on the one row format.
I've included a picture of what I'm trying to do (the colors don't matter)
Here is the HTML:
<div class="row">
<div class="column">Box 1</div>
<div class="column">Box 2</div>
</div>
Here is the CSS:
.column {
background-color: black ;
float: left;
width: 50%;
padding: 50px;
text-align: center;
font-size: 25px;
color: white;
}
Please note that the padding interferes with the width and because of that the .column div expands more than 50% of the screen width.
Unless you're trying to learn, what I would recommend you is to use a CSS framework such as Bootstrap. They make the life very easy when it comes to managing layouts.
.column {
background-color: black ;
width: calc(50% - 100px);
padding: 50px;
text-align: center;
font-size: 25px;
color: white;
display: block;
}
#media (min-width: 102px) {
.column {
float: left;
}
}
<div class="row">
<div class="column">Box 1</div>
<div class="column">Box 2</div>
</div>
You can use media queries for this purpose, you will need to find the limit, I have given 102px for the sake of the example, but you will need to find the pixel limits that works best for your case, probably in sync with mobile screen sizes.
The reason that it was stuck with the boxes one below the other was that you had a padding of 50px, so the whole width was 2 * (50% + 50px) = 100% + 100px and if you add any positive value to 100%, then the two items will not fit into a single row. Then, float: left is only needed if we are to display them in an inline manner. So, float: left is only needed in a case, you need a calc calculation to extract the 100px pixels from the divs. Finally, I have added display: block. Happy coding!
What you want could be achieved with a media query and CSS grid layout:
.column {
background-color: black;
padding: 50px;
margin: 5px;
text-align: center;
font-size: 25px;
color: white;
}
.row {
display: grid;
grid-template-columns: 1fr 1fr;
}
#media(max-width: 500px) {
.row {
grid-template-columns: 1fr;
}
}
<div class="row">
<div class="column">Box 1</div>
<div class="column">Box 2</div>
</div>

How to left-align items with each other in a grid that used justify-items: center

I am using grid. I centered my items but i want left position to be same on cross axis.
.box {
display: grid;
grid-template-columns: 1fr;
padding: 10px;
border: 1px solid blue;
gap: 1rem;
justify-items: center;
}
img {
width: 200px;
}
<div class="box">
<img src="img/featured.jpg">
<div class="item2">Item 2 Lorem</div>
</div>
Note : i want solution in only grid. There is many temporary fixes for that but i don't want that because i am looking for a perfect standard grid alignment solution for it.
You can do it a few ways:
1. Fixed Width Text (Not Responsive)
Based on the information you gave in your question. you can simply set the width of the item2 div to be the same as the image, e.g.:
.box {
display: grid;
grid-template-columns: 1fr;
padding: 10px;
border: 1px solid blue;
gap: 1rem;
justify-items: center;
}
.item2,
img {
width: 200px;
}
<div class="box">
<img src="https://via.placeholder.com/200x150">
<div class="item2">Item 2 Lorem</div>
</div>
2. The Responsive Way
This will allow us to responsively set up the 2 separate alignments you require: centre the container element, and left-align its contents, e.g.
<div class="box">
<div class="boxcontent">
Content here...
</div>
</div>
CSS: .boxcontent { text-align: left; }
Why this works:
You must have a defined relationship between the image and text, otherwise there is no way to tell the grid that these 2 individual elements must be positioned in relation to each other. You can do this by putting them in a container.
If you think about it, you are trying to do 2 separate alignments here:
make the image and text be centred in relation to the grid but also
make them be in alignment with each other so they are left aligned.
Putting them in a container achieves both of these objectives by creating a relationship between the image and text so that:
they act as a single unit in relation to the grid and also
allow them to be positioned in relation to each other regardless of the grid
Working Example:
.box {
display: grid;
grid-template-columns: 1fr;
padding: 10px;
border: 1px solid blue;
gap: 1rem;
justify-items: center;
}
.boxcontent {
text-align: left;
}
img {
width: 200px;
}
<div class="box">
<div class="boxcontent">
<img src="https://via.placeholder.com/200x150">
<div class="item2">Item 2 Lorem</div>
</div>
</div>
.box {
display: block;
margin: 0 auto;
width: max-content;
padding: 10px;
border: 1px solid blue;
}
img {
width: 200px;
}
.item2 {
margin-top: 1rem;
}
<div class="box">
<img src="pic_trulli.jpg" alt="Hi dear">
<div class="item2">Item 2 Lorem</div>
</div>

How to make divs not move during zooming in grid layout

I have the problem during zooming in browser. I split "details__view" to two grid columns but during zooming these columns do not keep their position and width. I tried to use position: absolute, and position:relative but then the division into columns completely disappears. Here is my html:
<div className="box__details">
<div className="details__container">
<div className="details__container--title">Details</div>
<div className="details__view">
<div className="label">Name</div>
<div className="value">XYZ</div>
</div>
<div className="details__view">
<div className="label">Second Name</div>
<div className="value">XYZ</div>
</div>
<div className="details__view">
<div className="label">Country</div>
<div className="value">XYZ</div>
</div>
</div>
</div>
and CSS:
.box__details {
display: grid;
grid-template-columns: 1fr 1fr;
column-gap: 30px;
}
.details__container {
border: 1px solid #eee;
padding: 10px;
border: 1px solid #eee;
}
.details__container--title {
padding-bottom: 15px;
}
.details__view {
display: grid;
grid-template-columns: .4fr .4fr;
padding-bottom: 15px;
}
.label {
padding-left: 5px;
}
.value {
text-align: left;
}
Do you have any ideas how can I make labels and values ​​do not change during zooming? Thank you in advance for help!
The columns do not keep their position because you're using fr. This makes the box stay in the place you put it in on the screens proportion thus moving the right a little left when zooming in (Kinda like using %). To make it stay in place you may have to use something like px;
By changing grid-template-columns: 1fr 1fr; to something like grid-template-columns: 427px 427px;

CSS Grid template with image breaks Firefox

I have two lines of text on top of another, each using different font-sizes.
To the left of these text lines, I would like to display an image, automatically scaled in width and height to fit the height of both of these text lines combined, while keeping the original aspect ratio.
I am using css grid trying to achieve this. It does work in chrome, but in firefox, it breaks.
Chrome:
Firefox:
As you can see, firefox fails to properly adjust the image grid area to fit the images size.
How could this be fixed?
The relevant code can be found here:
Jsfiddle: https://jsfiddle.net/5z6grjp2/
.parent {
width: 200px;
display: grid;
border: 1px solid #000;
grid-template-columns: auto 1fr;
grid-template-areas: "image textA" "image textB";
}
.image {
grid-area: image;
height: 100%;
}
.textA {
font-size: 20px;
background-color: #f99;
grid-area: textA;
}
.textB {
font-size: 15px;
background-color: #9f9;
grid-area: textB;
}
<div class="parent">
<img class="image" src="/archer.png">
<div class="textA">Text A</div>
<div class="textB">Text B</div>
</div>
This looks like a bug in Firefox.
The layout renders with the image at its inherent size. The sibling items, sizing to 1fr, don't use the actual remaining space. They use the space available when the image was rendered at its original size. So there's an overlap when the image expands. That's what appears to be happening.
The workaround is to force the image area to expand. A fixed width does this, although I know it may not be what you want.
.parent {
width: 200px;
display: grid;
border: 1px solid #000;
grid-template-columns: auto 1fr;
grid-template-areas: "image textA" "image textB";
}
.image {
grid-area: image;
width: 40px;
height: auto;
}
.textA {
grid-area: textA;
font-size: 20px;
background-color: #f99;
}
.textB {
grid-area: textB;
font-size: 15px;
background-color: #9f9;
}
<div class="parent">
<img class="image" src="http://hordes.io/data/archer.png">
<div class="textA">Text A</div>
<div class="textB">Text B</div>
</div>
jsFiddle demo