Adding margin-left to div after another div that has float:left - html

Sorry for the title, this is hard to explain in one sentence. Basically, I have a div container with two divs: 1 image that is float:left(ed) and another one with text. Here is what it looks like:
<div id="container">
<img src="http://boxedinfinity.com/wp-content/uploads/2013/12/Halo3.jpg" alt="Game Image" id="gameImage">
<div id="second">
<h2>Genre:</h2>
<p>First Person Shooter</p>
</div>
</div>
And here is a fiddle of what I am doing:
http://jsfiddle.net/qMQL5/880/
I want to add padding to the p part of the div with the text. As you see, I added margin-left:50px to #second p but it doesn't do anything. Basically, I want to "indent" the text "First Person Shooter" but I am having trouble doing this. Any ideas?

Update:
This is the result of your actual code now:
So, as you can see it is applying, but you have to know a few things:
img tag is an inline element
The IMG element embeds an image in the current document at the
location of the element's definition. The IMG element has no content;
it is usually replaced inline by the image designated by the src
attribute, the exception being for left or right-aligned images that
are "floated" out of line.
since you are floating the img tag it will start behaving like an inline-block element
Floating an inline or block level element will make the element behave
like an inline-block element (from here).
So, therefore, there is a few solutions to fix this issue:
Solution #1
(a quick fix [as you can see by adding a border to your #container]- I would advise to read the other solutions below)
only using you existing HTML markup and CSS:(which will make both <h1> and <p> indented)
make your img an block element by applying display:block (optional - to fix gap below img tag)
removing margin-left from p and adding margin-right to img
#container {
border:1px solid red
}
#gameImage {
width: 100px;
float: left;
height: auto;
display:block; /*new - optional*/
margin-right:10px /*new*/
}
#second {
background-color: green;
}
<div id="container">
<img src="http://boxedinfinity.com/wp-content/uploads/2013/12/Halo3.jpg" alt="Game Image" id="gameImage" />
<div id="second">
<h2>Genre:</h2>
<p>First Person Shooter</p>
</div>
</div>
Solution #2
Since you are using float already, let's go with clearfix
.clearfix:before,
.clearfix:after {
content: " ";
display: table;
}
.clearfix:after {
clear: both;
}
#container {
background-color: green;
border:1px solid red;
}
#second {
float:left;
/* background-color: green; /*choose what block will have the background*/ }
#gameImage {
width: 100px;
float: left;
height: auto;
}
#second > p {
padding-left: 10px;
box-sizing: border-box;
<div id="container" class="clearfix">
<img src="http://boxedinfinity.com/wp-content/uploads/2013/12/Halo3.jpg" alt="Game Image" id="gameImage" />
<div id="second">
<h2>Genre:</h2>
<p>First Person Shooter</p>
</div>
</div>
Solution #3
dropping the floats and using display:table/table-cell
#container {
display:table;
width:100%;
background-color:green;
border:1px solid red;
}
#container > div {
display:table-cell;
vertical-align:top;
}
#container > div:first-child, #container > div > img {
width:100px
}
#container > div > img {
display:block
}
#container > #second > p {
padding-left:10px
}
<div id="container">
<div>
<img src="http://boxedinfinity.com/wp-content/uploads/2013/12/Halo3.jpg" alt="Game Image" id="gameImage" />
</div>
<div id="second">
<h2>Genre:</h2>
<p>First Person Shooter</p>
</div>
</div>
Solution #4
using inline-block
#container {
width: 100%;
background-color: green;
font-size: 0;/* fix for inline-block elements gaps*/
border:1px solid red;
}
#container > div {
display: inline-block;
vertical-align: top;
font-size: 16px /* fix for inline-block elements gaps*/
}
#container > div:first-child,
#container > div > img {
width: 100px
}
#container > div > img {
display: block
}
#container > #second > p {
padding-left: 10px
}
<div id="container">
<div>
<img src="http://boxedinfinity.com/wp-content/uploads/2013/12/Halo3.jpg" alt="Game Image" id="gameImage" />
</div>
<div id="second">
<h2>Genre:</h2>
<p>First Person Shooter</p>
</div>
</div>
NOTE:
You may want to take a look at some articles regarding floats, here and here

