简体   繁体   中英

Make div 50% width of its contents

I have been looking around google/forums and havent really found an answer to this. I am wondering if I can, in CSS, size a div to 50% width of its contents. I see that calc() doesnt work with auto, but basically what I want to accomplish is width: calc(auto/2). Can I do this in CSS, or does it have to be done after the page is loaded in javascript/jquery?

Note:the height will be 100% with an image in it so I wont want to use px values.

Assuming that your internal content is just the image, maybe something like this:

var innerwidth = $('.parentdiv').children('img').width();
$('.parentdiv').css('width', innerwidth);

Maybe I'm misunderstanding the question, but I don't see why this wouldn't work. Hope it helps :)

Edit: I am agreeing with what the comments said - there really isn't a way to use CSS to figure out the size of a child.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM