Aligning an image with CSS - html

I am trying to center an image using CSS and Im pretty sure I am using the correct selector in CSS because I have resized the image, but I cant get it to align properly.
My HTML:
</br>
<div class="copyright">
<em>TWO GREEN THUMBS 2013© ALL RIGHTS RESERVED</em>
</div>
</br>
<div class="sponsors">
<a href="https://craneflight.org">
<img src="https://greenthumbsfarm.com/wp-content/uploads/2017/07/CRANE-FLIGHT-COMP-2-1.jpg" alt="crane-flight-logo" class="thumb" />
</a>
</div>
Here's what i tried for CSS:
.sponsors>img {
display: inline-block;
vertical-align: middle;
}
.sponsors>img {
display: inline-block;
vertical-align: middle;
}
img.thumb {
width: 100px;
height: 150px;
}
</br>
<div class="copyright">
<em>TWO GREEN THUMBS 2013© ALL RIGHTS RESERVED</em>
</div>
</br>
<div class="sponsors">
<a href="https://craneflight.org">
<img src="https://greenthumbsfarm.com/wp-content/uploads/2017/07/CRANE-FLIGHT-COMP-2-1.jpg" alt="crane-flight-logo" class="thumb" />
</a>
</div>
and
.sponsors>img.align-center {
display: block;
margin: 0px auto;
}
.sponsors>img.align-center {
display: block;
margin: 0px auto;
}
img.thumb {
width: 100px;
height: 150px;
}
</br>
<div class="copyright">
<em>TWO GREEN THUMBS 2013© ALL RIGHTS RESERVED</em>
</div>
</br>
<div class="sponsors">
<a href="https://craneflight.org">
<img src="https://greenthumbsfarm.com/wp-content/uploads/2017/07/CRANE-FLIGHT-COMP-2-1.jpg" alt="crane-flight-logo" class="thumb align-center" />
</a>
</div>
I am trying to get the image to align at the bottom of the page below where it says "TWO GREEN THUMBS 2013© ALL RIGHTS RESERVED", but I just can't figure it out. I have looked through multiple similar questions and none of the other solutions seem to be working in this case. Could it be I am not using the selector properly?
EDIT: I have the image resized with the "thumb" class selector because there will be several images and they will all need to be the same size. Here's the code:
img.thumb {
width: 100px;
height: 150px;
}

You can try to apply the style to the container DIV.
.sponsors {
text-align: center;
}

Try to aligning it with the div:
<div class="sponsors" align="center">
<a href="https://craneflight.org">
<img src="https://greenthumbsfarm.com/wp-content/uploads/2017/07/CRANE-FLIGHT-COMP-2-1.jpg" alt="crane-flight-logo" class="thumb"/>
</a>
</div>

I added a container around both elements and made that an inline-block (so it would be only the width of its contents and stay left) .
Then I centered the image inside that container by applying text-align-center to the .sponsors DIV . I also centered the top line to be center aligned with the image using text-align: center.
BTW: You had a wrong CSS selector for the image. There is an a tag between the div and the image, so .sponsors>img won't have any effect. Either .sponsors>a>img or just .sponsors img
.container1 {
display: inline-block;
}
.copyright {
text-align: center;
}
.sponsors {
text-align: center;
}
.sponsors img {
width: 100px;
height: 150px;
}
<div class="container1">
<div class="copyright">
<em>TWO GREEN THUMBS 2013© ALL RIGHTS RESERVED</em>
</div>
<div class="sponsors">
<a href="https://craneflight.org">
<img src="https://greenthumbsfarm.com/wp-content/uploads/2017/07/CRANE-FLIGHT-COMP-2-1.jpg" alt="crane-flight-logo" class="thumb" />
</a>
</div>
</div>

Related

<img> and center aligned <span> in the same line doesn't work

