Hide vertical scrollbar, keep horizontal and still able to scroll - html

I'm trying to write HTML/CSS in a way that I have a div which can be scrolled both vertically and horizontally, but ONLY the horizontal scrollbar is visible. So the vertical scrolling is done using mouse wheel and horizontal using the scrollbar at the bottom (and other, usual controles like keyboard etc.). The usually suggested solution with -webkit-scrollbar display none doesn't apply here because it can only hide both scrollbars, hence the new question.
So, basically, I need behaviour like this, except the horizontal scrollbar should still be there:
.content {
width:400px;
height:200px;
}
.container {
overflow-x: auto;
overflow-y: auto;
height: 100px;
width: 200px;
}
.container::-webkit-scrollbar {
display: none;
}
<div class="container">
<div class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
</div>

You can use -webkit-scrollbar, but you need to use width and height instead of display: none
div {
width: 100px;
height: 100px;
font-size: 48px;
overflow: auto;
}
::-webkit-scrollbar {
height: 0px;
width: 8px;
border: 1px solid #fff;
}
::-webkit-scrollbar-track {
border-radius: 0;
background: #eeeeee;
}
::-webkit-scrollbar-thumb {
border-radius: 0;
background: #b0b0b0;
}
<div>
Lorem ipsum dolor sit amet.
</div>
Shift scroll to move horizontally
width will just affect the vertical scroll while height will just affect the horizontal scroll. If you set height: 0px it will hide the horizontal scroll bar, but keep the vertical one.

Related

css to center a popup on the screen, works on computer and tablet, but not phone

Trying to display a help popup in the center of the screen, using css. On my computer and ipad, it works fine, but on iPhone the popup is vertically centered within the entire scrollable vertical space, which means I sometimes have to scroll up or down to see it. I want it centered within the currently visible screen at the time it is invoked. (Note the page height is not static, i.e. grows and shrinks based on user activity).
Here is the relvant css:
.helpcontent {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
transition: 200ms ease-in-out;
border: 1px solid black;
border-radius: 10px;
z-index: 10;
background-color: khaki;
width: 500px;
max-width: 80%;
padding: 10px 15px;
}
<div class="helpcontent">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
You have several problems.
You must define a height, otherwise it will grow with the content, and it may happen that you have more content than space available vertically. You will have to use overflow: hidden to hide the overflow.
You are not really centering the window. If you want to do it I recommend you to change the focus. Make an element occupy all the available window, give it a display: flex and center it with align-items and justify-content.
An example.
<section class="modal">
<div class="modal__content">
<!-- Div that will be centered on the screen and will contain all your content. -->
</div>
</section>
.modal {
display: flex;
justify-content: center;
align-items: center;
position: fixed;
inset: 0;
}
.modal__content {
width: 10rem;
height: 10rem;
border: 1px solid black;
}
You already have an interactive tag that is designed for the task: <dialog>.

Make image take up the full height of a div that changes size?

I will try to make this sound as easy as possible.
I am trying to place 2 div containers, side by side, and have them be the same height at all times.
The right div will be regular text. The amount of text in here will vary since I plan on using this for different pages.
The left div will be composed of 2 smaller containers - a title block, and an image block beneath it.
Here is a visual example of what I'm trying to achieve. The green box is supposed to be the full photo
Example Photo
I would like the photo in the image block of the left side to take up the full height/width of the box - (similar to background-position: cover that is used in CSS). I'd prefer to use a regular img tag instead of setting it as a div background.
The issue that I am having is that the image height on the left takes priority over the text box on the right hand side, and causes both containers to appear much longer than I want. I want the text block on the right to be prioritized, and the image block changes height based on that.
I've tried using object-fit: contain, but it isn't working, unfortunately. The closest I've gotten is to use width: 100%, but then it makes the height way too big.
Here's what I have so far:
.main {
display: flex;
}
.main .left {
width: 40%;
float: left;
}
.main .left .title {
background-color: black;
text-align: center;
display: block;
height: 90px;
padding: 50px;
color: white;
font-size: 40px;
}
.photo {
height: auto;
width: 100%;
}
.photo img {
width: 100%;
}
.main .right {
width: 60%;
float: right;
}
<div class="main">
<div class="left">
<div class="title">This is my Title</div>
<div class="photo"><img src="https://image.shutterstock.com/z/stock-photo-pastoral-green-field-with-long-shadows-in-tuscany-italy-275372477.jpg"></div>
</div>
<div class="right">
<p>text goes here lalalalalala</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</div>
<style>
.main {
display: flex;
}
.main .left {
width: 40%;
display: flex;
flex-direction: column;
}
.main .left .title {
background-color: black;
text-align: center;
display: block;
height: 90px;
padding: 50px;
color: white;
font-size: 40px;
}
.photo {
width: 100%;
overflow: hidden;
position: relative;
flex-grow: 1;
}
.photo img {
width: 100%;
position: absolute;
}
.main .right {
width: 60%;
}
</style>
Notes:
I made the image absolutely positioned so its own height won't stretch our flex row.
The image is being cropped by height. If the title is taller than the text (or the same height), you won't see the image at all.
I made the left column also display flex and the photo box flex grow so that the title can stay the same height and the photo box will stretch the rest of the way to match the right column.
We don't need float left/right for flex items.

