How do I put text and an image side by side? - html

I need to get my text and image to be next to each other on the same line, Without using tables. Float: left and right are not working.
<h1 id="profileHead">Connor Clarke - Lorem ipsum dolor sit amet.</h1>
<div id="profile">
<img id="profilePic" src="pictures/profilePicture.jpg">
<p id="profileDesc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis in justo libero. In dapibus vulputate augue at auctor. Aliquam sagittis odio quis magna ornare, at molestie neque mattis. Proin non orci ac arcu cursus tempus et ac purus. Nam aliquet.</p>
</div>
#profilePic { width: 300px; height: 300px; float: right;}
#profileDesc {float: left;}

There are several different ways I'd think up of solving this.
The solution that involves changing the width of #profileDesc does not display gracefully if the layout is too narrow (it breaks if 50% is less than 300px), so I don't recommend that one.
The recommendation of divy3993 works, but it's not the solution I'd think of, and it wouldn't be best in all circumstances--it depends what you want to do, how you want this to fit into the rest of your page.
You can achieve a similar effect by removing the one line in your css:
#profileDesc {float: left;}
And you will find it displays as you want.
Try adding a new p element, and you'll see different behavior (in the solution with the floated img outside the p, the subsequent p will continue to wrap alongside the image, in divy3993's solution, there will be a gap).
Another solution, yielding yet different behavior, is to use a containing div with position:relative, allowing you to use absolute positioning for the image, then add padding to the right side of the div so the content in it doesn't spill over onto the image. This can be useful if you want text or other content to continue down in a column that never gets wider than the image, useful if you want to put other stuff under the image in a separate column. To do this:
<h1 id="profileHead">Connor Clarke - Lorem ipsum dolor sit amet.</h1>
<div id="profile">
<div id="column">
<img id="profilePic" src="pictures/profilePicture.jpg">
<p id="profileDesc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis in justo libero. In dapibus vulputate augue at auctor. Aliquam sagittis odio quis magna ornare, at molestie neque mattis. Proin non orci ac arcu cursus tempus et ac purus. Nam aliquet.</p>
</div>
</div>
<style>
#profilePic { position: absolute; right: 0px; top: 0px; width: 300px; height: 300px; float: right;}
#column {position: relative; padding-right: 310px;}
</style>

You do not have to put your image inside the paragraph, you can do it without.
Here is a jsfiddle example
Your <p> is taking up 100% of the width, change your #profileDesc width to something like 50%. The <p> being 100% is forcing it to wrap.
#profileDesc {float: left; width: 50%;}
#profilePic { width: 300px; height: 300px; float: right;}

maybe use calc?
* {
padding: 0;
margin: 0;
}
#profile{
overflow: hidden;
}
#profilePic {
width: 300px;
height: 300px;
float: right;
margin-left: 20px;
}
#profileDesc {
width: calc(100% - 320px);
float: left;
}
<div class="b">
<img id="profilePic" src="pictures/profilePicture.jpg"/>
<p id="profileDesc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis in justo libero. In dapibus vulputate augue at auctor. Aliquam sagittis odio quis magna ornare, at molestie neque mattis. Proin non orci ac arcu cursus tempus et ac purus. Nam aliquet.</p>
<div style="clear: both"></div>
</div>

Related

Having 2 responsive Divs Side By side

