i have an image inside a div and want the image to have some space from the borders of the div its contained in, i have tried adding padding but the image still fills the whole div and seems to ignore the padding values i’ve added, any help please, here’s the code in the html file
<body>
<div id="railwayblock">
</div>
</body>
here’s the css file
#railwayblock{
height: 500px;
border-style: solid;
border-width: 10px;
border-color: green;
padding-right:50px;
padding-left:50px;
background-image: url("animated_train.jpg");
background-size:cover;
}
this is what the image looks like
i want it to have some space from the borders of the div that its contained in as i have mentioned before.
2
Answers
What if you tried using an HTML
img
tag instead of background?For example,
To add padding around the image inside the #railwayblock div, you need to set the padding property directly to the #railwayblock div itself, not to the background-image.
Here’s how you can modify your CSS:
By setting padding: 50px;, you add 50 pixels of padding around all four sides of the #railwayblock div, creating space between the border and the content (including the background image).
With this adjustment, the image inside the #railwayblock div should now have space from the borders of the div as specified by the padding values. Adjust the padding values as needed to achieve the desired spacing.