I'm new to coding and basically don't understand anything and I've been trying to solve this problem for about a week and haven't got anywhere close
here is my HTML code
.cats {
height: 118px;
width: auto;
object-position: 1px;
display: inline-block;
vertical-align: middle;
}
<div>
<img class="cats" src="black.jpeg" alt="beautiful cat">
<span style="text-align: center;">cutie</span>
</div>
I'm trying to have the image at the left like it is by default and have my text in the same line aligned to the center of the page.
Is this what your looking for? For the <img> to be positioned at the top left of the viewport and the <span> text to be centered on the page. Try this out.
.cats {
height: 118px;
width: auto;
object-position: 1px;
display: inline-block;
vertical-align: middle;
}
.container {
display: flex;
align-items: center;
}
.container span {
/* width: 85%; vary width to your liking if need be */
margin: 0 auto;
}
<div class="container">
<img class="cats" src="https://i.ibb.co/56tbCQP/Screen-Shot-2021-02-25-at-8-46-16-PM.png" alt="beautiful cat">
<span style="text-align: center;">cutie</span>
</div>
You can add display: block to span tag.
<span style="text-align: center; display: block">cutie</span>
If I'm understanding you correctly that you want the text centered and to the right of the photo then flexbox can help here. Try this snippet:
<div style="display: flex; align-items: center">
<img class="cats" src="black.jpg" alt="beautiful cat">
<span style="text-align: center; padding-left: 20px;">cutie</span>
</div>
If you are wanting the text below the picture I suggest using <figure> and <figcaption>:
<figure>
<img class="cats" src="black.jpg" alt="beautiful cat">
<figcaption style="text-align: center;">cutie</figcaption>
</figure>

2 headers lined up with 2 images side by side

I have the following HTML structure currently:
<div id="image" style="margin: 20px;">
<h5>
<span>DriverName1</span>
<span>DriverName2</span>
</h5>
<img src=/>
<img src= />
</div>
I am trying to make the images appear side by side which they are doing, but the headers for drivername1 and drivername2 are appearing right next to each other over the same image. How do I make these spans match up with the top left edge of each image? I tried adding separate headers over each image but that makes the images go vertically one on top of the other then.
you can consider using display:flex for this
check the following snippet
div{
display:flex;
}
.image {
display: flex;
flex-direction: column;
border:1px solid;
margin-left:10px;
}
img {
width: 100px;
height: 100px;
}
<div id="image" style="margin: 20px;">
<section class="image">
<header>DriverName1</header>
<img src="http://blog.caranddriver.com/wp-content/uploads/2015/11/BMW-2-series.jpg">
</section>
<section class="image">
<header>DriverName2</header>
<img src="http://blog.caranddriver.com/wp-content/uploads/2015/11/BMW-2-series.jpg" width=25px height=25px>
</section>
</div>
Non-flex solution
div * {
display: inline-block;
}
.image {
border: 1px solid;
margin-left: 10px;
}
img {
width: 100px;
height: 100px;
display: table-cell;
}
header {}
<div id="image" style="margin: 20px;">
<section class="image">
<header>DriverName1</header>
<img src="http://blog.caranddriver.com/wp-content/uploads/2015/11/BMW-2-series.jpg">
</section>
<section class="image">
<header>DriverName2</header>
<img src="http://blog.caranddriver.com/wp-content/uploads/2015/11/BMW-2-series.jpg" width=25px height=25px>
</section>
</div>
Hope this helps

Center items vertically without setting height

