Vertically Center HTML Element Within a Div of Dynamic Height [duplicate] - css

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
vertical-align: middle doesn't work
How can i make sure that the <a></a> is vertically centered in this div, regardless of the size of the div:
http://jsfiddle.net/XF9WM/
Thanks

Using display:table and display:table-cell, which works in all modern browsers (IE 8+):
.post {
width: 200px;
height: 200px;
background: rgba(0,0,0,0.8);
display:table;
}
.post h2 {
text-align: center;
width: 100%;
height: 100%;
display:table-cell;
vertical-align: middle;
}
.post h2 a {
color: #f7f7f7;
font-size: 2.2em;
font-weight: normal;
font-style: italic;
width: 100%;
}

I wanted to achieve it in a different way, (just for fun), and this one is working too:
jsFiddle
The idea is using a :before block element of 50% height.
html, body{
height:100%;
}
.post {
width: 400px;
height: 300px;
background: rgba(0,0,0,0.8);
}
.post h2:before{
content:"";
display:block;
height:50%;
}
.post h2 {
text-align:center;
height:100%;
}
.post h2 a {
color: #f7f7f7;
font-size: 2.2em;
margin-top:-20px;
display: block;
}
Hope it helps :-)

Give your link a line-height larger than the text will ever be, and vertically align it in the middle. Then absolutely position it's parent within the div to be at top: 50% and margin top minus half the link's line-height:
jsFiddle
.post {
position: relative;
}
.post h2 {
text-align: center;
width: 100%;
position: absolute;
top: 50%;
margin-top: -150px;
}
.post h2 a {
vertical-align: middle;
line-height: 300px;
white-space: nowrap;
}
It even works in IE7.

[UPDATED]
You can also do it like this: jsFiddle
.post {
width: 400px;
height: 200px;
background: rgba(0,0,0,0.8);
position:relative;
}
.post h2 {
position:absolute;
top:50%;
width:100%;
text-align:center;
}
.post h2 a {
line-height:100%;
color: #f7f7f7;
font-size: 2.2em;
margin-top:-20px;
display: block;
}

If you don't mind adding an extra HTML class, your simplest way is just to use vertical-align: middle.
http://jsfiddle.net/Wexcode/HtNJM/
<div class="post">
<span></span><h2>Hello!</h2>
</div>
CSS:
* { margin: 0; padding: 0; }
.post {
background: rgba(0,0,0,0.8);
width: 200px;
height: 200px; }
.post span {
height: 100%;
vertical-align: middle;
display: inline-block; }
.post h2 {
position: relative;
text-align: center;
width: 100%;
vertical-align: middle;
display: inline-block; }
.post h2 a {
color: #f7f7f7;
font-size: 2.2em;
font-weight: normal;
font-style: italic;
display: block; }

Related

Margin not giving any results on divs

I'm not any kind of CSS pro. But margin doesn't seem to work in any other parts than the ul/li list provided below.
I have played with positions and other divs.
html, body {
margin: 0;
padding: 0;
}
body {
background-color: rgb(184, 184, 184);
background-repeat: no-repeat;
background-position: center;
background-size: auto;
background-image: url('/images/landing.jpg');
font-family: Montserrat;
}
.header {
top: 0;
position: fixed;
background-color: gray;
width: 100%;
height: 60px;
justify-content: center;
align-items: center;
display: flex;
}
.menu {
position: fixed;
top: 6px;
right: 0;
}
.menu ul li {
display: inline;
}
.menu ul li a {
color: white;
text-decoration: none;
font-size: 16px;
font-weight: lighter;
padding: 0 20px;
}
.welcome {
position: absolute;
color: white;
font-size: 50px;
top: 459px;
margin: auto;
}
HTML:
<div class='welcome'>
<span>Hello!</span>
</div>
Trying to get welcome div to center
You are setting margin: auto on an absolute positioned element. I suppose you are trying to center the 'welcome' element (as per your question, this fact is not clear). An absolute positioned element is dependent on its ancestors positioning and size.
One of the many solutions to center an absolute positioned element if to move it to 50% from the top and left of it's parent, and than apply translate to nudge it to the center: top: 50%; left: 50%; transform: translateX(-50%) translateY(-50%).
welcome class position is absolute so welcome class parent should be relative to center the welcome div. And also if you want to horizontally center the div you should change margin: auto; to margin: 0 auto;
Or you can make the parent div display to flex, then use align-items, justify-content to center.
.parent { display: flex, align-items: center, justify-content: center }
Try changing your welcome css class to this:
.welcome {
position: absolute;
left: 50%;
transform: translate(-50%);
}
Replace your .welcome css class with this css code :
html, body {
margin: 0;
padding: 0;
}
body {
background-color: rgb(184, 184, 184);
background-repeat: no-repeat;
background-position: center;
background-size: auto;
background-image: url('/images/landing.jpg');
font-family: Montserrat;
}
.header {
top: 0;
position: fixed;
background-color: gray;
width: 100%;
height: 60px;
justify-content: center;
align-items: center;
display: flex;
}
.menu {
position: fixed;
top: 6px;
right: 0;
}
.menu ul li {
display: inline;
}
.menu ul li a {
color: white;
text-decoration: none;
font-size: 16px;
font-weight: lighter;
padding: 0 20px;
}
.welcome {
left:50%; right:50%;
top:50%; bottom:50%;
position: absolute;
}
<div class='welcome'>
<span>Hello!</span>
</div>

