So basically i have this wrapper div, and all the elements inside it are floated elements. Only thing is the border of the wrapper div does not include the floated elements in it. Example:
<div id = "wrapper>
<div id = "content"></div>
</div>
heres the css:
#wrapper
{
width: 1000px;
margin-left: auto;
margin-right: auto;
border: 1px solid;
}
#content
{
border: 1px solid;
width: 850px;
height: 400px;
display: block;
float: left;
}
Basically #content is not enclosed within #wrapper's border, but still aligns within it, why is this?
You need to "clearfix" the container div.
Floated elements are not considered when calculating the dimensions of a container, however there are several workarounds to get what you want.
The Simplest
Just add a div like this one as the last child in your container div:
<div style="clear:both"></div>
As #Pekka comments there are many other ways to achieve the effect (without extra markup) listed in this SO question:
What methods of ‘clearfix’ can I use?
Because the point of float is to allow you to do things like
If you want to arrange elements in a row (with optional wrapping), then look to use Flexbox instead. It is a layout tool designed for that.
div {
display: flexbox;
border: 2px solid blue;
}
img {
margin: 3px;
}
<div>
<img src="https://placekitten.com/100/100" alt="">
<img src="https://placekitten.com/100/200" alt="">
<img src="https://placekitten.com/200/100" alt="">
<img src="https://placekitten.com/150/150" alt="">
<img src="https://placekitten.com/75/100" alt="">
<img src="https://placekitten.com/100/75" alt="">
<img src="https://placekitten.com/75/75" alt="">
<img src="https://placekitten.com/105/95" alt="">
</div>
If floats are the right tool for your use case, then Ed Eliot describes a large collection of different ways to make a container expand to contain it's floating children. I generally recommend setting overflow: hidden on it (to establish a new block formatting context).
div {
overflow: hidden;
border: 2px solid blue;
}
img {
float: left;
margin: 3px;
}
<div>
<img src="https://placekitten.com/100/100" alt="">
<img src="https://placekitten.com/100/200" alt="">
<img src="https://placekitten.com/200/100" alt="">
<img src="https://placekitten.com/150/150" alt="">
<img src="https://placekitten.com/75/100" alt="">
<img src="https://placekitten.com/100/75" alt="">
<img src="https://placekitten.com/75/75" alt="">
<img src="https://placekitten.com/105/95" alt="">
</div>
Related
Say I want my web page display things like this, but I want to use <figure> tag.
The problem is that simply attach the style on to the <figure> is not a perfect way.
For example, if add circular, then both of the <img> and <figcaption> will be "circulared".
<figure class="ui small right floated image circular">
<img src="/img/here.jpg">
<figcaption>Caption here...</figcaption>
</figure>
caption of the picture is "circulared" too
Can semantic-ui work like this:
<figure class="ui right floated">
<img class="image small circular">
<figcaption class="something else">
kind like inheritance?
I hope this will solve your problem with img tag and div let me know if any doubt check this code pen https://codepen.io/anon/pen/OdqKLy
html
<div class="card">
<div>
<img class="ui image circular" src="https://sample-videos.com/img/Sample-jpg-image-500kb.jpg">
</div>
<div>
Caption here...
</div>
</div>
CSS
.card {
width: 180px;
margin: 10px;
padding: 0px 10px;
text-align: center;
}
This answer is inspired by this answer.
CSS
<style>
[class*="left floated"] {
float: left;
margin-left: 0.25em;
}
[class*="right floated"] {
float: right;
margin-right: 0.25em;
}
figcaption[class*="centered"] {
margin-left: auto;
margin-right: auto;
text-align: center;
display: block;
}
</style>
Then, if you want to right float or left float the whole <figure>, just add it to the class, like below:
<figure class="right floated">
<img class="ui small bordered centered circular image" src="image.png">
<figcaption class="centered">
Caption here...
</figcaption>
</figure>
If you want the whole <figure> be centered, just leave the <figure> class blank, and the browser will render it to "center float" by default. But the centered in <img> is still necessary.
All right, this should be simple. Basically, what I'm trying to do here is create the layout for a carousel image gallery. JSFiddle is here...
http://jsfiddle.net/G5Us4/1/
CSS...
.gallery {
margin-left: auto;
margin-right: auto;
width: 600px;
background-color: #000000;
height: 300px;
overflow: hidden;
white-space: nowrap;
}
.image {
margin-right: 300px;
margin-top: 50px;
position: relative;
height: 200px;
}
#image {
margin-left: -600px;
width: 287px;
}
#image-two {
width: 231px;
}
#image-three {
width: 242px;
}
HTML...
<div class="gallery"
><img src="../images/templateone.jpg" id ="#image" class="image" alt=""
/><img src="../images/templatetwo.jpg" id="#image-two" class="image" alt=""
/><img src="../images/templatethree.jpg" id="#image-three" class="image" alt=""/>
</div>
It's so simple, and I've been afraid to ask, for fear of missing something stupidly simple. I've looked around online, and I simply cannot figure out why this simple margin is not working.
The effect I'm trying to get is that all of the images are in a horizontal line with large right margins separating them from the other images. Now that I have that done, I need to add a negative margin on the first image so that the last image starts in the center of the stage, and the other two are outside of the hidden to the left.
For some strange reason the first image does not want to go outside the image to the left. Help please!
Thanks in advanced!
P.S. end of the tags are in front of the tags to prevent a small space between the images that occurs if there is a line break in the code.
Firstly, change id="#image-three to id="image-three" in your HTML. Remove the # on image one and two also.
<img src="../images/templateone.jpg" id ="image" class="image" alt=""/>
<img src="../images/templatetwo.jpg" id="image-two" class="image" alt="" />
<img src="../images/templatethree.jpg" id="image-three" class="image" alt=""/>
Secondly, to get it how you described, just increase that negative margin.
#image {
margin-left: -920px;
width: 287px;
}
See - jsFiddle
remove # from ids in your html Code,
<div class="gallery"
><img src="../images/templateone.jpg" id ="image" class="image" alt=""
/><img src="../images/templatetwo.jpg" id="image-two" class="image" alt=""
/><img src="../images/templatethree.jpg" id="image-three" class="image" alt=""/>
</div>
On the website I'm working on (this), I have a div with an img in it. This is the html
<div><overlay> <img class="img1" height="225" src="NYC/wtc1.JPG" width="225" /></overlay</div>
<div><overlay> <img class="img2" height="225" src="NYC/wtcmem.jpg" width="225" /></overlay></div>
<div><overlay> <img class="img3" height="225" src="NYC/sky.jpg" width="225" /></overlay></div>
<p> </p>
nothing too complicated. This is the CSS for the classes img1, img2, and img3.
.img1
{
position:absolute;
left:12%;
}
.img2
{
display:block;
margin:auto;
}
.img3
{
position:absolute;
right:12%;
}
also pretty simple. But, if you look at the website, the 3rd image (at least for me on Safari) is much lower than the other two. Why would this happen? I don't see anything in the CSS or HTML that would cause this.
If you have some markup like this:
<div class="wrapper">
<div><img class="img1" height="225" src="http://rwzimage.com/albums/NYC/wtc1.JPG" width="225" /></div>
<div><img class="img2" height="225" src="http://rwzimage.com/albums/NYC/wtcmem.jpg" width="225" /></div>
<div><img class="img3" height="225" src="http://rwzimage.com/albums/NYC/sky.jpg" width="225" /></div>
</div>
Then I think this CSS will have approximately the effect you're after:
.wrapper {
display: table;
width: 960px;
}
.wrapper > div {
display: table-cell;
width: 33%;
text-align: center;
}
.wrapper > div:hover img {
opacity: 0.5;
}
Demo. I set width: 960px; so that it would force things to be wider than the JSFiddle window, but you could set width: 100%; for your page.
I've tried to do the best I can with your code, the following will work for you:
<div class="container" style="overflow:hidden; text-align:center;">
<div style="display:inline-block; margin: 0px 80px;">
<div class="overlay">
<img class="img1" height="225" src="NYC/wtc1.JPG" width="225">
</div>
</div>
<div style="display:inline-block; margin: 0px 80px;">
<div class="overlay">
<img class="img2" height="225" src="NYC/wtcmem.jpg" width="225">
</div>
</div>
<div style="display:inline-block; margin: 0px 80px;">
<div class="overlay">
<img class="img3" height="225" src="NYC/sky.jpg" width="225">
</div>
</div>
</div>
Note that <overlay> is not a valid HTML element. also I've seen on the page you used something like <margin>. It's not a good practice to invent HTML elements.. You can get all the functionality you need using regular <div>s (although I don't think this will break your page.. maybe only in older browsers..).
What I basically did:
Wrapped the three <div>s with a container with text-align:center. This will make the three divs inside it aligned to the center.
Added display:inline-block; to make all the divs follow the text-align.
Added margins to the divs to space them
Note that I strongly recommend to replace your <overlay> with something like <div class="overlay">
div tag naturally stack vertically. So you will need to add an id to each div or you could just put all the img in one div.
The block css attribute is effecting the layout. It is pushing the next img to the next line.
I have two divs with two images:
<div id="div1">
<div id="div2">
<img src="img1" />
</div>
<img src="img2" />
</div>
Second one is some smaller than first. How can I put second image on first image without using
#div2{
position: absolute;
}
I need to get similar result but without using position absolute property;
The main issue is that there are a lot of other elements, in parent div, not only div2.
Negative margins
You can do lots with negative margins. I've created an example with just two images without any divs.
img {
display: block;
}
.small {
margin: -202px 0 0 0;
border: 1px solid #fff;
}
.small.top {
position: relative;
margin: 0 0 -202px 0;
}
<img src="http://www.lorempixel.com/300/300">
<img class="small" src="http://www.lorempixel.com/200/200">
And some text
<img class="small top" src="http://www.lorempixel.com/200/200">
<img src="http://www.lorempixel.com/300/300">
And some more text
My question to you is why must you do this WITHOUT
#div2 {
position: absolute;
}
If the problem you are encountering is because it's absolute to the page and not the div then make sure #div1 has the following:
#div1 {
position:relative;
}
Its not a good approach to use negative margins. Especially when email templating, gmail reject negative margin and positions. So another way is
<div class='wrapDiv' style='width: 255px;'>
<div class='divToComeUp' style='
float: left;
margin-top: 149px; width:100%;'>This text comes above the .innerDiv
according to the amount of margin-top that you give</div>
<div class='innerDiv' style='width:100%; height:600px'>
Inner div Content
</div>
</div>
You could nest div2 inside div1:
<div id="div1">
<img src="\img1.png" />
<div id="div2">
<img src="\img1.png" />
</div>
</div>
For example if I have a parent div and 5 images I want to have centered within that div with a little margin around the images.
Use the text-align css value on the container:
div {
text-align: center;
}
And apply the margin to each of the images inside:
div img {
margin: 5px;
}
try this: http://jsfiddle.net/QrgGH/1/ substitue the divs for img nodes
<div class="container">
<div class="images">
<img src="#" alt="">
<img src="#" alt="">
<img src="#" alt="">
</div>
</div>
img{max-width: 100%; height: auto;}
.container{text-align:center;}
.images{margin: 5px;}
Read some information about block and inline elements