Align image center in floated div - image

I have a loop to loaded logo image in floated div block, I been tried few tips from stackoverflow but have no luck to make the logo align center and middle within the div, all logo height are not fixed and might have different height for each:
<div style="float:left; width:80px; height:80px; padding:8px; border:1px solid #ccc;">
<img width="78px" align="left" src="images/logo/logo1.jpg">
</div>
Please help, thanks.

Use positioning. The following worked for me...
With zoom to the center of the image (image fills the div):
div{
float:left;
display:block;
overflow:hidden;
width: 70px;
height: 70px;
position: relative;
}
div img{
min-width: 70px;
min-height: 70px;
max-width: 250%;
max-height: 250%;
top: -50%;
left: -50%;
bottom: -50%;
right: -50%;
position: absolute;
}
Without zoom to the center of the image (image does not fill the div):
div{
float:left;
display:block;
overflow:hidden;
width: 100px;
height: 100px;
position: relative;
}
div img{
width: 70px;
height: 70px;
top: 50%;
left: 50%;
bottom: 50%;
right: 50%;
position: absolute;
}

You really should move your CSS out of the style attribute and into a file like style.css if you have not done so. But if you really have to you can place the code below into inline styles like you have now.
Remove the align="left" attribute from your image tag and set the image's display to block. This will allow margin: 0 auto; to center your image for you inside the containing DIV.
Looks like you'll have to replicate a <table> with CSS to get the vertical centering you desire. Table cells allow vertical centering. To do this I've added and additional DIV with a class of .container. The .container DIV has it's display set to table and the .image-container DIV, which is acting like a table cell, has it's display set to table-cell.
CSS
.container {
float: left;
width: 80px;
height: 80px;
padding: 8px;
border: 1px solid #ccc;
display: table;
}
.image-container {
display: table-cell;
vertical-align: middle;
}
.image-container img {
display: block;
margin: 0 auto;
}
HTML
<div class="container">
<div class="image-container">
<img src="images/logo/logo1.jpg">
</div>
</div>
JSFiddle: http://jsfiddle.net/MJ5j4/

just create a class "centerImgWrapper", and wrap all your "center" Images anywhere in the Code with divs.
Thats pure CSS
.centerImgWrapper {
width: 100%;
height: 100%;
position: relative;
overflow: hidden;}
.centerImgWrapper img {
left: 0;
right:0;
top: 0;
bottom:0;
max-height: 100%;
max-width: 100%;
margin:auto;
position:absolute;
}
Just Check out the Fiddle.
MyFiddle
Dave

If you are trying to get it in the middle inside the div, have you tried:
<div style="width:800px; height:800px; padding:8px; border:1px solid #ccc;">
<img width="78px" src="mobiIconGreenGray.gif" style="margin-left:50%; margin-top:50%;">
</div>
I used "margin-left:50%; margin-top:50%;" INSIDE the IMG tag and got rid of the "align" and "float" attribute. I probably wouldn't want to use "align" in this case.
Either way, I hope it helps.

Images are more or less displayed as inline-blocks. So you can just set text-align: center; on the styles of the container div.
Getting something to be vertically aligned in the middle is complicated with css. If you're going to be dynamically placing the logo with JavaScript anyway, you can save yourself trouble and just center it vertically by specifying the margins with JavaScript.
Check out this demo: http://jsfiddle.net/jyvFN/1/
HTML
<div id="container">
<img id="logo" src="http://placekitten.com/100/100" />
</div>
CSS:
#container {
float: left;
background-color: blue;
width: 200px;
height: 150px;
text-align: center;
border: 1px solid red;
}
JavaScript
var logo = document.getElementById('logo');
var container = document.getElementById('container');
var margin = (container.clientHeight - logo.clientHeight) / 2;
logo.style.marginTop = margin + "px";
logo.style.marginBottom = margin + "px";

