Center a list of elements in a page - html

I'd like to center a list of elements in a page without redimensioning the elements and at the same time, fit as much as possible in the same row.
I manage to do it with several #media rules for each screen size from 1 element per row up to 4 elements, but I'd like to know if there is a way to do it in a way that it would fit whatever the width of the page is.
Here is what I've tried and works:
.container {
display: flex;
width: max-content;
flex-wrap: wrap;
flex-direction: row;
margin-left: auto;
margin-right: auto;
}
#media only screen and (min-width: 294px) { .container {
width: 290px;
}}
#media only screen and (min-width: 584px) { .container {
width: 580px;
}}
#media only screen and (min-width: 874px) { .container {
width: 870px;
}}
#media only screen and (min-width: 1164px) { .container {
width: 1160px;
}}
.rectangle {
width: 286px;
height: 180px;
margin-right: 2px;
margin-left: 2px;
margin-bottom: 24px;
background: #aeaeae;
}
<div class="container">
<div class="rectangle"></div>
<div class="rectangle"></div>
<div class="rectangle"></div>
<div class="rectangle"></div>
<div class="rectangle"></div>
</div>

If I understood correctly you want to center all the items that fit on one line - then you can use justify-content to replace the media queries
DEMO
.container {
display: flex;
flex-wrap: wrap;
flex-direction: row;
margin-left: auto;
margin-right: auto;
justify-content: center;
}
.rectangle {
width: 286px;
height: 180px;
margin-right: 2px;
margin-left: 2px;
margin-bottom: 24px;
background: #aeaeae;
}

What I like to do when trying to accomplish this is setting text-align: center; on the container, (.container in your case). Then for the elements you're wanting to have be centered, setting the display property to display: inline-block; (.rectangle in your case). This will put as many elements that'll fit, on to one line. And anything that doesn't fit on to the next, still centered. When you resize your window, the elements move down to the next line as needed.
Change your css to this:
.rectangle {
width: 286px;
height: 180px;
margin-right: 2px;
margin-left: 2px;
margin-bottom: 24px;
background: #aeaeae;
display: inline-block
}
.container {
/*display: flex;
width: max-content;*/
flex-wrap: wrap;
flex-direction: row;
margin-left: auto;
margin-right: auto;
text-align: center;
}
You'll need to comment out the first two lines in .container for this to work properly. Hopefully this works for you!
EDIT: I thought originally you were wanting your elements to be centered. Now I'm not sure. If you're wanting elements that are on subsequent lines to be left aligned like in your snippet, change text-align: center; to text-align: left;.
.rectangle {
width: 286px;
height: 180px;
margin-right: 2px;
margin-left: 2px;
margin-bottom: 24px;
background: #aeaeae;
display: inline-block;
}
.container {
/*display: flex;
width: max-content;*/
flex-wrap: wrap;
flex-direction: row;
margin-left: auto;
margin-right: auto;
text-align: center;
}
<div class="container">
<div class="rectangle"></div>
<div class="rectangle"></div>
<div class="rectangle"></div>
<div class="rectangle"></div>
<div class="rectangle"></div>
</div>

.container {
display: flex;
width: max-content;
flex-wrap: wrap;
flex-direction: row;
margin-left: auto;
margin-right: auto;
}
#media only screen and (min-width: 294px) { .container {
width: 290px;
}}
#media only screen and (min-width: 584px) { .container {
width: 580px;
}}
#media only screen and (min-width: 874px) { .container {
width: 870px;
}}
#media only screen and (min-width: 1164px) { .container {
width: 1160px;
}}
.rectangle {
width: 286px;
height: 180px;
margin-right: 2px;
margin-left: 2px;
margin-bottom: 24px;
background: #aeaeae;
}
<div class="container">
<div class="rectangle"></div>
<div class="rectangle"></div>
<div class="rectangle"></div>
<div class="rectangle"></div>
<div class="rectangle"></div>
</div>

