Is it possible to remove the padding on the image, but keep the padding for the text from within the style.css file:
<p>
Hello World
<img src="">
</p>
I've set style as:
style.css
p { padding: 5px 10px }
img { padding: 0px !important}
You need to think about how elements interact. Padding is space INSIDE the element and margin is space OUTSIDE the element. Both "push" an element around in the layout.
Images don't have padding. (at least not like other elements, you would only see it if you wanted an offset border)
Also, the text content of an element doesn't have any properties so you can't add/remove padding from that.
Your currently have something like this:
------------<p>-------------
some text
------------<img>-----------
------------</img>----------
------------</p>------------
This means the text is sitting directly on the image and there's space between the image and the bottom of the image and the end of the paragraph.
If you want space between the image and the text you would add a margin-top to the image to push it down. If you want to remove the space between the image and the end of the paragraph you can either remove the padding-bottom from the paragraph tag OR give the image a negative margin-bottom
p {
margin-bottom: 20px;
background-color: aqua;
}
img {
display:block;
}
p.one {
padding: 5px 10px;
}
p.two {
padding: 5px 10px 0 10px;
// top right bottom left
}
p.two img {
margin-top: 20px;
}
What you have
<p class="one">
some text
<img src="https://via.placeholder.com/150" />
</p>
Giving the image space
<p class="two">
some text
<img src="https://via.placeholder.com/150" />
</p>
I'm not exactly sure what you are trying to do; the padding will be independent for each element you specify it for; so setting padding for the paragraph will not apply it for the image, however setting it on the image will increase the height of the paragraph probably.
You also have the option to display them side by side without them being nested so the padding of the paragraph does not affect the image in any way at all.
p {
padding: 5px 10px;
border: 1px solid red;
}
img {
padding: 0px;
height: 20px;
width: 20px;
border: 1px solid blue;
}
.img2 {
padding: 15px;
height: 20px;
width: 20px;
border: 1px solid blue;
}
.p2 {
padding: 0px;
border: 1px solid red;
}
.p3 {
padding: 15px;
border: 1px solid red;
display: inline-block;
}
.container {
border: 1px solid green;
}
<!-- Paragraph with padding and image inside with padding -->
<p>
Hello World
<img src="">
<img src="" class="img2">
</p>
<!-- Paragraph without padding and image inside without padding -->
<p class="p2">
Hello World
<img src="">
</p>
<!-- Container with paragraph with padding and image without padding as separate elements -->
<div class="container">
<p class="p3">Hello World</p>
<img src="">
</div>
Related
Is it still possible to automatically place for examples div elements among themselves if they are positioned absolut?
In the code snippet there are three divelements which are placed among themselves. Is it somehow possible to delte the css style instructions for #elem2 and #elem3 and construct a general rule that the div elements should placed among themselves with an margin of e.g. 10px? So that I can easily add new div elements (variable height) with the css class column1 and don't have to worry about the margin-top respectively margin-bottom.
#containerGraph {
position: relative;
width: 700px;
height: 400px;
border: 1px solid black;
overflow:scroll;
}
/* set the position attribute for all div elements inside the mainContainer*/
#containerGraph > div {
position: absolute;
}
.paramElement {
width: 200px;
max-height: 90px;
border-radius: 18px;
text-align: center;
background-color: #e6e6e6;
color: #4d4d4d;
font-family: Verdana, Helvetica, sans-serif;
padding-bottom:5px;
font-size: small;
}
.column1{
margin-left:10px;
}
#elem2{
margin-top : 40px;
}
#elem3{
margin-top : 117px;
}
<div id="containerGraph" class="relative">
<div class="paramElement column1" id="elem1">
first
</div>
<div class="paramElement column1" id="elem2">
second with very large text text text text text text text text text text text text text
</div>
<div class="paramElement column1" id="elem3">
third
</div>
</div>
This is a strange need, and I don't see how to solve it the way the problem is posed.
However it seems that, if an absolute position is needed, it should be relevant (and so useful) only against the containerGraph, but clearly not for each one among others.
So a solution might be to:
insert a child <div> of your containerGraph, with an absolute position
it will contain the (formerly absolute) other <div>s
You can see it working in this snippet:
$('body').click(function() {
$('#elem2').remove();
$('#sub-container').append('<div class="paramElement column1" id="elem4">fourth</div>');
});
#containerGraph {
position: relative;
width: 700px;
height: 400px;
border: 1px solid black;
overflow:scroll;
}
/* set the position attribute for all div elements inside the mainContainer*/
#containerGraph > div {
position: absolute;
}
.paramElement {
width: 200px;
max-height: 90px;
border-radius: 18px;
text-align: center;
background-color: #e6e6e6;
color: #4d4d4d;
font-family: Verdana, Helvetica, sans-serif;
padding-bottom:5px;
font-size: small;
}
.column1{
margin-left:10px;
margin-bottom: 10px;
}
/*
#elem2{
margin-top : 40px;
}
#elem3{
margin-top : 117px;
}
*/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Click anywhere to change
<hr />
<div id="containerGraph" class="relative">
<div id="sub-container">
<div class="paramElement column1" id="elem1">
first
</div>
<div class="paramElement column1" id="elem2">
second with very large text text text text text text text text text text text text text
</div>
<div class="paramElement column1" id="elem3">
third
</div>
</div>
</div>
I have a div inside another div and I want this second div to ignore the padding, the second div to have full width and no margin bottom. How can I make it work?
#first {
padding: 10px;
border: 1px solid #000;
}
#second {
background-color: red;
color: #fff;
}
<div id="first">
first div with 10px padding
<div id="second">
no padding
</div>
</div>
Inner element can compensate parents padding by using negative margin.
#second {
background-color:red;
color:#fff;
margin: -10px;
}
http://jsfiddle.net/n5yx8903/1/
wrap text in p or span tag
<div id="first">
<p>first div with 10px padding</p>
<div id="second">
no padding
</div>
</div>
set margin for element wrapping text, this will be the cleanest solution.
https://jsfiddle.net/n5yx8903/
Set margin-left and margin-right to -10px.
#second{
background-color:red;
color:#fff;
padding: 0;
margin-left: -10px;
margin-right: -10px;
}
Here is Fiddle.
Well CSS is not intended to do this,
you may do this with negative margin, but as you see in the fiddle, it will then overflow:
http://jsfiddle.net/n5yx8903/6/
#first{
padding:10px;
border:1px solid #000;
}
#second{
background-color:red;
color:#0ff;
margin:-10px;
}
Maybe this SO answer will help you understand this problem better:Why does CSS not support negative padding?
That is not doable in CSS. I think when that happens there are two options you can do.
Use negative margin to compensate the padding
Restructure the HTML
I would choose the latter one whenever possible and use negative margin as a fallback option.
#container {
border: 1px solid #000;
}
#first {
padding: 10px;
}
#second {
background-color: red;
color: #fff;
}
<div id="container">
<div id="first">
first div with 10px padding
</div>
<div id="second">
no padding
</div>
</div>
You can just put "first div with 10px padding" text into another div:
#first {
border: 1px solid #000;
}
#second {
background-color: red;
color: #fff;
}
#inner {
padding: 10px;
}
<div id="first">
<div id="inner">
first div with 10px padding
</div>
<div id="second">
no padding
</div>
</div>
You can just switch the divs
#first{
border:1px solid #000;
}
#second{
background-color:red;
color:#fff;
padding:0px 10px;
}
<div id="first">
<div id="second">
first div with 10px padding
</div>
no padding
</div>
HTML
<div id="form">
<img class="photo" src="---" />
[...]
</div>
CSS
.photo{
float: left;
}
#form {
float: right;
padding: 35px;
line-height: 1.7em;
}
Problem is that the image is attached to the div. I tryed to put margin but don't work.
Some solution?
Instead of bringing in an outside image, draw a left border on your container (div#form) using
border-left: 1px solid black;
i am creating images with a polaroid like effect using padding around the image and setting the background colour to white
img.team {
background: none repeat scroll 0 0 white;
box-shadow: 0 9px 25px -5px #000000;
display: block;
margin-left: auto;
margin-right: auto;
max-width: 100%;
padding: 12px 12px 50px;
}
what I would like to do is write a caption within the space under the image and between padding, kind of like if i had wrote on it with a felt pen. Im using bootstraps thumbnails as my images.
I have tried a negative margin on the h5
h5 {
color: #333333;
font-size: 1.1em;
margin-top: -35px;
text-align: center;
}
HTML is
<div class="container">
<div class="row">
<div class="span4">
<div class="thumbnail">
<img class="team" src="http://placehold.it/160x120" title="Swimming" alt="Swimming" ></a>
<h5>Swimming</h5>
</div><!--End of thumbnail-->
</div><!--End of span4-->
however when i place another row of polaroids underneath they overlap the top ones because of this margin.
has anyone done anything like this before? any help appreciated
Instead of styling the <img>, style div.thumbnail instead:
<div class="thumbnail">
<img class="team" src="http://placehold.it/160x120" title="Swimming" alt="Swimming" ></a>
<h5>Swimming</h5>
</div>
CSS:
div.thumbnail {
background: none repeat scroll 0 0 white;
box-shadow: 0 9px 25px -5px #000000;
padding: 12px;
display: inline-block;
}
Here is a demo for you. Note that you should remove the negative margin from the <h5> too.
I've got a page with a thumbnail on it (more to come, so the float:left property is necessary). The thumbnail is a Div, with an anchor in it, in that anchor is an image and some text. The text is below the image. The ancho rinks to a pdf file. Simple.
http://www.bridgecitymedical.com/forms
The problem is that the text gets underlined on hover when you hover over it, and the thumb image gets a border and the text gets underlined when you hover over the thumb image. What I need is for then you hover over either the text or the image, that they BOTH get applied their respective hover states, e.g. image gets a border, text gets underlined. At the moment they function by themselves, but they need to be one, or it just looks odd.
Here's the markup:
<div class="form">
<a href="/wp-content/uploads/forms/Adolescent New Patient Paperwork.pdf" target="_blank">
<div class="form-wrapper">
<div class="form-thumb">
<img src="/forms/thumbs/1.jpg" alt="" />
</div>
Caption
</div>
</a>
</div>
and the css...
.form {
margin: 30px;
font-size:.8em;
width: 137px;
text-align: center;
}
.form-thumb{
width: 125px;
height: 150px;
padding:5px;
border: 1px solid rgba(0,0,0,.2);
float:left
}
.form-thumb:hover{
border: 1px solid #000;
}
The text underline comes from another part of the tylesheet by default.
Can I do this without javascript!?
Solved by Chris (selected answer). Here's his solution, with my thumb in a fiddle...
http://jsfiddle.net/7MLjZ/1/
Use the :hover state on .form-wrapper instead.
<div class='wrap'>
<div class='thumb'></div>
Text!
</div>
.wrap{width:60px; height:80px;}
.thumb{width:60px; height:60px; background-color:blue;}
.wrap:hover{text-decoration:underline;}
.wrap:hover .thumb{border:5px solid black;}
http://jsfiddle.net/7MLjZ/
This should work to make both the image's div border and the caption's text black when hovered:
.form {
margin: 30px;
font-size:.8em;
width: 137px;
text-align: center;
}
.form-thumb{
width: 125px;
height: 150px;
padding:5px;
border: 1px solid rgba(0,0,0,.2);
float:left
}
.form:hover .form-wrapper .form-thumb {
border: 1px solid #000;
}
a:hover {
color:#000;
}