Jump to content

What does top:100% does in this code?

Go to solution Solved by Hairless Monkey,
2 hours ago, shivajikobardan said:

top:100% only. What's it doing? I'm not getting it.

top: 100% sets the location to be directly below the parent element.

 

2 hours ago, shivajikobardan said:

with respect to li, .dropdown should be 100% from the top of li is what it probably means

So yes, this is correct, just another way of saying it.

 

Here's an example:

 

image.png.cf0b3439db4618bb49d4060e61c4c779.png

<!DOCTYPE html>
<html>
<head>
<style>
div.a {
  position: relative;
  width: 400px;
  height: 200px;
  border: 1px solid red;
}

div.b {
  position: absolute;
  top: 100%;
  border: 1px solid blue;
} 
</style>
</head>
<body>

<h1>The top Property</h1>

<div class="a">This div element has position: relative;
  <div class="b">This div element has position: absolute and top:100%.</div>
</div>

</body>
</html>

You can play with this here:

 

https://www.w3schools.com/cssref/tryit.php?filename=trycss_position_top2

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Grid Example</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="container">
      <div class="header">
        <li>HOme</li>
        <li>
          Category
          <ul class="dropdown">
            <li>
              mobiles
              <ul class="subdropdown">
                <li>apple</li>
                <li>samsung</li>
                <li>huwaei</li>
                <li>one plus</li>
                <li>redmi</li>
              </ul>
            </li>
            <li>laptops</li>
            <li>beauty</li>
            <li>fitness</li>
            <li>Accessories</li>
          </ul>
        </li>
        <li>About</li>
        <li>Gallery</li>
        <li>Contact</li>
      </div>
      <div class="maintop">main top</div>
      <div class="bottomleft">bottom left</div>
      <div class="bottomright">bottom right</div>
      <div class="leftsidebar">Left sidebar</div>
      <div class="footer">Footer</div>
      <div class="rightsidebar">right side bar</div>
    </div>
  </body>
</html>
 

CSS:

* {
  margin: 0;
  box-sizing: border-box;
}
.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  /* grid-template-rows: 5vh 45vh 45vh 5vh; */
}
.header {
  grid-column-start: 1;
  grid-column-end: 5;
  background-color: aqua;
  display: grid;
  grid-template-columns: auto auto auto auto auto;
  align-items: center;
  padding-top: 5px;
  padding-bottom: 5px;
}
li {
  list-style: none;
  text-align: center;
}
.maintop {
  grid-column-start: 2;
  grid-column-end: 4;
  background-color: purple;
}
.leftsidebar {
  grid-row-start: 2;
  grid-row-end: 4;
  background-color: green;
  height: 90vh;
}
.rightsidebar {
  grid-row-start: 2;
  grid-row-end: 4;
  background-color: burlywood;
  grid-column-start: 4;
  /* height: 80vh`; */
}
.bottomleft {
  background-color: orange;
  /* height: 50vh; */
}
.bottomright {
  background-color: gold;
  /* height: 50vh; */
}
.footer {
  background-color: chartreuse;
  grid-column-start: 1;
  grid-column-end: 5;
  height: 5vh;
}
li {
  position: relative;
}

.dropdown {
  display:none;
  position: absolute;
  background-color: blue;
  padding: 0;
  width: 20vw;
  top: 100%;
}
li:hover .dropdown {
  display: block;
}
.dropdown li {
  padding-top: 5px;
  padding-bottom: 5px;
  color: yellow;
}

.dropdown li:hover {
  color: blue;
  background-color: yellow;
}

li:hover {
  background-color: goldenrod;
}

.subdropdown{
  position:absolute;
  background-color:black;
  color:blue;
  left:100%;
  top:0%;
  padding:0;
  width:150px;
  /* padding-left:30px;
  padding-right:30px; */
  display:none;

}

.subdropdown li:hover{
  background-color:skyblue;
  color:yellow;

}

.dropdown li:hover .subdropdown{
  display:block;

}

I'm wondering what the below code is doing here?

 

li {
  position: relative;
}

.dropdown {
  display:none;
  position: absolute;
  background-color: blue;
  padding: 0;
  width: 20vw;
  top: 100%;
}

top:100% only. What's it doing? I'm not getting it. I'm aware of absolute and relative positioning. 

with respect to li, .dropdown should be 100% from the top of li is what it probably means, But I can't see that how and get how..Also is there an easier approach to building a drop down menu than this? I'm struggling to understand this code.

Link to comment
https://linustechtips.com/topic/1467531-what-does-top100-does-in-this-code/
Share on other sites

Link to post
Share on other sites

2 hours ago, shivajikobardan said:

top:100% only. What's it doing? I'm not getting it.

