Float based grid system rendering outside viewport - html

I set out to create a float based grid system and have added gutters between columns via padding: 0 0.75em; applied to columns with margin-right: -0.75em; and margin-left: -0.75; applied to rows.
When I have a full width container it renders outside the width of the viewport with the horizontal scrollbarr showing.
I want the leftmost and the rightmost element to stick along the edges of the viewport, but still want to preserve the gutters between elements. I must be missing something. Any help would be appreciated, thank you!
Here's CSS followed by HTML:
.container-full {
width: 100%;
}
.row {
margin-right: -0.75em;
margin-left: -0.75em;
}
.row::before,
.row::after {
display: block;
content: "";
clear: both;
}
[class*="column-"] {
float: left;
padding-right: 0.75em;
padding-left: 0.75em;
}
<div class="container-full">
<div class="row">
<div class="column-3">
<div class="exhibition-brief">
<img src="./img/img01.jpg" alt="Image">
</div>
</div>
<div class="column-3">
<div class="exhibition-brief">
<img src="./img/img02.jpg" alt="Image">
</div>
</div>
<div class="column-3">
<div class="exhibition-brief">
<img src="./img/img03.jpg" alt="Image">
</div>
</div>
<div class="column-3">
<div class="exhibition-brief">
<img src="./img/img04.jpg" alt="Image">
</div>
</div>
</div>
</div>

You can achieve this via using CSS Grid and the grid-gap property.
<div class="container">
<img src="https://picsum.photos/1920/1080" alt="">
<img src="https://picsum.photos/1920/1081" alt="">
<img src="https://picsum.photos/1920/1082" alt="">
<img src="https://picsum.photos/1920/1083" alt="">
</div>
.container {
width: 100vw;
height: 100vh;
display: grid;
grid-template-columns: repeat(4,1fr);
grid-gap: 0.75rem;
}
img {
max-width: 100%;
height: 100%;
object-fit: cover;
}

Related

Responsive group of images not staying in the same line on phone view

so i kinda have two problems
i´m trying to make a responsive container of images that when the resolution changes the images stay side by side.
my problem is when i change the resolution to a phone when testing the images show one below the other and one
and one image's width is smaller so the image shows up a bit weird
here's what i tried
html
<div class="container-fluid">
<div class="row">
<div class="col-sm">
<img src="brands/Adidas-Logo.png" alt="" id="brand_img_adidas">
</div>
<div class="col-sm">
<img src="brands/Nike_logo.png" alt="" id="brand_img_nike">
</div>
<div class="col-sm">
<img src="brands/the-north-face-logo-png-8.png" alt="" id="brand_img_north">
</div>
<div class="col-sm">
<img src="brands/tommy-hilfiger-logo-png-transparent.png" alt="" id="brand_img_tommy">
</div>
<div class="col-sm">
<img src="brands/zara-logo-0.png" alt="" id="brand_img_zara">
</div>
</div>
</div>
css
#brand_img_adidas
{
width: 150px;
height: 100px;
max-width: 100%;
margin-left: 10px;
}
#brand_img_nike
{
width: 200px;
height: 100px;
margin-left: 20px;
max-width: 100%;
}
#brand_img_north
{
width: 150px;
height: 150px;
margin-left: 20px;
max-width: 100%;
}
#brand_img_tommy
{
width: 150px;
height: 120px;
margin-left: 20px;
max-width: 100%;
margin-right: 0;
}
#brand_img_zara
{
width: 150px;
height: 150px;
margin-left: 10px;
margin-right: 0;
max-width: 100%;
}
This is not using bootstrap but this should help you get an idea about using flexbox.
So, to stack the images next to each other, we can use display: flex on the parent container.
By default, the flex-direction will be row which means that the child elements will be placed next to each other.
We can also provide flex-wrap: wrap property to make sure that if the elements overflow, it will be moved to next row.
You can read more about flexbox here
.row {
display: flex;
flex-wrap: wrap;
}
.row .col {
margin-right: 5px;
}
<div class="row">
<div class="col">
<img src="https://via.placeholder.com/150" alt="" id="brand_img_adidas">
</div>
<div class="col">
<img src="https://via.placeholder.com/150" alt="" id="brand_img_2">
</div>
<div class="col">
<img src="https://via.placeholder.com/150" alt="" id="brand_img_3">
</div>
<div class="col">
<img src="https://via.placeholder.com/150" alt="" id="brand_img_4">
</div>
<div class="col">
<img src="https://via.placeholder.com/150" alt="" id="brand_img_5">
</div>
</div>

