Combine overflow-y: scroll with overflow-x: visible - html

I have a div, that has flyout divs positioned absolute to the right. Its all working correctly but I need to add a max height to my main div. Since it will overflow, I add overflow-y scroll, but it messes everything up. It prevents my flyout divs from going outside of the main div, and places them inside with a horizontal scrollbar.
I've mocked up an example:
.menu {
height: 200px;
max-height: 200px;
width: 200px;
background-color: red;
margin-bottom: 50px;
}
.menu.overflow {
overflow-y: scroll;
background-color: purple;
height: 150px;
max-height: 150px;
}
.menu-item {
height: 30px;
width: 100%;
background-color: blue;
position: relative;
margin-bottom: 10px;
cursor: pointer;
}
.menu-item-flyout {
display: none;
width: 200px;
height: 100px;
position: absolute;
left: 100%;
background-color: green;
top: 0;
}
.menu-item:hover .menu-item-flyout {
display: block;
}
<!-- no overflow y scroll -->
working correctly:
<div class="menu">
<div class="menu-item">
<div class="menu-item-flyout"></div>
</div>
<div class="menu-item">
<div class="menu-item-flyout"></div>
</div>
<div class="menu-item">
<div class="menu-item-flyout"></div>
</div>
</div>
<!-- overflow y scroll -->
This is not working. I want to add overflow y
scroll to menu but it prevents the flyout
<div class="menu overflow">
<div class="menu-item">
<div class="menu-item-flyout"></div>
</div>
<div class="menu-item">
<div class="menu-item-flyout"></div>
</div>
<div class="menu-item">
<div class="menu-item-flyout"></div>
</div>
</div>

Unfortunately you can't mix overflow values. Per MDN on overflow-x:
If overflow-y is hidden, scroll or auto and this property is visible
(default) it will implicitly compute to auto.
auto, in this case, forces a scroll bar. There is no way to get around this: you cannot have a box overflow in only one direction.
As a general principal, menus based on hover are difficult to navigate as they are, but are treacherous from an accessibility standpoint. Adding scroll bars to the mix is a recipe for disaster. I'd recommend approaching the problem from another angle.

Related

CSS: Sidebar won't appear inside parent element

I'm trying to add two sidebars to both edges of the middle element. The left one works without an issue, however, the right one won't. Instead, it appears below its parent element (as seen in the picture) unless I position it as absolute, then however, it goes over the navbar.
Relevant css:
/* The parent element */
main {
margin: 0;
position: relative;
left: 22%;
right: 22%;
width: 56%;
height: 50vh;
background-color: #c5c5c5;
}
/* The correctly shown sidebar */
.sidenav {
margin: 0;
height: 100%;
width: 160px;
top: 7%;
left: 0;
background-color: #111;
overflow-x: hidden;
padding-top: 7%;
}
/* The wrongly shown sidebar */
.bar-right{
float: right;
margin: 0;
width: 200px;
height: 100%;
background-color: #111;
overflow-x: hidden;
padding-top: 7%;
}
HTML:
<main>
<div class="topbar">
[top bar stuff]
</div>
<div class="sidenav">
[usernamestuff]
Link1
Link2
Link3
</div>
<div class="bar-right">
<p>text for test</p>
</div>
</main>
Both sidebars are effectively identical so I don't understand why they behave so differently. How do I get them both to their appropriate edges of the main element?
I would recommend you to use Bootstrap. This will help you to achieve what you looking for so easy, by this code:
<div class="container">
<div class="row">
<div class="col-sm-3">
// First sidebar
</div>
<div class="col-offset-6 col-sm-3">
// Second sidebar
</div>
</div>
</div>

Set width of element to width of sibling

