Centering image horizontally and vertically [duplicate] - html

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to make an image center (vertically & horizontally) inside a bigger div
I am trying to make an image gallery on my website,
but I cannot center the image inside the div.
The CSS for the div is:
#animation
{
display : none;
position: absolute;
z-index:1;
background : rgba(0,0,0,0.7);
height: 100%;
width: 100%;
border: 0px;
}
And the image id is:
#pictures
{
...
}

One way to do it is a table cell display and vertical align: middle;
Fiddle

margin:auto; add autommatic margin and center the image have a try, but I need more code , could you post it?

margin:auto;
it will automatically center your div by adding equal margin to the left and right of your div block element

Check out these resources:
If you know the width of the div - How to make an image center (vertically & horizontally) inside a bigger div
If you don't know the width of the div - http://doctype.com/css-centering-wide-image-unspecified-dimensions-div-unspecified-dimensions

Put your image inside div and center it like this:
Fiddle here
div{
width:300px;
height:300px;
margin:0px auto;
margin-top:30px; //to pull it down
}

Related

Horizontally center object inside parallelogram

I currently have parallelogram that was created with a div and the css property clip-path. This shape is stake to the stop and bottom of the screen with a dynamic width as well. I'm trying to figure out how to horizontally (not vertically) center the content.
My issue is that any text that I try to center seems to center to the rectangle, not the parallelogram that I have created.
So far I've tried to use left and right padding, percentage based margins, and clip paths of the content within the div to no avail.
I've attached a codepen with where I'm at so far. Any help would be greatly appreciated.
You need to add
text-algin-center;
to your
.sidebar-callout-padding and
display:inline-block;
to your paragraphs (give them an unique ID)
#wrapper {
background-color: yellow;
text-align: center;
}
#yourdiv {
background-color: green;
display: inline-block;
}
<div id="wrapper">
<div id="yourdiv">Your text</div>
</div>

Prevent floated image from moving centered title in div [duplicate]

This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
How to align a div to the left and another div to the center?
(2 answers)
Closed 4 years ago.
I currently have a "header" div which contains the title of my site and an image of the product. The title is an h1 (heading) and the image is supposed to be floated to the left. I am centering the heading with text-align: center and using float: left on the image. The problem is that the image takes space and moves my title to the left which makes it look very strange when you compare it to the footer which is correctly centered.
It is possible that this is a duplicate of a common problem but due to my limited knowledge in CSS terms, it makes it very hard to find the answer by a Google search. I've used multiple search phrases but I've yet to find anyone with a similar problem.
I've tried multiple ways such as using position: absolute and similar ways which don't seem to work. I've also tried putting the img and h1 in a different order in the HTML, if I put the image after the heading it will cause the image to "drop down" under the whole div and this looks even odder.
Code (HTML & CSS)
| HTML
<div class="header">
<img src="assets/image/Logo-small.png">
<h1>Llama Capture</h1>
</div>
| CSS
.header {
width: 100%;
height: 6%;
text-align: center;
background-color: black;
}
.header img {
height: 100%;
float: left;
}
I expect the heading to be fully centered just as the footer is and I thought text-align would work without the influence of other elements in the div. The actual result would definitely irritate someone with an eye to detail.
I am thankful for anyone that could point me in the right direction, I might have overlooked this problem and so I'm happy if someone more experienced could share their knowledge on this.
use position:absolute; and left: right: values instead of float:right/left
.header {
width: 100%;
background-color: black;
color:#fff;
position:relative;
display:flex;
align-items:center;
justify-content:center;
}
.header img {
height: 100%;
position:absolute;
left:10px;
top:50%;
transform:translateY(-50%);
}
<div class="header">
<img src="https://picsum.photos/20">
<h1>Llama Capture</h1>
</div>

Resize images, and center horizontally and vertically within container