So basically I am trying to make a page with 2 responsive columns, the same size side by side and instead of using px for measurement I'm sizing them using percentage. No matter what I do, setting both divs to have a margin of 5% and width of 40% they should sit side by side in a container with 'display: inline-block'. But for some reason it's not.
Here's the J-Fiddle demonstrating my issue. I have content above and below these divs on the page I'm working on... so they can't interfere with that, using, declaring float positions just seems to complicate things further.
http://jsfiddle.net/avh9s6pa/
If you guys could help I'd really appreciate it. It'll be something simple no doubt, but it's driving me nuts.
.post-reg-columns {
width: 100%;
display:block;
}
.firstcolumn {
display: inline-block;
max-width: 40%;
margin:5%;
padding:10px;
}
.firstcolumn button {
color: #fff;
background-color: #6496c8;
text-shadow: -1px 1px #417cb9;
border: none;
font-size: 1.4em;
font-family: 'Bree Serif', serif;
font-weight:bold;
width: 100%;
padding:15px;
}
.firstcolumn button:hover {
background-color: #416386;
}
.secondcolumn {
display: inline-block;
background:#fff7ca;
max-width: 40%;
margin:5%;
padding:10px;
}
<div class="post-reg-columns">
<div class="firstcolumn">
<div class="title2"> Basic Listing </div>
<button>Test Button</button>
ewfwefw efwefwefw fwefwef wefwefwef wefwefwe fwefwef wefefw efwe fefwefwefw eiofj erio jweriojgjphi owriog jerioj gerijg ejfwefwef wefj weijfwe jfjiw efjwej wef weijf hello this is a test blah blah blah! My name is Chris Mayberry and this is a test
</div>
<div class="secondcolumn">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod sollicitudin magna, sed placerat dui pretium quis. Vivamus sit amet velit nisi. Etiam consectetur mauris ligula, id fermentum felis fermentum ac. Phasellus pharetra a lorem ac dictum. Nullam vitae tempor ex. Mauris in vehicula augue. Maecenas sit amet porttitor enim, eu vehicula orci. Aliquam id nisl non sem mattis varius in sed nulla. Nulla ultrices fringilla erat, vitae tincidunt turpis malesuada vitae. Cras vehicula ex at arcu eleifend cursus. Sed varius dignissim risus eu fringilla.
</div>
</div>
As per your code, both divs have 40% width which means total 80%. Further, 5% margin which mean 5*4 =20%. So total becomes 100% here. And in your second div it has also 5% margin which force it to break as (100%+20%) there is no more space. You need to remove margin:5%; from .firstcolumn.
Check this fiddel
Remove margin:5%; from .firstcolumn class.
Demo here
Update answer
.post-reg-columns {
width: 100%;
display:block;
margin:5%
}
Demo Here

HTML: <p> tag pushes down floating <div>

I'm working on a website design (relearning HTML and CSS, as I haven't used them for several years), and I've come across what appears to be a rather simple problem, and one I'm sure I've come across before, but can't remember how to fix.
In my design, I have the main content to the left of the page, and a sidebar to the right: jsfiddle. The sidebar is float: right;, and the content container is float: right;. The problem is, which ever element appears first pushes down the other element. I want them to sit side by side next to each other.
HTML:
...
<div id='page-container'>
<aside id='sidebar'>
<div class='sidebar-item'>
<p>Sidebar</p>
</div>
</aside>
<main id='body-container'>
<h1 id='main-title'>Welcome to WebsiteName!</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent ut erat
volutpat, semper metus id, suscipit justo. Maecenas ut lacus sit amet lacus
elementum tempus. Suspendisse sit amet sem venenatis, mollis enim vel,
vehicula nisi. Phasellus sed condimentum ligula. Curabitur vehicula sem in
volutpat vulputate. Maecenas feugiat ipsum quis quam euismod lacinia. In
congue vel dui ac dignissim. Maecenas iaculis, odio fermentum tincidunt
aliquam, elit massa tristique nisl, quis fringilla nisl purus sed risus.
Cras malesuada posuere elit sed auctor. Phasellus hendrerit adipiscing
commodo.</p>
<img id='image' src='logo.svg' alt='PollardNET | Home' />
</main>
<div class='clearer'>
<!--Needed to ensure floats work correctly!-->
</div>
</div>
...
CSS:
...
#sidebar {
float: right;
margin: 100px;
margin-left: 50px;
margin-top: 50px;
width: 250px;
padding: 5px;
padding-left: 20px;
border-left-style: solid;
border-left-width: 2px;
border-left-color: #a4a4a4;
}
#body-container {
float: left;
position: relative;
margin: 100px;
margin-right: 427px;
margin-top: 50px;
}
...
.clearer {
clear: both;
}
...
If I remove any text (p tags) from the content section, the problem seems goes away. For some reason this does not work in jsfiddle, but shows in Chrome. Does anyone know how I can fix this?
Remove float: left; from your #body-container. Generally speaking you don't need to specify two floats for each side, only one float is enough. See updated JSFiddle.
You can use position: absolute and set a width for both division.
#sidebar {
...
position: absolute;
}
#body-container {
...
position: absolute;
}
Put it in span instead of p or use float on p
p {
float:left;
};
If you don't want to remove the float, you can also take the margin-right off body-container and give it a width.
Floating an element makes it display:block. By default, a block element in HTML will have a full line width. That's why the body-container pushes the sidebar down if it doesn't have a set width or if it doesn't have display: inline

CSS equal height columns with :before - unexpected background behaviour/stacking order

