Align image with border 2 column on mobile - html

I have images where I want to do 5 columns on the desktop and 2 on the mobile, on the desktop it is working however on the mobile being img-responsive is showing only 1 image at a time and I want to show 2.
I've used hidden-xs but I think it's wrong. Two images are not aligned.
<style>
#tudo {
width: 100%;
}
#media screen and (min-width: 980px) {
#tudu {
margin-right: 50px;
}
#tudo1 {
position: relative;
width: 15%;
margin-left: 4%;
float: left;
border: 2px solid #35c9b1;
max-height: 300px;
}
}
#media screen and (max-width: 500px)
/* Mobile */
{
#tudo1 {
position: relative;
width: 46%;
margin: 2%;
float: left;
border: 2px solid #35c9b1;
}
}
</style>
<div id="tudo" align="left">
<div id="tudo1"><img class="img-responsive" src="https://picsum.photos/200"></div>
<div id="tudo1"><img class="img-responsive" src="https://picsum.photos/150"></div>
<div id="tudo1"><img class="img-responsive" src="https://picsum.photos/140"></div>
<div id="tudo1"><img class="img-responsive" src="https://picsum.photos/130"></div>
<div id="tudo1"><img class="img-responsive" src="https://picsum.photos/120"></div>
</div>

I'm not 100% sure what you would like to achieve, but I highly recommend looking into the CSS Grid Layout. You could use it to get the columns easily. One way would be like this.
#tudo {
display: grid;
width: 100%;
grid-template-columns: repeat(5, 1fr);
grid-column-gap: 10px;
grid-row-gap: 15px;
}
#media only screen and (max-width: 600px) {
#tudo {
grid-template-columns: repeat(2, 1fr);
}
}
With the grid-template-columns, you can specify how many columns you want, and you can also set each column to specific widths. The fr is for fractions, but you can also use e.g percentages and fixed widths in px.
E.g. if you want to have the first item in each row fixed and rest take up the available space, you can do the following:
grid-template-columns: 300px repeat(4, 1fr);
With this, the first item in each row will be fixed to 300px and the rest will take up 1/4 of available space.
See more here:
https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-columns
With grid-column-gap and grid-row-gap you can set the space between the rows and columns.
See this guide here for more info on CSS grid
https://css-tricks.com/snippets/css/complete-guide-grid/

In mobile styling you should decrease #tudo1 width 4px because of 2px left and 2px right border. You can use calc(46% - 4px)

Does this show the behaviour you're looking for?
Use padding instead of margin for spacing
Use box-sizing: border-box to avoid box-model width gotchas with borders and padding
Use max-width: 100% on images so they don't overflow the size of the container
Use inline-block + whitespace fix instead of float but that's up to you, if you use floats you need a clearfix on #tudo!
Do not use ID's multiple times, use classes instead.
Have main styles not wrapped in a media query, either go mobile first (e.g. your global styles are for mobile) or go desktop first and change your stuff for smaller screens selectively (used in below example based on your code)
Of course you could achieve the same thing with flexbox or css-grids, but I tried to stay close to what you provided as code-input.
* {
box-sizing: border-box;
}
#tudo {
font-size: 0;
width: 100%;
}
.tudo1 {
font-size: initial;
display: inline-block;
max-height: 300px;
padding: 4%;
position: relative;
width: 20%;
}
.tudo1 img {
border: 2px solid #35c9b1;
display: block;
max-width: 100%;
}
#media screen and (max-width: 500px)
/* Mobile */
{
.tudo1 {
width: 50%;
padding: 2%;
}
}
<div id="tudo" align="left">
<div class="tudo1"><img class="img-responsive" src="https://via.placeholder.com/300x300"></div>
<div class="tudo1"><img class="img-responsive" src="https://via.placeholder.com/300x300"></div>
<div class="tudo1"><img class="img-responsive" src="https://via.placeholder.com/300x300"></div>
<div class="tudo1"><img class="img-responsive" src="https://via.placeholder.com/300x300"></div>
<div class="tudo1"><img class="img-responsive" src="https://via.placeholder.com/300x300"></div>
</div>

