Why does one div height change in px but not in percent? - html

Just practicing around with div positioning. In this code, the green colored child div (the last fourth one from left), whenever I try to increase/decrease its height in %, there is no effect. However, when I try to increase/decrease its height in pixels the div expands/contracts accordingly. Why is that? I am talking about .child4 { }
html,body {
margin: 0px;
height: 100%;
width: 100%;
left: 0px;
top: 0px;
}
.parent {
background-color: rgba(0,0,0,1);
height: 100%;
width: 100%;
position: absolute;
}
.child1 {
background-color: rgba(153,153,153,1);
height: 200%;
width: 25%;
margin-right: 2%;
margin-left: 0%;
display: block;
/* [disabled]margin-bottom: 2%; */
float: left;
position: relative;
}
.child2 {
background-color: rgba(255,0,0,1);
height: auto;
width: 25%;
margin-left: 0%;
display: block;
float: left;
position: relative;
top: 0%;
margin-top: 0%;
}
.child3 {
height: auto;
width: 25%;
background-color: rgba(0,0,255,1);
float: left;
}
.child4 {
height: 50%;
width: 20%;
background-color: rgba(0,255,0,1);
float: left;
position: relative;
top: 0%;
}
----------
<div class="parent">
<div class="child1">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vehicula quis
dolor sed euismod. Fusce id tellus est. Nam eu rutrum urna. Donec mattis a
libero faucibus euismod. Suspendisse iaculis placerat sapien in ultrices.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vehicula quis eeeeeeeeeeeeeeeeeeeeeeeee
</div>
<div class="child2">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vehicula quis
dolor sed euismod. Fusce id tellus est. Nam eu rutrum urna. Donec mattis a
libero faucibus euismod. Suspendisse iaculis placerat sapien in ultrices.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vehicula quis
</div>
<div class="child3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vehicula quis
dolor sed euismod. Fusce id tellus est. Nam eu rutrum urna. Donec mattis a
libero faucibus euismod. Suspendisse iaculis placerat sapien in ultrices.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vehicula quis
</div>
<div class="child4">
kdkldkldkdkld
</div>
</div>

You set the height to 50% but 50% of what? What value is that? It's always the value of the parent. In this case .parent.
So what is the value of .parent? You have it set to auto which leads to what value in its height for .parent? Zero.
What is 50% of zero?
Get it? Good.
So set .parent to a height of, say, 300px. Then see what you get.

It's not working, because the parent doesn't have any height.
You can't take 25% of auto, but you can take 25% of 700px for example. The parent height should be in px to be able to make the child height in %.
Edit :
Now that you added height 100% to parent, it's looking weird, because it take 100% of the snippet container.

Have you tried setting the width of the .parent to a fixed size e.g. 500px? That way you can make the height a percentage based off of the width. There does need to be some fixed attribute to base the percentage off of. This works the same for width % so if you set a fixed height you can use % on the width

I don't understand very well what you want, but I saw that you need add some fix on your CSS code to show the .parent background because you are using float:left on .child. Maybe this solve your problem. This is the fix code:
.parent:after{
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both !important;
height: 0;
}

Related

What to do to align text as shown in the image using HTML and CSS?

