Reputation: 195
I'm new to web development and learning how to align elements to vertical centre. This is what I found, but it doesn't work. However if I add some meaningless characters in css, it'll work perfectly fine, including fullstops, comma, etc. I tried on Safari and Chrome and they behaved in the same way. Does these character mean anything?
Original solution:
<head>
<style>
#parent {
position: relative;}
#child {
position: absolute;
top: 50%;
height: 30%;
width: 50%;
}
</style>
</head>
<body>
<div id="parent">
<div id="child">Content here</div>
</div>
</body>
Solution with meaningless character:
<head>
<style>
#parent {
,
position: relative;}
#child {
position: absolute;
top: 50%;
height: 30%;
width: 50%;
}
</style>
</head>
Upvotes: 1
Views: 149
Reputation: 321
you should move the #parent
instead of the #child
Move the parent box not the child.
#parent{ position: absolute;}
is inherinting from <body>
#child {position: relative;}
is inheriting from #parent
HAPPY CODE! KEEP IT UP
:)
#parent {
border: 10px solid green;
position: absolute;
width: 50%;
top: 50%;
}
#child {
border: 5px solid red;
position: relative;
}
<div id="parent">
<div id="child">Content here</div>
</div>
Upvotes: 1
Reputation: 11
The comma invalidates the CSS style #parent because it's improper syntax. The CSS parser on browsers will just skip over it. It's the same as if you had just
<head>
<style>
#child {
position: absolute;
top: 50%;
height: 30%;
width: 50%;
}
</style>
</head>
<body>
<div id="parent">
<div id="child">Content here</div>
</div>
</body>
Why it works once you remove the #parent style is another matter explained here css - parent's position is absolute and child's position is relative and vice versa
Upvotes: 1
Reputation: 1579
it happens Because those characters are breaking the CSS for that element "#parent" it's like it's not there see my example
#child {
position: absolute;
top: 50%;
height: 30%;
width: 50%;
}
<head>
</head>
<body>
<div id="parent">
<div id="child">Content here</div>
</div>
</body>
Upvotes: 2
Reputation: 1
As you can see in my screenshot that character makes position: relative invalid value. I would like to recommand you Developer tools (enter link description here) so you can see css for particular tags when you click on them.
Upvotes: 0