CSS hide scroll bar if not needed

HtmlCss

Html Problem Overview


I am trying to figure out how I can hide the overflow-y:scroll; if not needed. What I mean is that I am building a website and I have a main area which posts will be displayed and I want to hide the scroll bar if content does not exceed the current width.

Also, my second question. I want to make it so when the posts exceed the current width, the width will increase automatically and the content won't go out of the box.

Does anyone have a clue how to do this?

Posts area:

.content {
	height: 600px;
	border-left: 1px solid;
	border-right: 1px solid;
	border-bottom: 1px solid;
	font-size: 15px;
	text-align: justify;
	line-height: 19px;
	overflow-y:scroll;
}

Main website container:

.container {
	margin: 0 auto;
	width: 757px;
	margin-top: 30px;
	text-align: center;
}

Html Solutions


Solution 1 - Html

Set overflow-y property to auto, or remove the property altogether if it is not inherited.

Solution 2 - Html

You can use overflow:auto;

You can also control the x or y axis individually with the overflow-x and overflow-y properties.

Example:

.content {overflow:auto;}
.content {overflow-y:auto;}
.content {overflow-x:auto;}

Solution 3 - Html

You can use both .content and .container to overflow:auto. Means if it's text is exceed automatically scroll will come x-axis and y-axis. (no need to give separete x-axis and y-axis commonly give overflow:auto)

> .content {overflow:auto;}

Solution 4 - Html

.selected-elementClass{
    overflow-y:auto;
}

Solution 5 - Html

overflow:auto; used to display auto scrollbar

Solution 6 - Html

.container {overflow:auto;} will do the trick. If you want to control specific direction, you should set auto for that specific axis. A.E.

.container {overflow-y:auto;} .container {overflow-x:hidden;}

The above code will hide any overflow in the x-axis and generate a scroll-bar when needed on the y-axis.But you have to make sure that you content default height smaller than the container height; if not, the scroll-bar will not be hidden.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThanos ParavantisView Question on Stackoverflow
Solution 1 - HtmlRJoView Answer on Stackoverflow
Solution 2 - HtmlAyyappan KView Answer on Stackoverflow
Solution 3 - HtmlAyyappan KView Answer on Stackoverflow
Solution 4 - HtmlPrateek AroraView Answer on Stackoverflow
Solution 5 - HtmlToqeer YousafView Answer on Stackoverflow
Solution 6 - HtmlNeeroView Answer on Stackoverflow