How to center images in div? - html

How to make these images stay in center? Here are my css and html codes.
My html code:
<div id="logos">
<div id="q">
<img id="round" src="img/i1.jpg" />
<img id="round" src="img/i1.jpg" />
<img id="round" src="img/i1.jpg" />
</div>
</div>
My css code:
#logos {
display: inline-block;
width:100%;
}
#q{
display: block;
}
#round {
border-radius: 50%;
display: inline;
margin: 0 5px;
width: 150;
height: 150;
position: cetner;
}

#image{
text-align:center;
}
#image img{
margin:0 auto;
}
<div class="image">
<img src="path_to_your_image" alt="">
</div>

Use text-align: center on the parent...
#q{
display: block;
text-align: center;
}
.round {
border-radius: 50%;
display: inline;
margin: 0 5px;
width: 150px;
height: 150px;
}
<div id="logos">
<div id="q">
<img class="round" src="http://placehold.it/150x150" />
<img class="round" src="http://placehold.it/150x150" />
<img class="round" src="http://placehold.it/150x150" />
</div>
</div>
Also, ID's must be unique. rounded should be a class not an ID.
Secondly, position: center; doesn't exist in CSS.
And finally, width: 150 and height: 150 must have a unit of measurement (probably px) though this will have no effect because the elements are inline - perhaps you meant inline-block?

Add to #round css:
position: relative;
display: block;
margin: auto;
width: 150px; /*units are needed */
height: 150px; /*units are needed */

The Code
<div class="flex-align">
<img class="round" src="http://placehold.it/150x150" />
<img class="round" src="http://placehold.it/150x150" />
<img class="round" src="http://placehold.it/150x150" />
</div>
The CSS
.flex-align {
display: flex;
align-items: center;
justify-content: center;
}

Related

Flexbox square items independent of item count