Your margin on #second must be as large as your image (100px) + the padding you want. Try with margin-left: 150px you will see.

Just use the space well with giving width to each element. You can give width as px or in % . You can try this
#second {
background-color: green;
float:left;
width:80%;
}

The non-floating element actually takes the entire width of the frame.
img {
float: left;
opacity: .75;
}
div {
background: aqua;
padding-left: 50px; /*nothing happens visually*/
}
<img src="http://lorempixel.com/150/150" />
<div>Hello</div>
To fix it you could simply set overflow:auto on it.
img {
float: left;
opacity: .75;
}
div {
background: aqua;
overflow: auto;
padding-left: 50px;
}
<img src="http://lorempixel.com/150/150" />
<div>Hello</div>

I'm not exactly sure what you're trying to achieve with the green box, but If you're just looking to keep things as they are, you need to make the <p> element and inline-block element so that it respects the floating <img> element and doesn't just wrap around it.
#second p {
display: inline-block;
margin-left: 10px;
}
http://jsfiddle.net/0za19kcx/

Related

Parent div height not fits child image height

This is probably a very easy question, but I can't figure out how to make parent div element's height the same as the child image height.
Here is a plunkr example
<div id="parent">
<img src="http://pad1.whstatic.com/images/thumb/3/3d/Become-a-Ghost-Step-5.jpg/aid2076316-728px-Become-a-Ghost-Step-5.jpg">
</div>
CSS
img {
width: 100%;
}
If you inspect parent div, then you'll find that it's height is bigger than the child image's. Thanks in advance.
You need to give the image a style of display:block otherwise you will get the gap at the bottom:
#parent {
border: 1px solid black
}
#parent > img {
display: block;
width:100%;
}
<div id="parent">
<img src="http://pad1.whstatic.com/images/thumb/3/3d/Become-a-Ghost-Step-5.jpg/aid2076316-728px-Become-a-Ghost-Step-5.jpg">
</div>
Just like Pete said. Add display:block to your #parent > img. Img is an inline-block element by default.
Alternative 1
Set display: block on your <img>, like #Pete demonstrates. This is probably the preferred solution.
#parent {
border: 1px solid black
}
#parent > img {
display: block;
width:100%;
}
<div id="parent">
<img src="http://pad1.whstatic.com/images/thumb/3/3d/Become-a-Ghost-Step-5.jpg/aid2076316-728px-Become-a-Ghost-Step-5.jpg">
</div>
Alternative 2
Remove white-spacing on your #parent by either setting line-height: 0 or font-size:0.
#parent {
border: 1px solid black;
line-height: 0;
}
<div id="parent">
<img src="http://pad1.whstatic.com/images/thumb/3/3d/Become-a-Ghost-Step-5.jpg/aid2076316-728px-Become-a-Ghost-Step-5.jpg">
</div>
Alternative 3
Add a negative margin. This is a rather poor solution, but I believe it's worth mentioning.
#parent {
border: 1px solid black;
}
#parent > img {
width:100%;
margin-bottom: -4px;
}
<div id="parent">
<img src="http://pad1.whstatic.com/images/thumb/3/3d/Become-a-Ghost-Step-5.jpg/aid2076316-728px-Become-a-Ghost-Step-5.jpg">
</div>

Images not appearing on the same line with display:inline-block