Vertical align: middle for text node

I'm trying to align vertically a text block. I have a :before pseudo-element and what I'd like to achieve is having both aligned to the middle.
Is it possible to achieve it without extra wrapper? Here's what my problem looks like:
HTML:
<div class="fail">Something's wrong</div>
<div class="success">
<span>Perfect middle</span>
</div>
CSS fail:
.fail
{
background: orange;
font-size: 30px;
display: inline-block;
}
.fail:before
{
background: red;
content: '✗';
text-align: center;
display: inline-block;
line-height: 60px;
margin-right: 10px;
vertical-align: middle;
width: 60px;
}
CSS success:
.success
{
font-size: 30px;
background: lime;
display: inline-block;
}
.success:before
{
background: green;
content: '✓';
text-align: center;
display: inline-block;
line-height: 60px;
margin-right: 10px;
vertical-align: middle;
width: 60px;
}
.success span {
vertical-align: middle;
}
It might be not so obvious from this picture, but the left one is too high, about 2-3 pixels of difference. It matters especially for smaller elements.
So my question is: can I achieve perfect middle with just one HTML tag + :before?
Because I dont like inline elements that much, I would do it like this:
.fail {
position: relative;
float:left;
padding-right: 10px;
line-height: 60px;
font-size: 30px;
background: orange;
}
.fail:before {
content: '✗';
display: inline-block;
width: 60px;
margin-right: 10px;
text-align: center;
background: red;
}

I can't keep the footer fixed to the bottom of screen when zoomed out

