I've got a jsfiddle here: https://jsfiddle.net/Lh7qbye2/7/
And a test web page here: https://shetline.com/test/test01.html
The question is this: Why doesn't the content of the inner <div> prevent the outer <div> from shrinking to less than the width of the inner <div> when you resize the containing window to a narrow size?
Updates based on the answer I got for the problem:
https://jsfiddle.net/Lh7qbye2/8/
https://shetline.com/test/test02.html
I can solve the problem for Chrome or Safari by using:
width: fit-content;
...on the outer <div>, but this doesn't solve the problem for Firefox or Edge. Further, MDN marks fit-content as an experimental API:
This is an experimental API that should not be used in production code.
word-break: break-all on the outer <div> kinda, sorta, helps, but messes up all word wrap. If I try to compensate by setting normal breaking on the <p> and <button> tags, the help provided by the outer break-all disappears.
One thing that really confuses me is that I know I've seen situations like this with no spill-over problem at all, and I didn't have to go out of my way to get the behavior I'd expect. What am I missing that's wrong in this simplified example?
body {
margin: 4em;
}
.demo {
background-color: #BFC;
box-sizing: border-box;
margin: 0;
padding: 1em;
position: relative;
/* width: fit-content; // With this it works for Chrome or Safari, but not for Firefox or Edge. */
/* word-break: break-all; // For some reason this sort of helps too, but of course messes up all word wrapping. */
/* If I try to apply "word-break: normal" to <p> and <button> tags to compensate, the inner <div> leaks out again. */
}
.demo p:first-child {
margin-top: 0;
}
.other-stuff {
align-items: center;
background-color: #AEB;
display: flex;
}
button {
margin-right: 1em;
}
.square {
display: inline-block;
background-color: #699;
height: 80px;
margin-right: 1em;
min-width: 80px;
width: 80px;
}
.circle {
border-radius: 50px;
display: inline-block;
background-color: #969;
height: 100px;
margin-right: 1em;
min-width: 100px;
width: 100px;
}
<div class="demo">
<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 class="other-stuff">
<button>One button</button>
<div class="square"></div>
<button>Another button</button>
<div class="circle"></div>
<button>Don't click!</button>
</div>
</div>
What you want can be done using a combination of inline-block and min-width:100%. A block element has its width defined based on its parent element (containing block) while inline-block will have its width defined by its content.
Adding the min-width:100% will make it behave as block element. it's not mandatory in this case since you already have a lot of content so you are sure to cover all the width:
body {
margin: 4em;
}
.demo {
background-color: #BFC;
box-sizing: border-box;
margin: 0;
padding: 1em;
position: relative;
display:inline-block;
min-width:100%;
}
.demo p:first-child {
margin-top: 0;
}
.other-stuff {
align-items: center;
display: flex;
}
button {
margin-right: 1em;
}
.square {
display: inline-block;
background-color: #699;
height: 80px;
margin-right: 1em;
min-width: 80px;
width: 80px;
}
.circle {
border-radius: 50px;
display: inline-block;
background-color: #969;
height: 100px;
margin-right: 1em;
min-width: 100px;
width: 100px;
}
<div class="demo">
<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 class="other-stuff">
<button>One button</button>
<div class="square"></div>
<button>Another button</button>
<div class="circle"></div>
<button>Don't click!</button>
</div>
</div>
Reduce the text on the top and min-width:100% will become mandatory to have full width behavior.
Run the snippet on full page
body {
margin: 4em;
}
.demo {
background-color: #BFC;
box-sizing: border-box;
margin: 0;
padding: 1em;
position: relative;
display:inline-block;
}
.demo p:first-child {
margin-top: 0;
}
.other-stuff {
align-items: center;
display: flex;
}
button {
margin-right: 1em;
}
.square {
display: inline-block;
background-color: #699;
height: 80px;
margin-right: 1em;
min-width: 80px;
width: 80px;
}
.circle {
border-radius: 50px;
display: inline-block;
background-color: #969;
height: 100px;
margin-right: 1em;
min-width: 100px;
width: 100px;
}
<div class="demo">
<p>Lorem ipsum </p>
<div class="other-stuff">
<button>One button</button>
<div class="square"></div>
<button>Another button</button>
<div class="circle"></div>
<button>Don't click!</button>
</div>
</div>
<div class="demo" style="min-width:100%;">
<p>Lorem ipsum </p>
<div class="other-stuff">
<button>One button</button>
<div class="square"></div>
<button>Another button</button>
<div class="circle"></div>
<button>Don't click!</button>
</div>
</div>
From the specification:
Block-level, non-replaced elements in normal flow
The following constraints must hold among the used values of the other properties:
'margin-left' + 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' + 'margin-right' = width of containing block
Note how the content play no role in defining the width.
'Inline-block', non-replaced elements in normal flow
If 'width' is 'auto', the used value is the shrink-to-fit width
Calculation of the shrink-to-fit width is similar to calculating the width of a table cell using the automatic table layout algorithm. Roughly: calculate the preferred width by formatting the content without breaking lines other than where explicit line breaks occur, and also calculate the preferred minimum width, e.g., by trying all possible line breaks. Thirdly, find the available width: in this case, this is the width of the containing block minus the used values of 'margin-left', 'border-left-width', 'padding-left', 'padding-right', 'border-right-width', 'margin-right', and the widths of any relevant scroll bars
the shrink-to-fit width is: min(max(preferred minimum width, available width), preferred width)
Note how the content is used to define the width.
The same apply to any kind of display value (grid, flex, table, etc) and the trick is to replace it with the inline-* equivalent (inline-grid, inline-flex, inline-table, etc)
You need to use flex-wrap: wrap; to break the contents to the next line.
In saying that, since you are using fixed width for buttons, this is your best option in smaller screens.
body {
margin: 4em;
}
.demo {
background-color: #BFC;
box-sizing: border-box;
margin: 0;
padding: 1em;
position: relative;
}
.demo p:first-child {
margin-top: 0;
}
.other-stuff {
align-items: center;
display: flex;
flex-wrap: wrap;
}
button {
margin-right: 1em;
}
.square {
display: inline-block;
background-color: #699;
height: 80px;
margin-right: 1em;
min-width: 80px;
width: 80px;
}
.circle {
border-radius: 50px;
display: inline-block;
background-color: #969;
height: 100px;
margin-right: 1em;
min-width: 100px;
width: 100px;
}
<div class="demo">
<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 class="other-stuff">
<button>One button</button>
<div class="square"></div>
<button>Another button</button>
<div class="circle"></div>
<button>Don't click!</button>
</div>
Related
I want to make a pink square over an image like in the picture below.
Update: The result:
Here's my code:
.pink-square {
position: absolute;
left: -50px;
top: -100px;
width: 50%;
padding: 0 1rem 1rem 1rem;
background-color: #FF3366;
}
.square-content {
position: relative;
margin-top: 100px;
margin-left: 50px;
}
<div class="square-content">
<img src="https://via.placeholder.com/400x400">
<div class="pink-square">
<h1>"</h1>
<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>
//- Reset CSS
* {
margin: 0;
padding: 0;
border: 0;
box-sizing: border-box;
}
body,
html {
height: 100%;
min-height: 100%;
}
Any ideas or suggestions on how to do this?
Thanks!
Edit: To see the rest of the code, please see it here => https://github.com/cate-k/demo-website-2
I used the image as the basis and positioned the pink square absolutely. Further documentation in the CSS code.
.pink-square {
position: absolute;
left: -50px;
top: -100px;
width: 50%;
padding: 0 1rem 1rem 1rem;
background-color: pink;
}
.square-content {
position: relative;
margin-top: 100px; /* Needed to make .pink-square visible completely */
margin-left: 50px; /* Needed to make .pink-square visible completely */
}
<div class="square-content">
<img src="https://via.placeholder.com/400x400">
<div class="pink-square">
<h1>"</h1>
<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>
Create a container (a div) that will contain both the image and the box. Then use absolute positioning to set the image to be flush against the right and bottom of the container, and to set the box to be flush against the top and left of the container. If you order it properly, the box should be on top of the image, but use could use z-index to make sure that it's on top.
Here's a simple example on codepen
.container {
position: relative;
width: 800px;
height: 400px;
background: #ccc;
margin: 0 auto;
}
.box {
position: absolute;
z-index: 1;
width: 400px;
height: 200px;
background: #FF3366;
}
.image {
position: absolute;
right: 0;
bottom: 0;
}
<div class="container">
<div class="box">
<h2>Put Content Here</h2>
</div>
<div class="image">
<img src="https://gradientjoy.com/600x300?id=24" alt="">
</div>
</div>
I found a solution for this a couple of days ago but forgot to post it here.
This is the code that works for me:
.container {
position: relative;
}
.square-over-image {
display: block;
margin-left: auto;
margin-right: auto;
width: 80%;
height: 40rem;
}
.text-block {
position: absolute;
bottom: 20px;
top: 20px;
background-color: #FF3366;
color: white;
padding-left: 0;
padding-right: 20px;
width: 25rem;
height: 30rem;
}
.rachel-ashburn-picture {
background-image: url(asset/image/rachel-ashburn.jpg);
border-radius: 50%;
background-position: center top;
background-size: cover;
height: 64px;
width: 64px;
left: 0;
top: 7vh;
margin-left: 4rem;
position: relative;
}
.rachel-name {
padding: 2rem
}
.rachel-title {
margin-left: 23.4rem;
margin-top: 2rem;
left: 0.5vw;
top: 0;
position: relative;
}
.rachel-ashburn {
position: unset;
line-height: 0.2rem;
.rachel-ashburn-picture {
background-image: url(asset/image/rachel-ashburn.jpg);
border-radius: 50%;
background-position: center top;
background-size: cover;
margin-left: 3.5rem;
top: 3rem;
}
.rachel-name {
left: 8.5vw;
top: 0;
position: relative;
}
.rachel-title {
left: 3.5vw;
top: -1rem;
}
}
.container
img.square-over-image(src="/asset/image/women-talking.jpg", alt="Friends")
.text-block
h4 ”
p Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean.
.rachel-ashburn
.rachel-ashburn-picture
span.rachel-name #[h5 Rachel Ashburn]
span.rachel-title #[p Web Designer]
Thanks for all your help, everyone!
I have this :
#toast {
position: fixed;
left: 0;
right: 0;
margin: auto;
bottom: 20px;
width: auto;/* not needed */
max-width: 90vw;
min-width: 250px;
font-size: 16px;
background: red;/* for refrence */
}
<div id="toast">Hi</div>
I want my element 250px wide, but instead it is 90vw. (250px can be 50px, still problems)
Why is it 90vw, and how do I make it 250px when it has no content? It should still be centered. transform: translateX(-50%) doesn't work because then it won't be wider than 50vw.
Because when you set left and right at the same time, combined with width:auto (the default), the element's width will be calculated to "fill in the offsets" - you're effectively setting 100% width here.
Remove the right:0 (or left) and the width is what you'd expect:
#toast {
background-color: pink;
position: fixed;
left: 0;
//right: 0;
margin: auto;
bottom: 20px;
width: auto;
max-width: 90vw;
min-width: 250px;
font-size: 16px;
}
<div id="toast">Hi</div>
To center your element, one pretty easy way would be to set display:flex on the body, because when you apply auto margins to a flex item, that item will automatically extend its specified margin to occupy the extra space in the flex container:
body {
display:flex;
}
#toast {
background-color: pink;
margin: auto;
max-width: 90vw;
min-width: 250px;
font-size: 16px;
}
<div id="toast">Hi</div>
Adding to what #Sweaver2112 Said, Because margins don't apply on absolute/fixed positioning, To center it you can try something like this.
*, *:before, *:after {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.parent {
background-color: brown;
text-align: center;
position: fixed;
bottom:0;
}
.toast {
/* To remove center being inherited */
text-align: initial;
/* Inline level elements get moved by text-align property of their parent */
display: inline-block;
max-width: 90vw;
min-width: 250px;
background: orange;
}
<div class="parent">
<div class="toast">
<h3>Triggering the min-width </h3>
</div>
<div class="toast">
<h3>Triggering the max-width </h3>
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>
This question already has answers here:
Last margin / padding collapsing in flexbox / grid layout
(4 answers)
Closed 5 years ago.
I'm trying to set up a page layout with flexbox such as the one illustrated here: https://jsfiddle.net/axedre/o6xw7pfg/, but I can't figure out why the .main div (as per the example) does not add padding to the right of the .content div when the latter grows beyond allotted space.
You can see what I mean by un-commenting line 30 in the SCSS section of the fiddle and scrolling the .main div all the way to the right.
Even if I remove the padding property from .main and add the same values to a margin property on .content I can't get the desired result, which is to have a space on the right-hand side of .content even when one scrolls to the right end of .main.
Any help is greatly appreciated, cheers!
Here is a simple fix for that, where to make .content display as inline-block and use margin instead of padding
This works on Firefox, Edge, IE11, but not on Chrome. Luckily though, by simply add a padding, e.g. padding-right: 0.1px;, to .main fixes that.
Updated fiddle
Stack snippet
body,
html {
height: 100%;
display: flex;
flex-direction: column;
flex: 1;
padding: 0;
margin: 0;
}
.top {
border: 2px solid lime;
display: flex;
flex: 1;
}
.top .firstcol {
border: 1px solid red;
display: flex;
flex-direction: column;
overflow: auto;
}
.top .firstcol .header {
border: 1px solid yellow;
}
.top .firstcol .main {
border: 1px solid navy;
flex: 1;
padding-right: 0.1px; /* fix for Chrome */
overflow: auto;
}
.top .firstcol .main .content {
display: inline-block; /* added */
margin: 1rem 2rem; /* added here as margin instead */
border: 2px solid maroon;
width: 1000px;
}
.top .firstcol .footer {
border: 1px solid magenta;
}
.top .secondcol {
border: 1px solid red;
}
.top .secondcol .othercontent {
border: 1px solid yellow;
width: 350px;
}
<div class="top">
<div class="firstcol">
<div class="header">header</div>
<div class="main">
<div class="content">content</div>
</div>
<div class="footer">
<code>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.</code>
</div>
</div>
<div class="secondcol">
<div class="othercontent">fixed</div>
</div>
</div>
Another solution would be to add an extra wrapper, displayed as inline-block, which might be more hack free than the previous.
Updated fiddle 2
Stack snippet 2
body,
html {
height: 100%;
display: flex;
flex-direction: column;
flex: 1;
padding: 0;
margin: 0;
}
.top {
border: 2px solid lime;
display: flex;
flex: 1;
}
.top .firstcol {
border: 1px solid red;
display: flex;
flex-direction: column;
overflow: auto;
}
.top .firstcol .header {
border: 1px solid yellow;
}
.top .firstcol .main {
border: 1px solid navy;
flex: 1;
overflow: auto;
}
.top .firstcol .main .fix-right-margin {
display: inline-block;
}
.top .firstcol .main .content {
border: 2px solid maroon;
width: 1000px;
margin: 1rem 2rem;
}
.top .firstcol .footer {
border: 1px solid magenta;
}
.top .secondcol {
border: 1px solid red;
}
.top .secondcol .othercontent {
border: 1px solid yellow;
width: 350px;
}
<div class="top">
<div class="firstcol">
<div class="header">header</div>
<div class="main">
<div class="fix-right-margin">
<div class="content">content</div>
</div>
</div>
<div class="footer">
<code>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.</code>
</div>
</div>
<div class="secondcol">
<div class="othercontent">fixed</div>
</div>
</div>
Figured it out thanks to this article, and jotted down this css class that does the trick:
.ensure-padding {
position: relative;
}
.ensure-padding::after {
content: "";
position: absolute;
left: 100%;
width: 2rem;
height: 1px;
}
Now one simply needs to add the .ensure-padding class to leave some space to the right of an element when it overflows its container, as shown here. The only downside is that the value for the width property on the ::after pseudo-class has to equal the value of the padding-right property on the .main container, so when the the latter changes the .ensure-padding property has to be updated accordingly.
I want a list of items that have a cross icon in the middle on the right hand side. To do this I used an anchor with some padding, floated right, and relatively positioned. The icon is a known width and height.
The items also have some text that should also be centered vertically. The additional problem is that this text has an arbitrary length and I do not want any overflow or wrapping. The text must not overlap the icon and must be allowed to go up to its edge.
The final piece of the puzzle is that the width of each item must grow to the size of its parent. The height should remain fixed.
Here is an example (https://jsfiddle.net/bj6806u6/3/)
div {
background-color: red;
padding-right: 50px;
display: inline-block;
width: 80%;
vertical-align: middle;
height: 50px;
}
p {
background-color: yellow;
height: 20px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
display: inline-block;
max-width: 100%;
}
a {
background-color: green;
float: right;
position: relative;
padding: 10px;
top: 6px;
right: -44px;
}
<div><p>This is a short paragraph.</p><a>X</a></div>
<br /><br />
<div><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><a>X</a></div>
Removing the -44px; and it is a bit more obvious what the problem is although I still have no idea how to solve it. Surely this must be possible, it seems like such a trivial problem.
So my question: How to keep the cross from moving down? I want it to always look like the first div.
You could do it with positioning instead of floating. The outer div gets relative positioning and then the x gets absolute positioning. It's positioned down from the top 50% of the height of the container and then negative margined back up half of the height of the x to place it in the middle.
div {
background-color: red;
padding-right: 50px;
display: inline-block;
height: 50px;
position: relative;
max-width: 100%;
box-sizing: border-box;
}
p {
background-color: yellow;
height: 20px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
max-width: 100%;
}
a {
background-color: green;
position: absolute;
padding: 10px;
top: 50%;
right: 4px;
margin-top: -19px;
}
<div><p>This is a short paragraph.</p><a>X</a></div>
<br /><br />
<div><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><a>X</a></div>
Does this help?
div {
background-color: red;
padding-right: 50px;
display: inline-block;
width: 80%;
vertical-align: middle;
height: 50px;
position: relative;
}
p {
background-color: yellow;
height: 20px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
display: inline-block;
max-width: 100%;
}
a {
background-color: green;
float: right;
position: absolute;
padding: 10px;
top: 6px;
right: 10px;
}
I positioned the div relative and then made the icon an absolute float from the right of that. Now sure if this is what you mean...
https://jsfiddle.net/bj6806u6/7/
Look at the code, you don't need to set padding-right on div and right on a (1 & 3), set max-width of p to full width subtract 50px (2) to take space for the cross.
div {
background-color: red;
/* padding-right: 50px; */ /* 1 */
display: inline-block;
width: 80%;
vertical-align: middle;
height: 50px;
}
p {
background-color: yellow;
height: 20px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
display: inline-block;
max-width: calc(100% - 50px); /* 2 */
}
a {
background-color: green;
float: right;
position: relative;
padding: 10px;
top: 6px;
/* right: -44px; */ /* 3 */
}
<div>
<p>
This is a short paragraph.
</p>
<a>X</a>
</div>
<br />
<br />
<div>
<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>
<a>X</a>
</div>
Nothing much need to change add div position relative, change a tag position absolute and right -44px to 5px. Live FIddle
div {
background-color: red;
padding-right: 50px;
display: inline-block;
width: 80%;
vertical-align: middle;
height: 50px;
position: relative; /*add position relative */
}
p {
background-color: yellow;
height: 20px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
display: inline-block;
max-width: 100%;
}
a {
background-color: green;
float: right;
position: absolute; /*Change relatie to absolute */
padding: 10px;
top: 6px;
right: 5px;/*Change -44 to 5px*/
}
<div>
<p>
This is a short paragraph.
</p>
<a>X</a>
</div>
<br />
<br />
<div>
<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>
<a>X</a>
</div>
I have keep the width fixed and expanded the text on hover.Is this the solution your looking for?
div {
background-color: red;
padding-right: 50px;
display: inline-block;
width: 80%;
vertical-align: middle;
height:auto;
}
p {
background-color: yellow;
height: 20px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
display: inline-block;
max-width: 100%;
}
.wrapR
{
max-width:80%;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
.wrapR:hover {
background-color: yellow;
overflow: visible;
}
a {
background-color: green;
float: right;
position: relative;
padding: 10px;
top: 6px;
right: -44px;
}
If the width of "X" is known or fixed, then you can limit the maximum width of adjacent text to make sure "X" is not moved or overlapped.
div {
background-color: red;
padding-right: 50px;
display: inline-block;
width: 80%;
vertical-align: middle;
height: 50px;
}
p {
background-color: yellow;
height: 20px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
display: inline-block;
max-width: calc(100% - 30px);
}
a {
background-color: green;
float: right;
position: relative;
padding: 10px;
top: 6px;
right: -44px;
width:10px;
}
My content text is displayed under content div. Inside that div I have floated div block which have menu content with links.
Problem is that my content text appears under menu div block and I want to force content text to break arround menu block.
Live example is heee
http://jsfiddle.net/qYcWc/
#content {
display: inline;
float: left;
height: auto;
margin-top: 0;
padding: 5px;
width: 570px;
}
#content .subLinks {
background-color:gray;
border: 1px solid black;
height: auto;
margin-left: 330px;
margin-top: 60px;
padding: 10px;
position: absolute;
width: 220px;
}
<div id="content">
<div class="subLinks">
<ul class="subLinksMenu">
<li><a title="LinkOne" href="/">LinkOne</a></li>
<li><a title="LinkTwo" href="/">LinkTwo</a></li>
</ul>
</div>
<h1>Lorem ipsum dolor sit amet</h1>
"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>
Pretty pretty messy CSS, will take on point by point, if you don't care to read, here's a demo for you
1) position: absolute; is a culprit here, when you use position: absolute; the div flows out of document flow and hence your div has no idea to wrap around the box.
2) Why use display: inline; and float: left; together?
3) Use of explicit margins, not required!
4) Always wrap text inside an element, say p for semantic meaning, it says yes, the text is a paragraph
Easy Example
I suggest floating your subLinks to the right. This will let the content text wrap around it:
#content .subLinks {
background-color:gray;
border: 1px solid black;
padding: 10px;
margin:0px 0px 15px 15px;
position: relative;
float:right;
}
http://jsfiddle.net/qYcWc/2/
Updated fiddle: http://jsfiddle.net/qYcWc/5/
Try simplifying your solution:
#content {
height: auto;
margin-top: 0;
padding: 5px;
width: 570px;
}
.subLinks {
border: 1px solid black;
height: auto;
padding: 10px;
width: 220px;
float: left;
margin: 0 5px 0 0;
}