In order to position two images beside each other (i.e. the two images are on the same line), I tried the following code:
<html>
<head>
<style>
#container {
width:100%;
}
img {
display:inline-block;
width:50%;
}
</style>
</head>
<body>
<h1 style="text-align:center"><strong>Test</strong></h1>
<div id="container">
<img src="Google-logo.png">
<img src="Google-logo.png">
</div>
</body>
The width of the container div should be shared equally by the two images, right? However, this does not happen and the images appear on two separate lines.
If, however, I use float:left instead, the images do appear on the same line. Why is this?
Remove the new line between the img tags:
<div>
<img src="..." alt=""><img src="..." alt="">
</div>
This happens because elements which are declared with inline or inline-block are sensitive to whitespace.
More information: on David Walsh's Blog
Commonly layouts are done with floats or flexbox instead.
Floats
/* Clearfix */
.wrapper:after {
content: '';
display: table;
clear: both;
}
.item {
float: left;
width: 50%;
height: 100px;
}
.item-1 {
background: red;
}
.item-2 {
background: blue;
}
<div class="wrapper">
<div class="item item-1"></div>
<div class="item item-2"></div>
</div>
Flexbox
.wrapper {
display: flex;
}
.item {
flex: 1; /* Or use width: 50%; */
height: 100px;
}
.item-1 {
background: red;
}
.item-2 {
background: blue;
}
<div class="wrapper">
<div class="item item-1"></div>
<div class="item item-2"></div>
</div>
try this code
#container {
max-width:100%;
display:flex;
}
img {
flex:1;
}
Give align property to images.
<img src="Google-logo.png" align="left">
<img src="Google-logo.png" align="left">
https://jsfiddle.net/bt2341yh/
use css float property:
img {
width:50%;
float:left;
}
see fiddle . no need of display.
Images give margins and padding so giving 50% width wont bring them in a line. Try reducing their widths or add float:left.
img{
display: inline-block;
width: 50%;
float: left;
}
According to the above solution it is very difficult to keep the page next to next line content. It will looks like minified version of our code. So try to add font-size:0 on your parent element #container. This will resolve this issue.
#container {
width:100%;
font-size:0;
}
DEMO

How to position div below previous div and not below the div with largest height

I have 3 divs...
if I float the divs to the left, it will look like this..
Now, I want "Div 3" to be positioned below "Div 2" like so..
So, I put
clear: both
to "Div 3" but it ended up looking like this:
"Div 3" went below the div with the largest height which, in this case, is "Div 1".. What should I do to achieve the positioning similar to that of picture 3?
You have a few options.
First, you can keep everything float: left and put a width on the parent container to prevent Div3 from being placed on the top line. The width will knock it down to the next line below Div2 as long as the paren twidth is > the width of div1 and div2.
Second, you could absolutely position the divs.
Lastly, if you dont want to do either of those, your best bet is to go with a JavaScript library like Masonry or Isotope. These libraries were created because the layout you want is very difficult to achieve in pure CSS.
I think you must use < span > for inline div.
(Cannot add comment, hence answered.)
This is what you expecting?
<div class="wrapper">
<div class="Div1"> </div>
<div class="Div2"> </div>
<div class="Div3"> </div>
</div>
.wrapper {
width: 205px;
}
.Div1 {
background: red;
width: 100px;
height:205px;
margin-bottom:5px;
float: left;
}
.Div2 {
background: green;
width: 100px;
height:100px;
margin-bottom:5px;
float: right;
}
.Div3 {
background: yellow;
width: 100px;
height:100px;
margin-bottom:5px;
float: right;
}
JSFiddle Demo
you need 2 outerdivs for that. try this JSFiddle
<body>
<div class="clearfix">
<div class="left">
<div class="div1"></div>
</div>
<div class="right">
<div class="div2"></div>
<div class="div3"></div>
</div>
</div>
</body>
CSS
.clearfix:after {
clear: both;
content: ".";
display: block;
height: 0px;
visibility: hidden;
}
.left, .right {
float: left;
}
.div1 {
background: red;
height: 200px;
width: 100px;
}
.div2, .div3 {
background: blue;
height: 100px;
width: 100px;
}
.div3 {
background: green;
}

Centering two divs containing img and h2?

