Basic CSS styling issue - html

I'm making a new website and in my sidebar I'm trying to add this section below but I'm struggling to get the about text to be to the right of the image. I've tried floating the text left but it didn't really work. The only CSS I've got so far is that the sidebar is 300px wide.
<div id="sidebarabout">
<h3>About Elliott Davidson</h3>
<img src="http://placehold.it/100x100"><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam rhoncus luctus odio, sed sagittis dolor volutpat ut. Pellentesque efficitur orci at nunc fermentum, nec feugiat erat gravida. Continue reading</p>
</div>

Add this css and check out
img{float:left;padding:0 15px 0 0}
https://jsfiddle.net/vasanthanvas/s4pb17tr/

Try like this
<p><img src="http://placehold.it/100x100"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam rhoncus luctus odio, sed sagittis dolor volutpat ut. Pellentesque efficitur orci at nunc fermentum, nec feugiat erat gravida. Continue reading</p>

img{float:left;margin:0 15px 0 0}
Padding will squeeze your image little.
You can use margin instead of padding.

You need to set the img and p to be either inline or inline-block on their CSS styles.
And also - depending how you want your text to "float" it may need a width:
https://jsfiddle.net/xoL510og/ << Example
<div id="sidebarabout">
<h3>About Elliott Davidson</h3>
<img style='display:inline-block;' src="http://placehold.it/100x100"><p style='display:inline-block; width:400px;'>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam rhoncus luctus odio, sed sagittis dolor volutpat ut. Pellentesque efficitur orci at nunc fermentum, nec feugiat erat gravida. Continue reading</p>
</div>

use display: table;
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
.item-inner{
display: table;
width: 100%;
padding: 20px;
}
.item{
display: table-cell;
vertical-align: top;
}
.item-text{
padding-left: 15px;
}
<div class="item-inner">
<div class="item">
<img src="http://placehold.it/100x100" />
</div>
<div class="item item-text">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam rhoncus luctus odio, sed sagittis dolor volutpat ut. Pellentesque efficitur orci at nunc fermentum, nec feugiat erat gravida. Continue reading</p>
</div>

When defining the img markup, make sure that it only affects the sidebarabout div. So define the div before img. Example:
#sidebarabout img{
float: left;
padding: 0 15px 0 0;
}
In case u declared a width in ur paragraph element, for example 200px, make sure to reduce this to 185px, since we're using a 15px padding. Otherwise it will mess up ur webpage because the div will become 315px width.
I noticed someone suggested using a margin. Don't use a margin, this will only push the image 15px to the left out of the div.
Also i'm missing a width, height and alt. Make sure to define ur img element the right way, otherwise it will not pass the W3C validator. Use the following:
<img src="http://placehold.it/100x100" width="100" height="100" alt="Description">

Related

responsive columns all same height with heading, paragraph, button all same height

