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;
Related
So, I can't seem to find a good way to do this, and Google hasn't helped much.
Basically, I have a UL with LI's, these LI all contain checkboxes with labels, each label can be of any length, and I want to present this in a table-like manner but flowing, so when the container width changes, the "grid" or table of items are reflowed to maintain the grid-like appearance. Here is an image that I hope explains:
See how the space for the "Small" in the first part is larger since the "Long option" won't fit, and then it does fit, the space for "Option" grows to the same size as the "Even longer option" to accommodate the new table/grid. And when all fit they all get their minimum size
So, just using flex these wouldn't line up. I am unsure whether "display: grid" could do this. Any ideas?
In my opinion, it is not possible with CSS alone in the grid system. But as you have already done correctly with the Flexbox system.
Why it doesn't work with grid?
You can align the grid system by columns and rows. What you want to do would theoretically only work with row alignment. Definitely not with the column template. Because the column affects the width of the rows below.
Because you wants flexible divbox lengths that adjust to the varying widths of the entire column, it is not possible with the grid system. Now you can say, hey, then I'll do it at row level. Yes, but then you would not be able to make breaks. For this reason, it is right to do what you want to do with the flexbox system.
Try this:
body {
margin: 20px;
}
.wrapper {
display: grid;
grid-gap: 0;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr) ) ;
color: #444;
}
.item {
border: solid 1px #444;
padding: 20px;
font-size: 20px;
}
<div class="wrapper">
<section class="item item1">Item 1</section>
<section class="item itemde">Item 1 Default</section>
<section class="item itemlt">Item 1 Longer than</section>
<section class="item itemltl">Item 1 longer than long</section>
</div>
With your HTML like this as you describe
<ul class="con">
<li>RICE</li>
<li>BEANS</li>
<li>PLANTAIN</li>
<li>POTATO</li>
<li>SUOP</li>
</ul>
Using Grid your CSS should be like so:
.con{
display: grid;
grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
}
li{
list-style: none;
border: 1px solid black;
}
The grid set the minimum and maximum size of an item in the grid and then auto-fits it to the grid container
You can play around with the minmax() values
You can use flex box, like this
.container {
background-color: aqua;
display:flex;
flex-wrap:wrap
}
.item {
background-color:red;
margin:1px;
}
<div class="container">
<section class="item">Item 1</section>
<section class="item">Item 1 Default</section>
<section class="item">Item 1 Longer than</section>
<section class="item">Item 1 longer than long</section>
</div>
is this not what you are looking for?
Here is with ul,Li : https://jsfiddle.net/gk0by3uj/1/
body {
margin: 20px;
}
.wrapper {
display: grid;
grid-gap: 0;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr) ) ;
color: #444;
}
.item {
border: solid 1px #444;
padding: 20px;
font-size: 20px;
}
Try this by adding to your wrapper class in div and body.
(This took two hours lol, really hoping it does the trick)
Here's the source I primarily used for help formatting the code.
Try;
function opt() {
document.getElementById("1").style.color = "#B1B1B1";
}
function sml() {
document.getElementById("2").style.color = "#B1B1B1";
}
function lng() {
document.getElementById("3").style.color = "#B1B1B1";
}
function vlng() {
document.getElementById("4").style.color = "#B1B1B1";
}
function reset() {
document.getElementById("1").style.color = "#000";
document.getElementById("2").style.color = "#000";
document.getElementById("3").style.color = "#000";
document.getElementById("4").style.color = "#000";
}
.grid-container {
display: grid;
grid-template-columns: auto auto auto auto;
}
.grid-container>div {
background-color: rgba(255, 255, 255, 0.8);
border: 1px solid black;
text-align: center;
padding: 12px;
}
.button-div {
text-align: center;
}
.button-div button {
font-size: 21px;
}
<div class="grid-container">
<div id="1" onclick="opt()">Option</div>
<div id="2" onclick="sml()">Small</div>
<div id="3" onclick="lng()">Long Option</div>
<div id="4" onclick="vlng()">Even Longer Option</div>
</div>
<br>
<br>
<div class="button-div">
<button onclick="reset()">Reset</button>
</div>
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;
}
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>
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>
I am trying to create a disk utilization chart React component, where I need different colored boxes of various sizes based on the percentage.
How do I do this in CSS & HTML? What is the best way to have these boxes sized based on a percentage?
https://codepen.io/blitzkriegz/pen/Gzgpzb
<div class="wrapper">
<div class="box i">
<div class="box j">20% Item 1</div>
<div class="box k">30% Item 2</div>
<div class="box l">40% Free</div>
</div>
</div>
CSS:
body {
margin: 5px
}
.box {
background-color: gray;
color: #fff;
border-radius: 5px;
padding: 20px;
font-size: 150%;
}
.box .box {
margin: 10px;
background-color: darkblue;
}
.j {
margin: 10px;
background-color: green;
}
.wrapper {
display: grid;
grid-gap: 10px;
grid-template-columns: 1fr 2fr 1fr 1fr 2fr 1fr;
background-color: #fff;
color: #444;
}
.i {
padding: 0;
grid-gap: 2px;
grid-column: auto / span 3;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}
Consider using flex-box for this, rather than grid.
Flex box gives you an easy and convenient means of achieving what you want here; to control and set variable percentage based widths of box elements in a horizontal arrangement (along the "row" axis) without the need for the clearfix trick.
One approach might be as follows:
body {
margin: 5px
}
.box {
margin: 10px;
background-color: darkblue;
}
.j {
background-color: green;
/* Set percentage explicitly */
width:20%;
}
.k {
/* Set percentage explicitly */
width:30%;
}
.l {
/* Set percentage explicitly */
width:40%;
}
.wrapper {
padding: 0;
background-color: gray;
color: #fff;
border-radius: 5px;
font-size: 150%;
/* Add this to use flex-box */
display:flex;
flex-direction:row;
}
<!-- simplifiy you markup -->
<div class="wrapper">
<div class="box j">20% Item 1</div>
<div class="box k">30% Item 2</div>
<div class="box l">40% Free</div>
</div>
If you're using ReactJS to render the markup via JSX (I noticed reactjs is tagged in your OP), you can use this approach to set the widths of boxes dynamically via inline styling:
<div className="wrapper">
<div className="box j" style={{ width : '20%' }}>20% Item 1</div>
<div className="box k" style={{ width : '30%' }}>30% Item 2</div>
<div className="box l" style={{ width : '40%' }}>40% Free</div>
</div>
Hope that helps!