I'm trying to vertically center certain items within a table cell. I've tried most solutions on stackoverflow and several other sites without any luck.
In this cell, the image is stuck at the top of the table cell, while the text is properly centered vertically:
<tr>
<td class='sidebar-middle'> <!--sets a left and right border-->
<a target="_blank" href="data/Standards.pdf">
<div style='width: 100%;text-align: center;overflow: hidden;'>
<div style='float: left;width: 34%; text-align: center;height: 100%;'>
<img src='images/logo.jpg' alt='Standards' style='width: 80px;vertical-align: middle;'/>
</div>
<p style='float: right; vertical-align: middle;width: 64%;'>Local Facility Standards to be Followed</p>
</div>
</a>
</td>
</tr>
However, using the same method, this DOES seem to work:
<tr>
<td class='sidebar-bottom'> <!--sets a left, right, and bottom border-->
<a target="_blank" href="Policies.html">
<div style='width: 100%;text-align: center;overflow: hidden;'>
<div style='float: left;width: 35%; text-align: center;height: 100%;'>
<img src='images/patch.png' alt='Policies' style='height: 80px;vertical-align: middle;'/>
</div>
<p style='float: right; vertical-align: middle;width: 64%;'>Policies</p>
</div>
</a>
</td>
In the first (frustrating) example, the image is 112 pixels in height, scaled down to 30. In the second (working) example, the image is 122 pixels in height, scaled down to 80. I suspect that image height has something to do with it, but can't get any further in resolving the problem.
While assigning classes to the elements I didn't see a change. When I replaced the <tr> and <td> with <div> and <section> it didn't change. It just works like the way you wanted it to. There's no style info provided for classes, .sidebar-middle and .sidebar-bottom so that might be your problem (or the rest of the code you neglected to post). Note: I didn't need to modify the div.C or the <section>s I added, so table components may have not been needed and the floats were sufficient.
When using inline styling heavily, your HTML gets cluttered and there's no easy way of fixing it should you have many lines of that coding disaster. As Paulie_D and hidanielle already stated, your vertical-align does not function on floated elements, and HTML table -layouts are so 90s. In the 21st century we use table-* CSS properties.
SNIPPET
.A {
width: 100%;
text-align: center;
overflow: hidden;
}
.B {
float: left;
width: 34%;
text-align: center;
height: 100%;
}
.img {
width: 80px;
height: 80px;
}
.note {
float: right;
width: 64%;
}
<div class='C'>
<section class='sidebar-middle'>
<a target="_blank" href="http://www.orimi.com/pdf-test.pdf">
<div class='A'>
<div class='B'>
<img src='https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png' alt='Lenna' class='img' />
</div>
<p class='note'>Local Facility Standards to be Followed</p>
</div>
</a>
</section>
</div>
<div class='C'>
<section class='sidebar-bottom'>
<a target="_blank" href="http://www.example.com">
<div class='A'>
<div class='B'>
<img src='https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png' alt='Lenna' class='img'>
</div>
<p class='note'>Policies</p>
</div>
</a>
</section>
</div>
Instead of floats, use CSS Tables (since you started with an actual table for layout).
* {
margin: 0;
padding: 0;
}
.inner {
display: table;
table-layout: fixed;
width: 100%;
text-align: center;
overflow: hidden;
}
.left {
display: table-cell;
width: 34%;
text-align: center;
background: pink;
}
img {
width: 80px;
}
.right {
display: table-cell;
width: 64%;
vertical-align: middle;
background: lightblue;
}
<a target="_blank" href="data/Standards.pdf">
<div class="inner">
<div class="left">
<img src='http://www.fillmurray.com/80/80' alt='Standards' />
</div>
<p class="right">Local Facility Standards to be Followed</p>
</div>
</a>

Make text in link fit perfectly below image

