position absolute is not working - html

How can I set the div #gallery in the center of the div #warp? Here is a demo JS Fiddle.
HTML:
<div id="warp">
<div id="header">
<img id="logo_l" src="images/centerwow-logo.png" alt="Centerwow - Web Design - Logo" />
<img id="Logo_r" src="images/web-design-logo.png" alt="centerwow.com - Portfolio" title="Portfolio" >
<div id="menu">
<li>About Us
<li><a href="#" name="aboutus" title="About Us" >Company Info</a></li>
<li><a href="#" name="profile" title="Profile" > Profile</a></li>
</div>
</div>
<div id="gallery"></div>
</div>
CSS:
html body{
margin:0;
padding:0;
}
body{
background:#F7F7F7;
}
#warp{
position:relative;
margin:0 auto;
}
#header {
position:relative;
background:#DEEAF4;
width:80%;
height: 100px;
margin:0 auto;
}
#logo_l{
position:absolute;
left:0;
top:0;
margin:0;
padding:0;
}
#Logo_r{
position:absolute;
width:272px ;
height:100px;
right:0;
top:0;
margin:0;
padding:0;
}
#menu li {
display: block;
vertical-align: top;
float:left;
margin: 0 5px;
}
#menu {
position:relative;
width:250px;
top: 35px;
margin: 0 auto;
}
#gallery{
position:absolute;
background:#EBF0F5;
width:80%;
height:70%;
margin:10px auto;
padding:10px;
}
​

You do not need to include position: absolute to center #gallery. This will already be achieved with margin: 10px auto.
Further, to set a percentage height to #gallery, its parent element must have a specific height. In this case, I assume you want it to be based on viewport height. To do so, every ancestor div must have a height of 100%:
*, html, body, #warp {
height: 100%;
}
JS Fiddle: http://jsfiddle.net/qc9WL/1/
NOTE: I also noticed that there are some bugs in your HTML and CSS.
You need a comma between body and html in line one of your style sheet
You should use <ul> or <ol> tags when making lists
Lastly, don't forget to close your <li> in line six

I'm not sure what you are trying to achieve here - position: absolute on the #gallery is exactly what's causing the problem (that's why margin: 10px auto is not working). If you remove it, the #gallery element will be in the centre.
Also, you could center horizontally an absolutely positioned element of known width by setting left: 50%; and margin-left: -40%; (-40% in this case, because you have the width for #gallery set to 80%)

Since you are specifying gallery width in percentage, why don't you try setting the left and right margin in percentage?
margin: 10px 10%;
places the division at center

Related

Put one div after another

I want to set two divs like the following.
http://snag.gy/ynuiY.jpg
This is my HTML code
<div id="topbar">
This is a top bar
</div>
<div id="wrapper">
wrapper
</div>
This is my CSS
#wrapper{
z-index:2;
marign-top: 30px;
width:80%;
height:auto;
background-color:#FFF;
left:auto ;
right:auto ;
margin: auto;
}
#topbar{
height:30px;
width:100%;
background-color:#333;
position:absolute;
top:0px;
left: 0px;
right: 0px;
color:#FFF;
overflow: hidden;
}
But the output is like following. (no wrapper)
http://i.stack.imgur.com/DXuWD.jpg
Please help me to solve this.
You have to change your #wrapper margin to padding
JSfiddle Demo
CSS
#wrapper{
padding-top: 30px;
}
Another solution is to set #topbar position to relative:
#topbar{
height:30px;
width:100%;
background-color:#333;
position:relative;/*Change to relative*/
color:#FFF;
overflow: hidden;
}
Also is margin-top no marign-top
fiddle
I think this is what you're after:
http://jsfiddle.net/ht8k40tr/
The problem is due to the order in which you're setting the margins.
Currently you're setting the top margin to be 30px, but then you're resetting that by setting all margins to auto, a simpler way to do this is just to set the margin as follows margin: 30px auto which sets the top margin to 30px, and the remaining margins to auto.
Change the "margin-top: 30px;" to "padding-top: 30px;"
#wrapper {
padding-top: 30px;
width:80%;
height:auto;
background-color:#FFF;
left:auto ;
right:auto ;
margin: auto;
}

The clickable area for my link is too large

