Why doesn't margin:auto center an image? - html

<html>
<head>
<title>Test</title>
</head>
<body>
<div>
<img src="queuedError.jpg" style="margin:auto; width:200px;" />
</div>
</body>
</html>
The div expands to 100% as it should but the image does not center itself. Why?

Because your image is an inline-block element. You could change it to a block-level element like this:
<img src="queuedError.jpg" style="margin:auto; width:200px;display:block" />
and it will be centered.

You need to render it as block level;
img {
display: block;
width: auto;
margin: auto;
}

Add style="text-align:center;"
try below code
<html>
<head>
<title>Test</title>
</head>
<body>
<div style="text-align:center;vertical-align:middle;">
<img src="queuedError.jpg" style="margin:auto; width:200px;" />
</div>
</body>
</html>

i know this is an old post, but wanted to share how i solved the same problem.
My image was inheriting a float:left from a parent class. By setting float:none I was able to make margin:0 auto and display: block work properly. Hope it may help someone in the future.

I've found that I must define a specific width for the object or nothing else will make it center. A relative width doesn't work.

open div then put
style="width:100% ; margin:0px auto;"
image tag (or) content
close div

<div style="text-align:center;">
<img src="queuedError.jpg" style="margin:auto; width:200px;" />
</div>

I have found...
margin: 0 auto; works for me. But I have also seen it NOT work due to the class being trumped by another specificity that had ... float:left;
so watch for that you may need to add ... float:none; this worked in my case as I was coding a media query.

Related

Center arbitrary image horizontally

Why is it so difficult (or as one answer said, "It is not possible.") to center an arbitrary image horizontally? I have had centralized images working for several years; suddenly they sit obstinately at the left. Has there been some recent change in CSS that causes this?
I expect the code below, modified from the CSS DIY, to work, but it does not.
<!DOCTYPE html>
<html><head>
<style>
img { display:block; }
</style>
</head>
<body>
<h2>Thumbnail Images</h2>
<p> ... </p>
<div style="margin: 0 auto;">
<img src="paris.jpg" alt="Paris"
width=15% >
</div>
</body></html>
I realize that scaling an image by percent width is (for no known) reason disallowed, but Jukka advised me to use it anyway, because it works in all browsers I have tried and does exactly what I want, which is to maintain image size proportional to page width. If I float the image right or left it works fine, and I can run a caption alongside the image, but the obvious 'margin : 0 auto;' fails, for no good reason I can see.
Margin : Auto
You can set the margin property to auto to horizontally center the element within its container.
The element will then take up the specified width, and the remaining space will be split equally between the left and right margins
Add
img {
display:block;
margin: 0 auto;
}
<!DOCTYPE html>
<html>
<body>
<h2>Thumbnail Images</h2>
<p> ... </p>
<div>
<img src="https://www.w3schools.com/css/trolltunga.jpg" alt="Paris" >
</div>
</body>
</html>
You should add the styles
display:block;
margin: 0 auto;
To your img element
<div style="width:100%;background:skyblue;">
<img style='display:block;width:25%;margin:0 auto;' src='https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQvl0jMbupgXjeP66hak-u3uwUPcqI3Ovx7zqiWkVhav2V8FjeY1A'/>
</div>

I am trying to centre a banner

I am very new to HTML and CSS.
I am trying to centre a banner at the top of the screen.
I have tried setting the margin left and right to auto, however when I try that, or text-align: center, nothing happens and I'm not sure why...
I placed the banner within a div.
<div class="bruceBanner">
<a href="http://www.example.com">
<img border="0" alt="XYZ Banner" src="http://www.fablevision.com/northstar/make/objects/banner3.gif" width="553" height="172">
</a>
</div>
And referenced its class like so.
.bruceBanner {
margin-left: auto;
margin-right: auto;
}
The full html is below, in case of any mistakes I am unaware of.
<DOCTYPE HTML>
<html>
<head>
<link type="text/css" rel="stylesheet" href="stylesheet.css"/>
<title>XYZ Products</title>
<meta charset="utf-8"/>
</head>
<body>
<div class="bruceBanner">
<a href="http://www.example.com">
<img border="0" alt="XYZ Banner" src="http://bit.ly/1QSpdbq" width="553" height="172">
</a>
</div>
</body>
</html>
.bruceBanner is a div element.
div elements by default are block-level elements, meaning they take up 100% width.
You need to set a width smaller than 100% or change it's display to an inline-block.
.bruceBanner {
margin: 0 auto;
width: 50%;
}
You forgot a .:
.bruceBanner {
^---
. in CSS is for a class. without the dot, you're trying to style an unknown/illegal html tag <bruceBanner>
From w3schools.com
.bruceBanner {
margin: auto;
}

How do I center an embedded SVG File?

I'm using an embedded SVG file as the top element on my page. (a header?)
It loads just fine, but nothing I try seems to get it centered.
Here's what I'm using at the start of the body:
<embed src="images/bannertest.svg" width="1180" height="200" type="image/svg+xml"/>
I've wrapped it up in a DIV and used CSS to "margin:auto" but can't get it to budge without just padding it. The problem is that it goes wonky when I resize the window.
HTML
<div class="parent">
<embed src="images/bannertest.svg" width="1180" height="200" type="image/svg+xml"/>
</div>
CSS
.parent{
display:block;
margin:0 auto;
}
or use this css
.parent embed{
display:table;
margin:0 auto;
}
I am adding code here. Check it may be it helps you..
HTML:
<div>
<img class="papa" height="100" src="http://s.cdpn.io/3/kiwi.svg">
</div>
CSS:
<style>
div { text-align:center}
</style>
Here is a fiddle http://jsfiddle.net/6sRR2/
Hope. It Helps You :)