i am trying to keep the footer div at the bottom of the screen when the content is shorter than the view port, but it also has to remain at the bottom on the page content(not the view port) when the content is taller than the view port.
So far it remains at the bottom of the content when zoomed in, but i can't get it to stick to the bottom when zoomed out. There are samples with position: absolute; that i have seen, but it makes the footer remain at the bottom of the view port and not the content when zoomed in.
I have to do this for a school unit, is there a way to do this?
For now i have the footer position as relative, though i guess that wont do anything but stick it close to the last div, yeah? but i seem to find what i need to do.
Here is the site:
http://www.foodforthought.webatu.com/Index.html When you zoom right out you will see the footer not stay at the bottom.
Here is my CSS:
* { padding: 0; margin: 0; font-family: verdana; }
html {
height: 100%;
}
body {
font-size: 13px;
color: #000;
background-color: white;
background-position: center;
background-repeat: repeat-y;
background-image: url('../images/background.jpg');
}
/* Main div container */
#wrapper {
margin: 0 auto;
width: 960px;
height: auto;
}
/* Header div */
#wrapper #header {
height: 200px;
background-color: green;
}
/* Special event section */
#header p {
position:relative ;
left: 30px;
top: -100px;
width: 300px;
z-index: 1;
color: white;
border-style: dashed;
padding: 5px;
border-width: 1px;
}
/* Horizontal list div */
#wrapper #navigation {
height: 25px;
background-color: white;
display: block;
}
/* Horizontal list */
#navigation ul {
padding-top: 5px;
list-style-type: none;
text-align: center;
width: 960px
}
/* Horizontal list items */
#navigation li {
display: inline-block;
text-transform: uppercase;
vertical-align: middle;
}
/* Horizontal list link */
#navigation a {
height: 25px;
width: auto;
display: block;
line-height: 15px;
text-align: center;
vertical-align: middle;
text-decoration: none;
color: black;
padding-left: 8px;
padding-right: 8px;
}
/* Horizontal list hover attribute */
#navigation a:hover {
color: Darkgrey;
}
/* Content div */
#wrapper #content {
display: inline-block;
height: auto;
width: 950px;
background-color: white;
padding-left: 10px;
padding-right: 0px;
}
/* Content headings */
#content h2 {
text-transform: capitalize;
padding: 10px;
}
/* Content image(global) */
#content img {
padding: 2px;
border-width: 1px;
margin: 10px;
margin-left: 20px;
Margin-right: 15px;
}
/* Content bullet list */
#content ul {
padding: 15px;
font-size: small;
margin-left: 10px;
}
/* Content paragraph text */
#content p {
padding-left: 10px;
font-size: small;
}
/* Content image */
#content #img1 {
float: left;
border-style: dashed;
}
/* Content image */
#content #img2 {
float: right;
border-style: solid;
border-width: 1px;
border-color: #BDA27E;
}
/* Content image */
#content #img3 {
float:left;
border-style:double;
margin-top: 20px;
margin-left: 8px;
border-width: 5px;
border-color: #996633;
}
/* Side menu div*/
#wrapper #content #menu {
float: right;
padding: 0px;
margin: px;
width: 220px;
height: 1118px;
}
/* Side menu*/
#menu ul {
list-style-type: none;
float: right;
text-align: right;
width: 170px
}
#menu li {
background-image: url('../images/pg_menu_bg.png');
}
/* Side menu link */
#menu a {
height: 30px;
display: block;
vertical-align: middle;
text-decoration: none;
color: black;
font-size: small;
padding-top: 8px;
margin-left: 10px;
margin-right: 0px;
}
/* Side menu hover attribute */
#menu a:hover {
color: darkgrey;
}
/* Footer div */
#wrapper #footer {
height: 40px;
background-color: #82AAF1;
width: 960px;
margin: 0px, auto;
position: relative;
bottom: 0;
}
/* Foot note */
#footer p {
text-align: center;
color: #6A1B1B;
padding-top: 15px;
font-size: 10px;
}
.padext {
padding-top:2px
}
Sorry i am still a beginner.
Thank you for you time.
If you HTML is like this:
<body>
<div id="wrapper">
<div id="header"></div>
<div id="content"></div>
<div id="footer"></div>
</div>
apply the following CSS.
#wrapper {
min-height:100%;
position:relative;
}
#content {
padding:10px;
padding-bottom:80px; /* Height of the footer element */
}
The padding-bottom of the #content is set as the height of the footer.
#footer {
width:100%;
height:80px;
position:absolute;
bottom:0;
left:0;
}
I recommend you to refer the following link : How To Keep Your Footer At The Bottom Of The Page With CSS

Two <li> are not lined up properly even tho they are the same width