I have an <img> logo that is wrapped inside a link, and the link is wrapped in a <div>.
My code below results in the clickable area for my link to extend 100% horizontally to both edges of the viewport.
How can I make the clickable area for my link to be the size of my logo?
jsFiddle
My HTML:
<div id="logo-container">
<div id="logo">
<img src="http://placehold.it/350x150" />
</div>
</div>
My CSS:
#logo-container{
width:100%;
float:left;
height:auto;
margin:0 auto 0 auto;
background:#ECECEA;
}
#logo{
margin:0 auto;
height:auto;
}
#logo img {
display:block;
margin:6px auto 10px auto;
}
#logo img{
width:330px;
height:auto;
}
This is cause image is set to display: block;, such expands it to the full available width, pushing the A element boundaries to the extreme.
Instead, keep the logo image inline and use text-align:center; for the #logo parent: http://jsfiddle.net/wLbo6mjr/10/
#logo{
text-align:center;
}
#logo img {
margin:6px 0 10px 0;
}
Setting a width on logo fixes it
#logo{
width: 330px;
height: auto;
margin: 0 auto;
}
http://jsfiddle.net/wLbo6mjr/8/

Aligning a image vertically center

I can't seem to get a image to align vertically in the header. It aligns horizontally but It is a little too far towards the top of the header.
The html:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="home_style.css">
</head>
<body>
<div class="header">
<div class="search_bar">
<input type="text" name="search" placeholder="Search magical instruments, tricks, books and more">
</div>
<div class="user_settings">
<img src="onebit_09.png" width="48px" height="48px">
</div>
</body>
</html>
The css:
html,body{
height:100%;
min-height:100%;
width:100%
min-width:100%;
}
.header{
margin-top:0;
margin-bottom:0;
height:10%;
width:100%;
background-color:#343430;
}
.search_bar input[type="text"]{
position:absolute;
left:20%;
top:4%;
width:27%;
padding:5px;
padding-right:50px;
outline:none;
border: 2px solid #9C4B8F;
border-radius: 5px;
background-color:#FBFBFB;
font-family: Cambria, Cochin, Georgia, serif;
font-size: 16px;
color:grey;
background-image: url('search.png');
background-position: 100% -10px;
background-repeat:no-repeat;
}
.search_bar input[type="text"]:focus{
background-color:#FFFFFF;
border-color:#333333;
}
.user_settings img
{
position:absolute;
top:5%;
left:95%;
height:48px;
width:48px;
margin:-24px 0 0 -24px;
}
The image that I'm trying to position is a little settings cog(user_settings) that is 48px wide and 48px high.
Here is a workaround:
#elementToAlignVertically
{
margin-top:50%;
transform:translate(0px,-50%);
-ms-transform:translate(0px,-50%);
-moz-transform:translate(0px,-50%);
-webkit-transform:translate(0px,-50%);
}
Basicaly you align the element 50% from the top and then use the translate to move it -50% of it's hight so it will center it self.
Probably you'll also have to set the height of the element for it to work or instead of percent use pixels that you want to move the element upwards.
there is a property vertical-align in css which can be valued at varied contents. Check this w3schools tuts.
Vertical-align property
I fixed the problem, I had forgotten to set the padding and margin of the html,body elements to 0. This caused a small white gap at the top of the screen that made it seem like the image was not positioned correctly. Sorry for the inconvenience the corrected code should look like:
html,body{
height:100%;
min-height:100%;
width:100%
min-width:100%;
margin:0;
padding:0;
}
Try using calc for that it will not work in all browser but in some of them will do:
.user_settings img
{
position:absolute;
top:5%;
left:95%;
height:48px;
width:48px;
margin-top: calc(50% - 24px);
margin-top: -webkit-calc(50% - 24px);
margin-top: -moz-calc(50% - 24px);
}
or you can try the margin auto:
.user_settings img
{
position:absolute;
top:0;
bottom:0;
left:95%;
height:48px;
width:48px;
margin-top: auto;
}
Or still my favorite and most reliable is to use some javascript to manipulate the position.
For example jQuery something like:
$(".user_settings img").css('top',$(".user_settings).height()/2-24);
Then make sure you also add a resize watcher like this:
$(window).on('resize', function(){
$(".user_settings img").css('top',$(".user_settings).height()/2-24);
});
In the near future (right now has like the 70% of the browser support) you can do this, a much simpler and elegant solution:
.container img{
width: 100%;
height: auto;
}
#supports(object-fit: cover){
.container img{
height: 100%;
object-fit: cover;
object-position: center center;
}
}

DIV set to overflow:scroll, but wont scroll all the way to bottom

I have a DIV menu that is set to 100% height with a overflow:scroll. Within the DIV I have a ul li. The problem I have is that it wont let me scroll all the way down to see the last li. I can barely see it.
I think it has something to do with my header because when I remove the header, I can see it. When I put back the header, it goes under the browser and cannot be scrolled all the way down to see the last li.
Both li and header are almost identical in height and it makes a lot of sense that the header is causing the problem. Not the header in particular, I think, but more of something I did in CSS.
Why cant I scroll all the way to the bottom? What is the solution?
Sample here: http://jsfiddle.net/D5KU3/2/
<div class="container">
<!--header-->
<div class="header">
</div>
<!--end header-->
<!--left-->
<div class="left">
<!--ul starts here-->
<ul>
<li class="hybrid">
<a href="#">
<p class="title">Why Cant</p>
<p class="type">I scroll all the way to the bottom</p></a>
</li>
Repeat li 20 times
</ul> <!--ul ends here-->
</div> <!--container ends here-->
CSS
body, html {
height:100%;
}
body {
background:white;
}
.container {
width:260px;
height:100%;
overflow:hidden;
background:silver;
margin:0 auto;
font-family:sintony;
}
.header {
width:100%;
height:60px;
background:#000;
}
.left {
width:260px;
height:100%;
background:#fff;
float:left;
overflow:scroll;
}
li.hybrid a {
display:block;
background:#16BF14;
height:60px;
width:260px;
text-decoration:none;
position:relative;
}
li.purple a {
display:block;
background:#3370CC;
height:60px;
width:260px;
text-decoration:none;
position:relative;
}
p.title {
position:relative;
padding-left:10px;
}
p.type {
font-size:12px;
position:relative;
padding-left:10px;
}
ul {
margin:0;
padding:0;
}
li p {
margin:0;
padding:0;
list-style-type:none;
}
As you have both the class="header" and class="left" elements in the container, and the class="left" element is 100% of the container, those are 100% plus 60 pixels together.
You can make room for the header by using box-sizing and padding-top in the container. That will make the inner size of the container 100% minus 60 pixels. Then use a negative top margin on the header to place it on top of that padding:
.container {
box-sizing: padding-box;
-moz-box-sizing: padding-box;
-webkit-box-sizing: padding-box;
padding-top: 60px;
}
.header {
margin-top: -60px;
}
Demo: http://jsfiddle.net/Guffa/D5KU3/11/
You might also want to get rid of the page margin, otherwise the 100% container and the margin is taller than the window:
body {
margin: 0;
padding: 0;
}
It's actually quite logic - you have your body and html set to 100%. This means the content of the body can't be higher then the available space in your browser - and so you don't see the bottom.
If you remove this CSS the problem is solved; although it might be better to set the body to min-height: 100%. This way the height of the page will always be the complete available space; unless it's content is more than that.
An updates jsfiddle: http://jsfiddle.net/D5KU3/3/
Remove the overflow: hidden; from .container class
.container {
width:260px;
height:100%;
background:silver;
margin:0 auto;
font-family:sintony;
}
http://jsfiddle.net/atYpX/
i would recommend following
.left {
position:absolute;
width:260px;
top:60px;
height:100%;
background:#fff;
overflow:scroll;
}
http://jsfiddle.net/D5KU3/8/

Margin causes 100% height to give horizontal scrollbars

It's been a while since I handcoded a website, and now I have the issue that my top-margin causes the 100% height to give a nasty scrollbar, as seen in the fiddle:
http://jsfiddle.net/qKGzA/
I can't figure out how to get rid of this, without cutting off the footer (like with using overflow:hidden).
It probably is a simple solution but I can't think of it :)
Thanks for your help!
My code:
html, body{
background-color:#ececec;
height:100%;
width: 100%;
margin:0;
padding:0;
}
div#wrapper{
background-color:#ffffff;
width: 962px;
height: auto !important;
min-height: 100%;
height:100%;
margin:0 auto;
padding:20px 15px 0px 15px;
position:relative;
display:block;
}
footer{
background-color:#363636;
width:95%;
height: 15px;
margin:0;
padding:10px;
position:absolute;
bottom:0;
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
color:#e1e1e1;
text-align:center;
}
footer p{
margin:0;
padding:0;
display:inline-block;
}
footer p.divider{
margin:0 20px;
}
HTML:
<body>
<div id="wrapper" >
<header>
header header
</header>
<menu> Menu menu</menu>
<section>
section section
</section>
<footer>
<p>x</p><p class="divider">~</p>
<p>x</p><p class="divider">~</p>
<p>x</p><p class="divider">~</p>
<p>x</p>
</footer>
</div>
</body>
The div#wrapper has height:100% and padding:20px 15px 0px 15px making it 100% + 20 pixels. if you add
box-sizing:border-box;
to div#wrapper the padding will be inside the 100%, and the scroll bars disappear.
A non-css3 alternative is to place an element at the top of the inside of the div#wrapper with a height of 20 pixels. Perhaps add the 20px top padding to the header element which is inside the wrapper for non CSS3 browsers.
a. Does your body have to have the height and width assigned? Couldn't you simply set the background. It should automatically span any width non-dependent on the user's screen.
b. You could set the body to position: absolute; left: 0; right: 0; top: 0; overflow: hidden;
This should allow your body to expand in height as needed.
c. You could assign an actual height in pixels, or em . Something like 700px should not go off the screen, but of course that would change if the user is mobile.