How to make an item background visible on overflow? - html

I have a small dropdown selection inside overflow box. Problem is I cannot make links background fill full width. Just try to scroll:
.outer {
width: 100px;
height: 100px;
overflow: auto;
border: 1px solid black;
}
.inner a {
background: none #ccc;
display: inline-block;
white-space: nowrap;
}
.inner.block a {
display: block;
}
<p>inline-block:</p>
<div class="outer">
<div class="inner">Some link title in here.</div>
<div class="inner">Some much longer link title in here.</div>
</div>
<p>block:</p>
<div class="outer">
<div class="inner block">Some link title in here.</div>
<div class="inner block">Some much longer link title in here.</div>
</div>
Also on Playground.
I can modify CSS and HTML any way, the container has position: absolute. Also, the background is an image repeating, not a solid color.
How to make the background behind the text to extend to the edge?

Just wrap the links in another div. If you need background images per link, attach them to inner div. If you need the whole bar clickable, flip the <a>'s and the <div>'s. Like this:
<div class="outer">
<div class="background">
<div class="inner">Some link title in here.</div>
<div class="inner">Some much longer link title in here.</div>
</div>
</div>
.background {
display: inline-block;
white-space: nowrap;
}
.background .inner {
background: none #ccc;
}
JsFiddle
Hope that takes everything you needed into account.

