1.8 KiB
1.8 KiB
title
title |
---|
Lock an Element to its Parent with Absolute Positioning |
Remember to use
Read-Search-Ask
if you get stuck. Try to pair program and write your own code
Lock an Element to its Parent with Absolute Positioning
Hint: 1
Use position: absolute;
.
try to solve the problem now
Hint: 2
Use the right
and top
properties.
try to solve the problem now
Spoiler Alert!
Solution ahead!
<style>
#searchbar {
position: absolute;
top: 50px;
right: 50px;
}
section {
position: relative;
}
</style>
<body>
<h1>Welcome!</h1>
<section>
<form id="searchbar">
<label for="search">Search:</label>
<input type="search" id="search" name="search">
<input type="submit" name="submit" value="Go!">
</form>
</section>
</body>
Code Explanation:
#searchbar{}
selects all the elements with the ID ofsearchbar
.position: absolute;
positions the element with respect to its nearest ancestor havingposition: relative;
.top: 50px;
andright: 50px;
offsets the element by50px
to the bottom and left respectively.