Why didn't you set align of the image center?
Then your code should be:
<div style="float:left; width:80px; height:80px; padding:8px; border:1px solid #ccc;">
<img width="78px" align="center" src="images/logo/logo1.jpg">
And I think it's also problem of the ratio of the image width vs the div block + it's padding and border.
Try to set it balance.

Related

Make responsive image

/How to set width and height of image to be 150px and to be responsive/
/HTML/
<div class="tabs__tab-image-container">
<img src="https://pbs.twimg.com/profile_images/1208234904405757953/mT0cFOVQ_400x400.jpg" class="tabs__tab-content-img">
</div>
/CSS/
.tabs__tab-image-container {
max-width: 150px;
min-height: 150px;
height: 100%;
width: 100%;
}
tabs__tab-content-img {
border-radius: 50%;
max-width: 150px;
height: auto;
display: block;
object-fit: cover;
}
You need to be clearer in your question. You want an image to ALWAYS be 150px square but the DIV to be responsive? Or do you want the IMAGE to be responsive within the DIV? As you're only showing a DIV with a single image in it, it's hard to tell what the goal is.
A responsive page is one that changes based on the viewport size; locking the image down to a fixed size kinda defeats the purpose in that case. Your CSS also isn't using the image class as it's incorrectly formatted.
The following fixes the image class as you've specified it, but all this does is round the image corners; the content won't be responsive:
CSS:
.tabs__tab-image-container {
max-width: 150px;
min-height: 150px;
height: 100%;
width: 100%;
}
.tabs__tab-content img {
border-radius: 50%;
max-width: 150px;
height: auto;
display: block;
object-fit: cover;
}
HTML:
<div class="tabs__tab-image-container">
<div class="tabs__tab-content"><img src="https://pbs.twimg.com/profile_images/1208234904405757953/mT0cFOVQ_400x400.jpg">
</div>
</div>
To make the page responsive, you can add viewports in CSS for different page sizes, or if it's just the image displaying (with a line of text in the div for example) use the following:
CSS:
.tabs__tab-image-container {
text-align:center;
font-family: "Comic Sans MS", cursive;
font-size: 10.0vw;
font-weight:700;
}
.tabs__tab-content img {
border-radius: 50%;
width: 100%;
}
HTML:
<div class="tabs__tab-image-container">
<div class="tabs__tab-content"><img src="https://pbs.twimg.com/profile_images/1208234904405757953/mT0cFOVQ_400x400.jpg">
Text Here
</div>
</div>
If you only need the image, you don't even need the tabs__tab-image-container class, just use the tab content class targeting the image.

Does anyone know how to make the 3 blocks fader in the current spring.io website?

I'd like to know how can I code the 3 blocks fader from the Spring.io website. Image here:
It has a dividing line that changes the image gradually as you move it.
This can be done with simple HTML and javascript code. Here's the complete jsfiddle https://jsfiddle.net/zn3b1hov/34/
Basic Idea is very simple.
First, You need 2 SVG images one is colored and another is grayscale. I am using this 2
grayscale-image
colored-image
Now Create 2 absolute div one on top of another and use these images as background.
Then create a slider as wide as the images. I am using HTML range type input
Finally change the top div's width according to sliders value.
Complete HTML, CSS and JS
<style>
#fader-diagram-your-app {
position: relative;
height: 286px;
}
#fader-diagram-modern-java-gray {
position: absolute;
height: 238px;
width: 800px;
margin: 31px auto;
background: url('https://spring.io/img/homepage/diagram-modern-java-gray-9a417697a51646e42df7e9d7f024709d.svg') no-repeat;
background-size: 100%;
}
#fader-diagram-modern-java-color {
position: absolute;
height: 238px;
max-width: 800px;
margin: 31px auto;
overflow: hidden;
width: 200px;
}
#fader-diagram-modern-java-color div {
position: absolute;
height: 238px;
background: url('https://spring.io/img/homepage/diagram-modern-java-color-e10b7eec68b1fe60eefeab0cf20a20da.svg') no-repeat;
background-size: 100%;
width: 800px;
}
#fader {
background-color: #34302d;
width: 6px;
height: 275px;
border-radius: 4px;
position: absolute;
left: 0;
}
#myRange {
width: 800px;
position: absolute;
top: 50%;
}
</style>
<div id="fader-diagram-your-app">
<div class="sidebyside" id="fader-diagram">
<div id="fader-diagram-modern-java-gray"></div>
<div id="fader-diagram-modern-java-color" style="width: 39.6875px;">
<div></div>
</div>
<div id="fader">
</div>
<input type="range" min="0" max="800" value="0" class="slider" id="myRange">
</div>
</div>
<script>
var slider = document.getElementById("myRange");
var coloredImage = document.getElementById("fader-diagram-modern-java-color");
var fader = document.getElementById("fader");
slider.oninput = function() {
coloredImage.style.width = this.value + "px";
fader.style.left = this.value + "px";
}
</script>