I'm trying to implement something based upon:
http://webdesign.tutsplus.com/tutorials/quick-tip-solving-the-equal-height-column-conundrum--cms-20403
In a nutshell, here is my test case:
http://jsfiddle.net/7CGCW/
Why are not both .panel's displayed with a green background?
All is well when my column sits atop a background-color'ed BODY (the second .panel), but when it sits atop a background-color'ed block (the first .panel, on top of .content), then the background of this DIV appears to sit atop my column, even when it is seemingly given an z-index that is lower in the stacking order.
The HTML:
<div class="content">
<div class="positioned">
<div class="panel">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam vel rutrum libero. Proin in metus id odio rutrum posuere at ac dui. Etiam sollicitudin in dui sed imperdiet. Suspendisse eu erat nec ipsum facilisis iaculis.
</div>
</div>
</div>
<div class="positioned">
<div class="panel">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam vel rutrum libero. Proin in metus id odio rutrum posuere at ac dui. Etiam sollicitudin in dui sed imperdiet. Suspendisse eu erat nec ipsum facilisis iaculis.
</div>
</div>
The CSS:
body {
background-color: red;
color: blue;
}
.content {
background-color: red;
z-index: -2;
}
.positioned {
position: relative;
}
.panel:before {
content: '';
position: absolute;
top: 0;
bottom: 0;
z-index: -1;
background-color: green;
width: 100%;
}
your content div needs to be positioned for z-index to work - read this about stacking context
at the moment it will have a z-index of 0 so it will be above your :before content
try
.content {
background-color: red;
position:relative;
z-index: -2;
}
Example
ps, that tutorial is rubbish - if he is going to use pseudo selectors to make a background be equal height then he may as well make use of the display table property for proper equal height columns
Much easier to do with less markup and styles and don't have to think about all that z-indexing and positioning

Right align content keeping left content first in code