I use some inline styling in the HTML Doc. I would like to achieve a flexbox with n divisions where divs are squared. Within these divs I want to add certain images (here a placeholder)
I was looking up some other threads where there was padding used as a measure to adjust the box "height" since it is calculated upon width. However this solution only expands the current box in height (outlined with the blue border).
Has anyone a tip on how to avoid this?
EDIT: Apparently the padding solution works while using units like vh and vw instead of percentage and as long as I do not insert an image
.container {
position: relative;
width: 90%;
max-height: 35%;
display: flex;
margin: 5%;
border: 5px solid black;
justify-content: space-around;
}
.box {
position: relative;
width: 100%;
margin: 2.5%;
border: 5px solid blue;
overflow: hidden;
}
.image {
position: relative;
width: 100%;
}
<div class="container">
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80">
</div>
</div>
We now have access to aspect-ratio in CSS although it is poorly supported at the time of writing.
The aspect-ratio CSS property sets a preferred aspect ratio for the box, which will be used in the calculation of auto sizes and some other layout functions.
.container {
width: 90%;
max-height: 35%;
display: flex;
margin: 5%;
border: 5px solid black;
justify-content: space-around;
}
.box {
margin: 2.5%;
flex: 1;
aspect-ratio: 1;
border: 5px solid blue;
overflow: hidden;
}
.image {
max-width: 100%;
display: block;
}
<div class="container">
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80">
</div>
</div>
To manage this, there is a little known trick (tbh I didn't know you could do this) with setting an aspect ratio on divs of unknown/dynamic widths. See this article.
I ended up adding position: absolute for the images to not mess with the height of the divs after applying this 1:1 ratio for your scenario:
.box:before {
content: "";
float: left;
padding-top: 100%; /* initial ratio of 1:1*/
}
This might be what you are trying to do:
.container {
position: relative;
width: 90%;
max-height: 35%;
display: flex;
margin: 5%;
border: 5px solid black;
justify-content: space-around;
}
.box {
position: relative;
width: 100%;
margin: 2.5%;
border: 5px solid blue;
overflow: hidden;
}
.box:before {
content: "";
float: left;
padding-top: 100%; /* initial ratio of 1:1*/
}
.image {
position: absolute;
width: 100%;
}
<div class="container">
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80" />
</div>
<div class="box">
<img class="image" src="https://images.unsplash.com/photo-1611817757571-75fe5c08ffd9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80">
</div>
</div>

How to align images horizontally side by side with captions underneath?

I want my three images to align side by side horizontally with captions under each of them, like in this link:(http://www.renaldi.com/projects/)
I tried to do them but failed. Here are my codes:
.photos {
display: flex;
justify-content: center;
}
.image {
display: block;
width: 30%;
}
.word {
display: block;
width: 100%;
text-align: center;
}
<div class="photos">
<img class="image" src="Project1.png">
<span class="word">Manhattan Sunday</span>
</div>
<div class="photos">
<img class="image" src="Project2.png">
<div class="word">Touching Strangers</div>
</div>
<div class="photos">
<img class="image" src="Project3.png">
<div class="word">I want your love</div>
</div>
How to fix them?
You can achieve this by adding a wrapper container, and applying some flexbox css to it. Setting the container to have a flex direction of row is what aligns them horizontally, and setting the flexdirection to column on the photos class is what places the caption beneath the image. For more info on flexbox see https://css-tricks.com/snippets/css/a-guide-to-flexbox/
HTML
<div class="container">
<div class="photos">
<img class="image" src="Project1.png" >
<span class="word">Manhattan Sunday</span>
</div>
<div class="photos">
<img class="image" src="Project2.png" >
<div class="word">Touching Strangers</div>
</div>
<div class="photos">
<img class="image" src="Project3.png" >
<div class="word">I want your love</div>
</div>
</div>
CSS
.container {
display: flex;
flex-direction: row;
flex-grow: 1;
}
.photos{
display:flex;
justify-content:center;
flex-direction: column;
flex-grow: 1;
}
.image{
display:block;
width:30%;
}
.word{
display:block;
width: 100%;
text-align:center;
}
Working example: https://jsfiddle.net/Matthew_/ro1kahj0/2/
You can use figure and figcaption to answer your caption question. It adds accessibility to your website.
<figure>
<img src="project1.png" alt="A picture of Manhattan">
<figcaption>Manhattan Sunday</figcaption>
</figure>
a parent container with flex direction row
flex direction column for each photos child
align items center to center image (thanks #Matthew)
added background color so you can see the extent of each element
.photos {
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
background: #fafafa;
}
.image {
display: block;
width: 30%;
}
.word {
display: block;
width: 100%;
text-align: center;
background: #f0f0f0;
}
.photo-list {
display: flex;
flex-direction: row;
justify-content: space-evenly;
}
<div class="photo-list">
<div class="photos">
<img class="image" src="https://picsum.photos/200/300?image=1">
<span class="word">Manhattan Sunday</span>
</div>
<div class="photos">
<img class="image" src="https://picsum.photos/200/300?image=2">
<div class="word">Touching Strangers</div>
</div>
<div class="photos">
<img class="image" src="https://picsum.photos/200/300?image=3">
<div class="word">I want your love</div>
</div>
</div>
I have been using CSS grid for these types of layouts:
CSS:
.threeColumnGrid {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-column-gap: 10px;
grid-row-gap: 10px;
}
figure.threeColumnGridItem {
display: block;
padding: 10px;
text-align: left;
background-color: #EEE;
margin-inline-start: 0;
margin-inline-end: 0;
}
figure.threeColumnGridItem img {
display: block;
width: 100%;
}
figure.threeColumnGridItem figurecaption {
display:block;
width: 100%;
font-size: 1rem;
margin: 0;
}
HTML:
<div class="threeColumnGrid">
<figure class="threeColumnGridItem">
<img src="http://placekitten.com/g/300/300" alt="" />
<figurecaption>Caption</figurecaption>
</figure>
<figure class="threeColumnGridItem">
<img src="http://placekitten.com/g/400/400" alt="" />
<figurecaption>Caption</figurecaption>
</figure>
<figure class="threeColumnGridItem">
<img src="http://placekitten.com/g/600/600" alt="" />
<figurecaption>Caption</figurecaption>
</figure>
</div>

How can I center and left align images?

I am working on an image gallery and want have the image's container be completely centered on the page, but the images are left aligned.
This is my desired output:
However, when I try to do a text-align: center on the container(id: gallery) I am getting the images displayed like this:
I tried following suit with a previous stack overflow question: CSS: Center block, but align contents to the left
and wrap the images in another div then align it with display: inline-block; and text-align: left; but the images just seem to align left on the entire page:
What can I do to accomplish my desired output?
HTML
<div id="gallery">
<div id="images">
<div class="container">
<a href="images/gallery/image1.jpg" data-lightbox="mygallery">
<img src="images/gallery/image1.jpg">
<div class="overlay">
<img src="images/magnify.png">
</div>
</a>
</div>
<div class="container">
<a href="images/gallery/image2.jpg" data-lightbox="mygallery">
<img src="images/gallery/image2.jpg">
<div class="overlay">
<img src="images/magnify.png">
</div>
</a>
</div>
</div>
</div>
CSS
#gallery{
text-align: center;
}
#images{
display: inline-block;
text-align: left;
}
img{
width: 300px;
cursor: pointer;
}
.overlay {
position: absolute;
right: 0;
left: 0;
cursor: pointer;
visibility: hidden;
color: transparent;
top: 0;
bottom: 0;
width: 100%;
height: 100%;
transition: all ease-in .3s;
}
.overlay > img{
height: 50%;
width: 50%;
top: 50%;
visibility: hidden;
left: 50%;
transform: translate(-50%,-50%);
position: absolute;
}
.overlay:hover > img{
visibility: visible;
}
.container {
position: relative;
display: inline-block;
margin: 5px;
}
.container:hover .overlay {
visibility: visible;
opacity: .6;
background: black;
color: white;
}
How about styling the image wrapper .images like
.images {
width:80%;
margin:0 auto;
text-align:left;
}
this works
body{
display: flex;
flex-flow: row nowrap;
justify-content: center;
align-content: center;
align-items: center;
}
section{
height:400px;
width:400px;
background:grey;
}
img {
margin:48px;
}
<section>
<img src="https://telecomputingarchitects.com/media/logo.png" height="24"/>
<img src="https://telecomputingarchitects.com/media/logo.png" height="24"/>
<img src="https://telecomputingarchitects.com/media/logo.png" height="24"/>
<img src="https://telecomputingarchitects.com/media/logo.png" height="24"/>
<img src="https://telecomputingarchitects.com/media/logo.png" height="24"/>
</section>
Give your #gallery div a max-width, text-align: center, and margin:auto, then put your header in another div inside the #gallery, but outside the #images. Then put text-align: left on your #images div.
See example below:
#gallery {
text-align: center;
max-width: 420px;
margin: auto;
}
img {
width: 100px;
cursor: pointer;
}
.container {
display: inline-block;
}
#images {
text-align: left
}
<div id="gallery">
<div id="header">
<h1>Header</h1>
</div>
<div id="images">
<div class="container">
<a href="http://thecatapi.com/api/images/get?id=d42">
<img src="http://thecatapi.com/api/images/get?id=d42">
</a>
</div>
<div class="container">
<a href="http://thecatapi.com/api/images/get?id=21o">
<img src="http://thecatapi.com/api/images/get?id=21o">
</a>
</div>
<div class="container">
<a href="http://thecatapi.com/api/images/get?id=49e">
<img src="http://thecatapi.com/api/images/get?id=49e">
</a>
</div>
<div class="container">
<a href="http://thecatapi.com/api/images/get?id=13v">
<img src="http://thecatapi.com/api/images/get?id=13v">
</a>
</div>
<div class="container">
<a href="http://thecatapi.com/api/images/get?id=6e6">
<img src="http://thecatapi.com/api/images/get?id=6e6">
</a>
</div>
<div class="container">
<a href="http://thecatapi.com/api/images/get?id=4bf">
<img src="http://thecatapi.com/api/images/get?id=4bf">
</a>
</div>
</div>
</div>
HTML
<h2>HEADER</h2>
<div class="container">
<img src=""/>
<img src=""/>
<img src=""/>
<img src=""/>
<img src=""/>
<img src=""/>
</div>
CSS
h2 {
text-align: center;
}
.container {
float: left;
}
img {
border: medium solid black;
width: 200px;
height: 350px;
margin: 5% 2%;
}

