I know there are a lot of questions like this, and I have tried many of the methods suggested by those who have answered those questions, however, nothing works. I am beginning to think that my methods are not wrong, but that something else in my code is interfering? Such as my main div
If anyone could help me out I would really appreciate it. Currently, my images are just stacked on top of each other (vertically) on the left edge of the container...
This is the HTML:
<div class="main">
<!-- a bunch of <p> tags here I won't include -->
<div class="img123">
<img height="200" width="200" src="images/image1.jpg" alt="some text" class="images"/>
</div>
<div class="img123">
<img height="200" width="200" src="images/image2.jpg" alt="some text" class="images"/>
</div>
<div class="img123">
<img height="200" width="200" src="images/image3.jpg" alt="some text" class="images"/>
</div>
</div>
This is the CSS solution I was trying to use for my images and the .main div
.image123 {
display: inline-block;
margin-left: auto;
margin-right: auto;
height: 30px;
}
#images{
text-align:center;
}
.main {
background-color: #FFFFFF;
margin: 5em auto;
padding: 25px;
border-radius: 1.5em;
width: 930px;
display: absolute;
}
My favourite way is to set up 2 classes site-wide:
.row {
display: flex;
flex-direction: row;
}
.col {
flex-direction: column;
}
This way you can align stuff easily using these two classes. Row should always be the parent container. Here is how you would handle the html:
<div class="row">
<div class="col img123">
<img height="200" width="200" src="images/image2.jpg" alt="some text" class="images"/>
</div>
<div class="col img123">
<img height="200" width="200" src="images/image2.jpg" alt="some text" class="images"/>
</div>
<div class="col img123">
<img height="200" width="200" src="images/image2.jpg" alt="some text" class="images"/>
</div>
</div>
If you want it evenly spaced use:
.col {
flex: 1;
}
Stackblitz example: https://stackblitz.com/edit/js-nddsvq
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>flexbox</title>
<style>
.main{
width:100%;
height:100%;
background-color:yellow;
display:flex;
justify-content: center;
}
</style>
</head>
<body>
<div class="main">
<div class="image"> <!--1 image-->
<img src="https://images.pexels.com/photos/1864189/pexels-photo-1864189.jpeg" width="200" height="200">
</div>
<div class="image"> <!--2 image-->
<img src="https://images.pexels.com/photos/1878095/pexels-photo-1878095.jpeg" width="200" height="200">
</div>
<div class="image"><!--3 image-->
<img src="https://images.pexels.com/photos/572487/pexels-photo-572487.jpeg" width="200" height="200" >
</div>
</div>
</body>
</html>
Related
I can't seem to get my title and anything below to centre no matter what I use in CSS. Any help would be appreciated!
<div class="title"><h1>Places to Stay</h1></div>
<div id ="imageContainer">
<img src="../assets/images/barcelona.jpg" alt="barcelona image" width="300" height="300">
<!--img src="https://www.klm.com/destinations/gb/en/europe/spain/barcelona"-->
<img src="../assets/images/milan.jpg" alt="milan image" width="300" height="300">
<!--img src="https://www.timeout.com/milan-->
<img src="../assets/images/paris.jpg" alt="paris image" width="300" height="300">
<!--img src="https://www.archdaily.com/922278/23-places-in-paris-every-architect-must-visit"-->
</div>
CSS
/*This comment is for id imageContainer. It sets a display as flex in a row and spaces it evenly*/
#imageContainer {
display:flex;
flex-direction: row;
justify-content: space-evenly;
}
/*This is a comment for the id "title", it sets titles to a different size and centers it*/
.title {
font-size: 3em;
text-align: center;
padding: 1px 2px 1px 5px;
}
It works fine on my side, but You can spacify h1 h2 and p in css.
#imageContainer {
display:flex;
flex-direction: row;
justify-content: space-evenly;
}
.title {
font-size: 3em;
text-align: center;
padding: 1px 2px 1px 5px;
}
h1, h2, p {
font-size: 3em;
text-align: center;
padding: 1px 2px 1px 5px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Places To Stay</title>
<meta charset="utf-8" />
<link href="../assets/css/first.css" rel="stylesheet" type="text/css" />
</head>
<body>
<header>
<img class="logo" src="../assets/images/mockz.jpg" alt="Mockz logo" />
</header>
<nav>
Home
</nav>
<main>
<div class="title"><h1>Places to Stay</h1></div>
<div id ="imageContainer">
<img src="../assets/images/barcelona.jpg" alt="barcelona image" width="300" height="300">
<!--img src="https://www.klm.com/destinations/gb/en/europe/spain/barcelona"-->
<img src="../assets/images/milan.jpg" alt="milan image" width="300" height="300">
<!--img src="https://www.timeout.com/milan-->
<img src="../assets/images/paris.jpg" alt="paris image" width="300" height="300">
<!--img src="https://www.archdaily.com/922278/23-places-in-paris-every-architect-must-visit"-->
</div>
<h2>Barcelona, Milan, Paris</h2>
<p>Just three of the fantastic locations we have on offer here at Mockz, the coolest hotels in Europe located in the heart of
these fantastic cities</p>
<div class="quote"><p>“Travel is the only thing you buy that makes you richer”</p></div>
<div class="quoteComment"><p>Here at Mockz we make sure that when you decide to travel, you do it in style</p></div>
</main>
<footer>
<p id = "footer"><span class="important">Contact Us</span> Email: booking#mockz.com Phone: 0191 268 9999 </p>
<!--img src for the logo is my own design based on ="https://news.marriott.com/brands/moxy-hotels/"-->
</footer>
</body>
I know there are conditions for centering a div tag and different conditions for centering an img tag but I am having trouble using them together. Can someone please help me?
A lot of other questions with answers on this site about centering divs and imgs.
</div>
<div id="draginto"></div>
<div id="cards">
<img src="Assignment10images/d8.gif">
<img src="Assignment10images/dJ.gif">
<img src="Assignment10images/dK.gif">
<img src="Assignment10images/d10.gif">
<img src="Assignment10images/dQ.gif">
<img src="Assignment10images/d9.gif">
</div>
#cards {
position: absolute;
}
I expect the images to center, but they just stay in the same position.
position absolute won't do the trick.
Try this:
<div id="cards">
<img src="Assignment10images/d8.gif">
<img src="Assignment10images/dJ.gif">
<img src="Assignment10images/dK.gif">
<img src="Assignment10images/d10.gif">
<img src="Assignment10images/dQ.gif">
<img src="Assignment10images/d9.gif">
</div>
#cards img {
margin: 0 auto;
display: block;
}
I don't know if this is what you mean but it seems like all you want to do is center the container for the images and let them flow left to right in a grid down the container? In which case it is as simple as this:
Set a width on the cards container and set the margin to auto to center it:
.cards {
margin: auto;
width: 60%;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="app">
<div class="cards">
<img src="https://via.placeholder.com/150">
<img src="https://via.placeholder.com/150">
<img src="https://via.placeholder.com/150">
<img src="https://via.placeholder.com/150">
<img src="https://via.placeholder.com/150">
<img src="https://via.placeholder.com/150">
<img src="https://via.placeholder.com/150">
<img src="https://via.placeholder.com/150">
<img src="https://via.placeholder.com/150">
</div>
</div>
</body>
</html>
https://jsbin.com/gawikud/edit?html,css,output
Use Flexbox or Grid
#cards {
display:flex;
justify-content: center;
}
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
I want to understand why images don't necessarily center properly. I was told that by default, <a> tags are inline elements, so in order to change their size, you will need to change their display property to block or inline block. Then, once you set the width of the <a> tag, you can set the width of the <img> tag to 100% and it will conform to the shape of the <a> tag.
HTML:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Photo Gallery</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="js/lightbox/lightbox.min.css">
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<div class=container>
<!-- <form action="form-field"> -->
<input type="search" id="site-search" placeholder="Search(16pt)">
<!-- </form> -->
<div class="images">
<img src="photos/thumbnails/01.jpg" alt="Image 1">
<img src="photos/thumbnails/02.jpg" alt="Image 2">
<img src="photos/thumbnails/03.jpg" alt="Image 3">
<img src="photos/thumbnails/04.jpg" alt="Image 4">
<img src="photos/thumbnails/05.jpg" alt="Image 5">
<img src="photos/thumbnails/06.jpg" alt="Image 6">
<img src="photos/thumbnails/07.jpg" alt="Image 7">
<img src="photos/thumbnails/08.jpg" alt="Image 8">
<img src="photos/thumbnails/09.jpg" alt="Image 9">
<img src="photos/thumbnails/10.jpg" alt="Image 10">
<img src="photos/thumbnails/11.jpg" alt="Image 11">
<img src="photos/thumbnails/12.jpg" alt="Image 12">
</div>
</div>
<script src="js/jquery-3.4.1.min.js"></script>
<script src="js/lightbox/lightbox-plus-jquery.min.js"></script>
<script src="js/main.js"></script>
</body>
</html>
CSS:
.container {
display: flex;
flex-direction: column;
margin: 0 auto;
padding: 10px;
}
input[type="search"] {
height: 30px;
}
.images {
border: 1px red solid;
padding: 0 auto;
}
/* If a flex container is set to width */
But I've tried that it still hasn't been working. I know images have their own dimensions when they get imported, but whats the logic here? I keep having to expierment with CSS settings when working with images because they are so unpredictable.
https://ibb.co/Y02j6VL
There might be 4 possibilities.
1) Change your .images padding to margin: 0 auto, like so:
.images {
border: 1px red solid;
margin: 0 auto;
}
2) Depending on your layout its also possible that images is taking 100% width.
So you need to also add text-align:center on images div.
3) Your images div can be tranformed to display:flex and then set justify-content: center on it.
4) If you want to use .container content to be centered. You can use align-item:center on the images div. Since, container has flex-direction:column
.container {
display: flex;
flex-direction: column;
align-items:center;
margin: 0 auto;
padding: 10px;
border: 1px solid black;
}
input[type="search"] {
height: 30px;
}
.images {
border: 1px red solid;
margin: 0 auto;
text-align: center;
}
<div class=container>
<div class="images">
<img src="photos/thumbnails/01.jpg" alt="Image 1">
<img src="photos/thumbnails/02.jpg" alt="Image 2">
<img src="photos/thumbnails/03.jpg" alt="Image 3">
<img src="photos/thumbnails/04.jpg" alt="Image 4">
<img src="photos/thumbnails/05.jpg" alt="Image 5">
<img src="photos/thumbnails/06.jpg" alt="Image 6">
<img src="photos/thumbnails/07.jpg" alt="Image 7">
</div>
</div>
Since your code is using display: flex; here is a solution that continues to use it. However it makes more sense to flex the images class. To center the content use align-items: center; justify-content: center; depending on flex-direction.
Sidenote: padding: 0 auto; is not a thing, there is no auto value for padding.
.container {
margin: 0 auto;
padding: 10px;
}
input[type="search"] {
height: 30px;
}
.images {
width: 245px;
display: flex;
flex-direction: row;
border: 1px red solid;
align-items: center;
justify-content: center;
flex-wrap: wrap;
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Photo Gallery</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="js/lightbox/lightbox.min.css">
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<div class=container>
<!-- <form action="form-field"> -->
<input type="search" id="site-search" placeholder="Search(16pt)">
<!-- </form> -->
<div class="images">
<div>
<img src="https://www.gravatar.com/avatar/b0327f4c2d7593ae3c7185a66d69d81b?s=48&d=identicon&r=PG&f=1" alt="Image 1">
</div>
<div>
<img src="https://www.gravatar.com/avatar/b0327f4c2d7593ae3c7185a66d69d81b?s=48&d=identicon&r=PG&f=1" alt="Image 1">
</div>
<div>
<img src="https://www.gravatar.com/avatar/b0327f4c2d7593ae3c7185a66d69d81b?s=48&d=identicon&r=PG&f=1" alt="Image 1">
</div>
<div>
<img src="https://www.gravatar.com/avatar/b0327f4c2d7593ae3c7185a66d69d81b?s=48&d=identicon&r=PG&f=1" alt="Image 1">
</div>
<div>
<img src="https://www.gravatar.com/avatar/b0327f4c2d7593ae3c7185a66d69d81b?s=48&d=identicon&r=PG&f=1" alt="Image 1">
</div>
<div>
<img src="https://www.gravatar.com/avatar/b0327f4c2d7593ae3c7185a66d69d81b?s=48&d=identicon&r=PG&f=1" alt="Image 1">
</div>
<div>
<img src="https://www.gravatar.com/avatar/b0327f4c2d7593ae3c7185a66d69d81b?s=48&d=identicon&r=PG&f=1" alt="Image 1">
</div>
<div>
<img src="https://www.gravatar.com/avatar/b0327f4c2d7593ae3c7185a66d69d81b?s=48&d=identicon&r=PG&f=1" alt="Image 1">
</div>
<div>
<img src="https://www.gravatar.com/avatar/b0327f4c2d7593ae3c7185a66d69d81b?s=48&d=identicon&r=PG&f=1" alt="Image 1">
</div>
<div>
<img src="https://www.gravatar.com/avatar/b0327f4c2d7593ae3c7185a66d69d81b?s=48&d=identicon&r=PG&f=1" alt="Image 1">
</div>
<div>
<img src="https://www.gravatar.com/avatar/b0327f4c2d7593ae3c7185a66d69d81b?s=48&d=identicon&r=PG&f=1" alt="Image 1">
</div>
<div>
<img src="https://www.gravatar.com/avatar/b0327f4c2d7593ae3c7185a66d69d81b?s=48&d=identicon&r=PG&f=1" alt="Image 1">
</div>
<div>
<img src="https://www.gravatar.com/avatar/b0327f4c2d7593ae3c7185a66d69d81b?s=48&d=identicon&r=PG&f=1" alt="Image 1">
</div>
<div>
<img src="https://www.gravatar.com/avatar/b0327f4c2d7593ae3c7185a66d69d81b?s=48&d=identicon&r=PG&f=1" alt="Image 1">
</div>
<div>
<img src="https://www.gravatar.com/avatar/b0327f4c2d7593ae3c7185a66d69d81b?s=48&d=identicon&r=PG&f=1" alt="Image 1">
</div>
<div>
<img src="https://www.gravatar.com/avatar/b0327f4c2d7593ae3c7185a66d69d81b?s=48&d=identicon&r=PG&f=1" alt="Image 1">
</div>
</div>
</div>
<script src="js/jquery-3.4.1.min.js"></script>
<script src="js/lightbox/lightbox-plus-jquery.min.js"></script>
<script src="js/main.js"></script>
</body>
</html>
I want the first image to stick to the top of the container and the following images should be below it... top:0; lets the image be..200px above the container and if I just say position:relative its always in the middle...;
<div class="container">
<div class="card_left">
<p style="font-size:1.6em; padding: 15px 0;"><b>Title</b></p>
<p style="font-size:1.2em;">Long text</p>
</div>
<div class="card_right">
<img src="../res/images/artikel1bild.PNG"/>
<img src="../res/images/artikel1bild.PNG"/>
</div>
Use display: block so there will be no other images in the same line and margin: auto for centering the image
img {
display: block;
margin: auto;
width: 200px;
}
<div>
<img src="https://www.w3schools.com/css/paris.jpg" />
<img src="https://www.w3schools.com/css/paris.jpg" />
<img src="https://www.w3schools.com/css/paris.jpg" />
</div>
Just add <br /> after each image if you want to stick to HTML:
<div class="card_right">
<img src="../res/images/artikel1bild.PNG"/><br />
<img src="../res/images/artikel1bild.PNG"/><br />
</div>
Or the better way would be to make a separate CSS file and set display: block; for your img tags:
img {
display: block;
}
Example: https://jsfiddle.net/nk8fbr76/
try this
img {
margin: 0, auto;width: 200px;display:inline-block;height:100px;
}
<div class="card_right">
<img src="https://www.w3schools.com/css/img_fjords.jpg"/>
<img src="https://www.w3schools.com/css/img_fjords.jpg"/>
</div>
I want a div to contain several images in a single scrollable row. The following code works if i replace the images with a long string, but doesn't work with images.
<html>
<div style="overflow: auto">
<img src="a.jpg">
<img src="b.jpg">
<img src="c.jpg">
<img src="d.jpg">
</div>
</html>
It will work. Maybe white-space property is what you're looking for though? See below:
div {
width: auto;
overflow-x: auto;
white-space: nowrap;
}
<div>
<img src="https://placehold.it/400x200">
<img src="https://placehold.it/400x200">
<img src="https://placehold.it/400x200">
<img src="https://placehold.it/400x200">
<img src="https://placehold.it/400x200">
<img src="https://placehold.it/400x200">
<img src="https://placehold.it/400x200">
</div>
Please set height and width of the div so it will work see the below example
<style>
.auto-div {
background-color: #00FFFF;
width: 400px;
height: 300px;
overflow: auto;
}
</style>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="auto-div">
<img src="https://www.w3schools.com/css/img_forest.jpg" /><img src="https://www.w3schools.com/css/img_forest.jpg" /><img src="https://www.w3schools.com/css/img_forest.jpg" /><img src="https://www.w3schools.com/css/img_forest.jpg" /><img src="https://www.w3schools.com/css/img_forest.jpg" /><img src="https://www.w3schools.com/css/img_forest.jpg" />
</div>
</body>
</html>
Here you are not giving any height and width to the div tag so the style overflow: auto is not working for you.
<div style="width:150px;height:150px;overflow:auto;border:1px solid black">
<image src="images.jpg"></image>
<image src="images.jpg"></image>
<image src="images.jpg"></image>
<image src="images.jpg"></image>
</div>