I have a slightly unusual CSS challenge to overcome.
I have a two column layout, whereby the width of the left column is set by the width of a main image, and the right allowed to fill the remaining space. There is a container under the main image, which could have a natural width greater than the main image. However, I want this div to be the same width as the main image, and the overflow to be hidden. Here is my effort at attempting this:
.outer {
margin-right: 5px;
position: relative;
}
.left {
float: left;
}
.right {
width: auto;
overflow: hidden;
}
.contentOuter {
overflow: hidden;
}
.content {
width: 500px;
}
.inner {
background-color: grey;
color: white;
width: 100%;
height: 150px;
}
<div class="outer left">
<div class="image">
<img src="http://placehold.it/350x150" />
</div>
<div class="contentOuter">
<div class="content">
<img src="http://placehold.it/500x50" />
</div>
</div>
</div>
<div class="outer right">
<div class="inner">
Hello world!
</div>
</div>
But as you can see, .contentOuter stretches to the width of its contents, regardless of what I attempt.
One major caveat I have is that apart from .content having a fixed width, I don't want any other hard-coded widths in my CSS; everything should be completely fluid, and the dimensions of the columns determined by the dimensions of the .image img.
So, I am after something that visually looks like this, but without the hard-coded max-width on .content:
.outer {
margin-right: 5px;
position: relative;
}
.left {
float: left;
}
.right {
width: auto;
overflow: hidden;
}
.contentOuter {
overflow: hidden;
}
.content {
max-width: 350px; /* Hard-coded for demo purposes */
}
.inner {
background-color: grey;
color: white;
width: 100%;
height: 150px;
}
<div class="outer left">
<div class="image">
<img src="http://placehold.it/350x150" />
</div>
<div class="contentOuter">
<div class="content">
<img src="http://placehold.it/500x50" />
</div>
</div>
</div>
<div class="outer right">
<div class="inner">
Hello world!
</div>
</div>
One option, though that depends on further requirements you may have, it so simply add to the lower block:
position: absolute;
width: 100%;
This takes it out of the flow, and the enclosing element will not take its width into account for sizing, only that of the image on top. The overflow: hidden will then hide whatever overflows.
The drawback is that the height of the enclosing element (or the position or subsequent elements) will not take into account the size of this element.
jsFiddle: https://jsfiddle.net/jacquesc/rsz0hb1g/
A quick way to solve this would be to simply use some jQuery. It would only take two lines of code to achieve this.
var imgWidth = $('.image').width();
$('.content').width(imgWidth);

How to remove resizing content when overflow when is hover?

I have problem with overflow in on my website. When mouse is not over the left menu, it look OK, but when I move mouse over the menu and :hover is active, content (menu is a content) is a little smaller. I don't want this effect when :hover is active, menu is a little smaller.
Effect that I want is like on Youtube. If you are logged into your account, and if you pressed menu icon next to Youtube logo, you will get scroll menu. When mouse is not over the menu, nothing happens. But when you move your mouse over the menu, overflow is visible and this not change content size.
In brief:
I have this:
when overflow is active, content is smaller
What I want is this:
when overflow is active, content is NOT smaller
How can I do it ?
Thanks :)
My understanding, though it maybe flawed is that you have to already have the space that the scroll bar will populate there. You didn't give us any code so I had to fabricate it, but this is what I came up with
https://jsfiddle.net/link2twenty/bxz60j4r/
HTML:
<div class="sidebar">
<div class="container">
<div class="profile">
<img src="http://moss-side.yoursquaremile.co.uk/img/default-profile-pic.png" />
</div>
<div class="card">
<img src="https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-person-128.png" />
<span>Test card data</span>
</div>
<div class="spacer">
<hr>
</div>
<div class="card">
<img src="https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-person-128.png" />
<span>Test card data</span>
</div>
<hr>
<div class="card">
<img src="https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-person-128.png" />
<span>Test card data</span>
</div>
<hr>
<div class="card">
<img src="https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-person-128.png" />
<span>Test card data</span>
</div>
<hr>
<div class="card">
<img src="https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-person-128.png" />
<span>Test card data</span>
</div>
</div>
</div>
CSS:
.sidebar {
position: absolute;
left: 0;
top: 0;
bottom: 0;
width: 400px;
overflow: hidden;
padding-right: 17px;
}
.sidebar:hover {
padding-right: 20px;
overflow-y: auto;
}
.sidebar .container {
width: 400px;
background: #8cc34b;
margin-bottom: -25px;
}
.card {
opacity: .7;
width: 100%;
height: 120px;
background: #8cc34b;
}
.card span {
position: relative;
float: right;
padding-right: 50px;
top: 50%;
}
hr {
margin-left: 120px;
margin-right: 20px;
border-color: darkgreen;
opacity: .7;
}
You can try the following:
.scrollable {
border: 1px solid black;
width: 100px; /* specify the width you like here */
overfow: hidden;
}
.scrollable:hover {
overfow-y: scroll;
}
nav {
border: 1px solid grey;
width: 80px; /* specify the width which is calculated as widthOfScrollable - widthOfScrollbar */
}
.item1, .item2 {
height: 10px;
width: 100%;
}
.item1 { background: red }
.item2 { background: blue }
<div class="scrollable">
<nav>
<li class="item1"></li>
<li class="item2"></li>
</nav>
</div>
Borders and colors are used to show the blocks.
Here you preserve some space for the scrollbar on the right side. Problem here is to preserve exactly the scrollbar size for the current user / browser / OS. They all could have differences in scrollbar width value, so there are two ways for you to go:
Use this function from here Getting scroll bar width using JavaScript to calculate the proper size of the scrollbar
Make the scrollbar margin big enough to cover any case on any operating system