how do i move all images to the right in css

I'm currently running the following code:
HTML
<div style="img"/>
<img src="spiderman.png" alt=""/>
<img src="superman.png" alt="" height="25%" width="25%"/>
<img src="batman.png" alt="" />
</div>
CSS
div{
text-align: right;
}
div img {
display: list-item;
width: 100px;
height: 100px;
}
div:after {
content: '';
display: inline-block;
width: 100
}
I cant seem to get the images to remain in a vertical line and move to the right of the screen, any suggestions would be helpful
display: flex; flex-direction: column; align-items: flex-end; on the parent will put them in a column aligned to the right.
.img {
display: flex;
flex-direction: column;
align-items: flex-end;
}
.img img {
width: 100px;
height: 100px;
}
<div class="img">
<img src="spiderman.png" alt="">
<img src="superman.png" alt="" height="25%" width="25%">
<img src="batman.png" alt="">
</div>
You could also float them right, then clear: right so they're in a column.
.img {
overflow: auto;
}
.img img {
width: 100px;
height: 100px;
float: right;
clear: right;
}
<div class="img">
<img src="spiderman.png" alt="" />
<img src="superman.png" alt="" height="25%" width="25%" />
<img src="batman.png" alt="" />
</div>
You don't need to worry about aligning items to the right with text-align, changing their display, or using :after at all. Simply float the images to the right, and clear them as well :)
div img {
float: right;
clear: both;
width: 100px;
height: 100px;
}
<div>
<img src="http://placehold.it/100" alt="" />
<img src="http://placehold.it/100" alt="" />
<img src="http://placehold.it/100" alt="" />
</div>
Hope this helps! :)

