I don't know if that is a best practice, but I used a background to put some kind of "ok icon" on a div.
My problem is that the background is setted to left and I can't put a padding there on the left side of the icon.
<div id="martu">
<div class="text">
dummy text dummy text dummy text dummy text dummy text
</div>
</div>
CSS
#martu {
background: url('image') no-repeat scroll 0px 8px #FFB9D9;
padding: 5px 5px 5px 10px;
margin-left: 5px;
}
.text { font-size:17px; padding-left:20px;}
Fiddle: http://jsfiddle.net/9up0kmou/
PS. I know that an option would be to put the image direct on that div, but my dilema is that if background images support paddings or margins.
You can use the background-position CSS property to adjust the location of your background image on your div, for example:
#martu {
background-position:20px 20px; // where the values are x & y coordinates from the original default position
}
But no, short of actually adding whitespace to the image in an image editor (not a good idea, it would add unnecessary size to the file), there's no way of adding background-image-padding.
See this JSFiddle, where I have arbitrarily placed the tick icon in the middle of the element using background-position.
It's then a simple matter of adjusting the div padding to make sure the text doesn't overlap the image.
I'd probably do something like this myself. Using the pseudo-elements ::before and ::after is brilliant for placing icons and other things. That way, you get clean code, and you'd need less wrapping elements.
#martu::before {
content: url("http://findicons.com/files/icons/2015/24x24_free_application/24/ok.png");
float: left;
margin-right: 10px;
}
Try this: http://jsfiddle.net/87obhb57/5/
Long story short: you can't have padding on the background image, so the trick here is to set up a background-color on the outer div that gives you the block appearance; setting up a padding on it that will provide the effect you are looking for.
#martu {
padding: 5px;
background-color: #FFB9D9;
}
And finally, setting the background-image of the inner div to what you want plus a padding-left that is big enough as to ensure that the text and the image won't overlap.
#martu div.text {
background-image: url('something');
background-repeat: no-repeat;
padding-left:34px;
}
Related
I'm using an image from Amazon directly on my website, so I can't manually trim it in an image editing program.
Around the image is lots of white space, which is annoying because under this image, there is text. And this makes the gap between the image and the text under it really big (because of said white space at the bottom of the image).
Also, every image contains a different amount of white space, so I can't just set a fixed negative margin-bottom.
I know that mix-blend-mode: multiply; makes the white space transparent, but the gap is still there because the white (now transparent part) still takes up space in the layout.
How do I make the white part go away so other html elements can use that space?
Here's a codepen: https://codepen.io/AlessioG/pen/VweqMEg
You can use the object-fit css property to crop the image.
Set the container div to the cropped size that you want the image to be, and set the image to that same width and height, then use object-fit on the image.
<div id="crop-container">
<img id="crop-image">
</div>
#crop-container {
width:200px;
height:200px;
}
#crop-image {
object-fit:cover;
width:200px;
height:200px;
}
Just use object-fit and then assign a higher number to width than height in proportion. Please dont use element names like div p or id names etc. Since it is pretty hard to overwrite that.
.image {
margin: 0;
object-fit: cover;
width: 250px;
height: 200px;
}
.header-two {
margin: 10px;
font-weight: bold;
display: block;
font-size: 20px;
}
<div class="container">
<img class="image" src="https://i.imgur.com/l2QrzBg.jpg">
<h2 class="header-two">This is a text</h1>
</div>
If you have a CSS you can assign the img a display block.
I've finally found a solution, go to https://www.iloveimg.com/crop-image, upload your image and crop the whitespace, or use imagemagick to crop the whitespace automatically
I have a <div> with a border:15px. It has an image on the left-corner as given in the fiddle
http://jsfiddle.net/user1212/e7Gez/17/
Now, this image is pushing down the text from the top. How can I make the text get margin-top:0;
I do not want to use the image as background-image since the 15px border overlaps it.
Any solutions?
Just use float: left; on your image, see updated Fiddle. You can also use a negative margin-right on it to prevent it from pushing the text to the left, but then you need to mess with the z-index of it and the rest of the content, which means additional HTML markup for styling purposes; see this Fiddle.
OK, I'm going to leave my other answer as a more direct "this is how you fix things doing it your way" answer, but I think a better approach here would be to use position: absolute; -- this achieves the effect with much less fuss.
Fiddle
I'm styling the image as follows:
img#post-backgrnd {
position: absolute;
top: -1px;
left: 0px;
}
This forces the image to go to the upper-left corner, which is where we want it (the top: -1px; is because the image is slightly misaligned with the border), and position: absolute; means it does not take up space, so it doesn't push the text at all. However, done this way, the image covers up the text. The better solution here is to edit the image, fixing the alignment issue (and going to top: 0;), and making the white section actually transparent.
If for some reason that cannot be done, you could fix this overlapping issue by using z-index, but it means that your parent, your image, and the text need three different z-index values, which means that you need a new element for your text. For an example of this, see this Fiddle.
Just add vertical-align:top to your <div> like:
.content {
width: 500px;
height: 500px;
border: 15px solid #E4EAF3;
/* background: #ffffff url('http://smitra.net76.net/post-bg.jpg') no-repeat top left;*/
margin-top:0;
padding-top:0;
vertical-align:top;
}
see it in jsfiddle
I am trying to build a simple div with a span of text inside of it.
<div id="bottom-text">
<span>ONE STOP</span>
</div>
And here is the simple CSS styling I have in effect for "#bottom-text":
#bottom-text{
font-weight:700;
font-size:50px;
text-align:center;
margin:auto;
position:relative;
padding-top:25px;
height:65px;
width:auto;
}
For some reason, the text "ONE STOP" displays partially outside of #bottom-text. (only the top portion of all the letters...) I've tried using padding to fix it but the text then overflows partially into the padding region!
Can anyone help me figure out why this text is displaying outside the div it is supposed to be contained within? (I've been testing Chrome and Firefox)
Thanks all.
.largefont {
color: #0066FF;
font-family:arial;
font-size: 6px;
display: inline;
}
<span class="largefont">block level span</span>
Assign a class to the span and play with that.
look at your code, the #bottom-text is 65px height, the font-size is 50px, and padding-top is 25px
65-(50+25) = -10
So you will see only the top 10 pixel of your text.
Set padding-top to a lesser amount, and play with just so it is correct
Check your line-height. Only thing I can think of is you might have some styles elsewhere that are adding some in. Try adding "line-height: 1;" to your existing #bottom-text CSS so that your text is actually 50px high. Or, if you want the text to vertically center in #bottom-text make your line-height match the height of #bottom-text (65px).
How can I align a floating image on the right-bottom corner of my text? I have a div which has a background colore and contains a text. I want to see an image in that div tag, in the right-bottom corner. Any CSS solution??
<div id="myText">
A lot of text here A lot of text here A lot of text here A lot of text here A lot of text here A lot of text here A lot of text here A lot of text here A lot of text here A lot of text here A lot of text here A lot of text here A lot of text here
<img id ="info_ico" src="images/key_ico.png" />
<div>
#myText
{
background:#fdf6cc;
min-height: 90px;
margin-left:1px;
width: 913 px;
padding-left: 30px;
}
#info_ico
{
float:right;
clear:right;
}
Now what to change to see the image in the bottom of the text?
If the length of the content is not going to change you can always place the image just before the end of the text, keep it floated right as you're doing and the last bit of text will flow down the left hand side.
Example here: http://jsfiddle.net/pBDaJ/
CSS unfortunately does not provide a way to do this.
The only thing you can do is to experimentally change where you insert the img inside your text, until it ends up where you want it to be: http://jsfiddle.net/KQFBb/1/
This is obviously not viable if the content is dynamic, or the dimensions of the container can otherwise change.
In that case, the only option left is to use JavaScript.
Using background you should be able to do this.
Add to your myText div styling:
background: url('images/key_ico.png') no-repeat;
background-position: 100% 100%;
This will place the picture in the bottom right corner of the div.
Hope this helps :-)
You can use the ::before pseudo-element to add a specific amount of height above the floated image. Just float the pseudo-element to the right and give it some height; then float the image to the right, clearing the pseudo-element.
.wrapper::before {
content: '';
float: right;
height: 300px; /* Height of the container minus the height of the image */
}
.wrapper img {
float: right;
clear: right;
}
If you don't know the height of the content, some light JavaScript is necessary. See http://jsfiddle.net/3jvJh/ for a working example.
I want to create a headline (h2) with an image at the right-most area of the bounding box. I have the layout almost right except I can't push the image a little bit to the right of the element's bounding box -- how would I tweak my css so it is displayed correctly?
I'm trying to do something like this:
[{someHeadLineText}{dynamic space }{image}{5px space}]
where the [] indicate the total available width of my content.
Html:
<div class="primaryHeader">
<h2>News</h2>
</div>
Css:
.primaryHeader h2 {
background-color: green; /* the header looks like a box */
color: black;
background: transparent url(../images/edit.png) no-repeat right center;
border: 1px solid red;
}
I am placing the image to the right of my h2 element and centered vertically -- but how do I adjust the placement of the background image?
I'm afraid I think you can't. You can use either right or a pixel value as the image's x-position but that pixel value will always be relative to the left corner of the bounding box. Adding padding won't help either, it will just extend the bounding box further.
The only solution I know for this is either adding the shift to the image itself, or using an absolutely positioned element (with a slight offset) hovering behind the element - but that would require you know the width and height in advance.
Edit: evil, hacky idea. I have no time to try this out right now, but it should work if the h2 is a display: block.
Give the h2 a position: relative.
Place a div or other element inside the h2 with the following:
position: absolute;
left: 0px;
top: 0px;
right: 5px; /* This is the shift */
bottom: 0px;
background-image: url(...);
background-position: right center;
background-repeat: no-repeat;
z-index: -1; /* I don't know whether this will overwrite the h2's content */
this could lead to the desired effect, I'm not sure as I have not tried.
The element may overlay the h2's other content, in which case you would have to put the rest into a <span> element with position: relative and z-index: 1.
It's really hacky. Better put the padding into the image itself, much cleaner.
Can you add padding pixels in the image itself?
You could ditch the background image and use an image instead.
<div class="primaryHeader" style="padding-right: 5px;">
<img src="../images/edit.png" alt="" style="float: right;" />
<h2>News</h2>
</div>
You can look into CSS3 background positioning. It works in all the modern browsers (not IE, of course).