Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make partially off screen div which will not add horizontal scroll to the screen

Tags:

html

css

overflow

/* body{
    overflow-x: hidden;
} */
.divOne{
    /* overflow-y: hidden; */
    position: relative;
    background-color: aqua;
    height: 100px;
    width: 100%;
}
.divTwo{
    background-color: red;
    height: 300px;
    width: 300px;
    position: absolute;
    top: -30px;
    right: -80px;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="./index.css" />

    <title>learning about canvas</title>
  </head>
  <body>
    <div class="container-div">
      <div class="divOne">div1</div>
      <div class="divTwo">div2</div>
    </div>
  </body>
</html>

I want to make it so that the horizontal scroll bar wouldn't appear below. I want to make the overflowing 2 div overflow: hidden . but overflow : hidden only works when its applied to body and that causes a lot of problems.

like image 622
VINIT CHURI Avatar asked Dec 02 '25 08:12

VINIT CHURI


1 Answers

You need to add absolute positioning rules for the container-div class, relative positioning for the body tag, with the overflow-x: hidden rule.

Now your block .divTwo is hidden in the viewing area, I have a width of 300x300.

body {
    position: relative;
    overflow-x: hidden;
}

.container-div {
    position: absolute;
    left: 0;
    right: 0;
}

.divOne{
    /* overflow-y: hidden; */
    background-color: aqua;
    height: 100px;
    width: 100%;
}
.divTwo{
    background-color: red;
    height: 300px;
    width: 300px;
    position: absolute;
    top: -30px;
    right: -80px;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="./index.css" />

    <title>learning about canvas</title>
  </head>
  <body>
    <div class="container-div">
      <div class="divOne">div1</div>
      <div class="divTwo">div2</div>
    </div>
  </body>
</html>
like image 96
s.kuznetsov Avatar answered Dec 04 '25 00:12

s.kuznetsov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!