Related

How do I create 2 column divs that stack on top of each at different screen sizes?

The div container is breaking my page. It's overriding a container used elsewhere. Is there any other alternative I can use to still have the same effect i.e the div columns sit next to each but at different screen resolutions, they stack on top of each other, and both retain equal width and length as they shrink?
How do I create two responsive divs that sit next to each other and stack on top of each other at different screen sizes? The dimensions are specific (each div is 350 x 217 px).
One div will have a text centred horizontally within the div but also left aligned and the other will be an image.
Below is the ideal end result. I'm a newbie to dev and this is for an assignment that's overdue. I've been fiddling around for the past couple of days and I keep going round in circles.
Responsive view on smaller screens:
* {
box-sizing: border-box;
}
.column {
flex: 1;
}
.left {
background-color: #e0e620;
}
.center {
margin: auto;
border: 3px solid green;
padding: 50px 50px;
}
.right {
background-color: #E5E5E5;
}
.row {
display: flex;
}
img {
width: 100%;
display: block;
}
#media screen and (max-width: 600px) {
.row {
width: 100vw;
display: block;
}
<div class="row">
<div class="column left">
<div class="center">The Info.<br />
<a class="link-button-green" href="" title="Info
guide">Download now</a>
</div>
</div>
<div class="column right">
<img src="https://via.placeholder.com/350x217.jpg">
</div>
</div>
The image is 350 x 217. Here is the ideal look below:
The below screen is what I would like to achieve without changing the existing width and height of yellow div? How do I go about achieving that?
add display: grid to the parent element .row;
add grid-template-columns: 1fr 1fr to the parent element (.row) to have a 2-column layout.
For screens at 600px or below you change the grid-template-columns to 1fr for the element .row to get a 1-column layout. To have both elements have the same height, you can use grid-auto-rows: 1fr on the parent element.
To maintain the normal block-level-behavior you add display: flex; flex-direction: column to the .left column. Flexbox will allow you do vertical center the text.
To vertical center the text you have to align it to the main-axis (flex-direction: column) with justify-content: center
* {
box-sizing: border-box;
}
.row {
display: grid;
grid-template-columns: 1fr 1fr;
}
#media only screen
and (max-width: 600px) {
.row {
grid-template-columns: 1fr;
grid-auto-rows: 1fr;
}
}
.left {
background-color: yellow;
display: flex;
flex-direction: column;
justify-content: center;
}
img {
width: 100%;
display: block;
object-fit: contain;
}
<div class="row">
<div class="column left">
<div class="center">The Info.<br />
<a class="link-button-green" href="" title="Info
guide">Download now</a>
</div>
</div>
<div class="column right">
<img src="https://via.placeholder.com/350x217.jpg">
</div>
</div>
Your question needs work.
You want the specific div widths to be 350px x 217px, but when you get to a min-width of 600px, that width of 350px isn't going to work anymore. You will struggle to have a responsive page if you set explicit heights and widths. You need to let elements fill their spaces naturally.
That said, I've created a solution that I think would work best based on the images you've provided.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
img {
max-width: 100%;
height: auto;
}
.container {
display: flex;
flex-direction: column;
}
#media screen and (min-width: 600px) {
.container {
flex-direction: row;
min-height: 217px;
}
}
.inner {
background-color: #E0E61F;
display: flex;
justify-content: center;
align-items: flex-start;
flex-direction: column;
width: 100%;
min-height: 217px;
}
.inner span {
padding-left: 1rem;
min-height: unset;
}
.inner img {
min-height: 217px;
object-fit: cover;
}
<div class="container">
<div class="inner">
<span>The Info</span>
<span>Download Now</span>
</div>
<div class="inner">
<img src="https://placekitten.com/1600/900" alt="">
</div>
</div>
Like mentioned above i would start with mobile view. You can create a parent div around your two boxes called container with the following css properties
display:flex
flex-direction: column
Then add a media query set at the width you would like this divs to be side by side and change the flex direction on your container div to row
flex-direction: row

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>

