Vertically Align Fixed Size Div - html

I have read all the other questions on Stackoverflow related to this topic, and none seem to fix my issue.
I have a nav bar at the top of my page that is fixed to the top left corner then a div in the centre containing an image and some texts.
My content is as so:
<div class="center">
<div id="logo"> <a class="home" href="index.html"> <img class="noborder" src="images/logocat.png" alt="" /> </a>
</div>
<div id="top">
<p class="toptext">TEXT</p>
</div>
<div id="contact">
<table border="0" align="center">
<tr>
<td><a href="http://be.net/crookedcartoon"></td>
<td><p class="contact-text1"> CROOKEDCARTOON#GMAIL.COM </p>
<p class="contact-text2"> Alex: TEXT </p></td>
<td><img class="noborder" src="images/seriesindex.jpg" onmouseover="this.src='images/serieshover.jpg'" onmouseout="this.src='images/seriesindex.jpg'" alt="" />
</td>
</tr>
</table>
</div>
<div id="about">
<p class="content-text-index">TEXT</p>
</div>
</div>
The CSS for the mentioned div is:
.center {
height: 984px;
width: 504px;
position:absolute;
top:50%;
left:50%;
margin-top:-492px;
margin-left:-257px;
padding:0px;
}
It wont centre vertically or horizontally there is no div or wrapper surrounding this div, or any conflicting css (as far as im aware) and it's becoming a pain.
Can anyone suggest a fix? I have tried the half margin/width etc idea and that is what is displayed above.

I notice you are using #center instead of '.center` in your CSS.
.center { /* as you have used a class in your HTML */
height: 984px;
width: 504px;
position:absolute;
top:50%;
left:50%;
margin-top:-492px;
margin-left:-257px;
padding:0px;
}

ok, forget everything and just use this CSS
html, body{ height: 100%; }
.center {
height: 984px;
width: 504px;
position:absolute;
top:50%;
left:50%;
margin-top: -492px;
margin-left: -257px;
padding:0px;
}
And let me know if it's working.
Use CSS flex-box : http://www.sketchingwithcss.com/samplechapter/cheatsheet.html
.center {
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row /* works with row or column */
flex-direction: row;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
}
You can go with CSS flex-box. and for old versions of IE you need use either this jquery solution http://dipaksblogonline.blogspot.in/2011/02/div-content-vertical-align-centermiddle.html
or use the display: table; property CSS to vertically align text to middle of div

You can apply a ghost element to fill the 100% of the height of a parent element.
.wrap {
text-align: center;
height: 500px;
background: #d4d4d4;
}
.wrap:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.center {
display: inline-block;
vertical-align: middle;
width: 300px;
}
for a markup like this:
<div class="wrap">
<div class="center">...</div>
</div>
An example: http://jsfiddle.net/LayyM/

Related

Icons in footer

I have a footer with four icons and they lie on top of each other. My aim is to arrange the four icons side by side. I tried different things but nothing worked. Either I have the icons all at one point in the middle, the left side at the bottom or vertical in a row. Would ne nice to get some help for that!:)
<style>
.i{
margin: 50px;
}
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: red;
color: white;
text-align: center;
float: left;
}
</style>
<body>
<p class="container" div align="center" class = "i">
<img src="static/website/media/profile.jpg" class="rounded-circle" alt="My image" width="254" height="186"></p>
<p class = "i" div align="center">I am a 20 years old computer science student from Berlin. Let's go for a walk!</p>
<div class = "footer">
<p><img src="static/website/media/iconfinder_4691356_discord_icon.svg" alt="discord"></p>
<p><img src="static/website/media/iconfinder_1964405_linkedin_logo_media_social_icon.svg" alt="discord"></p>
<p><img src="static/website/media/iconfinder_4202766_email_gmail_mail_icon.svg" alt="gmail"></p>
<p><img src="static/website/media/iconfinder_287723_goodreads_icon.svg" alt="goodreads"></p>
</div>
</body>
</html>
First of all, it doesn't make sense to use this if there will only be an image inside the "p" tag. I removed them.
We created a new element named ".footer--icons" in the footer and included all the images.
The next is easy, we set the Element to "display:flex" and align it all side by side.
See: excellent article about flex-box
Also with "align-items" we have centered them all on the "Y" axis and with "justify-content" we have centered them all on the "X" axis relative to their parent.
.i {
margin: 50px;
}
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: red;
color: white;
text-align: center;
float: left;
}
.footer--icons {
display: flex;
align-items: flex-center;
justify-content: center;
}
.footer--icons > img {
margin: 5px;
}
<p class="container" div align="center" class="i">
<img src="static/website/media/profile.jpg" class="rounded-circle" alt="My image" width="254" height="186"></p>
<p class="i" div align="center">I am a 20 years old computer science student from Berlin. Let's go for a walk!</p>
<div class="footer">
<div class="footer--icons">
<img src="static/website/media/iconfinder_4691356_discord_icon.svg" alt="discord">
<img src="static/website/media/iconfinder_1964405_linkedin_logo_media_social_icon.svg" alt="discord">
<img src="static/website/media/iconfinder_4202766_email_gmail_mail_icon.svg" alt="gmail">
<img src="static/website/media/iconfinder_287723_goodreads_icon.svg" alt="goodreads">
</div>
</div>
Just use CSS flex-blox and .footer as your container.
.footer {
display: flex;
justify-content: center/flex-start;
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: red;
color: white;
text-align: center;
float: left;
}
With justify-content you can control your flex-items either on center or left side of the parent container which is what you want to happen.
Add the following to you css footer class:
display:flex;
flex-direction:row;
Remove the paragraph tags:
<div class = "footer">
<img src="static/website/media/iconfinder_4691356_discord_icon.svg" alt="discord">
<img src="static/website/media/iconfinder_1964405_linkedin_logo_media_social_icon.svg" alt="discord">
<img src="static/website/media/iconfinder_4202766_email_gmail_mail_icon.svg" alt="gmail">
<img src="static/website/media/iconfinder_287723_goodreads_icon.svg" alt="goodreads">
</div>
Then amend your .footer class to the following:
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: red;
color: white;
display: flex;
}
To center the images, add justify-content: center to the footer class.
To distribute them evenly, add justify-content: space-between.
For a complete guide to flexbox (display: flex), you can check this article out: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

