Fitting a div to a multiline text span [duplicate] - html

This question already has answers here:
CSS when inline-block elements line-break, parent wrapper does not fit new width
(2 answers)
Closed 1 year ago.
I have run into this problem:
span {
width: fit-content;
background-color: yellow;
}
div {
width: fit-content;
border: black 1px solid;
max-width: 100px;
}
<div>
<span>Fooooo Barrrrrrrr</span>
</div>
<p>The right side border should be touching the right side of the longest line.</p>
I would like the div to be as close to the size of the span as possible. I have put width: fit-content; there but it seems to only fit to its own max-width. I am not asking to fit the div to the boundries of every line, but it should be as wide as the widest line in span. How can I resolve this?

Try with this:
.customblk span {
background-color: yellow;
}
.customblk div {
border: black 1px solid;
max-width: max-content;
}
<section class="customblk">
<div>
<span>Fooooo Barrrrrrrr </span>
</div>
<p>The right side border should be touching the right side of the longest line.</p>
</section>

Here you can use min-content, or if you don't want the words to break you can use max-content only on the div and let the span have auto width (it's the default, so no need to explicitly set it). Note that your CSS will influence all divs on your page, so to test this you need to set unique classes or ids to not have the first css influence the second css.
span.span1 {
width: fit-content;
background-color: yellow;
}
div.div1 {
width: min-content;
border: black 1px solid;
max-width: 100px;
}
span.span2 {
background-color: yellow;
}
div.div2 {
border: black 1px solid;
width: max-content;
}
<div class="div1">
<span class="span1">Fooooo Barrrrrrrr</span>
</div>
<br>
<div class="div2">
<span class="span2">Foooooo Barrrrrrrrrr</span>
</div>

Related

Why i don't have my three divs in one row on my layout? [duplicate]

This question already has answers here:
How to remove the space between inline/inline-block elements?
(41 answers)
CSS: Width in percentage and Borders
(5 answers)
Closed 1 year ago.
I have this html
<div class="parent">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
i wanted this boxes to be in the same row and they to be equal. So i setted the parent div to be 100% width, and inside my items to be 33.%.
So 3 x 33.3 = 99.9.
.parent {
width: 100%;
border:1px solid blue;
}
.box {
width: 33.3%;
border: 1px solid red;
display: inline-block;
}
body {
margin: 0px;
padding: 0px;
}
That should pass inside the parent which takes 100% width of the whole window.
But the box number 3 gets down instead next to box number 2. Why is that ?
Give the Parent box a font-size of 0. The white space between each inline-block div is causing the last one to go to the next line. Be sure to give the inline-block divs each their own regular font-sizes though so any text they have isn’t invisible.
Also, make sure the inline-block divs have “box-sizing:border-box” so that their borders are included in the 33.3% width
Basically, this:
.parent {
width: 100%;
border:1px solid blue;
font-size: 0px;
}
.box {
width: 33.3%;
border: 1px solid red;
display: inline-block;
box-sizing: border-box;
font-size: 16px;
}
You can add box-sizing: border-box to your .box div so that the 1px borders are included in the width:
.box {
width: 33.3%;
border: 1px solid red;
box-sizing: border-box;
display: inline-block;
}
You have added several other bits to the widths of your elements. The borders will add 2px.
Also browsers add their own default settings for margin/padding.
You will often see at the top of CSS stylesheets:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
This tells the browser not only not to add its own styling but also if styling is added further down the cascade that padding and border widths should be included in the width of an element. So for example if you set a width at 100px and then add some padding to it it will still have width 100px. This can make calculating how much space is actually being taken up somewhat easier.
However, there is one more thing in the case of inline-blocks - if there is whitespace between them in the layout the browser will add a (small) whitespace between them in laying them side by side. I don't know what the ultimate recommended way of getting rid of this might be (there's lots of options if you search). This snippet just gets rid of the whitespace in the HTML text so it doesn't arise.
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.parent {
width: 100%;
border: 1px solid blue;
}
.box {
width: 33.3%;
border: 1px solid red;
display: inline-block;
}
body {
margin: 0px;
padding: 0px;
width: 100vw;
}
<div class="parent"><div class="box">1</div><div class="box">2</div><div class="box">3</div>
</div>
a better way to do this is using flex.
#parent{
display:flex;
border:solid 1px red;}
.box{
width:33.3%;
border:solid 1px black;
text-align:center;}
<div id="parent">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>

Wrap text on many lines [duplicate]