I want the text below my images to fit exactly below them. Longer texts continue in the same line which is not I want. I want it to go to the next line and stay below the corresponding image.
I also tried using break-word and aligning center but it doesn't work.
.row div {
display: inline-block;
padding-left: 5%;
padding-right: 5%;
}
.row div img {
max-width: 100%;
max-height: 100%;
}
.row div a {
word-wrap: break-word;
text-align: center;
}
<div class="row">
<div>
<a href="#">
<img src="martial.png">
</br>Manchester United 2015-16 Martial kit
</a>
</div>
<div>
<a href="#">
<img src="ars.png">
</a>
</div>
<div>
<a href="#">
<img src="bvb.png">
</a>
</div>
<div>
<a href="#">
<img src="lewandowski1.png">
</a>
</div>
</div>
You gonna have to add additional div container for every image and link.
See the example below:
.row div{
display: inline-block;
padding-left: 5%;
padding-right: 5%;
}
.row div img{
max-width: 100%;
max-height: 100%;
}
.row div a{
word-wrap:break-word;
text-align:center;
}
.img-container{
display: inline-block; /* added */
float: left; /* added */
text-align: center; /* added */
}
<div class="row">
<div class="img-container">
<img src="http://i3.mirror.co.uk/incoming/article6363634.ece/ALTERNATES/s615b/Anthony-Martial-signs-for-Manchester-United.jpg"></br>Manchester United 2015-16 Martial kit text aded text aded text aded text aded text aded
</div>
<div class="img-container">
<img src="ars.png">
</div>
<div class="img-container">
<img src="bvb.png">
</div>
<div class="img-container">
<img src="lewandowski1.png">
</div>
</div>
If you want to set a predefined width and height you can do it adding rules to .img-container class. Other way it will take the image dimensions. And for centered links just add this rule to .img-container class:
text-align: center;
just give your inner divs a width
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
.row div {
border: 1px solid black;
width: 40%;
display: inline-block;
padding-left: 5%;
padding-right: 5%;
//overflow: auto;
}
.row div img {
max-width: 100%;
max-height: 100%;
}
.row div a {
word-wrap: break-word;
text-align: center;
}
</style>
</head>
<body>
<div class="row">
<div>
<a href="#">
<img src="http://e0.365dm.com/15/09/768x432/anthony-martial-manchester-united-press_3345052.jpg?20150904202157">Manchester United 2015-16 Martial fjkghdghjfdhghdfjgjgdfgjhghjhghjgdhjgdhgjdfgdjjhdgjhdffhgfhghdfgjhgdhjdghgfdgjgdfgdfjhjdfgdhjkit</a>
</div>
<div>
<a href="#">
<img src="http://e0.365dm.com/15/09/768x432/anthony-martial-manchester-united-press_3345052.jpg?20150904202157">Manchester United 2015-16 Martial fjkghdghjfdhghdfjgjgdfgjhghjhghjgdhjgdhgjdfgdjjhdgjhdffhgfhghdfgjhgdhjdghgfdgjgdfgdfjhjdfgdhjkit</a>
</div>
</div>
</body>
</html>
Use the min-content property on the element containing the img and caption. In this demo, I used the figure and figcaption (and main) elements instead of divs to create semantic layout. It'll work the same way if you prefer `div's instead.
.row figure {
display: inline-block;
padding-left: 5%;
padding-right: 5%;
/* min-content needs to be prefixed */
width: -moz-min-content;
width: -webkit-min-content;
width: min-content;
}
.row figure img {
display: inline-block;
/* Changed max-* to min-* to demonstrate images in different sizes */
min-width: 100%;
min-height: 100%;
}
.row figure a {
word-wrap: break-word;
text-align: center;
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Captions</title>
</head>
<body>
<main class="row">
<figure>
<a href="#">
<img src="http://placehold.it/300x150/9a7/a10.png&text=Manchester+United+2015-16+Martial+Kit">
<figcaption>Manchester United 2015-16 Martial Kit</figcaption>
</a>
</figure>
<figure>
<a href="#">
<img src="http://placehold.it/100x50/fd3/b0d.png&text=ARS">
<figcaption>Manchester United 2015-16 Martial Kit</figcaption>
</a>
</figure>
<figure>
<a href="#">
<img src="http://placehold.it/200x100/fa8/375.png&text=BVB">
<figcaption>Manchester United 2015-16 Martial Kit</figcaption>
</a>
</figure>
<figure>
<a href="#">
<img src="http://placehold.it/400x200/048/Fee.png&text=lewandowski1">
<figcaption>Manchester United 2015-16 Martial Kit</figcaption>
</a>
</figure>
</main>
</body>
</html>

Background color not covering full height of div

---HTML
<div id="story">
<div id="individual">
<img src='uploads/1231924837Picture.png'/>
<h2>2009-01-14</h2>
<h1>Headline</h1>
<p>stroy story etc stroy story etc stroy story etc</p>
</div>
<br />
<div id="storynav">
<a href='home.php?start=0'>1</a>
<a href='home.php?start=1'>2</a>
<a href='home.php?start=2'>3</a>
<a href='home.php?start=3'>4</a>
<a href='home.php?start=4'>5</a>
<a href='home.php?start=5'>6</a>
<a href='home.php?start=6'>7</a>
<a href='home.php?start=7'>8</a>
<a href='home.php?start=8'>9</a>
</div>
</div>
---CSS
#story img{
border: none;
float: right;
display: inline;
padding: 10px;
margin-top: 30px;
}
#story{
width: 600px;
height: inherit;
background-color:black;
margin-left: 34px;
margin-bottom: 3px;
}
#individual{
background-color: #000000;
clear:both;
}
#storynav{
font-size: 10px;
text-align: center;
}
(source: bionic-comms.co.uk)
The above code and css is giving me a headache because, as the picture shows, the div background color gets confused when i add images in. This is dynamic content but i thought it would be easier to show the static html. Can any one tell me what i am doing wrong? The background color should cover the picture as well. Thanks!
EDIT
Thanks for that. It is something i had previously tried but it doesn't do anything. I have also tried a spacer in there as well and that doesn't do anything. Flummoxed!
You are setting the image to float right which means that the container div cannot work out it's actual height. You need to clear the floated element which essentially lets the container know how large the image actualy is.
You will need to add an element with the style clear: both; underneath the img tag in your HTML, preferably at the end of the div like so:
<div id="story">
<div id="individual">
<img src='uploads/1231924837Picture.png'/>
<h2>2009-01-14</h2>
<h1>Headline</h1>
<p>stroy story etc stroy story etc stroy story etc</p>
</div>
<br />
<div id="storynav">
<a href='home.php?start=0'>1</a>
<a href='home.php?start=1'>2</a>
<a href='home.php?start=2'>3</a>
<a href='home.php?start=3'>4</a>
<a href='home.php?start=4'>5</a>
<a href='home.php?start=5'>6</a>
<a href='home.php?start=6'>7</a>
<a href='home.php?start=7'>8</a>
<a href='home.php?start=8'>9</a>
</div>
<div class="clear"></div> <-- add this here
</div>
And add the class:
.clear
{
clear: both;
}
Read that: http://www.quirksmode.org/css/clearing.html
In short, try this:
---HTML
<div id="story">
<div id="individual">
<img src='uploads/1231924837Picture.png'/>
<h2>2009-01-14</h2>
<h1>Headline</h1>
<p>stroy story etc stroy story etc stroy story etc</p>
</div>
<br />
<div id="storynav">
<a href='home.php?start=0'>1</a>
<a href='home.php?start=1'>2</a>
<a href='home.php?start=2'>3</a>
<a href='home.php?start=3'>4</a>
<a href='home.php?start=4'>5</a>
<a href='home.php?start=5'>6</a>
<a href='home.php?start=6'>7</a>
<a href='home.php?start=7'>8</a>
<a href='home.php?start=8'>9</a>
<div class="clear"></div>
</div>
</div>
---CSS
#story img{
border: none;
float: right;
display: inline;
padding: 10px;
margin-top: 30px;
}
#story{
width: 600px;
height: inherit;
background-color:black;
margin-left: 34px;
margin-bottom: 3px;
}
#individual{
background-color: #000000;
clear:both;
}
#storynav{
font-size: 10px;
text-align: center;
}
.clear {
clear: both;
}