In my example, links are clickabe in full width, and you can add repeateable background easly.
<div class="outer">
<div class="background">
<div class="inner">Some link title in here.</div>
<div class="inner">Some much longer link title in here.</div>
</div>
</div>
-
.outer {
width: 100px;
height: 100px;
overflow: auto;
border: 1px solid black;
}
.outer a{
display: block;
background: url(http://nonessentials.org/wp-content/uploads/2014/01/dots-small-pattern.png);
background-size:cover;
}
.background {
background: none #ccc;
display: inline-block;
white-space: nowrap;
}
JsFiddle

I hope this could be the solution:
<p>inline-block:</p>
<div class="outer">
<div class="table">
<div class="inner">Some link title in here.</div>
<div class="inner">Some much longer link title in here.</div>
</div>
</div>
.outer {
width: 100px;
height: 100px;
overflow: auto;
border: 1px solid black;
}
.table {
display:table;
}
.inner {
display:table-row;
}
.inner a {
background: none #ccc;
display: block;
white-space: nowrap;
}
.inner a:hover {
background-color:orange;
}
Demo: http://jsfiddle.net/jscohdL0/27/
So, wrapper div again, but with display:table property, and inner divs as table-rows... Clickable, hoverable™...

Without using any wrapper:
.inner {
width: 300px;
}
.inner a {
background: none #ccc;
display: inline-block;
white-space: nowrap;
width:100%; /*addition*/
}
With wrapper:
.outer {
width: 100px;
height: 100px;
overflow: auto;
border: 1px solid black;
}
.cover {
background: none #ccc;
display: inline-block;
white-space: nowrap;
}
.inner a {
width: 100%;
display: inline-block;
}
.inner.block a {
display: block;
}
<p>inline-block:</p>
<div class="outer">
<div class="cover">
<div class="inner">Some link title in here.
</div>
<div class="inner">Some much longer link title in here.
</div>
</div>
</div>
<p>block:</p>
<div class="outer">
<div class="cover">
<div class="inner block">Some link title in here.
</div>
<div class="inner block">Some much longer link title in here.
</div>
</div>
</div>

Related

CSS prevent DIV from wrapping when using text-overflow: ellipsis

I'm trying to fit some text and divs all into a single line (without wrapping) and using text-overflow: ellipsis. However in all my experimenting (I can't even recall all the things I've tried), the text fills up the entire line, and the divs get pushed down onto a new line.
I'd like the text to truncate so the blue boxes are on the same line as the text.
I'm able to get it to work with JS, but I want a pure CSS solution.
EDIT:
Sorry, I should have added some more details.
The text length is variable
The solution should allow for a responsive page design (I put the width: 400px to constrain the container, but in reality it's responsive, sorry I know my question was misleading.)
.page-container{
width: 400px;
background: yellow;
}
div.header {
white-space: nowrap;
display: inline;
width: 100%;
}
div.one-line-div {
font-size: larger;
white-space: nowrap;
width: 100%;
text-overflow: ellipsis;
display: inline-block;
overflow: hidden;
}
.move-divs {
display: inline-block;
float: right;
}
.div1, .div2, .div3, .div4 {
float: right;
margin-right: 12px;
cursor: pointer;
display: inline-block;
width: 30px;
height: 30px;
background: blue;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<body>
<div class="page">
<div class="page-container">
<div class="header">
<div class="one-line-div">Text text, so much text here, what do we do with all this text?.
<div class="more-divs">
<div class="div1">
</div>
<div class="div2">
</div>
<div class="div3">
</div>
<div class="div4">
</div>
</div>
</div>
</div>
</div>
</div>
</body>
Have you tried flex-box? Based on what I've tested it should work for you. You'll need to wrap your text in another div, though. And also need to change some things from inline-block back to block, etc. Basically flex-box is the new layout engine that allows you to do some awesome stuff. Generally you shouldn't ever need float if you use flex-box. Check out this guide on flex-box from CSS-Tricks. You can do some amazing things with it. Let me know if you have any questions regarding my answer. I didn't want to go into too much specifics because that'd be a pretty big rabbit hole.
.page-container{
width: 400px;
background: yellow;
}
/*
You don't need this anymore with flex.
div.header {
white-space: nowrap;
display: inline;
width: 100%;
}*/
/* Updated to use flex box. */
div.one-line-div {
display: flex;
flex-direction: row;
font-size: larger;
}
/* define the style for our .text element */
.text {
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
}
/* our .move-divs needs to be flex too */
.more-divs {
display: flex;
flex-direction: row;
}
/* I removed the floats and display inline, since you don't need them */
.div1, .div2, .div3, .div4 {
margin-right: 12px;
cursor: pointer;
width: 30px;
height: 30px;
background: blue;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<body>
<div class="page">
<div class="page-container">
<div class="header">
<div class="one-line-div">
<div class="text">
Text text, so much text here, what do we do with all this text?.
</div>
<div class="more-divs">
<div class="div1">
</div>
<div class="div2">
</div>
<div class="div3">
</div>
<div class="div4">
</div>
</div>
</div>
</div>
</div>
</div>
</body>
The solution: put the text in a "span" element . then do the following styles
.page-container{
width: 400px;
background: yellow;
}
div.header {
white-space: nowrap;
display: inline;
width: 100%;
}
div.one-line-div {
font-size: larger;
white-space: nowrap;
width: 100%;
text-overflow: ellipsis;
display: inline-block;
overflow: hidden;
}
.move-divs {
display: inline-block;
float: right;
}
.div1, .div2, .div3, .div4 {
float: right;
margin-right: 12px;
cursor: pointer;
display: inline-block;
width: 30px;
height: 30px;
background: blue;
}
.myText {
max-width: 55%;
text-overflow: ellipsis;
overflow: hidden;
display: inline-block;
}
.more-divs {
display: inline-block
}
<div class="page">
<div class="page-container">
<div class="header">
<div class="one-line-div">
<span class="myText">Text text, so much text here, what do we do with all this
text?.</span>
<div class="more-divs"">
<div class="div1">
</div>
<div class="div2">
</div>
<div class="div3">
</div>
<div class="div4">
</div>
</div>
</div>
</div>
</div>
It is easy and best using flex or grid , though here using float as you said.
When using float this display:inline-block is not needed because float it self makes elements display inline
.page{
background: yellow;
}
.page-container{
width: 400px;
}
div.header {
white-space: nowrap;
width: 100%;
}
div.one-line-div {
font-size: larger;
white-space: nowrap;
width: 100%;
text-overflow: ellipsis;
overflow: hidden;
float: left;
}
.move-divs {
float: right;
}
.div1, .div2, .div3, .div4 {
margin-right: 12px;
cursor: pointer;
display: inline-block;
width: 30px;
height: 30px;
background: blue;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<body>
<div class="page">
<div class="page-container">
<div class="header">
<div class="one-line-div">Text text, so much text here, what do we do with all this text?.</div>
<div class="more-divs">
<div class="div1">
</div>
<div class="div2">
</div>
<div class="div3">
</div>
<div class="div4">
</div>
</div>
</div>
</div>
</div>
</div>
</body>

Overflow area of div has no style [duplicate]

This question already has answers here:
Make background color extend into overflow area
(4 answers)
Closed 4 years ago.
I'm trying to create a table like structure that is scrollable horizontally. To do that I have a wrapper div that has overflow-x: auto, a div for each row and a div for each cell.
I want to apply a style to the row but the style is only applied to those elements that are visible.
.inner {
flex: 1 0 10em;
height: 2em;
background-color: green;
}
.outer {
border-bottom: 10px solid red;
display: flex;
}
.box {
width: 20em;
overflow-x: scroll;
}
<div class="box">
<div class="outer">
<div class="inner">1</div>
<div class="inner">2</div>
<div class="inner">3</div>
</div>
</div>
I want all of the green boxes to have a red bottom border, but the border only appears on those items that are not overflowing. What am I missing?
You may try this instead:
.inner {
flex: 1 0 10em;
width:10em; /*Specify a width */
height: 2em;
background-color: green;
}
.outer {
border-bottom: 10px solid red;
display: inline-flex; /* to take the width of content and not container*/
}
.box {
width: 20em;
overflow-x: scroll;
}
<div class="box">
<div class="outer">
<div class="inner">1</div>
<div class="inner">2</div>
<div class="inner">3</div>
</div>
</div>
I dont know exactly what you mean, but I hope this helps:
Since the CSS you use only for the outer,It does just that and put it only for the part that is visible. To achieve bottom red border for all of them, you have to put the border on the inner part.
HTML:
<div class="box">
<div class="outer">
<div class="inner">1</div>
<div class="inner">2</div>
<div class="inner">3</div>
</div>
</div>
CSS:
.inner {
flex: 1 0 10em;
height: 2em;
background-color: green;
border-bottom: 10px solid red;
}
.outer {
width: 20em;
display: flex;
}
.box {
width: 20em;
overflow-x: scroll;
}
Hope it helps :)

Center div on wrapping (when it doesn't fit on the line)

I am trying to create this layout using only CSS:
When title fits:
When title doesn't fit:
The btn on the right should be centered if it wraps.
I tried this:
.container {
width: 100%;
border: 1px solid grey;
padding: 5px;
}
.block {
padding: 5px;
border: 1px solid orange;
float: left;
}
.right-block {
float: right;
}
<div class="container">
<div class="block">Logo</div>
<div class="block">Title that is too long</div>
<div class="block right-block">right-btn</div>
<div style="clear: both"></div>
</div>
But obviously, the btn is still on the right after it wraps. Any idea how to center it when it wraps ? And I'd like to avoid javascript.
Fiddle here: http://jsfiddle.net/b7rvhwqg/
Pure CSS solution using a flexbox layout:
Updated Example Here
The trick is to add justify-content: center/flex-wrap: wrap to the parent .container element for horizontal centering. Then adjust the first element's margin-right value to auto in order to prevent the last element from being centered when it's on the same line.
(You may need to resize the browser to see how it adjusts).
.container {
width: 100%;
border: 1px solid grey;
padding: 5px;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.logo-text {
display: flex;
margin-right: auto;
}
.block {
padding: 5px;
border: 1px solid orange;
}
.center-block {
white-space: nowrap;
margin-right: auto;
}
<div class="container">
<div class="logo-text">
<div class="block logo">Logo</div>
<div class="block text">This title is short.</div>
</div>
<div class="block right-block">right-btn</div>
</div>
<div class="container">
<div class="logo-text">
<div class="block logo">Logo</div>
<div class="block text">This title is slightly longer than the other one. This title is longer than the other one...</div>
</div>
<div class="block right-block">right-btn</div>
</div>
There is an issue to achieve this via Pure CSS. The div is already having a float and you want to have a "long title" to accommodate that float and at the same time, you want the other right float to jump and become center. This is currently not possible. I believe, you need to consider media queries, but again, that will be a dependent solution, but your title looks like independent of expanding/contracting.
is it ok for you if the title will just fit depending on what width u want?.. for example:
{Logo}Title is toooooooooooooooooooooooooooooooooolong {btn}
it will become like this:
{Logo}Title is tooo... {btn}
it will be cut, then only ". . ." will continue
Flexbox is the most suitable for this task:
<div class="container">
<div class="block logo">Logo</div>
<div class="block title">Title that is too long Title that is too long</div>
<div class="block right-block">right-btn</div>
<div style="clear: both"></div>
</div>
.container {
display: flex;
flex-wrap: wrap;
width: 100%
border: 1px solid grey;
}
.block.logo {
flex-grow: 1;
border: 1px solid orange;
}
.block.title{
flex-grow: 10;
border: 1px solid orange;
}
.right-block {
flex-grow: 1;
border: 1px solid orange;
text-align: center;
}
http://jsfiddle.net/gmrash/7b8w982t/
.container {
width: 100%;
border: 1px solid grey;
padding: 5px;
}
.block {
padding: 5px;
border: 1px solid orange;
float: left;
}
.ellipsis{
text-overflow: ellipsis;
/* Required for text-overflow to do anything */
white-space: nowrap;
overflow: hidden; width: 75%;
}
.right-block {
float: right;
}
<div class="container">
<div class="block">Logo</div>
<div class="block ellipsis">Title that is too long Title that is too long Title that is too long that is too long Title that is too long</div>
<div class="block right-block">right-btn</div>
<div style="clear: both"></div>
</div>
jsFiddle

Content goes out of div

Really can't figure out what's wrong with it, but all the content I add into div, goes out of it, just like it's not in it.
Check it here: JSFiddle!
HTML___
<div id="wrapper">
<div id="container">
<div id="header">
<div id="logo">
TEXT GOES OUTSIDE OF DIV :'((
</div>
</div>
</div>
</div>
CSS___
#container {
width: 960px;
margin: 20px auto 0 auto;
background: yellow;
}
#header {
position: relative;
width: 100%;
background: yellow;
border: 1px solid black;
padding: 2px; /*just to see the div*/
}
#logo {
float: left;
}
You need to clear your floats:
<div id="wrapper">
<div id="container">
<div id="header">
<div id="logo">
TEXT NOW APPEARS INSIDE DIV :)
</div>
<div style="clear: both;"></div>
</div>
</div>
</div>
Because you've floated your logo, any content following it will wrap around it. Which is what is causing the effect you're seeing.
Add overflow:auto to your #header div to restore the expected behavior:
#header {
position: relative;
width: 100%;
background: yellow;
border: 1px solid black;
overflow:auto;
}
jsFiddle example
Floating the child essentially removes it from the flow and the parent collapses. Adding the overflow rule gives you the behavior you expected.
I'd urge you to use flex. It's quite robust and lets you create any kind of layout you want without any issues really. I've added a menu to the right hand side just to illustrate your logo in actual context.
<!-- HTML -->
<div id="wrapper">
<div id="container">
<div id="header">
<div id="logo">
TEXT GOES OUTSIDE OF DIV :'((
</div>
<div id="content-menu">
<div id="menu">
Home
Contact
About
About
</div>
</div>
</div>
</div>
</div>
Corresponding CSS:
/* CSS */
#container {
width: 960px;
margin: 20px auto 0 auto;
background: yellow;
}
#header {
position: relative;
width: 100%;
margin: 1.2em auto;
background: yellow;
border: 1px solid black;
padding: 2px; /*just to see the div*/
display: flex;
}
#logo { flex: 1; }
#content-menu { flex: 4;}
#menu { display: flex; }
#menu > a {
display: inline-block;
text-align: center;
line-height: 32px;
text-decoration: none;
color: #000;
flex: 1;
}