How to centre two images next to each other in css/html [duplicate]

This question already has answers here:
CSS - center two images in css side by side
(5 answers)
How to place two divs next to each other? [duplicate]
(13 answers)
Closed 4 years ago.
I am fairly new to html and css, and I was trying to make two images go side by side in the center of the screen (automatically adjusting themselves when the screen is scaled).
I have only been able to get the two images to go next to each other and not been able to center them or make them automatically scale.
<head>
<style>
* {
box-sizing: border-box;
}
.column {
float: left;
width: 33.33%;
padding: 5px;
}
.row::after {
clear: both;
display: table;
}
</style>
</head>
<body>
<div class="row">
<div class="column">
<img src="img_snow.jpg" alt="Snow" style="width:100%">
</div>
<div class="column">
<img src="img_forest.jpg" alt="Forest" style="width:100%">
</div>
<div class="column">
<img src="img_mountains.jpg" alt="Mountains" style="width:100%">
</div>
</div>
</body>
</html>
The two images should go next to each other in the centre of the screen, scaling themselves on different sized browser windows and when browser is zoomed in or out. I should be able to change the size of the images to what I need.
Here is a way to do it with flexbox
JSFiddle demo here
* {
height: 100%;
}
img {
display: block;
height: auto;
max-width: 100%;
}
.images {
display: flex;
justify-content: center;
align-items: center;
margin: auto 0;
padding: 0 200px;
}
#media screen and (max-width: 768px) {
.images {
flex-direction: column;
padding: 0;
}
}
<div class="images">
<img src="https://placekitten.com/600/400">
<img src="https://placekitten.com/600/400">
</div>
Add the following css-
body
{
text-align:center;
}
.row
{
display:inline-block;
}
I hope this will help you.
<html>
<head>
<style>
* {
box-sizing: border-box;
}
.ul_class {
width: 100%;
display:block;
text-align: center;
}
.li_class {
width: 33.33%;
display:inline-block;
margin-right: -4px;
}
</style>
</head>
<body>
<ul class="ul_class">
<li class="li_class"><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTpVNXBVaIl8ne-AYJfYxls2Js2dcYJohl5e-d3ompbZxrjtzlk" alt="Snow" style="width:100%"></li>
<li class="li_class"><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTpVNXBVaIl8ne-AYJfYxls2Js2dcYJohl5e-d3ompbZxrjtzlk" alt="Snow" style="width:100%"></li>
</ul>
</body>
</html>
Use below code in your css.
OR
You can use flex. check below link.
https://www.w3schools.com/cssref/css3_pr_justify-content.asp
.row{ width:80%; margin:0 auto; }
try using flex on the row element
.row {
display: flex;
justify-content: center;
align-items: center;
}

Cannot horizontally align images in print preview

i have two images shown in the browser which is horizontally align. my objective is to print them using javascript however the preview shows that the 2nd image is going under the first image as if it has a new line. What I need is to print this pictures horizontally align. I also tried adjusting the padding-top to align them but it doesnt work.
<img id = "telin_logo" class="responsive-img" src="img/telin.jpg" style="width: 133px;height: 77">
<img id = "telin_logo2" class="responsive-img" src="img/telin.jpg" style="width: 133px;height: 77px;">
what I mean by print is printing by printer. i used #media print to adjust the layout. sorry for missing out this information
my solution is purely based on css . you can see my snippet . i hope it helps you .
.container{
width:100%;
border:1px solid #222;
position:relative;
height:100%;
}
body,html{
height:100%;
width:100%;
}
.left{
width:50%;
float:left
}
.right{
width:50%;
float:left;
}
<div class="container">
<div class="left">
<p style="text-align:center"><img src="red.png"/></p>
</div>
<div class="right">
<p style="text-align:center"><img src="red.png"/></p>
</div>
</div>
You can also use below properties.
div {
width: 100%;
height: 77px;
border: 1px solid gray;
display: flex;
align-items: flex-start;
text-align: center;
margin: 0 auto;
}
img {
align-self: center;
margin: 0 auto;
}
<div>
<img src="http://placehold.it/133x77/CCCCCC&text=telin_logo">
<img src="http://placehold.it/133x77/CCCCCC&text=telin_logo2">
</div>

