Why are relative divs not generating height? - html

I have a 2 column grids. In column 1, there will be multiple instances of one div. Inside the one div there are two divs positioned absolutely - a number and a description. For some reason when I have multiple divs, they stack ontop of one another, which is because the 'template' div has no height.
Why a height is not automatically generated by the content within that div?
.main {
position: relative;
margin: 25px 0;
display: block;
}
.col1 {
width: 200px;
position: absolute;
left: 0;
}
.col2 {
width: 200px;
position: absolute;
right: 0;
}
.indicator {
width: 10px;
height: 10px;
background-color: #0f0;
color: #454;
position: absolute;
left: 0;
}
.text {
position: absolute;
right: 0;
}
.box {
position: relative;
}
<div class="main">
<div class="col1">
<div class="box">
<div class="indicator">
1
</div>
<div class="text">
Text text text text text text text text text text text text text text text text text text text text text text text text text.
</div>
</div>
<div class="box">
<div class="indicator">
1
</div>
<div class="text">
Text text text text text text text text text text text text text text text text text text text text text text text text text.
</div>
</div>
<div class="box">
<div class="indicator">
1
</div>
<div class="text">
Text text text text text text text text text text text text text text text text text text text text text text text text text.
</div>
</div>
<div class="box">
<div class="indicator">
1
</div>
<div class="text">
Text text text text text text text text text text text text text text text text text text text text text text text text text.
</div>
</div>
</div>
<div class="col2">
Col 2 content.
</div>
</div>
​Fiddle:
http://jsfiddle.net/techydude/UcFXX/1/
This is a very basic question, but I would rather understand the reasoning behind the issue without creating a work around by adding height.

Absolute positioning removes the element from the "flow", so as far as its parent is concerned, it's not really there and it isn't calculated as part of the parent's height.

Related

How to have <div>'s on the same row/line HTML

I am trying to make it so one <div> is on the same line/row another <div> by default they just appear below each other. I have found out that this only happens when the text is multiple lines. Here is the code:
h1 {
text-align: center;
}
h2 {
text-align: left;
}
.info {
text-align: left;
font-size: 20px;
float: right;
}
.content {
align-self: auto;
border: 1px solid black;
color: gray;
width: 150px;
max-width: 150px;
float: left;
}
.p1 {
text-align: left;
margin-left: 10px;
}
<h1>Good Health and Well being</h1>
<div class='info'>
<h2><b>What do good health and well-being mean?</b></h2>
<p>TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT</p>
<h2><b>Why are good health and well-being so important?</b></h2>
<p>TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT</p>
<h2><b>How do you keep good health?</b></h2>
<p>TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT</p>
</div>
<div class='content'>
<h1>Content</h1>
<p class='p1'>content</p>
</div>
Here is an image of it.
And here is an image when the text doesn't use multiple lines:
Lastly here is an image of what i want
Reason this is happening is because that having multilines increases the width of your info div. It keeps on adjusting till it becomes (100%-150px) 150 being the width of your content div. Adding width to info will solve the issue , add the width such to give margin in between as I have updated the css.
h1 {
text-align: center;
}
h2 {
text-align: left;
}
.info {
text-align: left;
font-size: 20px;
float: right;
width: calc(100% - 200px)
}
.content {
align-self: auto;
border: 1px solid black;
color: gray;
width: 150px;
max-width: 150px;
float: left;
}
.p1 {
text-align: left;
margin-left: 10px;
}
<h1>Good Health and Well being</h1>
<div class='info'>
<h2><b>What do good health and well-being mean?</b></h2>
<p>TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT </p>
<h2><b>Why are good health and well-being so important?</b></h2>
<p>TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEX </p>
<h2><b>How do you keep good health?</b></h2>
<p>T TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT </p>
</div>
<div class='content'>
<h1>Content</h1>
<p class='p1'>content</p>
</div>
The div element is a block level element, but there is a way you can bypass that while styling it through css.
Firstly, you'd give the divs the same id attribute since they have different classes. Then through the css file you could add the float attribute, whose values can be either left or right.
So it could look something like this
<h1>Good Health and Well being</h1>
<div class='info' id="box">
<h2><b>What do good health and well-being mean?</b></h2>
<p>TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT</p>
<h2><b>Why are good health and well-being so important?</b></h2>
<p>TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT</p>
<h2><b>How do you keep good health?</b></h2>
<p>TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT</p>
</div>
<div class='content' id="box">
<h1>Content</h1>
<p class='p1'>content</p>
</div>
#box {
float: left;
}
Playing around with the size (width and height) of each individual div can help you make them fit nicely. Or an alternative is using the position attribute, but I find using float much less of a pain.
There are multiple ways you can inline div's side by side. Using float can be tricky and will require clearing of div elements to stack properly.
There are some good new properties of CSS for setting Grids of elements. One in the example below is using the flex property on the parent container of both div's you want to be side by side. There is another CSS style property grid, which can be a little difficult to understand at first but its best when you get used to it.
<!DOCTYPE html>
<html>
<body>
<style>
.flex {
display: inline-flex;
flex-flow: nowrap;
justify-content: space-between;
}
.info,.content {
width: 49%;
}
.content{
background: lightgray;
padding: 1rem
}
</style>
<div class="flex">
<div class='info'>
<h2>
<b>What do good health and well-being mean?</b>
</h2>
<p>TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT</p>
<h2>
<b>Why are good health and well-being so important?</b>
</h2>
<p>TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT</p>
<h2>
<b>How do you keep good health?</b>
</h2>
<p>TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT</p>
</div>
<div class='content'>
<h1>Content</h1>
<p class='p1'>content</p>
</div>
</div>
</body>
</html>
Hope this helps you.