Organize responsive images on desktop and mobile layouts

I'm trying to align two images side by side - pair after pair down by landing page on wide screen and single image - one after another on mobile screen layout:
<div class="container">
<div class="set">
<div class="column">
<img src="" id="img3" class="images">
</div>
<div class="column">
<img src="" id="img4" class="images">
</div>
<div class="column">
<img src="" id="img5" class="images">
</div>
<div class="column">
<img src="" id="img6" class="images">
</div>
</div>
</div>
container css:
.container {
height: relative;
padding: 0px 12px;
margin-top:0px;
margin-bottom:0px;
border-radius: 0px;
background-color: transparent;
}
set and column css:
* {
box-sizing: border-box;
}
.set {
display: flex;
}
.column {
flex: 33.33%;
padding: 5px;
}
images css:
.images {
position: relative;
width: 100%;
height: auto;
padding: 23px;
}
Actual result is a scalable images in one line with both screen layouts, left is a wide screen, right is mobile:
but I'm trying to get this result:
You just need to give one more parameter in your css file
#media screen and (max-width:768px){
.set{display:block}
}

Fill width of a div containing a horizontal array of images with css

I have a container div with multiple varying-width, uniform-height images. The images are horizontally arranged with no gaps between them.
What I have so far:
#imageContainer {
width: 80%;
border: 1px solid blue;
position: relative;
left: 50%;
transform: translateX(-50%);
}
#images {
display: flex;
width: 80%;
position: relative !important;
margin: 2px;
}
#imageContainer img {
display: inline-block;
position: relative;
margin: 1px;
}
<div id="imageContainer">
<div id="images">
<img src="https://dummyimage.com/400x100/000/fff" alt="">
<img src="https://dummyimage.com/320x100/000/fff" alt="">
<img src="https://dummyimage.com/420x100/000/fff" alt="">
<img src="https://dummyimage.com/540x100/000/fff" alt="">
</div>
</div>
I would like for all of the images to scale to fit within my div, filling it, but I can't seem to find where I can do this with multiple images.
I question is basically this one but with a twist of also scaling to fit.
Unless you want a gap in your box you should remove width/position from the inner #images container. To make the images scale down you can add a min-width. If you want the images to not distort add an object-fit:
#imageContainer {
width: 80%;
border: 1px solid blue;
position: relative;
left: 50%;
transform: translateX(-50%);
}
#images {
display: flex;
margin: 2px;
}
#imageContainer img {
display: inline-block;
position: relative;
margin: 1px;
/* Important bits: */
min-width: 0;
object-fit: contain;
}
<div id="imageContainer">
<div id="images">
<img src="https://dummyimage.com/400x100/000/fff" alt="">
<img src="https://dummyimage.com/320x100/000/fff" alt="">
<img src="https://dummyimage.com/420x100/000/fff" alt="">
<img src="https://dummyimage.com/540x100/000/fff" alt="">
</div>
</div>
(Btw, centering using left + transform can cause blurriness in some rendering engines.)
I prefer using Bootstrap for most of my front-end styling as they provide lots of nifty "features" that make it easy for me to create a nice looking front-end without having to spend hours with CSS trying to get it looking perfect or making it scalable to multiple viewports.
Hence, here is how I would solve it using Bootstrap-4. (Here is a Codepen link)
<div class="container">
<div class="row outline-primary">
<div class="col">
<img src="https://dummyimage.com/400x100/000/fff" class="img-fluid h-100">
</div>
<div class="col">
<img src="https://dummyimage.com/320x100/000/fff" class="img-fluid h-100">
</div>
<div class="col">
<img src="https://dummyimage.com/420x100/000/fff" class="img-fluid h-100">
</div>
<div class="col">
<img src="https://dummyimage.com/540x100/000/fff" class="img-fluid h-100">
</div>
</div>
</div>

How to resize multiple images side by side to to fit the current screen size?