Yes, you can achieve it in various ways without targeting media queries. Here is one example:
lets wrap the .rectangle in another div
<div class="container">
<div>
<div class="rectangle"></div>
</div>
<div>
<div class="rectangle"></div>
</div>
<div>
<div class="rectangle"></div>
</div>
<div>
<div class="rectangle"></div>
</div>
<div>
<div class="rectangle"></div>
</div>
</div>
Declare 25% width for each rectangle holding div:
*{
box-sizing: border-box;
}
.container {
width: 100%;
margin-left: auto;
margin-right: auto;
}
.container > div{
width: 25%; float: left; padding-right: 5px; margin-bottom: 24px; height: 180px;
}
.container > div::nth-child(4){
padding-right: 0;
}
.rectangle{background: green; height: 180px; }
Here is the codepen

Related

Flip two horizontal tiles to vertical on mobile using CSS

I am new to responsive styling and couldnt find a solution to my problem elsewhere:
I have two horizontally aligned tiles on Desktop viewport. One is a div that contains text, the other is an image.
My code for this:
.tile-image{
width: 70%;
}
.tile-text{
width: 30%;
background-color: #d9b886;
color: white !important;
font-size: 2vh;
display: table;
word-wrap: break-word;
white-space: normal;
}
.tile-text-inner{
display: table-cell;
vertical-align: middle;
padding: 20px;
}
.container{
display: flex;
flex-direction: row;
}
<div class="container">
<div class="tile-text">
<div class="tile-text-inner">
TEXT
</div>
</div>
<div class="tile-image">
<img src="https://test-shop.tt-gmbh.de/media/a9/b8/4c/1638172501/13811-AdobeStock_294559939_New-Africa.jpg">
</div>
</div>
On mobile viewport I want to display both tiles vertically with the image on top and the text tile below. Like in the image below:
How can I achieve this?
You can use media queries
#media screen and (max-width: 600px) {
.container {
flex-direction: column-reverse;
}
.tile-text{width: 100vw}
}
.tile-image{
width: 70%;
}
.tile-text{
width: 30%;
background-color: #d9b886;
color: white !important;
font-size: 2vh;
display: table;
word-wrap: break-word;
white-space: normal;
}
.tile-text-inner{
display: table-cell;
vertical-align: middle;
padding: 20px;
}
.container{
display: flex;
flex-direction: row;
}
#media screen and (max-width: 600px) {
.container {
flex-direction: column-reverse;
}
.tile-text{width: 100vw}
}
<div class="container">
<div class="tile-text">
<div class="tile-text-inner">
TEXT
</div>
</div>
<div class="tile-image">
<img src="https://test-shop.tt-gmbh.de/media/a9/b8/4c/1638172501/13811-AdobeStock_294559939_New-Africa.jpg">
</div>
</div>
In comments they were saying truth you have to use flex-direction: column but it won't work because what you had written css isn't responsive friendly code so I re-created new one, use media-queries for responsive, and please avoid using a lot div wrapping it makes lots of complexity
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
display: flex;
position: relative;
width: 100%;
height: 100vh;
}
.container img {
width: 100%;
height: 100%;
position: absolute;
}
.container .tile-text{
display: flex;
justify-content: center;
align-items: center;
width: 30%;
z-index: 9;
height: 100%;
background: #d9b886;
}
.container .tile-text .tile-text-inner{
color: #FFF;
}
#media screen and (max-width: 761px){
.container{
flex-direction: column-reverse;
}
.container .tile-text{
width: 100%;
height: 30%;
}
}
<div class="container">
<div class="tile-text">
<div class="tile-text-inner">
Marry Christmas
</div>
</div>
<img src="https://test-shop.tt-gmbh.de/media/a9/b8/4c/1638172501/13811-AdobeStock_294559939_New-Africa.jpg">
</div>

How can i make my box of image responsive using HTML and CSS