I'm trying to align a block of text so it looks like in the image. However, without acceptable result. The text is located in the floating div which is repeating. Same divisions are floating next to each other.
My HTML code:
<div id="bottomPanel">
<div class="bottomItem">
<img src="image.png" alt="mouse" class="imgBottom">
Lorem ipsum dolor sit amet
<p class="item_bottomAbout">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur gravida, massa ut suscipit suscipit, massa elit sollicitudin eros, nec lacinia neque odio a est. Phasellus tincidunt nulla eget lorem sodales</p>
</div>
</div>
My CSS code:
#bottomPanel {
float: left;
width: 100%;
text-align: center;
}
.bottomItem {
float: left;
width: 100%;
margin-top: 10px;
background-color: #e6e6e6;
}
.imgBottom {
float: left;
padding-top: 25px;
padding-left: 14px;
}
.item_bottomDesc {
float: left;
font-family: Arial;
font-weight: bold;
font-size: 12px;
text-decoration: none;
color: #133855;
padding-top: 42px;
padding-left: 18px;
display: block;
}
.item_bottomAbout {
float: left;
font-family: Arial;
font-size: 12px;
width: 376px;
height: auto;
text-align: justify;
display: block;
padding-top: 50px;
color: #7b7a79;
}
This is how the elements should look like:
The text I can't align properly is the long one.
Division has 100% width which is approx. 774px. bottomPanel is a div where all floating .bottomItem are located.
Any ideas?
So not much wrong here overall. Just that you have a few extra things you don't need.
Namely, bottomDesc and bottomAbout do not need to be floated, and the padding-top for those two elements need adjusting too.
I have also added an explicit width/height for the image, feel free to adjust as needed but it's a good idea to define it. I also replaced its padding with appropriate margins.
Finally, I removed the #bottomPanel styles altogether - again float is not needed here, and width:100% is default anyway. Also, the text-align:center was unnecessary.
Here's the revised code for you to look at:
.bottomItem {
float: left;
width: 100%;
margin-top: 10px;
background-color: #e6e6e6;
}
.imgBottom {
float: left;
margin: 14px;
width: 100px;
height: 100px;
}
.item_bottomDesc {
font-family: Arial;
font-weight: bold;
font-size: 12px;
text-decoration: none;
color: #133855;
padding-top: 14px;
padding-left: 18px;
display: block;
}
.item_bottomAbout {
font-family: Arial;
font-size: 12px;
width: 376px;
height: auto;
text-align: justify;
display: block;
}
<div id="bottomPanel">
<div class="bottomItem">
<img src="http://placehold.it/100x100" alt="mouse" class="imgBottom">
Lorem ipsum dolor sit amet
<p class="item_bottomAbout">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur gravida, massa ut suscipit suscipit, massa elit sollicitudin eros, nec lacinia neque odio a est. Phasellus tincidunt nulla eget lorem sodales</p>
</div>
</div>
Instead of float i suggest you use flexbox for this, and you dont have to use padding to get vertical align.
.bottomItem {
display: flex;
flex-direction: row;
align-items: center;
margin: 10px;
}
.right {
max-width: 60%;
margin: 0 10px;
}
<div id="bottomPanel">
<div class="bottomItem">
<img src="http://placehold.it/150x150">
<div class="right">
Lorem ipsum dolor sit amet
<p class="item_bottomAbout">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur gravida, massa ut suscipit suscipit, massa elit sollicitudin eros, nec lacinia neque odio a est. Phasellus tincidunt nulla eget lorem sodales</p>
</div>
</div>
<div class="bottomItem">
<img src="http://placehold.it/150x150">
<div class="right">
Lorem ipsum dolor sit amet
<p class="item_bottomAbout">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur gravida, massa ut suscipit suscipit, massa elit sollicitudin eros, nec lacinia neque odio a est. Phasellus tincidunt nulla eget lorem sodales</p>
</div>
</div>
<div class="bottomItem">
<img src="http://placehold.it/150x150">
<div class="right">
Lorem ipsum dolor sit amet
<p class="item_bottomAbout">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur gravida, massa ut suscipit suscipit, massa elit sollicitudin eros, nec lacinia neque odio a est. Phasellus tincidunt nulla eget lorem sodales</p>
</div>
</div>
</div

HTML - Vertically align responsive content

I am trying to vertically align content inside of a block - the content is an image on left (no text under it) and a paragraph of text.
When the text is on a large screen, the height of the paragraph is smaller than the height of the image and thus the paragraph should be vertically aligned to middle. On the other hand, while the content is displayed on smaller screen, the paragraph's height is bigger than the image and the image should be vertically aligned.
Centering the image is easy, because I know the height, but how to handle responsive paragraph? Everything i have tried resulted in some weird behavior - for example blocks overlapping each other when on a small screen. So I start to thing that its not possible by CSS and I need some Javascript for it.
HTML:
<div class="entry">
<img src="http://placehold.it/100x100" alt="" />
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus eu rhoncus eros. Nunc ac mollis velit. Cras vitae odio fringilla, consectetur felis id, maximus quam. Nulla placerat felis nec malesuada mattis. </p>
</div>
<div class="entry">
<img src="http://placehold.it/100x100" alt="" />
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus eu rhoncus eros. Nunc ac mollis velit. Cras vitae odio fringilla, consectetur felis id, maximus quam. Nulla placerat felis nec malesuada mattis.</p>
</div>
CSS:
div.entry {
margin-bottom: 10px;
min-height: 100px;
padding: 0 10px 0 120px;
position: relative;
}
div.entry img {
bottom: 0;
left: 0;
margin: auto 0;
position: absolute;
right: 0;
top: 0;
width: 100px;
}
The example can be found here: http://jsfiddle.net/46psK/2583/
Solved using Flexible box method. Set the parent container as flex and align the items inside to be centered along the cross-axis
div.entry {
margin-bottom: 10px;
min-height: 100px;
padding: 0 10px 0 120px;
position: relative;
display: flex;
align-items: center;
}
div.entry img {
bottom: 0;
left: 0;
margin: auto 0;
position: absolute;
right: 0;
top: 0;
width: 100px;
}
<div class="entry">
<img src="http://placehold.it/100x100" alt="" />
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus eu rhoncus eros. Nunc ac mollis velit. Cras vitae odio fringilla, consectetur felis id, maximus quam. Nulla placerat felis nec malesuada mattis.</p>
</div>
<div class="entry">
<img src="http://placehold.it/100x100" alt="" />
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus eu rhoncus eros. Nunc ac mollis velit. Cras vitae odio fringilla, consectetur felis id, maximus quam. Nulla placerat felis nec malesuada mattis.</p>
</div>
Try using display: table-cell. The modified css is as below.
div.entry {
margin-bottom: 10px;
min-height: 100px;
padding: 0 10px;
display: table;
}
div.entry img, div.entry p {
display: table-cell;
vertical-align: middle;
}
div.entry img {
margin:0 10px 0 0;
width: 100px;
}

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>