I am implementing a carousel with images. The carousel is 960px wide, and contains 5 images in containers of width 960px/5 = 192px (and height 119px).
I want the images to be as large as possible inside their containers, without changing the aspect ratio of the images. I also want the images to be centered both horizontally and vertically within their container.
By hacking around for hours, and using the center tag, I have managed to construct what I describe above. Please see a fiddle here.
The problem is with the container of the second image (as shown by the black border). While the second image is centered horizontally, the container is shifted down a little.
I'm trying to implement an overlay on the images, and need the containers to all be at the same height. How can I have the containers all at the same height? Is there a better/cleaner approach that does not use the center tag?
You could add vertical-align:top; to your #carousel-images .image{} css
Or middle or bottom...
Uh? Why did I get downvoted on this?
http://jsfiddle.net/y2KV7/
I got it to work by doing the following:
HTML:
<div id="wrapper">
<div id="carousel-images">
<img src="http://eurosensus.org/img/initiatives-300/30kmh.png" />
<img src="http://eurosensus.org/img/initiatives-300/affordableEnergy.png"/>
<img src="http://eurosensus.org/img/initiatives-300/basicIncome.jpg"/>
<img src="http://eurosensus.org/img/initiatives-300/ecocide.jpg"/>
<img src="http://eurosensus.org/img/initiatives-300/educationTrust.jpg"/>
</div>
</div>
CSS:
#wrapper
{
width: 100%;
text-align: center;
background: blue;
}
#carousel-images
{
width: 960px;
white-space: nowrap;
font-size: 0;
margin: 0 auto;
}
#carousel-images img
{
display: inline;
max-width: 192px;
max-height: 119px;
border: 1px solid black;
vertical-align: middle;
}
Click here to view working jsFiddle demo
First, don't make the world come back to 10 years ago. do not use tag for formating. I would also suggest you to get some reading about different between div and span as well as display attribute. you could easily find information on http://www.w3schools.com.
if you want a center container. you could use css margin auto trick.
like margin:5px auto; would center the container horizontally.

vertical-align in div with height 100% [duplicate]

This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 8 years ago.
Here's my situation: I'm trying to make a page with two DIVsfilling whole page (height 100%, width 50% each). Also, the content in the DIVs is to be vertically aligned to middle.
Is there an easy way to achieve this without hacks or javascript?
I've tried body,html{height:100%;} .mydiv {display:table-cell;height:100%;vertical-align-middle}
but that doesn't work...and with that code, i have to specify width in pixels instead of percentage
Live Demo
I just made a jsFiddle showing my suggestion to a solution. Here I take into account that you want two <div>s filling 50% of the width each, 100% height, and that you want the content to be vertically aligned in the middle. Here is the simplified working solution with source code.
HTML
<div id="outer">
<div id="table-container">
<div id="table-cell">
This content is vertically centered.
</div>
</div>
</div>
CSS
#outer {
position:absolute;
top:0;
left:0;
width:50%;
height:100%;
}
#table-container {
height:100%;
width:100%;
display:table;
}
#table-cell {
vertical-align:middle;
height:100%;
display:table-cell;
border:1px solid #000;
}
For reference, I used this tutorial.
position: absolute;
top: 0;
bottom: 0;
Will give you a box that fills to 100% height. Make sure your HTML and BODY tags are large enough:
html, body {
height: 100%;
}
Do you want this type of design ? => Example Fiddle

Image in absolute position div [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to make an image center (vertically & horizontally) inside a bigger div
I have a div which is with absolute position (width/height are 100%).
Notice, not px, percents.
Inside this div I have an image. How do I center this image in this div?
CSS:
/* where margin-left = {img width}/2, and margin-top= {img height}/2 */
.bigdiv {
width:100%;
height:100%;
position:absolute;
}
.bigdiv img {
position:absolute;
left:50%;
top:50%;
margin-left:-10px;
margin-top:-10px;
}
HTML:
<div class="bigdiv"><img src="eg.png" /></div>
You could also put your margin-left, margin-top commands as a style on the img tag instead (since they're unique per image).