How to stack two images on top of each other?

Image example of what I need
I basically copied the code off of a YouTube video. I am a rookie so try and explain as easily as possible how to stack two images on top of each other.
They are the same width and same height images and need to be aligned horizontally and vertically.
.image {
position: absolute;
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -260px;
}
<div class="image">
<img src="car.png">
</div>
There are a few ways to do this. The most simple would probably be to edit your CSS to do the following:
CSS:
.image {
width: 100%; /* Image container is now full-width */
}
.image img {
margin: 40px auto; /* "auto" will center block elements */
display: block; /* Set images to be "block" so they obey our auto margin */
}
HTML:
<div class="image">
<img src="path/to/image1.jpg">
<img src="path/to/image2.jpg">
</div>
JSFiddle
For horizontal and vertical centering:
While some may prefer the flex method, I prefer the table-cell method for simple alignment. Try this:
CSS:
.image {
width: 100%;
height: 500px /* Modify this to fit your needs */
display: table;
}
.image .centered {
display: table-cell;
vertical-align: middle;
}
.image .centered img {
margin: 40px auto;
display: block;
}
HTML:
<div class="image">
<div class="centered">
<img src="http://fillmurray.com/360/100">
<img src="http://fillmurray.com/360/100">
</div>
</div>
JSFiddle
If the question is only to put second image under the first just br tag
If the question is to make on blank page 2 images in center one under one:
<table style="width: 100%; height: 100%; border-spacing: 0px; border-collapse: collapse; padding: 0px; margin: 0; border: 0;">
<tr style="height: 100%">
<td style="text-align: center; vertical-align: middle; height: 100%;">
<img src="1.jpg" /><br><br>
<img src="2.jpg" />
</td>
</tr>
</table>
For body and html you need also height: 100%
I'm pretty sure all positioning is possible with div so probably I'm complicated with tables but I was too tired to find the code for vertical positioning so I used tables in my work.

How to position image in the center/middle both vertically and horizontally

<div id="photo_leftPanel" style="float: left; width: 604px; position: relative;">
<img src="bla.jpg">
</div>
How can i make the image start from the middle of this box? (middle both vertical and horizontal)
There are several ways to do this, and if it needs to work in all browsers (IE7+ and the rest) you need to do different things to make it work in some of the cases.
Use absolute position. This only works if you know the size of the image.
Here you set it to position: absolute; left: 50%; top: 50%; margin: -<half height of image> 0 0 -<half width of image>.
See example here: http://jsfiddle.net/JPch8/
Use margin: 0 auto;text-align: center; and line-height/font-size.
This is a bit more tricky, since line-height doesn't work as it should in IE for inline-block elements like images. That's where the font-size comes in.
Basically, you set the line-height of the image container to the same as the container's height. This will vertically align inline elements, but in IE you need to set the font-size instead to make it work.
See example here: http://jsfiddle.net/JPch8/2/
In modern browsers that support display: flex you can do it by simply setting the container div to display: flex; align-items: center; justify-content: center;
See example here: https://jsfiddle.net/ptz9k3th/
HTML:
<div id="photo_leftPanel">
<img src="bla.jpg">
</div>
CSS:
div.photo_leftPanel {
position: relative;
}
div.photo_leftPanel > img {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
put the image in a <div> with text-align:center; without specifying the size of the box
<div class="picture_div" style="margin:0px auto; text-align:center;">
<img src="media/BezierCurve.gif" />
</div>
alternatively you can specify width and the height of the <div> box in order to center the image (actually the div box).
<div id="blue" style="border:1px solid blue; width:100px; height:100px; margin:10px auto;">
<img src="media/BezierCurve.gif" />
</div>
"float:left; position:relative" probably doesn't work as expected. Floated elements are considered absolute.
To get the image centered vertically you need a height on the div, and you need height on it's parents. (Centering vertically is kind of a pain). My example below will work if those are your only elements but be aware that height: 100% on the containers will likely affect the rest of your layout.
<html>
<head><title></title>
<style type="text/css">
html, body {
height: 100%;
}
#photo_leftPanel {
height: 500px; /*guessing*/
width: 604px;
float: left;
}
#photo_leftPanel img {
margin: auto;
vertical-align: middle;
}
</style>
</head>
<body>
<div id="photo_leftPanel">
<img src="bla.jpg" />
</div>
</body>
</html>
A suitable solution for modern browsers is flexbox. A flex container can be configured to align its items both horizontally and vertically.
<div style="display: flex; align-items: center; justify-content: center; width: 600px; height: 600px;">
<img src="bla.jpg">
</div>
I hope I understand what you are trying to achieve.
<div id="photo_leftPanel" style="float: left; width: 604px; position: relative;">
<center><img src="bla.jpg" style="vertical-align:middle;"></center>
</div>