Vertically centered bootstrap div overflows the window upon resize -- how to prevent?

I have a standard Bootstrap grid layout that's been vertically centered.
However, upon window resize, the div content overflows the window, obscuring the top div content. How can I prevent this?
Please see minimal example here: http://jsfiddle.net/8pkubef7/
<div style="width:50%;position:absolute;top:50%;left:25%; transform: translateY(-50%);">
<div class="container">
<div class="row">
<div class="col">
<h1> Header 1 </h1>
</div>
</div>
<div class="row">
<div class="col-md-12">
<p> Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text</p>
<p> Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text </p>
<p> Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text </p>
</div>
</div> <!-- end row -->
</div> <!-- end container -->
</div> <!-- end vertical center div -->
Try this:
<div class="container">
<div class="floating">
<div class="row">
<div class="col">
<h1>
Header 1
</h1>
</div>
</div>
<div class="row">
<div class="col-md-12">
<p> Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text</p>
<p> Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text </p>
<p> Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text </p>
</div>
</div>
css:
.row {
background: #f8f9fa;
margin-top: 20px;
}
.col {
border: solid 1px #6c757d;
word-break: break-all;
}
.container {
position:relative;
}
.floating {
float: left;
position: absolute;
left:25%;
right:25%;
top: 50%;
}
http://jsfiddle.net/c6eyq2rn/

Place text on the right side of the div