How to lay divs horizontally without overflowing to new line?

I have a left floating div which serves as a sidebar (red). Next to it, there is another div that stores the page content (green). The elements inside the content div are left floating (blue).
I want to be able to scroll the boxes horizontally when the browser width is too small to accommodate them; for example if there are a lot of boxes. Instead, the content div moves below the sidebar div and I am scrolling the whole page.
Here is the page layout when the browser window is wide enough:
Here is the HTML:
<div id="container">
<div id="sidebar">Sidebar</div>
<div id="content">
<p class="box">Box 1</p>
<p class="box">Box 2</p>
<p class="box">Box 3</p>
</div>
<div style="clear: both;"></div>
</div>
And here is the CSS:
* {
margin: 0px;
padding: 0px;
}
#container {
background: yellow;
}
#sidebar {
float: left;
background: red;
}
#content {
float: left;
white-space: nowrap;
background: green;
}
.box {
width: 200px;
height: 250px;
background: blue;
margin: 10px;
float: left;
}
Please help me understand what am I doing wrong. Thank you.
You simply need something to wrap your content with the CSS property overflow-x: scroll;.
**Important: This is a CSS3 feature and some browsers may not support overflox-x. Therefore, I highly suggest you read the following: http://net.tutsplus.com/tutorials/html-css-techniques/html5-and-css3-without-guilt/.
Preview: http://jsfiddle.net/WXFJU/
HTML
<div id="container">
<div id="sidebar">Sidebar</div>
<div class="overflow-x">
<div id="content">
<p class="box">Box 1</p>
<p class="box">Box 2</p>
<p class="box">Box 3</p>
</div>
</div>
<div style="clear: both;"></div>
</div>​​​​​
CSS
* {
margin: 0px;
padding: 0px;
}
#container {
width: 100%;
background: yellow;
}
#sidebar {
float: left;
background: red;
}
#content {
float: left;
white-space: nowrap;
background: green;
}
.box {
width: 200px;
height: 250px;
background: blue;
margin: 10px;
float: left;
}
.overflow-x {
overflow-x: scroll;
display: block;
}
​
You need to set a width for the container too.
Your content-div also needs to have a width.
#content {
width: 1000px;
}