Div not expanding by content

My wrapper div is not expanding in height by it's content. On the other hand, it expands by the header div on the page, and the nav div, but not by the sidebar which lies inside another div. Here is the HTML:
<div id="wrapper">
<div id="header">
</div>
<div id="nav">
nav content goes here
</div>
<div id="pagecontent">
<div id="sidebar">
some sidebar stuff like login form
</div>
</div>
</div>
and the CSS (only the necessary css, not like webkit box shadows):
#wrapper {
width: 900px;
margin: 0 auto;
padding-bottom: 80px;
}
#header {
height: 160px;
width: 100%;
}
#nav {
width: 100%;
height: 40px;
}
#sidebar {
float: right;
width: 140px;
height: 100%;
}
#pagecontent {
width: 900px;
}
the sidebar, which has height 100%, goes outside the wrapper div, you could look at it on http://craftersinn.net
You've missed to add css rule overflow:hidden in #wrapper css.
Add overflow: hidden; to your wrapped CSS, also make sure your selectors are actually selecting elements:
<div id="pagecontent">// remove page
#content {
http://jsfiddle.net/Kyle_Sevenoaks/bPT49/
Add <div style="clear: both;"></div> after
<div id="sidebar">
some sidebar stuff like login form
</div>
You should read this article on floats, especially the section "Clearing the Float".
In short, you can use clear: both, overflow: hidden or the clearfix method. Good luck!
Here discussed the same problem. Check for clearing floats solution and explanation about overflow: hidden;
css box model does not stretch using padding

CSS Using div to simulate tables

I'm new as webdesigner and I have to create a portion of a page that has 3 columns: a menu on the left side, the central body and a vertical banner. I can't use tables, so I've created a similar HTML:
<div class="Body">
<div class="LeftMenu">My menu</div>
<div class="Content">Foo body</div>
<div class="VerticalBanner">My menu</div>
</div>
While the CSS:
.LeftMenu {
width: 20%;
}
.Content {
margin: auto;
left: 20%;
position: absolute;
top: 0px;
width: 60%;
}
.VerticalBanner {
left: 80%;
margin: auto;
position: absolute;
top: 0%;
width: 20%;
}
So, my problem using that code is that the parent div (Body) takes the height of the first div (LeftMenu), which is not the bigger. This causes the content of "Content" and "VerticalBanner" to flow out "Body" and to go under the Footer div. If I use the float attribute, the "Body" div collapse without dimensions and then the footer div slides under the three columns inside "Body".
I also tried with display attribute, but Internet Explorer doesn't support this and some columns have strange behaviour.
What is the correct way to do this?
I think you should use floats for your DIVs. It's much easier after that to move them around.
Use display: table-*:
.Body { display: table; }
.Left, .Content, .VerticalBanner { display: table-cell; }
See e.g. this JSfiddle.
To stop the body div from collapsing you can use
.body{ overflow: hidden; }
I'm don't think you need position absolute.
<div class="Body">
<div style="width:20%;float:left;">My menu</div>
<div style="width:60%;float:left;">Foo body</div>
<div style="width:20%;float:left;">My menu</div>
<div style="height:1px;font-size:1px;clear:both;"> </div>
</div>