I would like my text to remain centered by I would like to move it to the right side of the div. My div is covered with an image background.
I tried changing:
<div class="col-sm-12">
to:
<div class="col-sm-12" style="float:right;">
but it didn't have any effect. Is there a way for me to do this within bootstrap or do I have to use my custom css? And how can this be done?
HTML:
<div id="box" class="vcenter">
<div class="container">
<div class="row center">
<div class="col-md-12">
<div class="row">
<div class="col-sm-12">
<p>Text text text text text text text text text ext text text text text text text text/p>
<p>Text text text text text text text text text ext text text text text text text text/p>
<p>Text text text text text text text text text ext text text text text text text text/p>
</div>
</div>
</div>
</div>
</div>
</div>
CSS:
.vcenter {
display: flex;
align-items: center;
}
#box {
background: url("url here") no-repeat center;
height: 500px;
}
.center {
text-align: center;
}
JSFiddle Demo
You can use bootstrap's grid system
For example like this if your left and right side (column) should be same width
<div class="col-xs-6">
<p>Something left</p>
</div>
<div class="col-xs-6 center">
<p>Something right</p>
</div>
Full example is here: https://jsfiddle.net/woeote07/3/
If you don't need left column you can use offset. Just use latest bootstrap version for this because some older ones doesn't support offset for all column types.
<div class="col-xs-6 col-xs-offset-6 center">
Example here: https://jsfiddle.net/woeote07/5/
Your text is centered right now. If you want it to be on the right side of the page, you can't do the 'col-sm-12', as that takes up the entire row.
Instead, do something like this:
<div class="col-sm-6"><p>the left side</p></div>
<div class="col-sm-6"><p>Text text text text text text text text</p></div>
Then if you want it centered inside the columns, just assign text-align: center; to them.
Weave: http://kodeweave.sourceforge.net/editor/#0b6cf679309a05c75de936bc691576ca
Forked Fiddle: https://jsfiddle.net/woeote07/4/
I would like my text to remain centered by I would like to move it to the right side of the div.
First off .col-sm-12 has a width 100% by default (being the same width as it's parent). Thus you can change it to 50% and then you can use float to have your text on the right side.
.col-sm-12 {
float: right;
width: 50%;
}
Here's a simple snippet!
.vcenter {
display: flex;
align-items: center;
}
#box {
background: url("http://2.bp.blogspot.com/-WEArLbKazLg/Ujg2PL6NG7I/AAAAAAAABSo/UZE_Z-UwPVQ/s1600/Simple-white-light-web-vector-design-image-wallpaper.jpg") no-repeat center;
height: 500px;
}
.center {
text-align: center;
}
.col-sm-12 {
float: right;
width: 50%;
}
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<div id="box" class="vcenter">
<div class="container">
<div class="row center">
<div class="col-md-12">
<div class="row">
<div class="col-sm-12">
<p>Text text text text text text text text text ext text text text text text text text</p>
<p>Text text text text text text text text text ext text text text text text text text</p>
<p>Text text text text text text text text text ext text text text text text text text</p>
</div>
</div>
</div>
</div>
</div>
</div>
One way to accomplish this with bootstrap would be to add a separate div that maybe only had a span of 2. This would essentially push your current div to the right side of the screen.
<div class="row">
<div class="col-sm-2">
<p>First Text Area</p>
<p>First Text Area</p>
<p>First Text Area</p>
</div>
<div class="col-sm-10">
<p>Text text text text text text text text text ext text text text text text text text</p>
<p>Text text text text text text text text text ext text text text text text text text</p>
<p>Text text text text text text text text text ext text text text text text text text</p>
</div>
</div>
Plunker Example
Otherwise an easy solution would be to add a margin-left attribute to the style of the div like so.
<div class="col-sm-12" style="margin-left: 60px;">
<p>Text text text text text text text text text ext text text text text text text text</p>
<p>Text text text text text text text text text ext text text text text text text text</p>
<p>Text text text text text text text text text ext text text text text text text text</p>
</div>
Plunker Example 2
col-sm-12 can't float if it has the same width as its parent. Specify a width (absolute or percentage), and it can float.
Like this: new Fiddle
.vcenter {
display: flex;
align-items: center;
}
#box {
background: url("http://2.bp.blogspot.com/-WEArLbKazLg/Ujg2PL6NG7I/AAAAAAAABSo/UZE_Z-UwPVQ/s1600/Simple-white-light-web-vector-design-image-wallpaper.jpg") no-repeat center;
height: 500px;
}
.center {
text-align: center;
}
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<div id="box" class="vcenter">
<div class="container">
<div class="row center">
<div class="col-md-12">
<div class="row">
<div class="col-sm-12" style="float:right; width:50%">
<p>Text text text text text text text text text ext text text text text text text text</p>
<p>Text text text text text text text text text ext text text text text text text text</p>
<p>Text text text text text text text text text ext text text text text text text text</p>
</div>
</div>
</div>
</div>
</div>
</div>

How can I align multi-line text next to an image without it wrapping underneath?