I'm trying to build a simple HTML and CSS card, I have a container that contains box-image and body-card, and the design work perfectly on the desktop version but when I arrived at my breakpoint of media query my image it not shrinking with the container, I don't want the image to pass the container when I change the width of the viewport. I'm looking for the best practice :D
HTML
<section class="card">
<div class="card-container">
<div class="box-img">
<img src="./images/drawers.jpg" alt="decor"/>
</div>
<div class="body-card">
<h1> Shift the overall look and feel by adding these wonderful
touches to furniture in your home</h1>
<p>Ever been in a room and felt like something was missing? Perhaps
it felt slightly bare and uninviting. I’ve got some simple tips
to help you make any room feel complete.</p>
<div class="author">
<img src="./images/avatar-michelle.jpg" alt="">
<div class="author-info">
<h4>Michelle Appleton</h4>
<small>20 April 2021</small>
</div>
</div>
</div>
</div>
</section>
CSS
body {
font-family: "Manrope", sans-serif;
margin: auto;
background-color: hsl(210, 46%, 95%);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.card-container {
position: relative;
display: flex;
justify-content: center;
align-items: center;
max-width: 1140px;
width: 95vw;
background-color: #fff;
border-radius: 15px;
}
.box-img {
position: relative;
width: 540px;
height: 442px;
}
.box-img img {
height: 100%;
width: 100%;
object-fit: cover;
overflow: hidden;
border-radius: 15px 0px 0px 15px;
}
.body-card {
position: relative;
padding: 2rem 4rem 2rem 3rem;
max-width: 600px;
}
MediaQuery
#media (max-width: 992px) {
.card-container {
flex-direction: column-reverse;
width: 65vw;
}
.box-img {
width: 602px;
height: 308px;
}
}
This is the problem
Just set your width to 100%?
body {
font-family: "Manrope", sans-serif;
margin: auto;
background-color: hsl(210, 46%, 95%);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.card-container {
position: relative;
display: flex;
justify-content: center;
align-items: center;
max-width: 1140px;
width: 95vw;
background-color: #fff;
border-radius: 15px;
}
.box-img {
position: relative;
width: 540px;
height: 442px;
}
.box-img img {
height: 100%;
width: 100%;
object-fit: cover;
overflow: hidden;
border-radius: 15px 0px 0px 15px;
}
.body-card {
position: relative;
padding: 2rem 4rem 2rem 3rem;
max-width: 600px;
}
#media (max-width: 992px) {
.card-container {
flex-direction: column-reverse;
width: 65vw;
}
.box-img {
width: 100%;
}
}
<section class="card">
<div class="card-container">
<div class="box-img">
<img src="https://images.unsplash.com/photo-1531171596281-8b5d26917d8b?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1500&q=80" alt="decor"/>
</div>
<div class="body-card">
<h1> Shift the overall look and feel by adding these wonderful
touches to furniture in your home</h1>
<p>Ever been in a room and felt like something was missing? Perhaps
it felt slightly bare and uninviting. I’ve got some simple tips
to help you make any room feel complete.</p>
<div class="author">
<img src="./images/avatar-michelle.jpg" alt="">
<div class="author-info">
<h4>Michelle Appleton</h4>
<small>20 April 2021</small>
</div>
</div>
</div>
</div>
</section>
If you want the image to have the same size as the card then use width:100%;. This will change the width of the image to the width of the container.
What you have to change is written below ->
#media (max-width: 992px) {
.card-container {
flex-direction: column-reverse;
width: 65vw;
}
.box-img {
width: 602px; <--- Remove
width:100%; <---- Add
height: 308px;
}
}
You're almost there, but you need to make a few small changes:
your section really ought to have an explicit display declaration
you could give section a display: block but it makes more sense simply to move display: flex; justify-content: center; align-items: center; down from body to section
in a couple of places you've used width for elements with a flex-parent - but the whole idea is that these flex-children elements should have flexible dimensions, so use flex: [expand tendency] [shrink tendency] [start-width] instead
Working Example:
N.B. Use the Full Page link to see the full effect
body {
font-family: "Manrope", sans-serif;
margin: auto;
background-color: hsl(210, 46%, 95%);
min-height: 100vh;
}
section.card {
display: flex;
justify-content: center;
align-items: center;
}
.card-container {
position: relative;
display: flex;
justify-content: center;
align-items: center;
background-color: #fff;
border-radius: 15px;
}
.box-img {
position: relative;
display: block;
flex: 0 1 540px;
max-height: 442px;
}
.box-img img {
height: 100%;
width: 100%;
object-fit: cover;
overflow: hidden;
border-radius: 15px 0px 0px 15px;
}
.body-card {
position: relative;
padding: 2rem 4rem 2rem 3rem;
max-width: 600px;
}
#media (max-width: 992px) {
.card-container {
flex-direction: column-reverse;
width: 65vw;
}
.box-img {
flex: 0 1 602px;
max-height: 308px;
}
}
<section class="card">
<div class="card-container">
<div class="box-img">
<img src="https://picsum.photos/540/442" alt="decor"/>
</div>
<div class="body-card">
<h1> Shift the overall look and feel by adding these wonderful
touches to furniture in your home</h1>
<p>Ever been in a room and felt like something was missing? Perhaps
it felt slightly bare and uninviting. I’ve got some simple tips
to help you make any room feel complete.</p>
<div class="author">
<img src="./images/avatar-michelle.jpg" alt="">
<div class="author-info">
<h4>Michelle Appleton</h4>
<small>20 April 2021</small>
</div>
</div>
</div>
</div>
</section>
Further Reading:
https://developer.mozilla.org/en-US/docs/Web/CSS/flex
https://css-tricks.com/snippets/css/a-guide-to-flexbox/

