I'm doing a website and I trying to center a text but I don't know what the top is not working. It works if I use something like this:
up:25px;
But this doesn't work when I want to use this:
up:50%;
Can you help me? This is my code:
.absolute{
position:absolute;
}
.relative{
position:relative;
}
.white{
background-color:white;
}
#menu-title{
width:300px;
z-index:5;
top:50%;
left:calc(50% - 150px);
top:calc(50% - 2.5em);
}
<div class='relative' id='menu'>
<div class='absolute white' id='menu-title'>
<h2 >Menu</h2>
</div>
</div>
Considering you want to center the <h2> in the <div> with id of 'menu-title', you have several ways to do that.
If you want to use the top property you first have to define the position to fixed, like this:
#menu-title {
position: fixed;
top: 50%;
}
The other way to do that is to use margins:
#menu-title {
margin-top: 100px;
}
You can always change the px.
You should use position:fixed
and also depends on the size of the text you can move it to exactly in the center with transform
#menu-title{
position: fixed;
top: 50%;
left: 50%;
/* bring your own prefixes */
transform: translate(-50%, -50%);
}
hope that helps
Make position:fixed
.absolute{
position:fixed;
}
.relative{
position:relative;
}
.white{
background-color:white;
}
#menu-title{
width:300px;
z-index:5;
top:50%;
left:calc(50% - 150px);
top:calc(50% - 2.5em);
}
<div class='relative' id='menu'>
<div class='absolute white' id='menu-title'>
<h2 >Menu</h2>
</div>
</div>
First of all... it IS working, only not as you expected it. The CSS top:calc(50% - 2.5em), is relative to the first POSITIONED ancestor element. In your case this is the relative (parent) element with id="menu". The height of this element is 0. Therefore it works as it should, but apperently not as you expected.
You might have expected that the menu had a certain height. It does not, because its only child (the menu-title div) is positioned absolute. Absolute positioned elements do not grow their parents.
More likely is that you expected that the title would position relative to the viewport height, instead of relative to its parent.
There are three ways to position the menu-title relative to the viewport height:
Solution 1. Remove relative positioning from the parent
This will make the parents position static (not positioned) and the first positioned ancestor element becomes the viewport. The viewport has a 100% height by nature. Therefore this solution works. A working example of this solution can be found here: http://codepen.io/anon/pen/bBLZva
Solution 2. Give the parent 100% height
When removing the relative positioning from the ancestor(s) is not an option, you can give the parent an 100% height. This, however, is not a straight forward task. Simply adding the CSS height: 100% to the parent is not enough. The height of the menu div is relative to the height of its parent element and not to the height of the viewport. Therefore you need to set their parents explicitly to 100% (viewport height). This can be achieved by adding: body,html {height: 100%;} to the CSS. A working example of this solution can be found here: http://codepen.io/anon/pen/XNZGOv
Solution 3. Use position fixed
Know that position: fixed is fundamentally different than position: absolute AND has compatability issues on older iOS and Android versions (stock browser). However it MIGHT result in the expected behaviour. This can be explained by the fact that 'position: fixed' implies positioning relative to the viewport (and not to the parent). You should use position fixed on the title itsself. A working example of this solution can be found here: http://codepen.io/anon/pen/PbQgpB
Related
This question already has answers here:
Pseudo element not full container width when border used
(3 answers)
Closed 1 year ago.
As I know, child's width percentage's standard is parent's content box(only the content, without padding or margin.). So if there's a padding in parent and child's width is 100%, child's width is smaller than parents. But If I position child as a absolute and parent as a relative, child's width is just equal to the parent's no matter padding and margin in parents. Like this:
<div class="first">HI
<div class="second">
HELLO
</div>
</div>
css code
.first{
background-color: yellow;
width:100px;
height:100px;
padding:10%;
position:relative;
}
.second{
background-color: blue;
position:absolute;
width: 100%;
height:100%;
opacity:40%;
}
Eventhough parent's position and relative so Child is totally dependent on '.first'. What's the standard of child's width in this case?
This snippet shows the result of setting the second div to have position relative and then position absolute. You can see that the absolutely positioned element takes on the width of its parent including the padding.
.first {
background-color: yellow;
width: 100px;
height: 100px;
padding: 10%;
position: relative;
}
.second {
background-color: blue;
width: 100%;
height: 100%;
opacity: 40%;
}
.relative {
position: relative;
}
.absolute {
position: absolute;
}
<h2>The blue square has relative position</h2>
<div class="first">HI
<div class="second relative">
HELLO
</div>
</div>
<h2>The blue square has absolute position</h2>
<div class="first">HI
<div class="second absolute">
HELLO
</div>
</div>
The reason seems to be that:
when a box has position: absolute its containing box is the parent's padding box.
See the accepted answer to: Absolute positioning ignoring padding of parent though I am struggling to find the exact description of that in the actual standard documents and it would be good if someone could point out a primary reference.
UPDATE: thanks to Temani Afif who has pointed out this SO answer which has info. from an actual specification:
The standard of the % for position:absolute is of the nearest positioned ancestor block and if no ancestor is positioned, it is relative to body element. In your case since the first is positioned relative the second will be relative to first and if u remove the position attribute of first, second will be positioned relative to body.
You can also check this - https://www.w3schools.com/css/css_positioning.asp
Why does wrapper div not have a height? If I set the height (height:200px) the green background appears but how to set with auto height?
Here is my code (JSFiddle):
HTML:
<div class="wrapper">
<div class="effect"></div>
<div class="content">
...content
</div>
</div>
CSS:
.content {
position: absolute;
background-color:red;
}
.wrapper, .effect {
background: green;
}
.wrapper {
position: relative;
width: 630px;
}
.effect {
width:100%;
position: absolute;
}
It is not working (i.e. parent element not having any height) because all the immediate descendant of the .wrapper element is absolutely positioned — this will have the effect of taking them out of the flow of the document, therefore causing the parent's dimension to collapse to nothing.
You will also notice that the effect is the same when you float all
descendants of the parent wrapper, because float also has the
effect of taking normal elements out of the document flow.
There are only two ways to prevent this from happening, both of which involving declaring a certain height for the parent .wrapper element:
Either you explicitly state a height for the parent (see example fiddle)
Or use a relative height (say, in percentages or viewport units) that is not dependent on its own content.
You should reconsider your design strategy, and what you're trying to achieve. There is probably other ways to achieve what you intend to do, will you mind showing us?
I'm using absolute positioning to have 100% height of the screen. I also want to limit the resizing of my page so I add min-width in my header and content divs. The problem I'm facing is that once resizing reaches the min-width boundary, right side margin disappears. Any suggestions how to make it work?
HTML:
<div id="header">Header</div>
<div id="content">Content</div>
CSS:
html, body{
background: blue;
padding:0px;
margin:0px;
}
#header{
height:30px;
background: yellow;
position: absolute;
top:20px;
right:20px;
left:20px;
min-width:500px;
}
#content{
background:green;
position: absolute;
top:50px;
right:20px;
left:20px;
bottom:20px;
min-width:500px;
}
jsfiddle
I found a solution my self. The problem is that if you apply min-width on the element which has padding or margin, that padding or margin collapses when you resize the window. So the solution was to wrap that element inside root element and set min-width on the root element.
P.S. I also tried box-sizing approach suggested by Sam but effect was almost the same: margin shrinks but doesn't disappear all. Applying min-width on outer element solves the issue as well.
Here is an example
updated fiddle
You can use the CSS box-sizing property to accomplish this in modern browsers (IE8+)
I have a container DIV set to "position:fixed", that includes another div set to position absolute.
Is there any way I can make this included div with the width and the height to fit the content, and not always 100%?
This is what I have: http://jsfiddle.net/ydSqU/
<div class="transparent_background">
<div id="window">hello.</div>
</div>
This is what I would like: http://jsfiddle.net/3BYPu/ (without having to manually set width/height).
<div class="transparent_background">
<div id="window" style="width:30px; height:30px">hello.</div>
</div>
aur0n,
If possible, the best and easiest way to do this is to use javascript to calculate the height and width of the elements then position the inner element accordingly.
To accomplish this, you simply take the width of the containing element and divide it by two, then set the left value of the inner element to that amount minus half its own width. Then, do the same for the height.
Also, one of the things you might have missed is that your inner div should be set to position: relative and display: inline. The reason your div is stretching to match the width of the containing element is because position: absolute takes the element out of normal flow, while position: relative leaves it in normal flow.
Here is the code I used (using jQuery):
// centering the width
$("#window").css("left", ($(".transparent_background").width()/2)-($("#window").width()/2) + "px");
// centering the height
$("#window").css("top", ($(".transparent_background").height()/2)-($("#window").height()/2) + "px");
With this solution, you don't have to manually set width and height. Also, having an inner div with multiple lines of text will not be a problem.
fiddle: http://jsfiddle.net/ydSqU/3/
Can you try the below,
#window {
border:2px dotted red;
background:white;
width:50%;
height:auto;
position: absolute;
top: 50%;
left: 50%;
margin-left: -25%;
vertical-align:center;
}
#aur0n change the css like this
#window {
border:2px dotted red;
background:white;
position: absolute;
clear:both;
}
I have the following structure for a slide within a site created for an iPad
<div id="slide4">
<div class="slide4A1"></div>
<div class="slide4A2"></div>
<div class="slide4A3"></div>
</div>
Each block contains background images which are controlled via media queries to allow them to fill the screen whether the Ipad is portrait or landscape.
I need to animate the blocks but want the positions to be as follows -
.slide4A1 - fixed left
.slide4A2 - fixed centre
.slide4A3 - fixed right
I have tried floating left and right for slides 1 and 3 and centering with auto margin- but the layout go's astray - any ideas?
js fiddle here - http://jsfiddle.net/chunk_pd/PAqSb/6/
First you have to clean your CSS - combining position: absolute; and float won't work. Use your wrapper <div id="slide4"> and set its positioning as relative and width: 100%. Both animated divs have to have position: absolute; top: 0px; in order to reach top of the wrapper.
What is more - you have a typo in your JS:
$('.slide4A1').animate({left:'0px;',}, 1600); just remove ; after 0px and voila.
I believe this is what you expected to see:
CSS:
#slide4 {position: relative; }
.slide4A1, .slide4A3{ position:absolute; top: 0; }
.slide4A1{background:#000; width:120px; height:1024px; left:-120px;}
.slide4A2{background:#367ab2; width:531px; height:1024px; margin-left:auto; margin-right:auto; position:relative!important; display: none;}
.slide4A3{background:#ff7e00; width:120px; height:1024px; right:-120px;}
JS:
function goDown3bPt2(){
$('.slide4A1').animate({left:'0',}, 1600);
$('.slide4A2').fadeIn('slow');
$('.slide4A3').animate({right:'0',}, 2100);
}
Updated jsfiddle.
Apply width :100%, margin:0 auto to slide4
width :33 % to slide4A1,slide4A2,slide4A3
Float Left to slide4A1 and float right to slide4A3 classes....!!