HTML/CSS center a div inside another div and make it expand - css

HTML looks like this
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Oppgave 5</title>
<style type="text/css">
</style>
</head>
<body>
<div id="animWrap">
<div id="boks"></div>
</div>
</body>
</html>
What i want to do is make the div with the id "boks" expand and "eat" the entire div "animWrap".
So my css is as following:
body{
margin: 0px;
}
#animWrap{
height: 600px;
width: 960px;
background-color: rgb(145, 75, 75);
margin: auto;
position: relative;
}
#boks{
width: 250px;
height: 175px;
top: 200px;
left: 380px;
background-color: rgb(180, 100, 100);
position: absolute;
transition: all 5s linear 0s;
}
#animWrap:hover #boks {
height: 400px;
width: 580px;
}
What i noticed is that it only expands to the right and towards the bottom. Probably because it isnt centered properly. Is there any way to expand it in every direction from the middle with only html and css?

Of course, you can do that but you have to apply a trick. Just calculate the space remaining. In this case I think the space covered by the whole div is 960px and #bok got 580px while hovering so the space at left = 190px and right = 190px.
Now you can apply the hover css like this:
#animWrap:hover #boks {
height: 400px;
width: 580px;
left:190px;
}
This will do the trick and animate aligning at center. Its working in mine.
Happy Coding!
Cheers!!

If you you set #boks width & height respectively to 100%, and comment out it's top and left values, it should fill the entire parent div:
#boks{
width: 100%;
height: 100%;
background-color: rgb(180, 100, 100);
position: absolute;
transition: all 5s linear 0s;
}

Related

prevent jumping of affixed element when scrolled

I'm using bootstrap's affix plugin here
HTML:
<html>
<head>
...
<style>
h1 {
width: 100px;
height: 100px;
background: green;
margin: 0;
top: 10px;
}
h1.affix {
top: 8px;
}
body {
padding: 10px;
height: 5000px;
}
</style>
</head>
<body>
<h1 data-spy="affix" data-offset-top="10">yo!</h1>
</body>
</html>
Whenever I scroll down for a considerable distance (using scroll on the right, not the mouse scroll), the div jumps. How can I prevent that? Apperantly the problem is div scrolls up beyond the screen, then it's applied a fixed position and it moves down causing it to jump. I tried using transition to make it jump smoother, but for some reason it didn't work.
h1 {
width: 100px;
height: 100px;
background: green;
margin: 0;
transition-property: top;
transition-duration: 3s;
}
How can I fix this?
Try this:
Use affix class
<h1 class="affix">yo!</h1>
Now it will not jump !

What's the best way to break a div outside of a its set width parent container

I run into this issue a lot where I need the width of an inner container (like a wrapper with a set width of 960px) to span a width of 100%, and I'm unable to touch the html so it must all be done with css.
I know I can position: absolute; that guy to break him out of the wrapper... but is there another... better way?
Here is a JsFiddle link to help make it a little clearer:
http://jsfiddle.net/KRyF6/
<!-- html -->
<div id="container">
<div id="inner-container"></div>
</div>
<!-- CSS -->
#container {
width: 500px;
height: 500px;
background: gray;
margin: auto;
}
/* here's the container that I want to be 100% */
#inner-container {
width: 100%; /* :( */
height: 100px;
background: black;
}
Edit:
Here is a jsFiddle with my absolute position version... what I'd like to know is if this can be done without absolute positioning
http://jsfiddle.net/KRyF6/3/
<div id="container2">
<div id="inner-container2">
</div>
#container2 {
width: 500px;
height: 500px;
background: gray;
margin: auto;
clear: both;
margin-top: 20px;
}
#inner-container2 {
background: black;
width: 100%;
height: 100px;
position: absolute;
left: 0;
}
Well there is another way of doing this.
Body -> Container -> child
Now this way as the title suggest is passing its width to the container and from the container to it's child. This way the child can get the total width of the body.
Explanation
The only problem you are facing here is that an static width will not keep the <body> width in mind(aka the viewport). So you have to use percent values for the width so it will be based on the <body>:
#container {
/*width: 500px;*/
width: 70%;
height: 500px;
background: gray;
margin: auto;
}
Now the child knows that the width of its parent(#container) is 70% of the total body.
However a width of 100% will only get 70% of the <body> width. Instead you need 100% + the 30% of the 70%. And 30% of 70% is like 42%( 35% would be 50%).
Now we got the 100% of the <body>. Now you can let it look like it is outside the container width a negative margin. To center it you want it to be minus half of the 42%(=30% of the body) which you just calculated:
#inner-container {
/*width: 100%; /* :( */
width: 142%;
margin-left: -21%;
height: 100px;
background: black;
}
jsFiddle
However, is this easy to use?
Well it is an answer to your question. It is possible without using position: absolute.
Would position absolute be easier?
Definitely:
#inner-container {
width: 100%; /* :( */
height: 100px;
background: black;
position: absolute;
left: 0;
}
Only two lines of code without any calculates :)
jsFiddle
I'm gonna go out on a limb and say 'no' on this one.
There's no way of making the inner div span the entire width of the page without breaking it out of the document flow, i.e. absolutely positioning it.
I think it could be done only with absolute position:
#inner-container {
width: 100%; /* :( */
height: 100px;
background: black;
left: 0;
position: absolute;
}
Fiddle
Actually, this can be done with calc() in conjunction with viewport units vw
FIDDLE - Working in iE10+
#inner-container {
height: 100px;
background: black;
width: 100vw;
margin-left: calc(250px - 50vw);
}
Actually, according to the spec (see example 14) - this should work (that is - in all browsers), but for now there's a bug where calc doesn't work with viewport units.
Alternatively you could do this:
#inner-container {
height: 100px;
background: black;
margin-right: calc(250px - 50vw);
margin-left: calc(250px - 50vw);
}
-- this way - in IE it will look as required, and on other browsers it will look centered as you have it.
FIDDLE