body {
font: 18px/1.1em "Helvetica Neue", Helvetica, Arial, sans-serif;
color: #ffffff;
}
a {
font: 18px/1.1em "Helvetica Neue", Helvetica, Arial, sans-serif;
color: #ffffff;
text-decoration: none;
}
.container {
margin: auto;
margin-top: 5%;
width: 1280px;
height: 800px;
background-image: url(../img/background.jpg);
overflow: hidden;
}
.content {
margin: 100px 0 164px 170px;
}
.logo a {
font-size: 65px;
font-weight: 200;
line-height: 26px;
}
.main {
margin-top: 94px;
}
.main li {
background-position: center center;
background-repeat: no-repeat;
display: inline-block;
height: 150px;
width: 150px;
margin-right: 10px;
margin-bottom: 10px;
}
.main li.home {
background-color: #3387ea;
background-image: url(../img/home.png);
width: 150px;
height: 150px;
}
.main li.about {
background-color: #f9be3e;
background-image: url(../img/about.png);
width: 150px;
height: 150px;
}
.main li.portfolio {
background-color: #d3573e;
background-image: url(../img/portfolio.png);
width: 280px;
height: 150px;
}
.main li.photos {
background-color: #59b0e2;
background-image: url(../img/photos.png);
width: 150px;
height: 150px;
}
.main li.testimonials {
background-color: #33af95;
background-image: url(../img/testimonials.png);
width: 150px;
height: 150px;
}
.main li.hire {
background-color: #86a73f;
background-image: url(../img/hire.png);
width: 310px;
height: 150px;
}
.main li.blog {
background-color: #151a26;
width: 440px;
height: 150px;
}
.main li.contact {
background-color: #7e5b8c;
background-image: url(../img/contact.png);
width: 150px;
height: 150px;
}
The bottom 's are not lined up with top ones even though they match each other in total width.
Each has a margin-right 10px and margin-bottom 10px.
I am using reset style sheet to remove browser setting.
I cannot think of anything that will not allow it to align properly.
Codepen
Please help. Thank you!
This is because the white-space between inline block elements (in this case, the list items) also tabs and new lines between HTML elements are count as a white space.
You could either use CSS float or just remove the white space as follows:
EXAMPLE HERE
.main ul {
font: 0/0 a; /* Set font-size and line-height to 0 for the container */
}
.main li {
/* Then reset the valid value on list items */
font: 18px/1.1em "Helvetica Neue", Helvetica, Arial, sans-serif;
/* other declarations */
}
There are couple of ways to remove the space between inline(-block) elements:
Minimized the HTML
Negative margins
Comment the white space out
Break the closing tag
Set the font size of the parent to zero then reset that for children
Float the inline items instead
Use flexbox
Your choice.
That's because there's still a space between your tiles.
To solve this, make your hire me tile's width 315px.

Overflowing issues

I am working on a HTML/CSS website, with mainly 4 divs (wrapping, top, menu and content), using a centered layout.
The problem is that the text inside my #content overlaps and I can't force it inside the div. Also, when I enable scrolling on the div and disable it on HTML, the scrolling just won't work for the div. So I get stuck with the overlapping content and no option to scroll down.
So, resuming it, I want to use 3 fixed divs, centered, leaving two side-borders and want my background-image to not move. Only the content should scroll inside of it.
Here's my CSS code:
<style type=text/css>
html
{
overflow: none;
}
head
{
}
body
{
background-color: #030B12;
}
p
{
font-family: verdana;
font-size: 12px;
color: #FFFFFF;
text-align: left;
}
h1
{
margin-top: 25px;
font-family: verdana;
font-size: 16px;
color: #FFFFFF;
text-align: center;
}
bg2
{
position: fixed;
top: 0;
left: 0;
}
#wrapper
{
height: 100%;
width: 70%;
margin: auto;
background-image: url('bg2.jpg');
background-position: fixed;
background-repeat: no-repeat;
}
#top
{
background-image:url('top.jpg');
background-repeat: no-repeat;
background-position: fixed;
height: 15%;
width: 100%;
margin: auto;
}
#menu
{
height: 60px;
width: 100%;
background-image: url('navi_bg.png');
}
#content
{
overflow: auto;
display: block;
}
ul
{
list-style-type: none;
height: 60px;
width: 663px;
margin: auto;
}
ul a
{
background-image: url(navi_bg_divider.png);
background-repeat: no-repeat;
background-position: right;
padding-right: 22px;
padding-left: 16px;
display: block;
line-height: 60px;
text-decoration: none;
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 21px;
color: #FFFFFF;
}
ul li
{
list-style-type: none;
float: left;
}
ul a:hover
{
color: #3F5F7B;
}
Based on what is available, I've created a layout that I think is what you are looking for. There are some issues with getting the top li menu items to center, but I am sure you could work that out pretty easily. You should specify the height of the content area if you want to scroll just that area, and keep the wrapper's background static.
div#content{
max-height:300px;
overflow-y:scroll;
}
Here is the fiddle

Resources