viewport height, whilst centralising the image within slider css

I have a slider on a homepage I am currently working on. I am trying to achieve a full viewport height that takes up the whole width for the screen.
The only way I can currently achieve this is by either stretching the image, or the image isn't centred.
The image needs to be aligned roughly centred horizontally and vertically, so customers can see the centre of the image on any width of browser, and without stretching the image out of proportion.
I have tried the background-size: cover; on the element with no success as its not a background img. the containers have 100vh currently, but the width is the issue.
The issue is located here http://joeybox.info/ . I realise with the menu and the logo above the image the 100 viewport height will rest under the "fold", however I am placing the logo and menu over the image eventually, once I have figured out the css.
I have tried many solutions found within the stack overflow forum and none work in my scenario.
My current css, after deleting the in-correct code, is:-
.bx-wrapper img {display: inherit;
height: 100vh;
max-width: inherit;}
.ewic-wid-imgs {height: 100vh;
max-width: unset;
width: unset;}
.bx-wrapper img {display: inherit;
height: 100vh;
max-width: inherit;}
html=
<div class="slider-box">
<div id="ewic-con-385">
<div style="display: none;" id="preloader-385" class="sliderpreloader">
</div>
<div style="max-width: 100%; margin: 0px auto;" class="bx-wrapper">
<div style="width: 100%; overflow: hidden; position: relative; height: 633px;" class="bx-viewport">
<ul style="width: 315%; position: relative; left: 0px;" class="bxslider-385">
<li style="float: left; list-style: outside none none; position: relative; width: 1349px; margin-right: 10px;" class="ewic-slider bx-clone">
<img title="Qw Direct Leather Keyrings" class="ewic-wid-imgs" src="http://joeybox.info/wp-content/uploads/2015/07/qw-direct-leather-keyrings.jpg">
<div class="ewic-caption"><span>Qw Direct Leather Keyrings</span></div>
</li><li style="float: left; list-style: outside none none; position: relative; width: 1349px; margin-right: 10px;" class="ewic-slider">
<img title="Qw Direct Leather Keyrings" class="ewic-wid-imgs" src="http://joeybox.info/wp-content/uploads/2015/07/qw-direct-leather-keyrings.jpg">
<div class="ewic-caption"><span>Qw Direct Leather Keyrings</span>
</div></li><li style="float: left; list-style: outside none none; position: relative; width: 1349px; margin-right: 10px;" class="ewic-slider bx-clone">
<img title="Qw Direct Leather Keyrings" class="ewic-wid-imgs" src="http://joeybox.info/wp-content/uploads/2015/07/qw-direct-leather-keyrings.jpg"><div class="ewic-caption">
<span>Qw Direct Leather Keyrings</span>
</div></li></ul></div>
<div class="bx-controls bx-has-controls-direction bx-has-controls-auto">
<div class="bx-controls-direction"><a class="bx-prev disabled" href="">Prev</a>
<a class="bx-next disabled" href="">Next</a></div><div class="bx-controls-auto"><div class="bx-controls-auto-item"><a class="bx-start active" href="">Start</a></div>
<div class="bx-controls-auto-item"><a class="bx-stop" href="">Stop</a></div></div></div></div><br>
</div>
</div>
To achieve this you could add to your image inside .bx-wraper:
.bx-wrapper img {
display: inherit;
position:absolute;
left:0;
right:0;
top:0;
bottom:0;
margin:auto;
max-width: inherit;
}
now you need to set the parent li to relative:
.bx-wrapper ul li {
position:relative;
width:100%;
height:100%;
list-style: none !important;
margin: 0 !important;
}
and change your parent ul:
.bx-wrapper ul{
width:100%;
}
I have solved the above by changing my image slider. My new slider plugin (Envoke Supersized) uses a background image and was easier to use background-size:cover; css. Although after this full viewport height on mobile widths didn't look good with my images so I used 40vh on mobile widths.
I would recommend anyone trying to do the same to ensure your image is a background image, or replace the slider with a static background image for small browser widths.

