Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overflow-x not working

Tags:

I am trying to implement a slider for my gallery. Right now,the css has an issue where the overflow-x doesnt work properly. (I want a horizontal slider and no vertical scroll)

Here's the fiddle:

Please do take a look.

.testimg{     height: 100%;     width: 100%; } #testDiv{     width: 400px;     overflow: auto;     float: left;     overflow-y:hidden;     border:1px solid black;  } .testimgdiv{     width: 120px;     height: 100px;     float: left; } 
like image 692
Nevin Madhukar K Avatar asked Jun 10 '14 04:06

Nevin Madhukar K


People also ask

Why does overflow hidden not work?

It is because you are using position absolute. You cannot use position absolute with overflow hidden, because position absolute moves the targeted element out of context with the document structure.

How do I fix the overflow in CSS?

To change this, set the min-width or min-height property.” This means that a flex item with a long word won't shrink below its minimum content size. To fix this, we can either use an overflow value other than visible , or we can set min-width: 0 on the flex item.

What does overflow-X do in CSS?

The overflow-x property specifies whether to clip the content, add a scroll bar, or display overflow content of a block-level element, when it overflows at the left and right edges.


1 Answers

Once you've floated the elements, you've taken them out the document flow and it won't be calculated in the parent's width. You have to use a combination of display: inline-block on the items instead of float, and then use white-space: nowrap on the parent.

#testDiv{   width: 400px;   overflow-x: auto;   border:1px solid black;   white-space: nowrap;    font-size: 0; } .testimgdiv{   width: 120px;   height: 100px;   display: inline-block; } 

Fiddle

Note: I'm using font-size: 0 to make the items appear right next to each other.

UPDATE

As this post is quite old, it's probably worth noting that this can be done with less code using flexbox (depending on the browser support level required):

#testDiv {   width: 400px;   display: flex;   overflow-x: auto; }  .testimgdiv {   width: 120px;   height: 100px;   flex: 0 0 auto; }
<div id="testDiv">   <div class='testimgdiv'>     <img class="testimg" src="https://picsum.photos/100/100/" />   </div>   <div class='testimgdiv'>     <img class="testimg" src="https://picsum.photos/100/100/" />   </div>   <div class='testimgdiv'>     <img class="testimg" src="https://picsum.photos/100/100/" />   </div>   <div class='testimgdiv'>     <img class="testimg" src="https://picsum.photos/100/100/" />   </div>   <div class='testimgdiv'>     <img class="testimg" src="https://picsum.photos/100/100/" />   </div>   <div class='testimgdiv'>     <img class="testimg" src="https://picsum.photos/100/100/" />   </div> </div>
like image 189
davidpauljunior Avatar answered Sep 19 '22 18:09

davidpauljunior