CSS responsive margin top - html

I want to position a <div class="container"></div> in the middle of the screen in such a way so that it's responsive to any screen size. The red marked area on the screenshot should always appear in the middle of the screen.
How to position it? Currently I'm using margin-top:85px when I squeeze the browser, the distance between the red marked area and the navbar should decrease as well.

Have you tried absolute centering? You would need to add a position relative to the parent container... You would also need to declare a height on the container element...
.parent-container {
position: relative;
}
.container {
width: 50%;
height: 50%;
overflow: auto;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
I hope this helps...

Working code snippet has been added. This code will centre your div both horizontally and vertically for any screen size.
Set the css property position:relative for parent of the container.
.container {
width: 400px;
height: 300px;
background-color: #ccc;
position: absolute;
/*it can be fixed too*/
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
/*this to solve "the content will not be cut when the window is smaller than the content": */
max-width: 100%;
max-height: 100%;
overflow: auto;
}
<div class="container">
</div>

Try with this example
.container {
width: 75%;
margin: 50px auto 0;
}

Define some width on your container and set margin top & bottom to some desired value and left & right values to auto so that it will always split the remaining space on the both sides equally.
.container{
width : 84%
margin : 2rem auto;
}

Use this in your container class
.container{
position:fixed;
top:0;
left:0;
right:0;
bottom:0;
width:auto;
height:200px; /****set height****/
margin:auto;
}
It will work

Related

How to both center a <div> and have it use fixed positioning?

I have a div with some text on my page, and I want it to be at the bottom. I did this using fixed positioning:
div#popup{
position: fixed;
bottom: 0;
But I also want it to be centered. I tried giving it a width of 40% and auto margins, but that doesn't work (it doesn't work with the combination of the above code) :
div#popup{
position: fixed;
bottom: 0;
width: 40%;
margin-left: auto;
margin- right: auto;
How can I achieve this?
Thanks.
If you know width of div you can use negative margin-left for horizontal position (which equals half of width).
div {
position: fixed;
bottom: 0;
left: 50%;
width: 40%;
height: 30px;
margin-left: -20%;
background: blue;
}
JSFiddle
If you don't know width, just use wrapper and inline-blocks:
HTML:
<section>
<div>la-la-la</div>
</section>
CSS:
section {
position: fixed;
bottom: 0;
width: 100%;
text-align: center;
}
div {
display: inline-block;
border: 1px solid red;
color: red;
}
JSFiddle
I encourage You to check two nice tutorials (quick read):
http://www.barelyfitz.com/screencast/html-training/css/positioning
http://learnlayout.com/position.html
I think You need to describe position like this:
div#popup{
position: fixed;
bottom: 0;
right: 50%;
}
First off, you should never use fixed positioning to get your footer to stick to the bottom. To get the footer to stick to the bottom of the screen, set all your divs to relative, then add an extra div the same height as the footer (set a height for your footer) between the content and the footer. Then put a margin of negative that height on your content div. Works perfectly.
To centre it, use width auto and margin left and right auto or just use text-align center

align image right and give content leaving image space

I want an out put similar to this Image..!
following is what i've tried to do.JSfiddle
P.S: I cannot edit the structure of the content.
Since you can't edit the HTML, you can't use floating properly, which would be the perfect solution.
But then you can use absolute positioning:
div {
width: 500px;
min-height: 100px; /* image height */
position: relative;
text-align: justify;
}
img {
width:100px;
position: absolute;
top: 0;
right: 0;
}
p:first-child {
max-width: 400px; /* wrapper width - image width */
}
Demo

Absolute vertical centering causes parts of the div to disappear when it exceeds the browser window vertical size?