How to make image resize with media query?

How would I make it so the image resizes properly when viewed on smaller screens. Right now, the image is over the container when viewed on smaller screens. There are also small gaps between the top/left of the container and the image. Would I have to resize the image in the media query or expand the width of my container in the media query?
.container {
width: 88%;
margin: 50px auto;
margin-top: 0px;
}
.heading {
text-align: center;
font-size: 30px;
margin-bottom: 50px;
}
.row {
display: flex;
flex-direction: row;
justify-content: space-around;
flex-flow: wrap;
}
.card {
width: 30%;
background: #fff;
border: 1px solid #ccc;
margin-bottom: 50px;
}
.image {
width: fit-content;
height: fit-content;
}
.card-body {
padding: 30px 10px;
text-align: left;
font-size: 18px;
}
.card-body .btn {
display: block;
color: #fff;
text-align: center;
background: black;
margin-top: 30px;
text-decoration: none;
padding: 10px 5px;
}
#media screen and (max-width: 1000px) {
.card {
width: 40%;
}
.heading {
text-align: auto;
}
.card-header {
margin: auto;
}
.image {
width: fit-content;
height: fit-content;
}
}
#media screen and (max-width: 620px) {
.container {
width: 100%;
}
.heading {
padding: 20px;
font-size: 20px;
text-align: auto;
}
.card {
width: 80%;
}
.image {
width: fit-content;
height: fit-content;
}
}
<div class="container">
<div class="heading">
<h1>Latest Posts</h1>
</div>
<div class="row">
<div class="card">
<div class="image">
<img src="https://via.placeholder.com/1920x1080.jpg">
</div>
<div class="card-body">
<p>
Text Here
</p>
Read more
</div>
</div>
I typically put width: 100%; on images in my projects and height: auto this way the image will be more responsive and scale up and down. You can reduce the width for the smaller media query if you want an even smaller image (width: 85%; for example) or I would probably personally end up reducing the width of the container to get the desired result.
1st: Remove your CSS for the class .image
2nd: Add this CSS line to the base-css (not within the media queries):
img {
object-fit: contain;
width: 100%;
}
What will that do?
object-fit: contain will keep the image aspect ratio while width: 100% will cause the image to fit exactly the given space. The height is set automatically according to the width while it maintain the image aspect ratio as mentioned above.
.container {
width: 88%;
margin: 50px auto;
margin-top: 0px;
}
.heading {
text-align: center;
font-size: 30px;
margin-bottom: 50px;
}
.row {
display: flex;
flex-direction: row;
justify-content: space-around;
flex-flow: wrap;
}
.card {
width: 30%;
background: #fff;
border: 1px solid #ccc;
margin-bottom: 50px;
}
.image {
width: fit-content;
height: fit-content;
}
.card-body {
padding: 30px 10px;
text-align: left;
font-size: 18px;
}
.card-body .btn {
display: block;
color: #fff;
text-align: center;
background: black;
margin-top: 30px;
text-decoration: none;
padding: 10px 5px;
}
img {
object-fit: contain;
width: 100%;
}
#media screen and (max-width: 1000px) {
.card {
width: 40%;
}
.heading {
text-align: auto;
}
.card-header {
margin: auto;
}
}
#media screen and (max-width: 620px) {
.container {
width: 100%;
}
.heading {
padding: 20px;
font-size: 20px;
text-align: auto;
}
.card {
width: 80%;
}
}
<div class="container">
<div class="heading">
<h1>Latest Posts</h1>
</div>
<div class="row">
<div class="card">
<div class="image">
<img src="https://via.placeholder.com/1920x1080.jpg">
</div>
<div class="card-body">
<p>
Text Here
</p>
Read more
</div>
</div>