Why is my CSS grid-template-columns stacking and not being consistent

So I have these carousel thumbnails that supposedly keep adding to the right until each of them reached the width of 120px and they'll go into the next row. These thumbnails are wrapped by a wrapper which its width fits its content.
HTML:
<div class="carousel-thumbnail-wrapper">
<div class="carousel-thumbnail mod-active">
<img src="./img/project-slider-1.jpg" alt="">
</div>
<div class="carousel-thumbnail">
<img src="./img/project-slider-2.jpg" alt="">
</div>
</div>
CSS:
.cg-carousel > .carousel-thumbnail-wrapper {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
column-gap: 8px;
width: fit-content; /** the width fits its content... */
max-width: 100%; /** ...but can only grow not more than 100% of its parent's */
}
.cg-carousel > .carousel-thumbnail-wrapper > .carousel-thumbnail {
box-sizing: border-box;
border-radius: var(--border-radius-xs);
cursor: pointer;
position: relative;
margin-bottom: 8px;
max-width: 200px; /** each thumbnail has maximum 200px width */
}
Normally, if everything goes right it will show like this: Images keep adding to the right
It does work like that sometimes. But when I refresh the page or go to another page back to back, for some reason they're stacking, like this: Images suddenly stacking
What did I do wrong?
So your code isn't wrong per se, but your selectors are a little off, and your images need a width of 100%, otherwise they will not resize - their parent will, but they will still remain their original size. So if you change your selectors so that they actually target the elements in your html, and set the width of the images to 100%, it should work perfectly. The javascript in my other answer makes your code fully fool-proof as it combats any css issues older browsers may face. But if old browsers are not a problem, then use this answer. Below is a snippet. Here is a jsfiddle link if you want to resize the viewport, add images or play with the code in general: https://jsfiddle.net/258b9x6e/1/
.carousel-thumbnail-wrapper {width: 100%}
.carousel-thumbnail-wrapper {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
column-gap: 8px;
width: fit-content; /** the width fits its content... */
max-width: 100%; /** ...but can only grow not more than 100% of its parent's */
}
.carousel-thumbnail {
box-sizing: border-box;
border-radius: var(--border-radius-xs);
cursor: pointer;
position: relative;
margin-bottom: 8px;
max-width: 200px; /** each thumbnail has maximum 200px width */
}
img {
width: 100%;
}
<div class="carousel-thumbnail-wrapper">
<div class="carousel-thumbnail mod-active">
<img src="https://via.placeholder.com/200" alt="">
</div>
<div class="carousel-thumbnail">
<img src="https://via.placeholder.com/200" alt="">
</div>
</div>
The following snippet will resize the images, to make them small enough to be able to fit on one row, until they are 120px in width, which is when they will no longer be shrunk, but moved to the next row. This achieves the same results as my other answer, but also works on older browsers, at the expense of having to use JS. My other answer only works on newer browsers but does not need any JS.
Here, I've used 3 imgs but you can use as many as you would like This is also responsive, so you can resize your browser and the images will still fit well.
document.querySelectorAll("img")[document.querySelectorAll("img").length-1].onload = re_calculate_image_width;
function re_calculate_image_width() {
let count = document.querySelectorAll(".img-wrapper").length;
document.querySelector(".outer-wrapper").style.setProperty("--how-many", (count).toString());
}
alert("Important: Read this: \n If you dynamically add more images, you MUST call the ***re_calculate_image_width*** function when the added images have finished ***loading***.");
.outer-wrapper {
width: 100%;
background: rgba(255,255,0,0.6);
--how-many: 5;
}
.img-wrapper {
display: inline-block;
min-width: 120px;
max-width: 200px;
width: calc(90% / var(--how-many));
height: auto;
}
img {
display: inline-block;
margin: 0px;
padding: 0px;
height: 100%;
width: 100%;
box-shadow: 0px 0px 1px 1px #000;
}
<div class="outer-wrapper">
<div class="img-wrapper">
<img src="https://via.placeholder.com/200" alt="">
</div>
<div class="img-wrapper">
<img src="https://via.placeholder.com/200" alt="">
</div>
<div class="img-wrapper">
<img src="https://via.placeholder.com/200" alt="">
</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;

