I want to build a page so that all content is in the middle of the page. But as I resize the browser, the content goes to minus left and I cant even scroll it.
I want something like this: https://www.apple.com/au/mac/.
Here all content is in the middle and if I resize the browser to a smaller width then I can still scroll horizontally to see all content.
I have tried something similar but when I resize the browser, my content goes to the left beyond the left margin and I cant scroll to the left.
This is the code.
CSS:
html, body
width: 100%; height: 100%;
main
position: absolute; top:0; left: 0;
width: 1000px; height: 100%;
left: 50%; margin-left: -500px;
border: 1px solid;
dont use position absolute. try this style for your main:
.main { margin: 0 auto;}
Thanks Jatin. I tried this and it worked!
html, body
width: 1024px; height: 100%;
overflow: auto;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
main
position: relative;
background-color: grey;
Related
Ok, i thought of starting afresh following some confusions in my previous similiar post. Here, I am trying to know the exact "reason" as to why exactly my outer container div ("container" , pink) is not automatically expanding vertically to fit the content div ("content" , red) (which automatically expands vertically with length of text). I am looking a reason more than the solution, because the reason will help me understand the concept more deeply. Please copy dummy text loremipsum... several times in the "content" div so that it overflows from page
Screenshot
here is the code:
html, body {
width: 100%;
left: 0px;
top: 0px;
margin: 0px;
height: 100%;
}
.container {
width: 600px;
left: 0px;
position: relative;
right: 0px;
background-color: rgba(216,86,112,0.5);
margin: auto;
height: 100%;
}
.content {
height: auto;
width: 200px;
background-color: rgba(255,0,0,1);
position: absolute;
top: 0px;
bottom: auto;
left: 0px;
right: 0px;
margin: auto;
}
</style>
</head>
<body>
<div class="container">
<div class="content">
/* Paste dummy text here more than a page */
</div>
</div>
</body>
</html>
The problem is the following:
Instead of using
.container {
height: 100%;
}
try:
.container {
height: auto;
}
and instead of
.content {
position: absolute;
}
use
.content {
position: relative;
}
Here's why
When an element is set to be 'position: absolute' it wont collapse with any other element, that's why your container doesn't expand at all.
When an element is set to be 'Height:100%' it takes the height of its container, in your case the cointainer is the body which means it will take 100% percent of your screen (in your case), but your content is way higher than the screen and that's why it overflows your content.
Hope you understand....
Examining this HTML:
<div class="wrapper">
<div class="content">
</div>
</div>
<div class="footer">
<hr />
<p>some text</p>
</div>
and CSS:
.footer {
position: absolute;
height: 100px;
width: 100%;
bottom: 0;
background-color: black;
}
.wrapper {
padding-bottom: 100px;
background-color: blue;
height: 100%;
}
.content {
width: 200px;
height: 100px;
margin: auto;
background-color: green;
}
html, body {
width: 100%;
height: 100%;
}
You can see that footer have position absolute and stay at the bottom of the page. wrapper will cover the remaining space and contain a content inside it. I want to vertical-align content without breaking the current layout. Do you have any suggestion?
Here is JSFiddle link. (Note: jsfiddle doesn't work as expected, there always a space beneath footer, this behavior doesn't occur when run the HTML file in browser).
Note: I don't want to use fixed height for wrapper, I want it covers all the remaining space, so please don't suggest me to use line-height
I tried the example here but it doesn't seem to work
NOTE I want the layout easy to modify (like add a header or content at the top) without breaking it therefore I want to avoid using absolute position on wrapper and content
NOTE 2 Sorry for not to clarify, actually, content doesn't have fixed size, its size depend on the content inside it, so the solution using negative margin doesn't work as I mentioned above
Here is one approach using the following CSS:
.footer {
position: absolute;
height: 100px;
width: 100%;
bottom: 0;
background-color: black;
}
.wrapper {
background-color: blue;
position: absolute;
top: 0;
bottom: 100px;
left: 0;
right: 0;
}
.content {
width: 200px;
height: 100px;
background-color: green;
position: absolute;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -100px;
}
html, body {
width: 100%;
height: 100%;
margin: 0;
}
Use absolute positioning and then negative margins, since your content has well-defined
dimensions, this is relatively straightforward.
See demo at: http://jsfiddle.net/audetwebdesign/DgUV2/
For .wrapper, use the top, bottom, left and right offsets to stretch the div to the
full width and height, taking into account the 100px for the footer.
For .content, set top and left to 50%, the center point of the .wrapper and then adjust
for the center of the .content div using negative margins.
Remember to zero out the margin for the body or else you might see 10px whitespace
depending on your browser.
Add this to your .content
position: relative;
top: 50%;
transform: translateY(-50%);
Just 3 lines of code to vertical align
I was able to get it to work using Method 1 from the example you linked
I added the following:
.content {
width: 200px;
height: 100px;
margin: auto;
background-color: green;
/* THE BELOW WAS ADDED */
position: absolute;
top: 50%;
left: 50%;
margin: -100px 0 0 -100px;
}
html, body {
width: 100%;
height: 100%;
/* BELOW ADDED TO REMOVE EXTRA SPACE AROUND EDGES */
margin: 0;
}
jsFiddle of working example
I tried to create HTML document with one layer in center like that:
<body>
<div id="background">
LONG TEXT HERE
</div>
</body>
and I set
html, body {
margin: 0;
height: 100%;
min-height: 100%;
background-color: #color1;
}
and
#background {
width: 80%;
height: 100%;
position: absolute;
margin-left: 10%;
display: block;
margin-right: 10%;
background-color: #color2;
top: 0px;
bottom: 0px;
}
But somehow if the text exceed height of the display and when I scroll down there is no background. I have checked in FireBug it looks like if the whole document is only the size of the initial window.
I need to make it 100% height. Could you please help me?
Remove height: 100%; and position: absolute;
It will work. Like below
#background {
width: 80%;
margin-left: 10%;
display: block;
margin-right: 10%;
background-color: #093;
top: 0;
bottom: 0;
}
If the text is longer than the page, you will need to set the overflow property in CSS - otherwise elements will expand to fit their contents.
For example:
overflow: hidden;
Although this would make some text invisible, so you may want to use auto not hidden depending on what you are doing.
I've created a banner for my website that's made up of 3 iPhone images side-by-side, using background images and relative positioning for each. However, I'm having issues with the horizontal scrolling. That is even though the div's containing each iphone image extend beyond the width of the parent .content div, I don't want there to be a scrollbar when the overflow content isn't able to fit the browser width. Scrollbars should only be shown if the browser width is below 960px.
A similar effect is presently seen on Apple's homepage, where the hand/wrist reside "outside" the website's container, but no horizontal scrollbars are visible unless the browser's width is below 990px wide.
I hope I've explained this clearly, please let me know if it's not clear.
Here's the code I'm using:
<div class="content">
<div id="iphone-a"></div>
<div id="iphone-b"></div>
<div id="iphone-c"></div>
</div>
.content {
margin: 0 auto;
width: 960px;
height: auto;
text-align: left;
overflow-x: visible;
}
#iphone-a {
z-index: 1;
position: relative;
left: 50%;
bottom: 0;
margin-left: -306px;
height: 657px;
width: 590px;
background: url(images/banner.png) 0px 0px;
}
#iphone-b {
z-index: 0;
position: relative;
top: -545px;
left: 50%;
margin-left: -732px;
height: 319px;
width: 590px;
background: url(images/banner.png) 0px -658px;
}
#iphone-c {
z-index: 0;
position: relative;
top: -864px;
left: 50%;
margin-left: 144px;
height: 319px;
width: 590px;
background: url(images/banner.png) 0px -658px;
}
change
overflow-x: visible;
in .content to
overflow-x : hidden;
Edit : If that's not what u mean, and u just want visible to work correctly try using overflow instead of overflow-x
I have a div which needs to fill out the height of the browser's viewport,but still says in the same position when the user scrolls the web page up and down. position: fixed; does this, but I am unable to use it as it's making the overflow scroll bar of the div jerky and slow. Is there an position or method that I can use so for example I currently have:
div.panel {
position: absolute;
top: 36px;
right: 0;
overflow: auto;
background: #636362;
padding: 0 0 20px 0px;
width: 290px;
height: 100%;
}
I'm not sure what you mean with "jerky and slow", because all scrollbars act the same. This is how I would resolve your issue:
HTML:
<div class="fixed">I'm fixed!</div>
<p>Rest of page</p>
CSS:
html, body {
/* make sure the page is at least height of viewport */
height: 100%;
}
body {
/* because the fixed div is no part of the flow,
make sure it is not overlapping the webpage */
padding: 0 0 0 100px;
}
.fixed {
height: 100%;
width: 100px;
left: 0;
position: fixed;
background: #e0e0e0;
/* only vertical-scrolling, but can be changed of course */
overflow-y: scroll;
}
JSfiddled Live example
Works in at least IE7, IE8 and Firefox.