0

because i need to give a constant width & height to my div depending on my background image size,i need to find out that. here was many solutions for this issue....

like here: How do I get background image size in jQuery?

here is my code

body{
    background-repeat: no-repeat;
    background-size: 100% 100%;
    background-attachment: fixed;
    background-position: center;
    background-image: url(./mybgimg.png);
}
#a{
    position: absolute;
    margin: auto;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    max-width: 980px;
    max-height: 680px;
    background-image: url(../myImage.png);
    background-position: center;
    background-repeat: no-repeat;
    background-size: contain;
}
<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>sample</title>
</head>
<body>
<div class="container">
  <div id="a">
   <div >
     <!-- sth that should place exactly in this div depending on what size is my #a div ...    -->
   </div> 
  </div>
 </div>
</body>
</html>

how i get background image size of div with #a id ?

martin
  • 11
  • 3
  • _"here was many solutions for this issue.... like here: [...]"_ - exactly, there's answers for that elsewhere already - so what exactly is your question now, or how is it different? – CBroe Jun 03 '17 at 20:04
  • first when i use this code (that was accepted as a best solution ):var url = $('#myDiv').css('background-image').replace('url(', '').replace(')', '').replace("'", '').replace('"', ''); var bg ..... in my console i see error for replace method !? – martin Jun 03 '17 at 20:22
  • actually i am curious to know why we should replace url of my image – martin Jun 03 '17 at 20:25
  • and another thing is about this regular exertion : match(/^url\("?(.+?)"?\)$/)[1]) ... what it say??? – martin Jun 03 '17 at 20:27
  • Because reading the value of `background-image` will result in something like `url(...)`, maybe additionally using single or double quotes around the `...`, and since creating a new image element in JS to be able to read the intrinsic width and height of the image needs needs the URL to the image, which is what the `...` are, they need to be parsed out of that value first ... – CBroe Jun 03 '17 at 20:27
  • can you please write your suggestion code @CBroe – martin Jun 03 '17 at 20:39
  • Do you want to get the background-size value? – Lucas David Ferrero Jun 03 '17 at 20:46
  • i need to know in each client browser ,whatis my background size(both width & height in pixel) – martin Jun 03 '17 at 20:49
  • because in small devices my bg will be smaller... so i have to know what is it's size and then doing other things – martin Jun 03 '17 at 20:51

0 Answers0