Positioning vertical center - html

I want to center these 4 images. Horizontally it is working but vertically it won't work. Does anybody knows how to solve this problem? At the moment I've got a black header/footer section with 4 images centered horizontally. Everything is scalable but not the height of the images.
Am doing it right?
HTML:
<section>
<div class="pic">
<img src="img.png" alt="Pic" />
</div>
<div class="pic">
<img src="img.png" alt="Pic" />
</div>
<div class="pic">
<img src="img.png" alt="Pic" />
</div>
<div class="pic">
<img src="img.png" alt="Pic" />
</div>
</section>
CSS:
body {
margin:0;
padding:0;
max-width: 100%;
max-height:100%;
}
section {
position:absolute;
top:5%;
bottom:5%;
left:0;
width:100%;
height:90%;
}
section img {
width:12.5%;
float:left;
margin-left:10%;
}

Assuming the width of the image equals the height of the image (like the code you gave has), you can just use margin-top of 50% - imageHeight. That would look like
section img {
width:12.5%;
margin-top:32.5%;
float:left;
margin-left:10%;
}
Demo
If they're not, you can use this pure CSS approach

I think its easier to wrap the images in a container and then center the container in the container's parent, in this case you could use an Article tag to wrap the images an then center that article in the section like this
HTML
<section>
<article>
<div class="pic">
<img src="img.png" alt="Pic" />
</div>
<div class="pic">
<img src="img.png" alt="Pic" />
</div>
<div class="pic">
<img src="img.png" alt="Pic" />
</div>
<div class="pic">
<img src="img.png" alt="Pic" />
</div>
</article>
</section>
CSS
html, body {
height: 100%;
width: 100%;
}
section {
top: 5%;
left: 0%;
width: 100%;
height: 90%;
/* strech */
overflow: hidden;
}
article {
width:80%;
height:40%; /* change this to set height */
/* CSS absolute center begin */
border: 2px solid #0000ff;
margin: auto;
position: absolute;
display: inline-block;
top: 0;
bottom: 0;
left:0;
right:0;
/* CSS absolute center end*/
}
.pic {
position: relative;
float: left;
width: 12.5%;
height: 100%;
margin-left: 10%;
}
.pic img{
position: relative;
width:100%;
height:100%;
}
Hope it help you