I am trying to align a small image (logo) next to a heading on my page, and I want these two items to be centered (ideally, the heading would be centered, and the image would be to next to the heading). However, no matter what I try, I can't seem to make it work. Here's a sample:
<h2>Headline</h2>
<img src="logo.jpg">
Now, I have tried a couple of things here. I have tried giving the h2 a div with an id, and the image a div with another id - then giving them set widths and floating them. This at least puts them on the same line, but not in a way I want to.
I also tried to wrap those divs inside another div, like so:
#container {
width: 800px;
margin: 0 auto;
}
#h2div {
width: 40%;
float: left;
}
#imgdiv {
width: 10%;
float: left;
}
That only seems to divide the page so that the header gets 40% starting from the left, and the image gets 10% after that. I tried experimenting with z-index: -1 on the image, and if I then use text-align: center, I can center the headling. But then I have to give the picture a position:absolute or relative, which doesn't work well if the user zooms in or out..
How do I solve this? How do I get either the headline centered, and the image to display right next to it (sort of anchored to the "end" of the headline), or have the two share the center?
how about something like this:
<div id="container">
<h2>Headline</h2>
<img src="logo.jpg">
</div>
#container {
width: 800px;
margin: 0 auto;
text-align: center;
}
#container h2, #container img {
display: inline;
}
and jsfiddle - http://jsfiddle.net/Ygz4t/
img is inline element, so you should assign text-align:center; to the parent block element. Assuming you have such markup:
<div id="imgdiv">
<img src="logo.jpg">
</div>
your CSS could be like following:
#imgdiv {
text-align: center;
}
1) Wrap the h2 and img within a div (lets call it as container) and make display: inline-block to show h2 and img in same line
2) Then using text-align: center
HTML:
<div id="container">
<h2 style="display: inline-block">Headline</h2>
<img src="logo.jpg" />
</div>
CSS:
body {
width:1000px;
height: 2000px;
background: #ccc;
}
#container {
text-align: center;
width: inherit;
}
h2, img {
display: inline-block;
}
JSFiddle
HTML:
<div>
<h2>Headline</h2>
<img src="http://farm4.staticflickr.com/3694/10183642403_0c26d59769_s.jpg" />
</div>
CSS:
h2, img {
display:inline;
}
h2 {
margin: 0;
line-height: 100%;
}
img {
vertical-align: middle;
}
DEMO
I think you are trying this,
HTML
<div class="out">
<div class="inline">
<h2>TEST</h2>
</div>
<div class="inline">
<img src='http://s15.postimg.org/p4qxel6hz/agent_Photo.jpg' alt="testImage"/>
</div>
</div>
CSS
.out {
width: 800px;
margin: 0 auto;
}
.inline {
display:inline-block;
}
Updated JSFIDDLE
try this
<div id="center">
<h2>Headline</h2>
<img src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSR613FD45Dsf0nk_cJwlvpAUKsBM6JeOmNjAatjKKBHz_GFXt7rrvslw" alt="not found" />
</div>
Demo Fidle

HTML:Adjust height between br element

In the given fiddle, I want first word align to the top of the image and second aligned at the bottom of the image.
http://jsfiddle.net/himanshuy/W6ATN/15/
I have tried line-height for the span and margin for the br element but nothing seem to work.
I re-arranged some of your html. First, I put the img element and the two span elements in their own divs. So there are two inline divs. I also added a style to one of the spans. You can see the end result here: http://jsfiddle.net/W6ATN/17/
In case that fiddle fails, here is the markup:
html
<div class="box">
<div class="logo">
<div class="inlineDiv">
<img src="c:\work\img\logo3.jpg" width="80" height="80" />
</div>
<div class="inlineDiv">
<span class="spanTop">YAD</span>
<span>HIM</span>
</div>
</div>
</div>​
css
div.box {
background-color: #000000;
color: #FFFFFF;
width: 300px;
height: 400px;
text-align: center;
}
img {
margin-top:20px;
}
.inlineDiv
{
display:inline;
}
.spanTop
{
margin-top:10px;
position:absolute;
}
span {
font-size: 30px;
color: red;
clear:both;
line-height:45px;
}