Is there a way to move custom HTML scrollbar behind a div

I have some code snippets on my blog post here.
There I have custom scrollbar defined with this code below:
/* width */
::-webkit-scrollbar {
width: 20px;
}
/* Track */
::-webkit-scrollbar-track {
box-shadow: inset 0 0 5px grey;
border-radius: 10px;
}
/* Handle */
::-webkit-scrollbar-thumb {
background: #0ef;
border-radius: 10px;
}
The place where I have included the snippet is a custom div element with border radius of 2em. Now the custom scrollbar looks some bulged out as you can see in the image below:
How can I send the scrollbar behind the <div> so that the extra scrollbar may be removed?
P.S.: The border radius of the <div> should be maintained.
You can put a small div inside your outer div and make that the container of your scroll bar to stop the bulging out of these scroll bars eg. fiddle like
<div class="outer">
<div class="inner">
..content
</div>
</div>
.outer{
max-height:200px;
width:250px;
border-radius:10px;
padding:0px 0px;
background:#ccc;
}
.inner{
white-space: nowrap;
width:250px;
max-height:200px;
overflow-x:scroll;
overflow-y:hidden;
border-radius:8px;
}
/* width */
::-webkit-scrollbar {
width: 20px;
}
/* Track */
::-webkit-scrollbar-track {
box-shadow: inset 0 0 5px grey;
border-radius: 10px;
}
/* Handle */
::-webkit-scrollbar-thumb {
background: #0ef;
border-radius: 10px;
}
<div class="outer">
<div class="inner">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum
</div>
</div>

Div with height % not collapsing into itself with overflow auto

Alright so this is the third time I've looked for help on a particular element I'm working on in an app so I apologize for the repetition.
I'm building a questionnaire for an application and I want all the questions to be contained in a div and the overflow hidden. The problem I'm currently running into is if the height doesn't exceed 80% of the window size the containing div doesn't collapse into itself and there is an unnecessary amount of whitespace left over.
I have tried to switch the questionnaire-box height style to max-height but this caused the overflow content of the .questions div to be hidden instead of scrollable.
I have a strong suspicion I may need to handle this with js but I prefer straight CSS and HTML if possible.
Anyone have any suggestions?
html, body {
height: 100%;
}
.questionnaire-container {
display: flex;
position: fixed;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 255, .1);
}
.questionnaire-box {
position: relative;
width: 80%;
height: 70%;
margin: 0 auto;
background-color: #ffffff;
overflow: hidden;
}
.questions {
height: 100%;
padding: 1rem 2rem;
overflow-y: auto;
}
<div class="questionnaire-container">
<div class="questionnaire-box">
<div class="questions">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</div>
</div>
</div>
You have to move the padding from .questions to .questionnaire-box. Also, .questions has no (...-)width attributes set anymore, but the .questionnaire-box's max-width is calc(70vh - 2rem).
Fiddle: https://jsfiddle.net/moor131j/2/

How to wrap text around image in HTML/CSS like MS Word does it when choosing "Top and Bottom"

I'm trying to implement the following text wrapping in HTML. I want the image to always appear below the first line of text, whatever the width of the page is.
Illustration:
And when the page is resized to be a bit narrower, it should look like this:
Basically the same way Word does it when you choose "Top and Bottom" in the image text wrapping options.
I'm pretty sure there's no built-in CSS feature for this layout. I think it could be implemented with text-measurements in JS - but it doesn't seem like a very elegant solution.
Any ideas?
Edit
The solution should also work for placing the at the n-th line, not just the first line.
This seems to work - FIDDLE.
CSS
.bigdiv {
border: 1px solid black;
width: 80%;
}
.picturediv {
width: 100%;
float: right;
}
.picturediv img {
width: 100%;
}
I've adapted TimSPQR's answer to allow placing the image on any line.
This is achieved by adding a narrow div that floats above the image, the line-height of which controls how many lines of text appear before the image.
HTML:
<div class='bigdiv'>
Lorem
<div class="clear">
</div>
<div class='picturediv'>
<img src='http://www.hdpaperwall.com/wp-content/uploads/2013/11/Mickey-Mouse-Wallpaper-disney-6628369-1024-768.jpg'/>
</div>
ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
CSS:
.bigdiv {
border: 1px solid black;
width: 80%;
line-height: 1em;
}
.picturediv {
width: 100%;
float: right;
}
.picturediv img {
width: 100%;
}
.clear
{
float: right;
line-height: 2em;
}
Fiddle