I've successfully aligned one line of text with an image (fiddle).
<div class="profile-details-wrapper">
<img class="profile-picture" src="http://placehold.it/50x50" />
<span class="profile-details">
username
</span>
</div>
However, when I try to add another line of text, it wraps under the image (fiddle).
<div class="profile-details-wrapper">
<img class="profile-picture" src="http://placehold.it/50x50" />
<span class="profile-details">
username
<br />
username
</span>
</div>
How can I have multi-line text that exceeds the height of the image next to it, but does not wrap underneath it?
Bonus question: How would I go about aligning it vertically, too?
Building on #freestock.tk's tabular example..
.profile-details-wrapper { display: table-row; }
.profile-picture {
display: table-cell;
vertical-align: top;
margin-right: 10px;
}
.profile-details { display: table-cell; vertical-align: top; }
<div class="profile-details-wrapper">
<img class="profile-picture" src="http://placehold.it/50x50">
<div class="profile-details">
text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here
</div>
</div>
img {
float:left;
margin-top: 5px;
margin-right: 15px;
margin-bottom: 15px;
}
p {
display: table-cell;
width: 400px;
}
<div class="profile-details-wrapper">
<img class="profile-picture" src="http://placehold.it/50x50" />
<span class="profile-details">
<p>text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here </p>
</span>
</div>
How about giving a float: left; tag to both the picture and the description. I don't Know if that creates complications in your website but that way it will keep all the text on the left side
This is a bit hackish, but it should work...
.profile-picture {
display: block;
margin: 0 10px 10px 0;
float: left;
}
.profile-details { float: left; width: calc(100% - 50px - 10px); }
<div class="profile-details-wrapper">
<img class="profile-picture" src="http://placehold.it/50x50">
<div class="profile-details">
text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here text here
</div>
</div>

how do I get the bullet points of a <ul> to center with the text?

When I try to center a <ul> the text in the <li> centers but the bullet points stay on the far left of the page. Is there any way to get the bullet points to stay with the text when it is centered?
#abc{text-align: center; }
<div id="section1">
<div id="abc">
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
<div/>
<div/>
Add list-style-position: inside to the ul element. (example)
The default value for the list-style-position property is outside.
ul {
text-align: center;
list-style-position: inside;
}
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
Another option (which yields slightly different results) would be to center the entire ul element:
.parent {
text-align: center;
}
.parent > ul {
display: inline-block;
}
<div class="parent">
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
</div>
You can do that with list-style-position: inside; on the ul element :
ul {
list-style-position: inside;
}
See working fiddle
TL;DR
ul {
padding-left: 0;
list-style-position: inside;
}
Explanation:
The first property padding-left: 0 clears the default padding/spacing for the ul element while list-style-position: inside makes the dots/bullets of li aligned like a normal text.
So this code
<p>The ul element</p>
<ul>
asdfas
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
without any CSS will give us this:
but if we add in the CSS give at the top, that will give us this:
I found the answer today. Maybe its too late but still I think its a much better one. Check this one https://jsfiddle.net/Amar_newDev/khb2oyru/5/
Try to change the CSS code : <ul> max-width:1%; margin:auto; text-align:left; </ul>
max-width:80% or something like that.
Try experimenting you might find something new.
Here's how you do it.
First, decorate your list this way:
<div class="p">
<div class="text-bullet-centered">⁕</div>
text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text
</div>
<div class="p">
<div class="text-bullet-centered">⁕</div>
text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text
</div>
Add this CSS:
.p {
position: relative;
margin: 20px;
margin-left: 50px;
}
.text-bullet-centered {
position: absolute;
left: -40px;
top: 50%;
transform: translate(0%,-50%);
font-weight: bold;
}
And voila, it works. Resize a window, to see that it indeed works.
As a bonus, you can easily change font and color of bullets, which is very hard to do with normal lists.
.p {
position: relative;
margin: 20px;
margin-left: 50px;
}
.text-bullet-centered {
position: absolute;
left: -40px;
top: 50%;
transform: translate(0%, -50%);
font-weight: bold;
}
<div class="p">
<div class="text-bullet-centered">⁕</div>
text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text
</div>
<div class="p">
<div class="text-bullet-centered">⁕</div>
text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text
</div>