I'm struggling to achieve responsive column. Currently all columns are not the same height. When the screen gets smaller, the paragraphs are not the same level, I am trying to make them the same level no matter what screen size. My next problem is the button will also not be the same level in all columns.
Bellow is currently my HTML:
<div class="container">
<div class="section">
<img src="http://via.placeholder.com/500x400" alt="" width="400" height="300" style="max-width:90%;height:auto;" />
<h2>test heading text</h2 >
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
Learn more
</div>
<div class="section">
<img src="http://via.placeholder.com/500x400" alt="" width="400" height="300" style="max-width:90%;height:auto;" /> <h2>Fencing and Gates</h2>
<h2>test heading text long</h2 >
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis vestibulum faucibus turpis, sed blandit mauris bibendum sit amet. Praesent congue enim at orci dapibus accumsan. Maecenas id leo at elit vestibulum sagittis at in ex. Cras vulputate laoreet dictum. Vestibulum nec quam placerat, blandit orci in, hendrerit ante. </p>
Learn more
</div>
<div class="section">
<img src="http://via.placeholder.com/500x400" alt="" width="400" height="300" style="max-width:90%;height:auto;" />
<h2>test heading text longer </h2 >
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis vestibulum faucibus turpis, sed blandit mauris bibendum sit amet. Praesent congue enim at orci dapibus accumsan. </p>
Learn more
</div>
<div class="section">
<img src="http://via.placeholder.com/500x400" alt="" width="400" height="300" style="max-width:90%;height:auto;" />
<h2>test heading text longer longer </h2 >
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis vestibulum faucibus turpis, sed blandit mauris bibendum sit amet. Praesent congue enim at orci dapibus accumsan. </p>
Learn more
</div>
<div class="section">
<img src="http://via.placeholder.com/500x400" alt="" width="400" height="300" style="max-width:90%;height:auto;" />
<h2>test heading text</h2 >
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis vestibulum faucibus turpis, sed blandit mauris bibendum sit amet. Praesent congue enim at orci dapibus accumsan. </p>
Learn more
</div>
<div class="section">
<img src="http://via.placeholder.com/500x400" alt="" width="400" height="300" style="max-width:90%;height:auto;" />
<h2>Block Paved Driveways and Paths</h2>
<h2>test heading text</h2 >
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis vestibulum faucibus turpis, sed blandit mauris bibendum sit amet. Praesent congue enim at orci dapibus accumsan. Maecenas id leo at elit vestibulum sagittis at in ex. Cras vulputate laoreet dictum. Vestibulum nec quam placerat, blandit orci in, hendrerit ante. </p>
Learn more
</div>
<div class="section">
<img src="http://via.placeholder.com/500x400" alt="" width="400" height="300" style="max-width:90%;height:auto;" />
<h2>test heading text</h2 >
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis vestibulum faucibus turpis, sed blandit mauris bibendum sit amet. Praesent congue enim at orci dapibus accumsan. </p>
Learn more
</div>
<div class="section">
<img src="http://via.placeholder.com/500x400" alt="" width="400" height="300" style="max-width:90%;height:auto;" />
<h2>test heading text</h2 >
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis vestibulum faucibus turpis, sed blandit mauris bibendum sit amet. Praesent congue enim at orci dapibus accumsan. </p>
Learn more
</div>
<div class="section">
<img src="http://via.placeholder.com/500x400" alt="" width="400" height="300" style="max-width:90%;height:auto;" />
<h2>test heading text</h2 >
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis vestibulum faucibus turpis, sed blandit mauris bibendum sit amet. Praesent congue enim at orci dapibus accumsan. </p>
Learn more
</div>
</div>
Below is my CSS:
.container:after { /*clear float*/
content: "";
display: table;
width: 100%;
clear: both;
}
.container {
margin-left: 100px;
margin-right: 100px;
text-align: center;
padding-top: 30px;
}
.section {
float: left;
width: 33.3333%;
padding-bottom: 50px;
border: 1px solid;
box-sizing: border-box;
display: table-cell;
}
.section p {
padding-bottom: 50px;
text-align: center;
font-family: "Montserrat", sans-serif;
font-size: 19px;
padding-left: 25px;
padding-right: 25px;
}
.section h2 {
text-align: center;
font-family: "Slabo 27px", serif;
font-size: 24px;
font-weight: 700;
text-align: center;
padding-left: 25px;
padding-right: 25px;
}
#media (max-width: 768px) {
.section {
float: none;
width: auto;
}
}
below is a js fiddle of what I currently have
https://jsfiddle.net/b147rmdh/
Any help on this would be greatly appreciated!
You need to specify a height for each column for them to all be the same height. If you don't then the columns will size themselves based on the amount of content they contain (which is currently what is happening in your code). More specifically relating to your code, if you add a specific height to your .section class, then all the columns should be the same height.
Once you have done this, you could use absolute positioning on all of the elements within the columns (the headers, paragraphs, buttons, etc) so that they all line up. If you aren't very familiar with absolute positioning and the position property in general, here is a great reference that explains positioning well. Definitely recommend giving it a read and taking the time to get a good understanding of positioning since it's one of the most fundamental CSS skills.
I'd suggest a Flexbox with wrapping.
.container {
display: flex;
flex-wrap: wrap;
}
.section {
width: 33.3333%;
}
Once you've done that, you can use a similar method vertically if you wrap the upper content in one element and keep the button separate. Set your flex-direction to column and justify-content to space-between.

Issue with height:auto and Flexbox