I have a div with 5 rows and with multiple images in it. The Html looks something like this:
<div id="row3" class="row">
<div id="im15" class="column">
<img src="img/img15.jpg" alt="movie">
</div>
<div id="im16" class="column">
<img src="img/img16.jpg" alt="movie">
</div>
<div id="im17" class="column">
<img src="img/img17.jpg" alt="movie">
</div>
<div id="im18" class="column">
<img src="img/img18.jpg" alt="movie">
</div>
<div id="im19" class="column">
<img src="img/img19.jpg" alt="movie">
</div>
<div id="im20" class="column">
<img src="img/img20.jpg" alt="movie">
</div>
<div id="im21" class="column">
<img src="img/img21.jpg" alt="movie">
</div>
</div>
I have them side by side and my css looks like this:
html, body {
background-color: #222222;
overflow: hidden;
}
.column {
float: left;
}
.column img {
width: 200px;
height: 300px;
padding: 1px;
}
.images {
opacity: 0.4;
}
.row {
clear: both;
display: table;
}
It works and displays all images properly stacked with no left space in my screen size laptops, but for bigger screens this fails.
I am not able to understand how can I resize these many images to fit and leave no space for any screen size.
I would appreciate any help.
Try to use grid system.
grid-template-columns defines how many columns you want to divide, in the flowing example I divide 3 columns with the value 1fr 1fr 1fr.
grid-gap defines the gaps between each grids.
More information about css grid system, you can refer to this Youtube video:
https://www.youtube.com/watch?v=jV8B24rSN5o&t=1325s&frags=pl%2Cwn
It's very useful when doing some layouts with CSS.
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-gap: 1em;
}
.box {
border: 1px solid teal;
}
.box img {
width: 100%;
height: 100%;
}
<div class="container">
<div class="box">
<img src="" alt="">
</div>
<div class="box">
<img src="" alt="">
</div>
<div class="box">
<img src="" alt="">
</div>
<div class="box">
<img src="" alt="">
</div>
<div class="box">
<img src="" alt="">
</div>
<div class="box">
<img src="" alt="">
</div>
<div class="box">
<img src="" alt="">
</div>
</div>
Note : This is code for 3 images side by side horizontally
* {
box-sizing: border-box;
}
.column {
float: left;
width: 33.33%;
padding: 5px;
}
.row::after {
content: "";
clear: both;
display: table;
}

centering pictures HTML

I try to center pictures in the container. I've set left and right margin to 0 and still something is not working right.
#navbut {
width: 100%;
background: red;
margin: -7px 0 0 0;
color: white;
}
.container .box {
display: inline-block;
width: 10%;
margin-left: auto;
margin-right: auto;
}
.box img.Newspaper_pic {
width: 50%;
margin-left: auto;
margin-right: auto;
}
<section id="navbut">
<div class="container">
<div class="box">
<img src="http://placehold.it/100x100" alt="News-pic" class="Newspaper_pic">
</div>
<div class="box">
<img src="http://placehold.it/100x100" alt="News-pic" class="Newspaper_pic">
</div>
<div class="box">
<img src="http://placehold.it/100x100" alt="News-pic" class="Newspaper_pic">
</div>
</div>
</section>
What I am doing wrong that I cannot center pictures in one line?
If your images are set to inline-block, you have to use
text-align:center;
. If your images are set to block,
margin: 0 auto;
will work.
Looks like your pictures are centered, but only within the .box divs, you have to center those .box divs in the .container aswell. The .container also needs to be width 100% so it spans over the whole #navbut.
Try to use flex. flex is very easy to make for these kind of layouts because of these alignment properties.
Reference Link flex
Stack Snippet
body {
margin: 0;
}
.container, .box {
display: flex;
justify-content: center;
}
.Newspaper_pic {
display: block;
max-width: 100%;
}
<section id="navbut">
<div class="container">
<div class="box">
<img src="http://lorempixel.com/400/200/sports" alt="News-pic" class="Newspaper_pic">
</div>
<div class="box">
<img src="http://lorempixel.com/400/200/sports" alt="News-pic" class="Newspaper_pic">
</div>
<div class="box">
<img src="http://lorempixel.com/400/200/sports" alt="News-pic" class="Newspaper_pic">
</div>
</div>
</section>
Put whatever you want inside the center tags, e.g:
<center>
<img src="" alt="">
</center>