Floating images are not at the same height on relative position

I need a set of icons grouped under a div to appear to the right of my logo (which is located on the top-left corner). I need the logo and all icons to have a position of relative. I am using float:left for both divs, but the second div (the group of icons) appears below the logo and not to its right. I have also tried grouping them under an unordered list and input style="float:left" as HTML, but it does not work either.
Here is the CSS:
div.container {
width: 1111px;
}
#parent {
display: flex;
}
.logo {
float: left;
position: relative;
top: 0px;
margin-left: 0px;
}
#icons {
position: relative;
float: left;
white-space: nowrap;
width: 100%;
display: table;
max-width: 100%;
}
.all-icons {
position: relative;
float: left;
vertical-align: middle;
padding: 15px;
display: table-cell;
}
.all-icons img {
height: auto;
width: auto;
max-height: 77px;
max-width: 100%;
margin: 15px;
}
<body>
<div class="container">
<div id="parent">
<div class="logo"> <img src="http://placehold.it/77" width="27%" height="27%"> </div>
<div id="icons" style="float:left" class="all-icons">
<img src="http://placehold.it/77" width="389" height="317">
<img src="http://placehold.it/77" width="451" height="317">
<img src="http://placehold.it/77" width="427" height="317">
<img src="http://placehold.it/77" width="837" height="317">
<img src="http://placehold.it/77" width="594" height="317">
</div>
</div>
</div>
</body>
Is this what you look for?
div.container {
/* width: 1111px; */
}
#parent {
display: flex;
}
.logo img {
margin: 15px;
}
.all-icons {
white-space: nowrap;
}
.all-icons img {
margin: 15px;
}
<div class="container">
<div id="parent">
<div class="logo">
<img src="http://placehold.it/77/f00" alt="">
</div>
<div id="icons" class="all-icons">
<img src="http://placehold.it/77" alt="">
<img src="http://placehold.it/77" alt="">
<img src="http://placehold.it/77" alt="">
<img src="http://placehold.it/77" alt="">
<img src="http://placehold.it/77" alt="">
</div>
</div>
</div>
If you can't use flex, try display: table
div.container {
/* width: 1111px; */
}
#parent {
display: table;
}
.logo {
display: table-cell;
}
.all-icons {
display: table-cell;
}
.logo img {
margin: 15px;
}
.all-icons {
white-space: nowrap;
}
.all-icons img {
margin: 15px;
}
<div class="container">
<div id="parent">
<div class="logo">
<img src="http://placehold.it/77/f00" alt="">
</div>
<div id="icons" class="all-icons">
<img src="http://placehold.it/77" alt="">
<img src="http://placehold.it/77" alt="">
<img src="http://placehold.it/77" alt="">
<img src="http://placehold.it/77" alt="">
<img src="http://placehold.it/77" alt="">
</div>
</div>
</div>
Add #icons { text-align: right; }, i think this will help you.
please do remove #parent { display: flex;} and also remove #icons { width: 100% }
It looks in line to me (see below image) however the margins/padding make it look like it's below and to the right.
Other things which may prove useful having seen your code:
display: inline-table
http://flexboxgrid.com/
remembering to keep CSS files as small as possible, not as comprehensive as possible to make it easy to find and fix things
Your image of logo class not in center of its parent. Add
text-align: center;
margin: auto;
into you .logo class