I have found this vertical centring method which seems pretty common..
#container {
width: 960px;
height: 740px;
position: absolute;
top: 50%;
left: 50%;
margin-left: -480px;
margin-top: -370px;
}
What I'm trying to center here is the entire site, and this code goes perfectly as expected when the screen preview is larger than the div height (larger than 740px). However, Once the browser window is minimized less than div's vertical size (740px) parts of the header disappear above the top of the page.
I can sort of understand why this is happening seeing that 50% becomes less than half the div's size which will be equalized with margin-top.
What I'm looking for is a fix for this issue? Or even a completely different method, I just need to center the site both vertically and horizontally.
try this:
#container {
height: 740px;
width: 960px;
position: absolute;
margin: auto;
top: 0; right: 0; bottom: 0; left: 0;
}
By the way, Smashing Magazine recently published a nice article about this.
You need to add a media query:
#media screen and (min-height:740px) {
#container {
top:0;
margin-top:0;
}
}
This will only apply the formatting where the screen is at least 740px tall. If you want to learn more about media queries, check http://www.w3.org/TR/css3-mediaqueries/
Absolute Centering like Lino Rosa mentioned is the best approach here for easy horizontal and vertical centering while allowing you to add some responsive touches, like fixing your height issue.
Ideally, you should be using percentages for the width and height declarations so that your content will vary with the viewport. Of course, sometimes you just need pixels :-)
Here's what I've done:
.Absolute-Center {
position: absolute;
top: 0; right: 0; bottom: 0; left: 0;
margin: auto;
}
#container {
width: 960px;
max-width: 90%;
height: 740px;
max-height: 90%;
overflow: auto;
}
By setting a max-height and max-width, the box will never be more than 90% of the container (in this case, the browser viewport) even if it's less than 960px wide or 740px tall, so even small screens see a nice centered box. overflow: auto ensures that if the content is longer than the box, the user can scroll in the box to see the rest.
View the demo
If you must have the box exactly 960px by 740px no matter the screen size (forcing the user to scroll around to see all of the content on a small window), then only apply the Absolute Centering styles to #container using a media query, like so:
#container {
width: 960px;
height: 740px;
overflow: auto;
margin: auto;
}
#media screen and (min-height:740px) and (min-width: 960px) {
#container {
position: absolute;
top: 0; right: 0; bottom: 0; left: 0;
}
}
View the demo
I encountered the same issue. As the height of my element is dynamically changed, I can't give it a fixed height.
Here is a demo below, hope it helps.
.wrapper {
display: table;
height: 100vh;
width: 100%;
}
#container {
display: table-cell;
vertical-align: middle;
background-color: lightblue;
}
.content {
width: 30%;
height: 30%;
background-color: red;
}
<html>
</html>
<body>
<div class="wrapper">
<div id="container">
<div class="content">
</div>
</div>
</div>
</body>

CSS 100% is not being respected when I zoom

I want to have a yellow rectangle #box-over across the entire screen at width 100%.
At default zoom level on Google Chrome (I've not tried all browsers) it works fine unless I zoom in beyond 300% or so then that yellow box no longer is 100% of the screen it gets clipped and a horizontal scroll bar appears at the bottom.
I can't seem to figure out how to fix this behavior.
JSFIDDLE
http://jsfiddle.net/VySGL/1/
SCREENSHOT
SOURCE
<style type="text/css">
body {
padding: 0;
margin: 0;
background-color: black;
}
#box {
width: 100%;
position: relative;
}
#box #box-over {
z-index: 1;
width: 100%;
height: 50px;
background-color: yellow;
position: absolute;
top:10px;
opacity:0.8;
}
#box #box-over .box-column {
width: 900px;
margin: 0 auto;
zoom: 1;
position: relative;
height: 50px;
}
</style>
</head>
<body>
<div id="box">
<div id="box-over">
<div class="box-column">
</div>
</div>
</div>
</body>
</html>
An alternative for using 100% width is to set left and right to 0 in CSS like this...
See JSFiddle
#box #box-over {
z-index: 1;
left: 0;
right: 0;
height: 50px;
background-color: yellow;
position: absolute;
top:10px;
opacity:0.8;
}
#box #box-over .box-column {
left: 0;
right: 0;
margin: 0 auto;
zoom: 1;
position: absolute;
height: 50px;
}
Using the left and right properties ensure you never exceed 100% width even when you add padding (See this JSFiddle).
For example when you add padding to your div it will be 100% plus the padding. See JSFiddle
Your inner 900px div overflows the outer div's 100% width when you zoom. You can give a max-width:100% to .box-column:
#box #box-over .box-column {
[...]
max-width: 100%;
}
Fiddle
This will force the inner div to respect the container's width.
Another solution is to set the #box-over's overflow to hidden or scroll. This way the inner div will still have 900px width equivalent in aspect ratio.

How can I make a HTML ID go outside of my container?

I have a container # 900px width and then inside that I have an header at 100% width but it only takes 100% of the container, how can I make it take the whole entire page and ignore the container without taking it out of the container html tags?
#container {
width: 900px;
margin: auto;
padding: auto;
position: relative;
}
#header {
background-image: url(pat.png);
background-repeat: repeat;
padding: 0px;
margin: 0px;
height: 150px;
width: 100%;
}
Use absolute positioning. The header element would then be sized and positioned according to the nearest parent who has position: relative; defined (which by default is the <body> element). Like so:
#header {
position: absolute;
left: 0;
right: 0; /* it will span from the left to the right edges */
height: 100px; /* it helps to set a fixed-sized height too, but this isn't required */
}