top: 100% sets the location to be directly below the parent element.

 

2 hours ago, shivajikobardan said:

with respect to li, .dropdown should be 100% from the top of li is what it probably means

So yes, this is correct, just another way of saying it.

 

Here's an example:

 

image.png.cf0b3439db4618bb49d4060e61c4c779.png

<!DOCTYPE html>
<html>
<head>
<style>
div.a {
  position: relative;
  width: 400px;
  height: 200px;
  border: 1px solid red;
}

div.b {
  position: absolute;
  top: 100%;
  border: 1px solid blue;
} 
</style>
</head>
<body>

<h1>The top Property</h1>

<div class="a">This div element has position: relative;
  <div class="b">This div element has position: absolute and top:100%.</div>
</div>

</body>
</html>

You can play with this here:

 

https://www.w3schools.com/cssref/tryit.php?filename=trycss_position_top2

BabyBlu.2 (Primary): 

  • CPU: AMD Ryzen 5 9600X
  • Motherboard: Asus ROG STRIX B650E-F
  • RAM: G.Skill Flare X5 64GB (2x32GB) DDR5-6000 CL30 @ 6400MHz 30-40-40-96
  • GPU: MSI RTX 2080 Sea Hawk EK X, 2100MHz core, 8000MHz mem
  • Case: Phanteks Evolv X
  • Storage: XPG SX8200 Pro 2TB, 3x ADATASU800 1TB (RAID 0), Samsung 970 EVO Plus 500GB
  • PSU: Corsair HX1000i
  • Display: MSI MPG341CQR 34" 3440x1440 144Hz Freesync, Dell S2417DG 24" 2560x1440 165Hz Gsync
  • Cooling: Custom water loop (CPU & GPU), Radiators: 1x140mm(Back), 1x280mm(Top), 1x420mm(Front)
  • Keyboard: Corsair Strafe RGB (Cherry MX Brown)
  • Mouse: MasterMouse MM710
  • Headset: Corsair Void Pro RGB
  • OS: Windows 11 Pro

Roxanne (Wife Build):

  • CPU: AMD Ryzen 5 7600
  • Motherboard: Gigabyte B650I AORUS ULTRA
  • RAM: G.Skill Flare X5 32GB (2x16GB) DDR5-6000 @ 6000MHz 30-38-38-96
  • GPU: EVGA GTX 1080 FTW2 w/ LM
  • Case: Cooler Master MasterBox NR200
  • Storage: Samsung 850 EVO 250GB, Samsung 860 EVO 1TB, Silicon Power A80 2TB NVME
  • PSU: Corsair SF850L
  • Display: Dell Alienware AW3420DW GSync
  • Cooling: Arctic Liquid Freezer II 280mm
  • Keyboard: GMMK TKL(Kailh Box White)
  • Mouse: Glorious Model O-
  • Headset: SteelSeries Arctis 7
  • OS: Windows 11 Pro

BigBox (HTPC):

  • CPU: Ryzen 5800X3D
  • Motherboard: Gigabyte B550i Aorus Pro AX
  • RAM: Corsair Vengeance LPX 2x8GB DDR4-3600 @ 3600MHz 14-14-14-28
  • GPU: MSI RTX 3080 Ventus 3X Plus OC, de-shrouded, LM TIM, replaced mem therm pads
  • Case: Fractal Design Node 202
  • Storage: SP A80 1TB, WD Black SN770 2TB
  • PSU: Corsair SF600 Gold w/ NF-A9x14
  • Display: Samsung QN90A 65" (QLED, 4K, 120Hz, HDR, VRR)
  • Cooling: Thermalright AXP-100 Copper w/ NF-A12x15
  • Keyboard/Mouse: Rii i4
  • Controllers: 4X Xbox One & 2X N64 (with USB)
  • Sound: Denon AVR S760H with 5.1.2 Atmos setup.
  • OS: Windows 11 Pro

Harmonic (NAS/Game/Plex/Other Server):

  • CPU: Intel Core i7 6700
  • Motherboard: ASRock FATAL1TY H270M
  • RAM: 64GB DDR4-2133
  • GPU: Intel HD Graphics 530
  • Case: Fractal Design Define 7
  • HDD: 3X Seagate Exos X16 14TB in RAID 5
  • SSD: Inland Premium 512GB NVME, Sabrent 1TB NVME
  • Optical: BDXL WH14NS40 flashed to WH16NS60
  • PSU: Corsair CX450
  • Display: None
  • Cooling: Noctua NH-U14S
  • Keyboard/Mouse: None
  • 2.5Gb NIC
  • OS: Windows 10 Pro

NAS:

  • Synology DS216J
  • 2x8TB WD Red NAS HDDs in RAID 1. 8TB usable space
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×