I'm working on a layout for a thumbnails component which contains an image and text element. The images have a height: 100px; and auto width, while the text should span up to the width of the image width and the overflow should be hidden.
<div>
<img src="...">
<span>a bunch of text that should be cut off</span>
</div>
I've tried accomplishing this using flexbox but I can't get the text width to be equal to the image width. Instead, the text just grows 100% and stretches out the image.
See my jsbin
You need to specify container's width -> so that your text box knows it's width.
You need tell text to not wrap.
You need to hide the overflowing text.
You need ellipsis for overflowing text.
Following code works:
.main {
border: 1px solid red;
display: flex;
flex-wrap: wrap;
width: 50%; // px would also work
}
.main img {
height: 100px;
flex: 0 0 100%;
}
.main div {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
<div class="main">
<img src="http://i.imgur.com/uiGur1h.jpg" alt="">
<div>Text go here </div>
</div>
<div class="main">
<img src="http://i.imgur.com/uiGur1h.jpg" alt="">
<div>Text would go here but would be cut off if it's very long </div>
</div>
Refer example in https://developer.mozilla.org/en/docs/Web/CSS/text-overflow
Related
I am trying to achieve auto resizing in my div parent block. I would like my image to resize to an exact div block width or even better if text would resize image and changes div block width so that page look neat.
<body style="margin: auto;max-width: 800px">
<div class="messages" style="color: #002b55;max-width:100%;height: auto;font-size: 11px">
<img src=cid:header.png alt="header">
<div style="margin-top: 7px"><b>Lieber Nutzer,</b></div>
for example if the image is bigger than text i would like my image to resize to the longest line
You can have images and divs resize using display: flex; Also you can have your text change size using vw instead of px such as font-size:5vw; which is in relation to the size of the view window.
Check out what I did here. And here is the jsfillde for you to play around with the window size. https://jsfiddle.net/dcxp0uqe/
.messages {
width 50%;
height 50%;
display: flex;
font-size:5vw;
}
.messages * {
display: flex;
width: 50%;
height: 50%;
}
<body style="margin: auto;max-width: 800px">
<div class="messages" style="">
<img src="https://w3schools.com/html/img_girl.jpg" alt="header">
<div style="margin-top: 7px"><b>Lieber Nutzer,</b></div>
</div>
I have a div container with multiple children (also divs). I need the bottom child to be the same width as the text inside it. The parent container and the other children should have exactly the same width as the bottom child has, i.e. the bottom child determines the width of everything. How do I achieve that?
I suspected that flexbox might help, but I wasn't able to find anything related in the documentation. Is it even possible to do it in pure CSS or I have to use JavaScript for that?
Here is a fiddle that demonstrates the problem.
<div class="parent">
<div class="slave">Child that shouldn't dictate the width to anybody at all even though it's long</div>
<br>
<div class="master">Child that dictates the width to the parent and siblings</div>
</div>
What it does:
What I would like (ellipsis is unnecessary, it's fine if it just cuts off):
Wrap text of the slave by div and place it absolute. To add tree dots when text is too long apply text-overflow: ellipsis.
.parent {
background-color: green;
display: inline-block;
}
.slave {
background-color: blue;
color: red;
width: auto;
height: 40px;
position: relative;
}
.slave div {
position: absolute;
top: 0;
left: 0;
width: 100%;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
.master {
background-color: red;
color: black;
width: auto;
height: 40px;
}
<div class="parent">
<div class="slave">
<div>Child that shouldn't dictate the width to anybody at all even though it's long</div>
</div>
<br>
<div class="master">Child that dictates the width to the parent and siblings</div>
</div>
<div class="parent">
<div class="slave">
<div>Two words</div>
</div>
<br>
<div class="master">Child that dictates the width to the parent and siblings</div>
</div>
div {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
Hope this will be the solution for your problem
Why adding contenteditable="true" to a span inside a div, cause break line?
div{
width: 110px;
height: 80px;
border:1px solid #5aa;
}
<div>
<span>IamANowSpaceLineSample</span>
</div>
<br/>
<div>
<span contenteditable="true">IamANowSpaceLineSample</span>
</div>
The text "IamANowSpaceLineSample" is longer than the 110px width you have allocated to your div. It has no spaces and no css rule saying that it can break the word to fit, so it isn't getting wrapped to fit inside its parent div, and its extending outside the div.
However when you add contenteditable="true" the styling changes and the following gets applied (as you can see if you check the element in the element inspector):
span[Attributes Style] {
-webkit-user-modify: read-write;
word-wrap: break-word;
-webkit-line-break: after-white-space;
}
This sets word-wrap: break-word; which forces the browser to wrap the text to fit inside the 110px width of the parent div.
If you want to prevent it wrapping, you can use white-space: nowrap; (or make the div wide enough to fit!)
Examples:
div{
width: 110px;
height: 20px;
background-color: #aaa;
margin-bottom:2em;
}
span.nowrap{
white-space: nowrap;
}
<div>
<span>IamANowSpaceLineSample</span>
</div>
<div>
<span contenteditable="true">EditableWithDefaultStyle</span>
</div>
<div>
<span class = "nowrap" contenteditable="true">EditableWithNoWrapStyle</span>
</div>
I'm trying to create 3 div's with a width of 100% and height is 100% so that every div occupies the entire screen. Every div has a text with an image placed at the bottom middle of the entire div.
<div class="first">
<p>Some text is inserted here</p>
<img src="some-image" class="img img-responsive"/>
</div>
<div class="second">
<p>Some text is inserted here</p>
<img src="some-image" class="img img-responsive"/>
</div>
<div class="third">
<p>Some text is inserted here</p>
<img src="some-image" class="img img-responsive"/>
</div>
Hence I gave the images absolute positioning and my main div's relative positioning and gave some percentage values to the absolutely positioned images, so that they are aligned properly at the bottom center even when the screen is resized.
.first{
width : 100%;
height : 100%;
position : relative;
}
.second{
width : 100%;
height : 100%;
position : relative;
}
.third{
width : 100%;
height : 100%;
position : relative;
}
img{
position : absolute;
top : 60%;
}
Here comes my problem when I resize the window the image is also getting resized as it is responsive and as it is absolutely positioned when the image size is getting bigger, It is getting overlapped on the text. How should I get rid of this overlapping in responsive screens? thanks in advance :)
If you are creating a responsive layout, CSS Flexbox module is a very good place to start. If I have understood the description of the layout you are trying to achieve correctly, here is an example of how you might approach creating that layout in Flexbox:
body {
display: flex;
flex-direction: column;
margin: 0;
padding: 0;
}
div {
flex: 1 0 100%;
display: flex;
flex-direction: column;
justify-content: flex-end;
align-items: center;
min-height: 100vh;
}
.first{
background-color:red;
}
.second{
background-color:yellow;
}
.third {
background-color:green;
}
img {
width: 40vw;
height: 10vw;
margin-bottom:12px;
background-color: rgba(0,0,0,0.3);
border: 4px solid rgba(0,0,0,0.4);
}
<div class="first">
<p>Some text is inserted here</p>
<img src="some-image" class="img img-responsive"/>
</div>
<div class="second">
<p>Some text is inserted here</p>
<img src="some-image" class="img img-responsive"/>
</div>
<div class="third">
<p>Some text is inserted here</p>
<img src="some-image" class="img img-responsive"/>
</div>
I have user generated content that needs to scale to fit in the parent container.
I don't have control of anything inside the div.
.container {
max-width: 300px !important;
border: 1px solid black;
}
<div class="container">
<p><img style="width: 800px;" src="http://placekitten.com/g/800/200"><br></p>
<p>
Other content
</p>
<p>
CSS IS AWESOME
</p>
</div>
How to I keep the content bound?
So you have to specify for all children at any level have max-height of 100% of parent element :)
.container {
max-width: 300px;
border: 1px solid black;
}
.container *{
max-width:100%;
}
<div class="container">
<p><img style="width: 800px;" src="http://placekitten.com/g/800/200"><br></p>
<p>
Other content
</p>
<p>
CSS IS AWESOME
</p>
</div>
And I'd suggest you get rid of !important if nothing else is modifying element so CSS name would be justified for word Cascading :)
I'm not sure why you're setting the image width inline to a set pixel width, but this will ensure the image never exceeds the bounds of it's container:
CSS:
img {
max-width: 100% !important;
}