See the following code: http://jsfiddle.net/chricholson/tyLbE/1/
HTML
<section>
<div>Secondary content</div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam dictum nunc at nibh elementum vestibulum. Curabitur nisi tortor, porttitor sed facilisis vel, volutpat in quam.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam dictum nunc at nibh elementum vestibulum. Curabitur nisi tortor, porttitor sed facilisis vel, volutpat in quam.</p>
</section>
CSS
section { border: 1px solid red; overflow: hidden; }
div { float: right; width: 48%; height: 200px; background: #eee; }
p { width: 48%; }
I have a div (BoxA) floated to the right, which allows me to keep my paragraph tags outside of any separate container. I use the overflow hack to ensure that the outer container grows to the height of either a) the paragraphs or b) BoxA.
What I'd like, is to re-order the HTML to
Paragraphs
BoxA
The obvious solution to me is CSS positioning the secondary content but this of course prevents any growth of the outer container should BoxA box be longer than the paragraph list. I'd like to avoid any kind of Javascript here to set a height, it's not THAT important just desirable from an SEO point of view.
The other solution I can think of is to wrap the paragraphs in their own container, but this feels like unnecessary markup which shouldn't really be there (it's a visual thing which should really be handled by CSS).
I'm not sure if I've got you right but it seems that, in the markup, you want to move <p>s before <div> but you want the display to be same as your fiddle, i.e., gray area on right. check this fiddle to see if it solves your problem.
update
css
section{ border: 1px solid red; overflow: hidden; }
div{
background: none repeat scroll 0 0 #EEEEEE;
border: 1px solid #000000;
display: table-cell;
height: 246px;
width: 1%;
}
p{
clear: left;
float: left;
width: 48%;
}
and the markup (unchanged)
<section>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam dictum nunc at nibh elementum vestibulum. Curabitur nisi tortor, porttitor sed facilisis vel, volutpat in quam.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam dictum nunc at nibh elementum vestibulum. Curabitur nisi tortor, porttitor sed facilisis vel, volutpat in quam.</p>
<div>
Secondary content
</div>
</section>
I would suggest you to use a html wrapper like in this example:
http://jsfiddle.net/tyLbE/4/
<section>
<div class="left">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam dictum nunc at nibh elementum vestibulum. Curabitur nisi tortor, porttitor sed facilisis vel, volutpat in quam.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam dictum nunc at nibh elementum vestibulum. Curabitur nisi tortor, porttitor sed facilisis vel, volutpat in quam.</p>
</div>
<div class="right">
Secondary content
</div>
<div class="clear"></div>
</section>
and the css:
section { border: 1px solid red; overflow: hidden; }
div.left { float: left; width: 48%; }
div.right { float: right; width: 48%; height: 200px; background: #eee; }
div.clear { clear: both; }
you can leave out the "clear", since you dont have any content after the left and right box, but this is how you can stop floating if you want to have some content below the two boxes (even if they dont have the same height)
First off, +1 to Ejay, that was certainly the outcome I was after. However, a few things worried me about the implementation (notably the display: table-cell and width: 1%. I can't really fault it, because it did work, but my gut instinct was screaming at me that something wasn't right and might catch me out in the future. It did actually slightly break, dependant on container width I'd get a 1px gap in Chrome:
Anyway, it inspired me to try harder and I came up with this: http://jsfiddle.net/z6TMJ/2/
HTML
<section>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam dictum nunc at nibh elementum vestibulum. Curabitur nisi tortor, porttitor sed facilisis vel, volutpat in quam.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam dictum nunc at nibh elementum vestibulum. Curabitur nisi tortor, porttitor sed facilisis vel, volutpat in quam.</p>
<div>Secondary content</div>
</section>
CSS
section {
border: 1px solid red;
overflow: hidden;
}
div {
background: none repeat scroll 0 0 #EEEEEE;
margin-left: 50%;
width: 50%;
height: 246px;
border: 1px solid #000;
box-sizing: border-box;
}
p {
clear: left;
float: left;
width: 50%;
}
The paragraphs are styled in the same way, but now the div is left alone to do its natural thing. It is then positioned on the right using margin-left (removing the margin will actually make the div appear behind the paragraphs.
While this may not suit ALL scenarios, where I know a few things like the width of the container and the widths of the paragraphs and secondary content this way seems fine.

Text extends out of <div> once in <p> tag?

I am trying to do something very simple. I want my text to stay contained within my <div>. I was having this problem earlier with two floating divs. I tried clearing them, and using word-wrap however that did not solve the problem. So I put that on hold then moved on to something new.
While creating another div that was not floating, I noticed once I placed the text inside the <p></p> tag, the text extended outside the div again. I know this because initially I forgot to place the text in the <p> tag, and the text stayed in the box. Once I corrected it, the text extended out again. Please help. I am pulling my hair out for what I know should be something simple.
Side Note: I do not want to use overflow:scroll;. I don't have enough text for that.
#topNews {
width: 625px;
height: 220px;
position: relative;
font-family: Arial Arial, Helvetica, sans-serif;
font-size: 14px;
border-bottom: solid #7e7d7d thin;
float: left;
margin-left: -30px;
margin-top: -15px;
}
#about {
width: 320px;
height: 180px;
position: relative;
margin-right: 3px;
margin-left: 3px;
margin-top: 7px;
border: dashed red thin;
}
<div id="topNews">
<div id="about">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse et turpis sed metus fermentum pellentesque. Vestibulum auctor neque ac nunc elementum malesuada. Praesent non est sed libero vestibulum consectetuer. Sed vehicula. Vivamus quis tellus sit amet erat ultrices luctus. Fusce a ligula. Fusce viverra libero vitae velit.
</p>
</div>
</div>
Its Gone out of div because
#topNews {
width: 625px;
height: 220px;
position: relative;
font-family: Arial Arial, Helvetica, sans-serif;
font-size: 14px;
border-bottom: solid #7e7d7d thin;
float: left;
margin-left: -30px;
margin-top: -15px;
}
In your code you specifies margin with -ve value. So its starting position may be in -ve x-axis.
Use +ve x-axis values...
like
margin-left: 0px;
margin-top: 0px;
Fiddle link
Text IS inside a div, its just the div is positioned out of you view because you used:
margin-left: -30px;
margin-top: -15px;
check out this image: click (i edited -30 and -15 to 0 and 0)
to make your job easier in future, use developer tools (f12 by default)
I just tried out your sample code and the text is contained in the DIV properly ..
I think this will helpful for you.
HTML
<div id="topNews">
<div id="about">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse et turpis sed metus fermentum pellentesque. Vestibulum auctor neque ac nunc elementum malesuada. Praesent non est sed libero vestibulum consectetuer. Sed vehicula. Vivamus quis tellus sit amet erat ultrices luctus. Fusce a ligula. Fusce viverra libero vitae velit.
</p>
</div>
<div id="about">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse et turpis sed metus fermentum pellentesque. Vestibulum auctor neque ac nunc elementum malesuada. Praesent non est sed libero vestibulum consectetuer. Sed vehicula. Vivamus quis tellus sit amet erat ultrices luctus. Fusce a ligula. Fusce viverra libero vitae velit.
</p>
</div>
</div>
CSS
#topNews{
width:625px;
height:220px;
position:relative;
font-family:Arial Arial, Helvetica, sans-serif;
font-size: 14px;
border-bottom:solid #7e7d7d thin;
}
#about{
width:200px;
float:left;
margin-right:3px;
margin-left:3px;
margin-top:7px;
padding:5px;
border: dashed red thin;
}
Perhaps you have css somewhere after that changes the size of your p tag. Also if you have anything with a width of 100% (ie: width: 100%;), it's always 100% plus the size of your margins/padding which would make elements stick out.