Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Div width auto to span Text?

Tags:

html

css

Basically I've:

<div>
   <span>Keep parent div width auto to this text</span>
</div>

Simply I need to set the div width extend to span Text.

Tried this but didn't worked :

div{
    position:absolute;
    height:50px;
    width:auto;
    background:#1a5f8c;
    border:1px solid #14496d;
    display:inline-block;
}
span{
    position:absolute;
    top:0;
    left:0;
    height:100%;
    width:100%;
    text-align: left;
    font-size:14px;
    font-weight:500;
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
    box-sizing:border-box;
    padding: 10px;
    color:#ddd;
}

Jsfiddle: https://jsfiddle.net/x0r4oz86/

All replies are much appreciated :)

like image 403
Jay wardan Avatar asked Dec 21 '25 11:12

Jay wardan


1 Answers

You just need to remove position:absolute from your span element

Here is the working fiddle:

https://jsfiddle.net/x0r4oz86/1/

like image 63
Commercial Suicide Avatar answered Dec 23 '25 00:12

Commercial Suicide



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!