How to place text on top of the image inside container with relative width 80%

i want to place text on top of the image inside container with width 80%
conainer.width 80% - code below not working
conainer.width 100% - code below working
screen shot of my brouser
<style type="text/css">
.conainer {
margin: auto;
width: 80%; /* change that to 100% code will work */
border: thin solid #000;
}
h2 {
position: absolute;
top: 200px;
left: 0;
width: 100%;
}
.image {
position: relative;
width: 100%; /* for IE 6 */
}
</style>
<div class="conainer">
<div class="image">
<img src="img/banners.jpg" width="100%" height="100%" />
<h2>some text gos here</h2>
</div>
</div>
You should just set the background of your div, instead of trying to place the h2 on top of an img tag
It works for me. Is there any more code?
I think the most code-efficient and compatible method would be to define your banner image as the background-image of div.image using CSS.
Replace your h2 style as
h2 {
position: fixed;//yours is on top of image but absolutely positioned, so not visible
top: 200px;
left: 0; //Specify where you want to put your image with top and left properly now.
width: 100%;
}
Ok! i found the problem change top 200 => top 0
h2 {
position: absolute;
top: 0px;
left: 0;
width: 100%;

How do I center an image if it's wider than its container?

Normally, you center images with display: block; margin: auto, but if the image is larger than the container, it overflows to the right. How do I make it overflow to the both sides equally? The width of the container is fixed and known. The width of the image is unknown.
A pure css solution
Requiring one extra wrapper (tested in FireFox, IE8, IE7):
Improved Answer
There was a problem with the original answer (below). If the image is larger than the container that outer is centered on with it's auto margins, then it truncates the image on the left and creates excessive space on the right, as this fiddle shows.
We can resolve that by floating inner right and then centering from the right. This still truncates the img off the page to the left, but it does so by explicitly pushing it that way and then centers back off of that, the combination of which is what prevents the extra horizontal scroll on the right. Now we only get as much right scroll as we need in order to see the right part of the image.
Fiddle Example (Borders in fiddle are for demo only.)
Essential CSS
div.outer {
width: 300px; /* some width amount needed */
margin: 0 auto;
overflow: visible;
}
div.inner {
position:relative;
float: right; /* this was added and display removed */
right: 50%;
}
div.inner img {
position: relative;
right:-50%; /* this was changed from "left" in original */
}
If you desire no right scroll at all for wide images
Then using the above, also set whatever element wraps outer (like body or a third wrapper) to have overflow: hidden.
Original Idea (for History)
Fiddle Example (Borders in fiddle are for demo only.)
HTML
<div class="outer">
<div class="inner">
<img src="/yourimage.png">
</div>
</div>
CSS
div.outer {
width: 300px; /* some width amount needed */
margin: 0 auto;
overflow: visible;
}
div.inner {
display: inline-block;
position:relative;
right: -50%;
}
div.inner img {
position: relative;
left:-50%;
}
Here's a 2 line CSS solution (a couple more lines might be required for cross-browser support):
img {
margin-left: 50%;
transform: translateX(-50%);
}
HTML
​<div class="image-container">
<img src="http://www.google.com/images/logo.gif" height="100" />
</div>​
CSS
.image-container {
width: 150px;
border: solid 1px red;
margin:100px;
}
.image-container img {
border: solid 1px green;
}
jQuery
$(".image-container>img").each(function(i, img) {
$(img).css({
position: "relative",
left: ($(img).parent().width() - $(img).width()) / 2
});
});
​
See it on jsFiddle: http://jsfiddle.net/4eYX9/30/
Alternative pure CSS solution is to use transform attribute:
HTML:
<div class="outer">
<img class="image" src="http://www.gstatic.com/webp/gallery/4.jpg" />
</div>
CSS:
.outer {
position: relative;
width: 100px;
border: 1px solid black;
height: 150px;
margin-left: 100px; /* for demo */
/* overflow: hidden; */
}
img.image {
width: 200px;
opacity: 0.7;
position: absolute;
left: 50%;
transform: translateX(-50%);
-webkit-transform: translateX(-50%);
}
Fiddle
Just to add a overflow:hidden to parent div to hide the extra area of the image.
Your best bet is to set it as background image of the container instead.
#container {
background: url('url/to/image.gif') no-repeat center top;
}
In fact there is a simpler pure css/html way (without large horizontal scroll) :
Html :
<div class="outer">
<img src="/my/sample/image.jpg">
</div>
Css :
If you don't want to see image overflow
div.outer img {
position: absolute;
left: -50%;
z-index:-1;
}
div.outer {
overflow: hidden;
position: relative;
height: 200px;
}
With image overflow visible
div.outer img {
position: absolute;
left: -50%;
z-index:-1;
}
div.outer {
overflow: visible;
position: relative;
height: 200px;
}
body, html {
overflow-x:hidden;
}
A background solution with image overflow visible :
Html :
<div class="outer">
<div class="inner"></div>
</div>
Css :
div.outer {
width: 100%;
height: 200px;
}
div.inner {
background: url('/assets/layout/bg.jpg') center no-repeat;
position: absolute;
left: 0;
width: 100%;
height: inherit;
}
assuming outer is in a width specified container.
I see this is an old post, so maybe everybody knows this by now, but I needed help for this and I solved it using flex:
.parent {
display: flex;
/* give it the width and height you like */
}
.parent img {
min-width: 100%;
min-height: 100%;
object-fit: cover;
}
I can only think of a Javascript solution since what you need to do is relatively position the image a negative amount to the left of its container:
jQuery
$(document).ready(function(){
var theImg = $('#container img');
var theContainer = $('#container');
if(theImg.width() > theContainer.width()){
theImg.css({
position: 'relative',
left: (theContainer.width() - theImg.width()) / 2
})
}
})
I found this to be a more elegant solution, without flex, similar to something above, but more generalized (applies on both vertical and horizontal):
.wrapper {
overflow: hidden;
}
.wrapper img {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
/* height: 100%; */ /* optional */
}
I don't think there is a pure CSS solution (Except for the next answer :)). However with Javascript it would be just a matter of finding the width of the image, subtracting the container width, dividing by two and you have how far to the left of the container you need.

