0

I want to have 2 elements in a div but only one should be visible, and I want to have a vertical scrollbar. Unfortunately, the second element is visible and there is no scrollbar.

   #visu {
        top:10px;
        height:180px;
        width:50%;
        overflow:auto;
        background-color:yellow;
    }
    #element1 {
         position:absolute;
         top:15px;
         left:80px;
     }
   
     #element2 {
          position:absolute;
          top:200px;
          left:80px;
     }
  
      .element {
          margin-right:-50px; 
       }
             
        .namecontainer {
           display:flex;
            border:4px solid #000033; border-radius:10px;
            padding:10px; box-sizing:border-box;
            background-color:white;
            width:280px;
            height:150px;
            margin-left:0px;
            align-items: center;
            justify-content:center:
            color:#1a1a1a;
        }

         .namecontainer p {
              font-size:35px;
              font-family: 'Arial';
              font-weight:bold;
              color:#1a1a1a;  
              text-align:center;
               width:380px;
           }
                 
              
 <div id="visu">
            <div id="element1" class="element">
                <div class="namecontainer">
                    <p class= "name" id="name1" >element 1</p> 
                </div>   
            </div> 
           
            <div id="element2" class="element">
                <div class="namecontainer">
                    <p class= "name" id="name3" >element 2</p> 
                </div> 
            </div>
             
    </div>

1
  • but do you want the second element to be visible on scroll? Commented Jun 15, 2016 at 15:22

2 Answers 2

1

You need to play with margin and drop absolute positionnong cause it takes element off the flow and is not necessary here https://jsfiddle.net/vpdc5L4m/13/

   #visu {
     top: 10px;
     height: 180px;
     width: 50%;
     overflow: auto;
     background-color: yellow;
   }
   
   #element1 {}
   
   #element2 {}
   
   .element {
     margin: 15px auto ;
   }
   
   .namecontainer {
     display: flex;
     border: 4px solid #000033;
     border-radius: 10px;
     padding: 10px;
     box-sizing: border-box;
     background-color: white;
     width: 280px;
     height: 150px;
     margin:auto;
     align-items: center;
     justify-content: center: color: #1a1a1a;
   }
   
   .namecontainer p {
     font-size: 35px;
     font-family: 'Arial';
     font-weight: bold;
     color: #1a1a1a;
     text-align: center;
     width: 380px;
   }
<div id="visu">
  <div id="element1" class="element">
    <div class="namecontainer">
      <p class="name" id="name1">element 1</p>
    </div>
  </div>

  <div id="element2" class="element">
    <div class="namecontainer">
      <p class="name" id="name2">element 2</p>
    </div>
  </div>

  <div id="element3" class="element">
    <div class="namecontainer">
      <p class="name" id="name3">a third one ?</p>
    </div>
  </div>

</div>

Sign up to request clarification or add additional context in comments.

Comments

0

To hide a CSS element, you can use

display: none;

1 Comment

I want the second element to be visible by using the scrollbar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.