How to center grid containing repeat(auto-fit, minmax(15rem, 1fr));

I am finishing my portfolio site, but I encountered a problem. I used auto-fit function, because I want it to be responsive, and I will keep adding new images there each month.
I found that I can't center it the way I want, because grid is filling empty space with additional columns which I don't need right now, so my grid is kissing left side of my website. Please help guys, you are the best! :)
<section class="projects">
<h2 class="h2">Projects</h2>
<div class="projects__part">
<h4 class="h4">Primary projects</h4>
<div class="projects__item">
<img src="https://hlfppt.org/wp-content/uploads/2017/04/placeholder.png" class="projects-item__img">
</div>
<div class="projects__item">
<img src="https://hlfppt.org/wp-content/uploads/2017/04/placeholder.png" class="projects-item__img">
</div>
<div class="projects__item">
<img src="https://hlfppt.org/wp-content/uploads/2017/04/placeholder.png" class="projects-item__img">
</div>
</div>
<div class="projects__part">
<h4 class="h4">Secondary projects</h4>
<div class="projects__item">
<img src="https://hlfppt.org/wp-content/uploads/2017/04/placeholder.png" class="projects-item__img">
</div>
<div class="projects__item">
<img src="https://hlfppt.org/wp-content/uploads/2017/04/placeholder.png" class="projects-item__img">
</div>
<div class="projects__item">
<img src="https://hlfppt.org/wp-content/uploads/2017/04/placeholder.png" class="projects-item__img">
</div>
</div>
</section>
.projects {
&__part {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr));
/*DESIRED RESPONSIVE EFFECT:
h4 {
margin-left: 22rem;
}
margin-left: 35rem;
*/
}
&-item {
&__img {
width: 14rem;
height: 14rem;
border-radius: 100%/100%;
}
}
h4 {
grid-column: 1 / -1;
}
}
Here's my code : https://codepen.io/maja5252/pen/XPPaBL?editors=1100#0
Again sorry if format of this post is not correct. Still don't know how to split my code to HTML and CSS part :)
A couple issues with your code:
-CSS grid likes to control the size of its columns, and you've got fixed height/width on your images. If you do a DevTools inspection of your code, you'll see your 3rd column is overflowing out of your grid because of this. I changed the height/width to 100%/100%. If you need square images, fix that issue in Photoshop not CSS.
-Centering the entire grid is pretty simple, all you need to do is add margin: 0 auto because it's a block-level element. But, a block level element takes up 100% width of the viewport, so centering a 100% width element won't produce any visible effect. I put the width at 80% to demonstrate how this might look centered, but you could use any non-100% value for this effect.
-I'm also not sure what you're doing with auto-fit in your grid. Just use 1fr and it works really well and the code is clean.
-I also made the whole grid responsive for you - not sure you want to do that, but might as well showcase it.
All the corrected CSS issues together:
.projects {
margin: 0 25px;
#media (min-width: 450px) {
width: 80%;
margin: 0 auto;
}
&__part {
display: grid;
grid-gap: 25px;
grid-template-columns: 1fr;
#media (min-width: 450px) and (max-width: 768px) {
grid-template-columns: 1fr 1fr;
}
#media (min-width: 768px) {
grid-template-columns: 1fr 1fr 1fr;
}
}
&-item {
&__img {
width: 100%;
height: 100%;
border-radius: 50%;
}
}
h4 {
grid-column: 1 / -1;
}
}
Demo:
https://codepen.io/staypuftman/pen/qMMPwp
I found out, that my H4 headings were disrupting an auto-fit effect, so it worked like auto-fill effect. Code is edited - I pulled headings out of the grid, and it got centered.
to #staypuftman - thanks for your advices - I'll keep them in mind in my future work. Now, when auto-fit works, you can see how responsive it is, without any media query. Again thanks for your effort to help me.