Centering an icon on top of an element's border

I'm trying to overlay an icon on top of an element's border. My current solution involves absolute positioning. I can hack it to fit as close to center as possible by using something like left: 40% but as soon as I resize the window, it moves out of the center.
Here's a JSFiddle showing what I've got so far. You'll see that if you resize the window, the icon moves out of center. https://jsfiddle.net/83on2jr9/
Is there an easier approach to this?
You could use margin:0 auto; with position:absolute; - providing that you have some other values set:
.landing-section2 .landing-icon {
position: absolute;
top:-16px;
right:0;
bottom:0;
left:0;
width:50px;
height:50px;
margin:0 auto;
}
JSFiddle
You can use calc in the .landing-section2 .landing-icon class :
left: calc(50% - 32px);
JSFiddle
Use a CSS transform. This is responsive and works for any size element and doesn't require any magic number for widths and margins.
.landing-section2 .landing-icon {
color: #357ca3;
font-size: 3em;
background: #2c2c2c;
z-index: 1000;
position: absolute;
padding-left: 10px;
padding-right: 10px;
left: 50%;
top: 0;
transform:translate(-50%,-50%);
}
JSfiddle Demo
Support is IE9 and up CanIUse.com
I find that when using absolute positioning, it's easier to use it as included in the JSFiddle I updated below. Basically, I wrap the "icon" in a span and attain much greater control.
.landing-section2 .landing-icon {
color: #357ca3;
font-size: 3em;
z-index: 1000;
position: absolute;
top: -28px;
left: 0;
width: 100%;
text-align: center;
}
.landing-icon span {
display: inline-block;
padding: 8px;
background: #2c2c2c;
}
Here is the updated Fiddle with working code: https://jsfiddle.net/83on2jr9/7/
I think, put 'margin-left: -32px' is easy way to move it to center without changing many other options.
also, it moves dynamically.
you can use display and margin too without position :) https://jsfiddle.net/83on2jr9/10/
.landing-section2 {
padding: 50px;
background-color: #2c2c2c;
text-align: center;
}
.landing-section2 .col-sm-4 > div {
border: 1px solid #357ca3;
padding: 20px;
position: relative;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
margin-bottom:2em;
}
.landing-section2 h3 {
color: white;
margin-top: 30px;
}
.landing-section2 p {
color: #ccc;
}
.landing-section2 .landing-icon {
color: #357ca3;
font-size: 3em;
background: #2c2c2c;
display:table;
margin:-1em auto 0;
padding:0 5px;
}
<div class='landing-section2'>
<div class='container'>
<div class='row'>
<div class='col-sm-4 landing-section2-pillar'>
<div>
<div class='landing-icon'>#</div>
<h3>
Section 1
</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas nunc nulla, fringilla et auctor et, congue sit amet nibh. Aenean vel est ante. Suspendisse quis tortor laoreet ligula vehicula commodo. Morbi suscipit, neque id vulputate mollis, orci sapien aliquam sem, ac laoreet ex nisi id leo.</p>
</div>
</div>
<div class='col-sm-4 landing-section2-pillar'>
<div>
<div class='landing-icon'>#</div>
<h3>
Section 2
</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas nunc nulla, fringilla et auctor et, congue sit amet nibh. Aenean vel est ante. Suspendisse quis tortor laoreet ligula vehicula commodo. Morbi suscipit, neque id vulputate mollis, orci sapien aliquam sem, ac laoreet ex nisi id leo.</p>
</div>
</div>
<div class='col-sm-4 landing-section2-pillar'>
<div>
<div class='landing-icon'>#</div>
<h3>
Section 3
</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas nunc nulla, fringilla et auctor et, congue sit amet nibh. Aenean vel est ante. Suspendisse quis tortor laoreet ligula vehicula commodo. Morbi suscipit, neque id vulputate mollis, orci sapien aliquam sem, ac laoreet ex nisi id leo.</p>
</div>
</div>
</div>
</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.