homepage site composed by 2 images arranged horizontaly

I want to arrange the two pictures in a way that will always compose the word "Charleston" in the middle of the screen. I want this to be responsive to different screen resolutions. Can you help me with that?
<div id="leftHalf"></div>
<div id="rightHalf"></div>
#leftHalf {
background: url(/charback3.jpg);
width: 50%;
position: absolute;
top: 0px;
left: 400px;
bottom: 0px;
height: 100%;
background-repeat: no-repeat !important;
}
#rightHalf {
background: url(/charback4.jpg);
width: 50%;
position: absolute;
right: 0px;
top: 0px;
height: 100%;
background-repeat: no-repeat !important;
}
View My Page
Here is one method, using display:inline to keep the two images on the same horizontal line. max-width:50% keeps the images at a maximum of 50% of their container's width without expanding beyond their native widths.
Note that using display:inline will preserve whitespace. So, remove the whitespace between your two <img> tags.
<div id="container">
<img src="/charback3.jpg" alt="" /><img src="/charback4.jpg" alt="" />
</div>
html, body {
margin:0;
}
div#container {
text-align:center;
}
div img {
display:inline;
max-width:50%;
}
WORKING EXAMPLE (jsfiddle)

Alternate to background-size property to use in IE8

I have to resize the buttons on the screen initial size of button 157*70px and required size on screen is 100*50px. It has to be compatible with IE8 where the background-size property is not working although this property works fine in FF.
HTML:
<div id="return_button">
<a class="new_return_button" name="PREVIOUS">Previous</a>
</div>
CSS:(Firfox)
.new_return_button{
background: url("images/previous.png") no-repeat scroll 0 0 rgba(0, 0, 0, 0);
backgound-size: 100px 50px;
color: #FFFFFF;
cursor: pointer;
display: block;
height: 70px;
line-height: 70px;
width: 157px;
}
#return_button{
color: #FFFFFF;
font-weight: bold;
height: 70px;
left: 10px;
line-height: 70px;
margin: 0;
position: absolute;
text-align: center;
width: 157px;
}
This css works fine in Firefox with background-size property and shrinks the image of 157*70px to area of 100*50px but doesn't work in IE8.
Please suggest a solution to this issue
One way to solve this is to use another element. You probably need to tweak the margins of the <span> to have it working as desired. Also note that this does not guarantee a specific height, instead it will give you the correct aspect ratio for the scaled graphic.
<style>
#return_button {
position: relative;
width: 100px;
}
#return_button img {
max-width: 100%;
height: auto;
}
#return_button span {
position: absolute;
top: 50%;
margin-top: -5px;
left: 10px;
right: 10px;
text-align: center;
}
</style>
<div id="return_button">
<img src="images/previous.png" alt="Button graphic">
<span>Button label</span>
</div>

Resources