How to make a <div> or <a href="#"> to align center

I'm using the following code in body section.
<center>Get Started</center>
Is there any alternative for <center> tag?
How can I make it center without using <center> tag?
Add text-align:center;display:block; to the css class. Better than setting a style on the controls themselves. If you want to change it you do so in one place.
You can do this:
<div style="text-align: center">
Get Started
</div>
You can use css like below;
Get Started
You can put in in a paragraph
<p style="text-align:center;">Get Started</p>
To align a div in the center, you have to do 2 things:
- Make the div a fixed width
- Set the left and right margin properties variable
<div class="container">
<div style="width:100px; margin:0 auto;">
<span>a centered div</span>
</div>
</div>
I don't know why but for me text-align:center; only works with:
text-align: grid;
OR
display: inline-grid;
I checked and no one style is overriding.
My structure:
<ul>
<li>
<a>ElementToCenter</a>
</li>
</ul>
You can use the code below:
a {
display: block;
width: 113px;
margin: auto;
}
By setting, in my case, the link to display:block, it is easier
to position the link.
This works the same when you use a <div> tag/class.
You can pick any width you want.
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
<h4 align="center">
Center
</h4>
</body>
</html>
There's also a handy tag... whatever you put in between, gets centered.
Like so:
<center>
<a href..... whatever>
</center>
you can try this
display inline-block to make it fit width,
margin-left to make it move 50% away from left,
and transform translateX to make it center his position
display: inline-block;
margin-left: 50%;
transform: translateX(-50%);
In your html file:
Get Started
In your css file:
.hpbottom{
text-align: center;
}

Filling image in div

I want to create a basic layout for webpage with divs and want to set images for their background.
Since I have smaller images I want to stretch them to fill in the divs.
There are many ways to do that. But I tried following:
</html>
<head>
<style>
img#bg {
top:0;
left:0;
width:100%;
height:100%;
}
<style>
<head>
<body>
<img src="body.jpg" alt="background image" id="bg" />
<div id="content"> </div>
<body>
</html>
This worked. Then I tried to make use of it in layout.
<div id="hmenu" style="zindex=1;height:80px;background-color:#007980"></div>
<div id="content" >
<img src="body.jpg" alt="background image" id="bg" />
</div>
This also worked. But when I tried to set image this way for a div with float:left or CSS width set, it did not worked:
<div id="header" style="zindex=1;height:300px;width:100%"></div>
<div id="hmenu" style="zindex=1;height:80px;background-color:#007980"></div>
<div id="content" style="float:right" >
<img src="body.jpg" alt="background image" id="bg" />
</div>
This doesnt work. In last HTML notice float:right.
I will like to stick to this method, not any jQuery method or others and also will like to know what is wrong here and what should be done to get the desired result with CSS modifications as I am learning this.
Seems like you want a background image
A good explanation can be found here
Basically you can make a div have a background using CSS and not having to put an tag inside, this is almost always preferable.
Example code for you could be:
body {
background-image: url('body.jpg');
background-size: cover;
}
In order for height: 100%, Top:0 etc to work you need to have a position applied to the element.
You don't as per the example code given. Give more code and i can help more. But from what you have given this is your problem.
background-size: cover;
Is a nice solution, but I'm not sure about the browser support, because it's CSS3.
I made a fiddle, is this what you were looking for?
http://jsfiddle.net/NQY6B/5/
By the way, change "zindex" to "z-index".
EDIT: I've updated the fiddle with text content in the div