I'm trying to setup a section of my page with 3 columns using Flexbox.
The 3 columns are set up just fine, the issue I am having is with the section1 div not being as tall as the children elements.
I have tried height: auto, height:100%, overflow: auto, overflow:visible, etc. The only time the section1 div changes height is when I specifically state a pixel height. It seems as though the flexbox items are acting as floats so I tried a clear:both to no avail.
I have searched both stackoverflow and other sites and have not found an answer which leads me to believe it is something I am doing wrong with flexbox.
body {
background: lightgrey;
}
.body {
position: relative;
width: 75% /* 747.75px */;
margin: auto;
top: -3.5em;
background-color: white;
border-top: 3px solid #ff8400;
}
.top-border {
display: block;
position: relative;
top: 2em;
border-top: 1px solid #eef3f0;
width: 95%;
left: 2.5%;
}
.section1 {
position: relative;
display: flex;
justify-content: space-around;
top: 5em;
height: auto;
}
<div class="body">
<div class="top-border"></div>
<div class="section1">
<div class="what-i-do">
<img class="what-i-do-icon" src="images/what-i-do.png" />
<h1 class="what-i-do-title">What I Do</h1>
<p class="what-i-do-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam semper quam arcu,
a consequat tellus cursus vel. Vivamus lacus massa, feugiat non malesuada sed, efficitur eu elit. </p>
<p class="view-more-btn">View More</p>
</div>
<div class="development">
<img class="development-icon" src="images/development.png" />
<h1 class="development-title">Development</h1>
<p class="development-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam semper quam arcu,
a consequat tellus cursus vel. Vivamus lacus massa, feugiat non malesuada sed, efficitur eu elit. </p>
</div>
<div class="design">
<img class="design-icon" src="images/design.png" />
<h1 class="design-title">Design</h1>
<p class="design-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam semper quam arcu, a
consequat tellus cursus vel. Vivamus lacus massa, feugiat non malesuada sed, efficitur eu elit. </p>
</div>
</div>
</div>
See Relative positioning
Once a box has been laid out according to the normal flow or
floated, it may be shifted relative to this position. This is called
relative positioning. Offsetting a box (B1) in this way has no effect on the box (B2) that follows: B2 is given a position as if B1
were not offset and B2 is not re-positioned after B1's offset is
applied. This implies that relative positioning may cause boxes to
overlap.
Here you don't want to shift a single box, you want it to push following content too. Then, you should use margins for that.
body {
background: lightgrey;
}
.body {
width: 75% /* 747.75px */;
margin: auto;
margin-top: -3.5em;
background-color: white;
border-top: 3px solid #ff8400;
}
.top-border {
top: 2em;
border-top: 1px solid #eef3f0;
width: 95%;
margin-left: 2.5%;
}
.section1 {
display: flex;
justify-content: space-around;
margin-top: 5em;
}
<div class="body">
<div class="top-border"></div>
<div class="section1">
<div class="what-i-do">
<img class="what-i-do-icon" src="images/what-i-do.png" />
<h1 class="what-i-do-title">What I Do</h1>
<p class="what-i-do-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam semper quam arcu,
a consequat tellus cursus vel. Vivamus lacus massa, feugiat non malesuada sed, efficitur eu elit. </p>
<p class="view-more-btn">View More</p>
</div>
<div class="development">
<img class="development-icon" src="images/development.png" />
<h1 class="development-title">Development</h1>
<p class="development-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam semper quam arcu,
a consequat tellus cursus vel. Vivamus lacus massa, feugiat non malesuada sed, efficitur eu elit. </p>
</div>
<div class="design">
<img class="design-icon" src="images/design.png" />
<h1 class="design-title">Design</h1>
<p class="design-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam semper quam arcu, a
consequat tellus cursus vel. Vivamus lacus massa, feugiat non malesuada sed, efficitur eu elit. </p>
</div>
</div>
</div>

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

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>

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.

Vertical positioning of inline-blocks

This question is kind of hard to explain with words, so I will link to a jsFiddle. jsFiddle. If you look at any of the links, you will see some boxes that are at different levels. These are supposed to tile across the screen, kind of like what you can see here. How can I stop them from having the weird vertical spacing above the elements and get them to tile properly? I think that it is caused by each elements contents having different heights, but I don't know how to fix it. Here is the code that you can find in the jsFiddle:
HTML:
<div id="elements">
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec id nunc ut erat facilisis pharetra. Sed egestas gravida mattis.</div>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam eu lectus eu purus pulvinar tincidunt. Phasellus at elit id nulla volutpat gravida sit amet vitae lorem. Nunc mattis venenatis varius. Aenean nec odio lorem. Nulla in turpis sed velit venenatis lacinia eget id ante. Maecenas quis massa nunc.</div>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. </div>
</div>
CSS:
#elements div {
display:inline-block;
width:250px;
height:250px;
border:solid thin #000;
}
Add vertical-align: top; to the CSS rule.
There are lot of techniques to fix this. Either float these elements like-
#elements div {
display:inline-block;
width:250px;
height:250px;
border:solid thin #000;
float:left;
}
or vertical-align: top; buyt as i think in case of inline-block elements flaot one is more robust that'll not last at any breakpoint and also where you haven't applied resets to the body.