Center a position:fixed element

I would like to make a position: fixed; popup box centered to the screen with a dynamic width and height. I used margin: 5% auto; for this. Without position: fixed; it centers fine horizontally, but not vertically. After adding position: fixed;, it's even not centering horizontally.
Here's the complete set:
.jqbox_innerhtml {
position: fixed;
width: 500px;
height: 200px;
margin: 5% auto;
padding: 10px;
border: 5px solid #ccc;
background-color: #fff;
}
<div class="jqbox_innerhtml">
This should be inside a horizontally
and vertically centered box.
</div>
How do I center this box in screen with CSS?
If your div has a known width and height, then you basically need to set top and left to 50% to center the left-top corner of the div. You also need to set the margin-top and margin-left to the negative half of the div's height and width to shift the center towards the middle of the div.
position: fixed;
width: 500px;
height: 200px;
top: 50%;
left: 50%;
margin-top: -100px; /* Negative half of height. */
margin-left: -250px; /* Negative half of width. */
Or, if your div has a dynamic/undefined width and/or height, then instead of the margin, set the transform to the negative half of the div's relative width and height.
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
Or, if your div has at least a fixed width and you don't care about centering vertically and old browsers such as IE6/7, then you can instead also add left: 0 and right: 0 to the element having a margin-left and margin-right of auto, so that the fixed positioned element having a fixed width knows where its left and right offsets start. In your case thus:
position: fixed;
width: 500px;
margin: 5% auto; /* Will not center vertically and won't work in IE6/7. */
left: 0;
right: 0;
Again, this works only in IE8+ if you care about IE, and this centers only horizontally not vertically.
I want to make a popup box centered to the screen with dynamic width and height.
Here is a modern approach for horizontally centering an element with a dynamic width - it works in all modern browsers; support can be seen here.
Updated Example
.jqbox_innerhtml {
position: fixed;
left: 50%;
transform: translateX(-50%);
}
For both vertical and horizontal centering you could use the following:
Updated Example
.jqbox_innerhtml {
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
You may wish to add in more vendor prefixed properties too (see the examples).
Or just add left: 0 and right: 0 to your original CSS, which makes it behave similarly to a regular non-fixed element and the usual auto-margin technique works:
.jqbox_innerhtml
{
position: fixed;
width:500px;
height:200px;
background-color:#FFF;
padding:10px;
border:5px solid #CCC;
z-index:200;
margin: 5% auto;
left: 0;
right: 0;
}
Note you need to use a valid (X)HTML DOCTYPE for it to behave correctly in IE (which you should of course have anyway..!)
Add a container like:
div {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
text-align: center;
}
Then put your box into this div will do the work.
Edit: as mentioned in the comments, the inner content needs to be set to display: inline-block assuming there're two divs like:
<div class="outer">
<div class="inner">
content goes here
</div>
</div>
Then the CSS for the inner needs to be:
.outer {
position: fixed;
text-align: center;
left: 0;
right: 0;
}
.inner {
display: inline-block;
}
Together with the outer div having a left: 0; right:0; and text-align: center this will align the inner div centered, without explicitly specifying the width of the inner div.
Just add:
left: calc(-50vw + 50%);
right: calc(-50vw + 50%);
margin-left: auto;
margin-right: auto;
Center fixed position element
(the simple & best way I know)
position:fixed;
top: 0; left: 0;
transform: translate(calc(50vw - 50%));
For centering it horizontally & vertically (if height is same as width)
position:fixed;
top: 0; left: 0;
transform: translate(calc(50vw - 50%), calc(50vh - 50%));
Both of these approaches will not limit centered element's width less than viewport width, when using margins in flexbox, inside centered element
#modal {
display: flex;
justify-content: space-around;
align-items: center;
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
inside it can be any element with diffenet width, height or without.
all are centered.
This solution does not require of you to define a width and height to your popup div.
http://jsfiddle.net/4Ly4B/33/
And instead of calculating the size of the popup, and minus half to the top, javascript is resizeing the popupContainer to fill out the whole screen...
(100% height, does not work when useing display:table-cell; (wich is required to center something vertically))...
Anyway it works :)
left: 0;
right: 0;
Was not working under IE7.
Changed to
left:auto;
right:auto;
Started working but in the rest browsers it stop working!
So used this way for IE7 below
if ($.browser.msie && parseInt($.browser.version, 10) <= 7) {
strAlertWrapper.css({position:'fixed', bottom:'0', height:'auto', left:'auto', right:'auto'});
}
I used vw (viewport width) and vh (viewport height). viewport is your entire screen. 100vw is your screens total width and 100vh is total height.
.class_name{
width: 50vw;
height: 50vh;
border: 1px solid red;
position: fixed;
left: 25vw;top: 25vh;
}
You can basically wrap it into another div and set its position to fixed.
.bg {
position: fixed;
width: 100%;
}
.jqbox_innerhtml {
width: 500px;
height: 200px;
margin: 5% auto;
padding: 10px;
border: 5px solid #ccc;
background-color: #fff;
}
<div class="bg">
<div class="jqbox_innerhtml">
This should be inside a horizontally and vertically centered box.
</div>
</div>
I just use something like this:
.c-dialogbox {
--width: 56rem;
--height: 32rem;
position: fixed;
width: var(--width);
height: var(--height);
left: calc( ( 100% - var(--width) ) / 2 );
right: calc( ( 100% - var(--width) ) / 2 );
top: calc( ( 100% - var(--height) ) / 2 );
bottom: calc( ( 100% - var(--height) ) / 2 );
}
It centers the dialog box both horizontally and vertically for me, and I can use different width and height to fit different screen resolutions to make it responsive, with media queries.
Not an option if you still need to provide support for browsers where CSS custom properties or calc() are not supported (check on caniuse.)
This one worked the best for me:
display: flex;
justify-content: center;
align-items: center;
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
To fix the position use this :
div {
position: fixed;
left: 68%;
transform: translateX(-8%);
}
simple, try this
position: fixed;
width: 500px;
height: 300px;
top: calc(50% - 150px);
left: calc(50% - 250px);
background-color: red;
One possible answer:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>CSS Center Background Demo</title>
<style type="text/css">
body {
margin: 0;
padding: 0;
}
div.centred_background_stage_1 {
position: fixed;
z-index:(-1 );
top: 45%;
left: 50%;
}
div.centred_background_stage_2 {
position: relative;
left: -50%;
top: -208px;
/* % does not work.
According to the
http://reeddesign.co.uk/test/points-pixels.html
6pt is about 8px
In the case of this demo the background
text consists of three lines with
font size 80pt.
3 lines (with space between the lines)
times 80pt is about
~3*(1.3)*80pt*(8px/6pt)~ 416px
50% from the 416px = 208px
*/
text-align: left;
vertical-align: top;
}
#bells_and_wistles_for_the_demo {
font-family: monospace;
font-size: 80pt;
font-weight: bold;
color: #E0E0E0;
}
div.centred_background_foreground {
z-index: 1;
position: relative;
}
</style>
</head>
<body>
<div class="centred_background_stage_1">
<div class="centred_background_stage_2">
<div id="bells_and_wistles_for_the_demo">
World<br/>
Wide<br/>
Web
</div>
</div>
</div>
<div class="centred_background_foreground">
This is a demo for <br/>
<a href="http://stackoverflow.com/questions/2005954/center-element-with-positionfixed">
http://stackoverflow.com/questions/2005954/center-element-with-positionfixed
</a>
<br/><br/>
<a href="http://www.starwreck.com/" style="border: 0px;">
<img src="./star_wreck_in_the_perkinnintg.jpg"
style="opacity:0.1;"/>
</a>
<br/>
</div>
</body>
</html>
Try using this for horizontal elements that won't center correctly.
width: calc (width: 100% - width whatever else is off centering it)
For example if your side navigation bar is 200px:
width: calc(100% - 200px);
This works wonderfully when you don't know the size of the thing you are centering, and you want it centered in all screen sizes:
.modal {
position: fixed;
width: 90%;
height: 90%;
top: 5%; /* (100 - height) / 2 */
left: 5%; /* (100 - width) / 2 */
}
What I use is simple. For example I have a nav bar that is position : fixed so I adjust it to leave a small space to the edges like this.
nav {
right: 1%;
width: 98%;
position: fixed;
margin: auto;
padding: 0;
}
The idea is to take the remainder percentage of the width "in this case 2%" and use the half of it.
Had this problem so I concluded that using a (invisible) container is the best option (based on answer #Romulus Urakagi Ts'ai). To make it with flexbox:
.zoom-alert {
position: fixed;
justify-content: center;
display: flex;
bottom: 24px;
right: 0;
left: 0;
z-index: 100000;
width: 100%;
&__alert {
flex: 0 0 500px;
padding: 24px;
background-color: rgba(212, 193, 105, 0.9);
border: 1px solid rgb(80, 87, 23);
border-radius: 10px;
}
}
(the syntax is SCSS but can be easily modified to pure CSS)
Center element of a div with the property of
position:fixed
Html and Css code
.jqbox_innerhtml {
position: fixed;
width:100%;
height:100%;
display: flex;
justify-content: space-around;
align-items: center;
left: 0;
top: 0;
width: 100%;
height: 100%;
border: 5px solid #ccc;
background-color: #fff;
}
<div class="jqbox_innerhtml">
This should be inside a horizontally
and vertically centered box.
</div>
Another simple solution is to set the width of the element to fit-content and set the left and right to 0px;
width: fit-content;
position: fixed;
left: 0px;
right: 0px;
This is useful if you don't know the width of the element.
The only foolproof solution is to use table align=center as in:
<table align=center><tr><td>
<div>
...
</div>
</td></tr></table>
I cannot believe people all over the world wasting these copious amount to silly time to solve such a fundamental problem as centering a div. css solution does not work for all browsers, jquery solution is a software computational solution and is not an option for other reasons.
I have wasted too much time repeatedly to avoid using table, but experience tell me to stop fighting it. Use table for centering div. Works all the time in all browsers! Never worry any more.

Resources