How to set margin of item in flexbox?

I want to set the margin of items in a flexbox to 2%.
But if I set each margin to 2%, they don't have same length visually.
I want to set distance to 2% between each item.
Also, i wanna set 2% distance from border of flexbox.
#wrap {
width: 800px;
}
#left {
width: 550px;
height: inherit;
float: left;
}
#flexbox {
display: flex;
flex-direction: column;
flex-wrap: wrap;
margin-left: 2%;
width: 100%;
overflow: hidden;
height: 500px;
}
.item {
width: 47%;
height: 200px;
margin-right: 2%;
}
<div id="wrap">
<div id="left">
<div id="flexbox">
<div class="item">div1</div>
<div class="item">div2</div>
<div class="item">div3</div>
<div class="item">div4</div>
</div>
</div>
</div>
The way I do it is using nested div and apply the margin within those divs, this ways you can have more controls over margin. Since you are using flexbox, you can use flex-basis and flex-grow to distribute your divs instead of using width and height.
You can try this:
*{
margin:0;
padding:0;
}
#wrap {
width: 800px;
background:#ece8e8;
overflow:hidden;
}
#left {
width: 550px;
height: inherit;
float: left;
background:#b8b8b8;
}
#flexbox {
display: flex;
flex-direction: column;
flex-wrap: wrap;
/*margin-left: 2%;*/
width: 100%;
overflow: hidden;
height: 500px;
}
.item {
/*width: 47%;*/
/*height: 200px;*/
/*margin-right: 2%;*/
background: grey;
flex-basis: 50%;
flex-grow: 1;
}
.item>div {
background: #454444;
margin: 2%;
height: 100%;
}
.item:nth-child(1)>div,
.item:nth-child(2)>div{
margin-right: 0%;
}
<div id="wrap">
<div id="left">
<div id="flexbox">
<div class="item"><div>div1</div></div>
<div class="item"><div>div2</div></div>
<div class="item"><div>div3</div></div>
<div class="item"><div>div4</div></div>
</div>
</div>
</div>

align two divs side by side with floating (responsive)

how can I make this container responsive so the text and the img automatically become block elements. I tried it out with flex direction but for someway it doesnt work. Can someone correct my code if necessary and suggest me a media query rule for the responsive design
<div class="top">
<h1>Welcome</h1>
<div class="portrait">
<img src="https://pixy.org/images/placeholder.png" alt="">
<h2>XXXXXXXXXX</h2>
</div>
</div>
.top h1{
display: flex;
align-items: center;
justify-content: center;
flex-grow: 1;
background-color: black;
height: 20vw;
margin-top: 0;
font-size: 5vw;
color: white;
text-shadow: 5px 5px rgb(142, 135, 136);
}
.top img {
width: 20vw;
}
thanks in advance
I think this is what you are after. display: flex; is very powerful property and useful when it comes to take up rest of the space and centering.
Modification
here is a demo, I would not suggest this approach with using max-width as it's not "mobile-first". But if this is what you want for this project then ok.
.container {
display: flex;
flex-direction: row;
background-color: deepskyblue;
}
#img {
width: 140px;
height: 140px;
}
#text {
display: flex;
align-items: center;
justify-content: center;
flex-grow: 1;
background-color: deeppink;
min-height: 100px;
}
#media screen and (max-width: 700px) {
.container {
flex-direction: column;
}
#img {
width: 100%;
height: auto;
}
}
.container {
display: flex;
background-color: deepskyblue;
}
#img {
width: 140px;
height: 140px;
}
#text {
display: flex;
align-items: center;
justify-content: center;
flex-grow: 1;
background-color: deeppink;
}
<div class="container">
<img id="img" src="https://www.archlinux.org/static/vector_tux.864e6cdcc23e.png" />
<div id="text">text on the left, next to the img</div>
</div>
Ok, well so here it is if I understood well what you are trying to accomplish. Correct me or update your question if I am wrong!
#img{
width: 200px;
height: 150px;
float: left;
}
#text{
overflow: hidden;
}
<div class="container">
<img id="img" src="https://www.archlinux.org/static/vector_tux.864e6cdcc23e.png"/>
<div id="text">text on the left, next to the img</div>
</div>