This question already has answers here:
CSS when inline-block elements line-break, parent wrapper does not fit new width
(2 answers)
Closed 1 year ago.
I have a text over many lines. Example:
Can the text be adapted to the div?
In this example, after the word "text" there is a space with color blue. In CSS there is a method to avoid the blue space after the word "text"?
If I have several texts with different lengths, is there a way to generalize this behavior?
.box {
border: 1px solid red;
width: 200px;
position: relative;
padding-top: 40px;
}
.label {
position: absolute;
top: -10px;
background-color: blue;
}
<h1>My First Heading</h1>
<div class="box">
I'm a box.
<div class="label">
I'm a label without a text wrapped.
</div>
</div>
To avoid space after text and align it property to both side, use text-align css property.
.box {
border: 1px solid red;
width: 200px;
position: relative;
padding-top: 40px;
}
.label {
position: absolute;
top: -10px;
background-color: blue;
text-align: justify;
}
<h1>My First Heading</h1>
<div class="box">
I'm a box.
<div class="label">
I'm a label without a text wrapped.
</div>
</div>
you could use a margin-right: <unit of measument>; in order to remove the excess blue though this would not be very responsive if the size of that box is going to change.

Empty child div with a background image to take up 100% of parent div height

Found it as not that easy task to force the child div to take the full height of its background image.
There are six obligatory terms:
Exact height is unknown for both div
Child div is empty
Background image size: cover
display: flex
No javascript
It's a list's item (thus no fullscreen)
.item {
display: flex;
border: 1px solid red;
height: 100%;
}
.inner {
background: url('https://images.unsplash.com/photo-1504608524841-42fe6f032b4b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=701&q=80') center center / cover no-repeat;
border: 1px solid blue;
width: 100%;
height: 100%;
}
<div class='item'>
Inner content
<div class='inner'></div>
</div>
All similar questions found on SO don't pass all or some of terms.
Would be grateful for any thoughts (especially from grid gurus).
To have the image determine the height of the inner div (as if it were an <img> element), use this.
.item {
display: flex;
border: 1px solid red;
height: 100%;
}
.inner {
border: 1px solid blue;
}
.inner::before {
font-size:0;
content: url('https://images.unsplash.com/photo-1504608524841-42fe6f032b4b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=701&q=80');
display:block;
}
<div class='item'>
<div class='inner'></div>
</div>
Note that this answers the question in the first sentence of the question body, which is not the same as the question in the title.

Setting width and margin to auto on inner div, CSS, HTML [duplicate]

This question already has answers here:
Best way to center a <div> on a page vertically and horizontally? [duplicate]
(30 answers)
Closed 7 years ago.
I have 2 divs. One inside of another. The one on the outside has a width and height of 500px, and the one on the inside have a width of auto. I'm trying to center the div on the inside, but I'm also trying to get the width of it to be auto. It's not adjusting the width to auto, and instead it's spanning the entire 500px of the outer div. I'm not sure what I did wrong, so please take a look at my code:
<style>
#outer {
border: 1px solid black;
width: 500px;
height: 500px;
}
#inner {
border: 1px solid black;
width: auto;
margin: auto;
}
</style>
<div id = 'outer'>
<div id = 'inner'> Inner </div>
</div>
Div's are display: block which defaults to filling the entire width of the parent. So when you are setting it to width: auto it's doing the default. If you want to make it conform to it's content, then set it to display: inline-block. Since inline-block can be centered like text just add text-align: center to your #outer div. Like so:
#outer {
border: 1px solid black;
width: 500px;
height: 500px;
text-align: center;
}
#inner {
border: 1px solid black;
display: inline-block;
}
<div id='outer'>
<div id='inner'>Inner</div>
</div>

limit middle div width to child div width

I have nested divs like so:
<div class="first">
<div class="second">
<div class="third"></div>
</div>
</div>​
The third div contains dynamic content - so I don't know it's dimensions.
What I want is the second div to take the width of the third div and not of the first div which is a lot bigger.
So in this demo, I want the border to enclose the green square.
Is this possible with css only? if so, how?
Thanks.
Put a float: left; in the second class. That should do the trick.
.second {
float: left;
}
or
.second {
display: inline-block; //not working on ie7
}
Actually div is a block level element so you can give the display:inline-block to second div and than it will take the third div width & height vic-versa...
CSS
.first
{
width: 500px;
height: 500px;
background: yellow;
}
.second
{
border: 5px solid blue;
display:inline-block;
}
.third
{
min-width: 100px;
min-height: 100px;
background: green;
}
DEMO