You can give the section position:relative; and then do a trick with the images.
The sample below makes them centered of the section, if you want to have the images spread out, but vertically aligned, you need to do this trick on the containing element (like a dive around the images in the section:
section {
position:relative;
top:5%;
bottom:5%;
left:0;
width:100%;
height:90%;
}
section img {
/*width:12.5%;
float:left;
margin-left:10%;*/
position:absolute;
top:50%;
margin-top:-25%;/* should be half the height of the image */
left:50%;
margin-left:-25%;/* should be half the width of the image */
}

Related

Center different size images in the same block size

I have different images encapsulated in square style blocks that I would like to always center in but I'm having a heck of a time trying to get them to do so.
I made an example of what's happening to me in similar design. Notice the product (the grill) is not actually centered in the imgblock container.
This starts to become very apparent with other product images that are much wider than narrow.
.imgBlock {
width:100px;
height:100px;
border:2px solid black;
margin:1px;
padding:4px;
}
.imgBlock img{
max-height:100%;
max-width:100%;
margin:auto;
display:block;
}
<div class="container">
<div class="imgBlock">
<img src="https://cdn.shopify.com/s/files/1/1150/2512/t/41/assets/WG-logo-short-black.png?43066">
</div>
<div class="imgBlock">
<img src="https://cdn.shopify.com/s/files/1/1150/2512/t/41/assets/M1-Grill-FAQ.jpg?43066">
</div>
</div>
First set the image to full width and height (or as desired). Now you can add object-fit: contain to contain the image to the imgBlock and now use object-position: center to align it - see demo below:
.imgBlock {
width:100px;
height:100px;
border:2px solid black;
margin:1px;
padding:4px;
}
.imgBlock img{
height:100%; /* set full height */
width:100%; /* set full width */
display:block;
object-fit: contain; /* constrains the image maintaining aspect ratio */
object-position: center; /* default position is center - so optional in this case */
}
<div class="container">
<div class="imgBlock">
<img src="https://cdn.shopify.com/s/files/1/1150/2512/t/41/assets/WG-logo-short-black.png?43066">
</div>
<div class="imgBlock">
<img src="https://cdn.shopify.com/s/files/1/1150/2512/t/41/assets/M1-Grill-FAQ.jpg?43066">
</div>
</div>
You can use the older positioning attributes as well as the Flex techniques. Make the main block position relative. Then set the img inside that block to position: absolute. Place that block element to top: 50% left: 50% of the parent element. Since this goes by the top left corner it will be slightly of the center. We will then use transform: translate(-50%, -50%) to bring it back to the true center.
More on position here at the MDN.
.imgBlock {
position: relative;
width:100px;
height:100px;
border:2px solid black;
margin:1px;
padding:4px;
}
.imgBlock img{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
max-height:100%;
max-width:100%;
display:block;
}
<div class="container">
<div class="imgBlock">
<img src="https://cdn.shopify.com/s/files/1/1150/2512/t/41/assets/WG-logo-short-black.png?43066">
</div>
<div class="imgBlock">
<img src="https://cdn.shopify.com/s/files/1/1150/2512/t/41/assets/M1-Grill-FAQ.jpg?43066">
</div>
</div>
You can set position relative to your div .imgBlock.
After that set the position absolute to your <img/> with all coordinates to 0 and margin auto.
Remember that a position absolute is referred to the nearest parent with position relative.
.imgBlock {
width:100px;
height:100px;
border:2px solid black;
margin:1px;
padding:4px;
position:relative;
}
.imgBlock img{
max-height:100%;
max-width:100%;
position: absolute;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
<div class="container">
<div class="imgBlock">
<img src="https://cdn.shopify.com/s/files/1/1150/2512/t/41/assets/WG-logo-short-black.png?43066">
</div>
<div class="imgBlock">
<img src="https://cdn.shopify.com/s/files/1/1150/2512/t/41/assets/M1-Grill-FAQ.jpg?43066">
</div>
</div>
You can add display: flex to imgBlock, then center horizontally with justify-content and vertically with align-items.
.imgBlock {
display: flex;
align-items: center;
justify-content: center;
width:100px;
height:100px;
border:2px solid black;
margin:1px;
padding:4px;
}
.imgBlock img{
max-height:100%;
max-width:100%;
margin:auto;
display:block;
}
<div class="container">
<div class="imgBlock">
<img src="https://cdn.shopify.com/s/files/1/1150/2512/t/41/assets/WG-logo-short-black.png?43066">
</div>
<div class="imgBlock">
<img src="https://cdn.shopify.com/s/files/1/1150/2512/t/41/assets/M1-Grill-FAQ.jpg?43066">
</div>
</div>

How to make two image button hyperlinks side-by-side take up full screen?

I want to have two images, left and right halves of the page, that when clicked, re-direct to a particular link.
When the user enters the webpage, I want them to see nothing but these two images (which are clickable), and I don't want it to be scrollable either, i.e. I just want it to fit on the single page.
Not very experienced with HTML - how to modify/rewrite the code below?
<a href="http://www.google.com">
<img src="left_image.png" alt="Go to Google!" >
</a>
<a href="http://www.facebook.com">
<img src="right_image.png" alt="Go to Facebook!" >
</a>
Looking something like this
html,body{
width:100%;
height:100%;
margin:0
}
div.container{
position:relative;
width:100%;
height:100%;
box-sizing:border-box;
}
.left, .right{
width:50%;
height:inherit;
float:left
}
a{
display:block;
width:100%;
height:inherit
}
.left{
background:url(http://www.planwallpaper.com/static/images/desktop-year-of-the-tiger-images-wallpaper.jpg);
background-size:cover;
}
.right{
background:url(https://pixabay.com/static/uploads/photo/2016/03/28/12/35/cat-1285634_960_720.png);
background-size:cover;
}
<div class="container">
<div class="left">
<a href="www.google.com" ></a>
</div>
<div class="right">
</div>
</div>
The best method I think would be to use a flexbox with viewport units (vh and vw) and use a backround-image with background-size: cover - see demo below:
body{
margin:0;
display:flex;
}
*{
box-sizing:border-box;
}
a {
width:50vw;
height: 100vh;
border:1px solid red;
display:flex;
overflow: hidden;
background-size: cover;
background-position: center;
}
a.left{
background-image: url('http://placehold.it/500x500');
}
a.right{
background-image: url('http://placehold.it/600x600');
}
<a class="left" href="http://www.google.com"></a>
<a class="right" href="http://www.facebook.com"></a>
There is a lot of answers to this so just choose what you like. This is how to work with it in general no matter what goes in the div.
.holder {
width: 100vw;
height: 100vh;
display: flex;
overflow:hidden;
}
.one, .two {
width: 50%;
}
.one {
background: red;
}
.two {
background: green;
}
<div class="holder">
<div class="one"><a href="http://www.google.com">
<img src="left_image.png" alt="Go to Google!" >
</a>
</div>
<div class="two"><a href="http://www.facebook.com">
<img src="right_image.png" alt="Go to Facebook!" >
</a></div>
</div>

Align an icon horizontally to image whose width vary with only CSS?

I have a image whose size varies and an icon. I want align the icon horizontally to the image like this:
UPDATE:
Add a jsfiddle example: http://jsfiddle.net/p20qj06u/ . I replaced the icon with <div class="icon">icon</div>. I have to keep the image in this position(floating to right), then put the "icon" in middle of the image.
Given the requirements you can add both images in one container you
can call .img-wrapper
Apply clear:both; to the whole container .img-wrapper which is
float: right.
Then display:block; on the icon and margin:0 auto;
DEMO http://jsfiddle.net/a_incarnati/p20qj06u/2/
.container .img-wrapper {
float: right;
}
.icon{
clear:both;
display:block;
margin:0 auto;
}
.container .img-wrapper {
float: right;
}
.icon{
clear:both;
display:block;
margin:0 auto;
}
<div class="container">
<div class="img-wrapper"><img src="http://7xi5mk.com1.z0.glb.clouddn.com/FivlNV1GzShuj6lGpdCNip_6BYP8" alt="" />
<img class="icon" src="http://placehold.it/20x20" alt="" />
</div>
Put the icon inside the image's div and set the icon margins to auto.
<div class="image">
<div class="icon"></div>
</div>
.icon {
Left:0;
Right:0;
Margin-left:auto;
Margin-right:auto;
}
You should be able to center the icon with
text-align: center;
Unless the containing element isn't set to
display: block;
If that doesn't solve it, I suggest providing a snippet of an example so that we can clearly see the issue and how to resolve it :)
Create one div as container. Center content, set container width (% or px).
UPDATE: (with float: left;) https://jsfiddle.net/sa1axf46/2/
html
<div class="container">
<img src="#" alt="img">
<div class="icon">ico</div>
</div>
css
.container {
text-align: center;
}
.container img {
float: left;
width: 100%;
}
.container .icon {
width: 100%;
}

Center a stack of div's CSS

I am trying to stack a div on top of a div and center those two divs horizontally in the page.
.sandBox{
position:relative;
}
.product-image, .option-image{
position:absolute;
margin-left:auto;
margin-right:auto;
}
.product-image{
z-index:-1;
}
<div class="sandBox">
<div class="product-image">
<img src="main-product-image.jpg" width="1440" height="560" alt=""/>
</div>
<div class="option-image">
<img src="overlay.png" width="1440" height="560" alt=""/>
</div>
</div>
This stack part works fine but I can not seem to get them centered no mater what I try.
This should make them centered.
.product-image, .option-image{
position:absolute;
margin-left:auto;
margin-right:auto;
left: 0; /*NEW*/
right: 0; /*NEW*/
}
DEMO: http://jsfiddle.net/hno8bxty/

How to make a content to align after wrapper?

I'm trying to position the contents:
g_image5 which I use it like a background of the entire wrapper
image7 which is the banner over the g_image5
text3 its a paragraph and I have align it in the middle of the wrapper
On my resolution it shows up on the middle but on a higher resolution for example it shows up in the left. I think its because when I aligned it with left:28,5% it goes after the html size which is 100% width.
So my question is: how should I write on the wrapper so that the content to be positioned after it?
html
{
width:100%;
height:100%;
overflow-y:auto;
}
<div id="wrapper" width=100% z-index:-1>
<div id="g_image5" style="position:absolute; overflow:hidden; left: 23%; top: 18%;
width:55%; z-index:0"><img src="images/content2.jpg" alt="" title="" border=0
width=100% height=150%></div>
<div id="image7" style="position:absolute; overflow:hidden; left:38%; top:20%;
width:25%; height:13%; z-index:3"><img src="images/2.png" alt="" title="" border=0
width=100% height=100%></div>
<div id="text3" style="position:absolute; overflow:hidden; left:28.5%; top:40%;
width:50%; height:20%; z-index:3">
<div class="wpmd">
<div><font face="Tahoma" class="ws13">Here is a paragraph and here's also the problem</font></div>
</div>
</div>
</div>
in your case the problem is with this combination:
#g_image5 {
width: 55%;
left: 23%;
top: 18%;
}
Instead you should do something like this, which will center all elements inside your wrapper and text inside horizontally:
#wrapper {
text-align: center;
}
then to have a scaleable image, just do something along these lines:
#g_image5 {
width: 70%;
height: auto;
}
An alternative is to avoid img overall and have a picture as a background in CSS:
#g_image5 {
background: url(/*path to your img */) no-repeat center center;
}