I get a flexible count of elements out of a database and put them into a list.
There is a container below the list. This container should take the rest height of the parent container.
On the code below you can see my current result. the Div in #rightContent should take the complete rest height. But i dont know how.
Html:
<section>
<aside>
I'm a sidebar!
</aside>
<main>
<article id="leftContent">
<img src="http://fs1.directupload.net/images/150422/6x5qv8ik.png">
</article>
<article id="rightContent">
<h1>Headline</h1>
<hr>
<ul>
<li>list1</li>
<li>list2</li>
<li>list3</li>
</ul>
<div>
<p>
a lot of text
</p>
</div>
</article>
</main>
</section>
CSS:
html, body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
section {
width: 100%;
height: 100%;
}
section aside {
position: fixed;
right: 50px;
width: 150px;
background-color: firebrick;
color: white;
padding: 25px;
height: 100%;
}
section main {
position: relative;
margin-right: 250px;
left: 0;
display: block;
width: auto;
border: 1px solid yellow;
}
section #leftContent {
position: relative;
display: block;
border: 1px solid green;
padding: 0;
margin: 0;
height: auto;
width: 50%;
}
section #leftContent img {
width: 100%;
height: auto;
display: block;
}
section #rightContent {
position: absolute;
display: inline-block;
border: 1px solid blue;
right: 0;
margin-left: 50%;
width: 49%;
height: 100%;
top: 0;
}
section #rightContent div {
border: 1px solid violet;
position: relative;
margin-bottom: 0;
}
http://jsfiddle.net/kzkpLg11/
This image shows what i want.
You could do it like this: http://jsfiddle.net/kzkpLg11/1/
Involves a few extra elements but the browser support will be ok. It's basically all about this piece of CSS. But I suggest you have a look at the fiddle.
.table {
display: table;
width: 100%;
height: 100%;
}
.row {
display: table-row;
}
.cell {
display: table-cell;
}
You could also solve this problem with Flexbox. But that might be a little trickier.
Related
I wonder why mainCountainerHeadLogo does not stretch parent div mainCountainerHead height?
If I scale the page, both mainCountainerHeadTitle and mainCountainerHeadMenu stretch mainCountainerHead just fine.
Sorry for my english and thanks in advance!
http://jsfiddle.net/gvcs0r6b/
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
}
.mainCountainer {
min-height: 100%;
width: 70%;
margin: 0 auto;
}
.mainCountainerHead {
background-color: aqua;
height: auto;
}
.mainCountainerHeadLogo {
height: 100px;
width: 20%;
background-color: blue;
float: left;
position: relative;
overflow: hidden;
}
.mainCountainerHeadLogo img {
position: absolute;
width: 100%;
height: auto;
top: -50%;
right: -50%;
bottom: -50%;
left: -50%;
margin: auto
}
.mainCountainerHeadTitle{
margin-left: 20%;
width: 80%;
height: auto;
text-align: center;
padding-top: 3%;
}
.mainCountainerHeadMenu{
margin-left: 20%;
text-align: center;
background-color: orange;
width: 80%;
height: auto;
padding-top: 2%;
text-align: center;
}
.mainLink {
display: inline-block;
padding: 5px;
}
.mainLinkButton {
width: 90px;
height: 30px;
background-color: green;
font-size: 16px;
border: none;
color: white;
padding: 5px;
}
.mainLinkButton:hover {
background-color: darkgreen;
}
.mainLinkDropdown {
position: relative;
display: inline-block;
padding: 5px;
}
.dropdownContent {
display: none;
position: absolute;
min-height: 30px;
min-width: 130px;
text-align: left;
background-color: #f1f1f1;
z-index: 10;
}
.dropdownContent a {
display: block;
color: black;
padding: 12px 16px;
text-decoration: none;
}
.mainLinkDropdown:hover .dropdownContent{
display: block;
}
.dropdownContent a:hover{
background-color: #ddd;
}
<div class="mainCountainer">
<div class="mainCountainerHead">
<div class="mainCountainerHeadLogo">
<img src="https://i.ibb.co/cYzWJFM/logo-Copy.jpg" title="logo" />
</div>
<div class="mainCountainerHeadTitle">
<h4>Welcome aboard!</h4>
</div>
<div class="mainCountainerHeadMenu">
<div class="mainLink">
<button class="mainLinkButton">Main</button>
</div>
<div class="mainLinkDropdown">
<button class="mainLinkButton">Dropdown</button>
<div class="dropdownContent">
Link 1
Link 2
Link 3
</div>
</div>
<div class="mainLink">
<button class="mainLinkButton">Contacts</button>
</div>
</div>
</div>
</div>
In answer to your question:
That's because the float property puts the HTML elements out of the normal page flow, and this causes what you're experiencing. Its effect is similar to position: absolute which is to move the element to "a different layer".
How to solve it?
Well... there are a lot of ways to achieve what you want, and almost all of them requires to refactorize your code. Actually, you have a lot of code that makes it difficult to achieve your goal. You should get rid of float and start using other technics like Flexbox.
I could show you a solution if you provide a sketch of the layout you want.
change the CSS for img to this
.mainCountainerHeadLogo img {
width: 100%;
height: 100%;
margin: auto
}
I have a div element (innerBar) inside another one (leftBar), and unless I specify the innerBar's border to have some width, the innerBar isn't starting at the top of the leftBar.
How do fix this?
.leftBar {
width: 10%;
height: 100%;
position: fixed;
top: 0;
left: 0;
border: 1px solid red;
}
.innerBar {
width: 100%;
height: 100%;
background-color: lightblue;
border: 1px solid yellow;
}
<div class="leftBar">
<div class="innerBar">
<p>Some Text</p>
</div>
</div>
It's not necessary to position the nested element out of the document flow at all.
This behaviour is a result of the default margin property declared on the nested p element (specifically the margin-top property), and can be rectified by either one of the following methods:
removing the margin property on the nested p tag:
.innerBar p {
margin-top: 0;
}
* {
box-sizing: border-box;
}
.leftBar {
width: 10%;
height: 100%;
position: fixed;
top: 0;
left: 0;
border: 1px solid red;
}
.innerBar {
width: 100%;
height: 100%;
background-color: lightblue;
/* additional */
display: inline-block;
vertical-align: top;
}
<div class="leftBar">
<div class="innerBar">
<p>Some Text</p>
</div>
</div>
declaring the following additional properties on the nested element
(innerBar):
.innerBar {
width: 100%;
height: 100%;
background-color: lightblue;
/* border: 1px solid yellow; */
/* additional */
display: inline-block;
vertical-align: top;
}
* {
box-sizing: border-box;
}
.leftBar {
width: 10%;
height: 100%;
position: fixed;
top: 0;
left: 0;
border: 1px solid red;
}
.innerBar {
width: 100%;
height: 100%;
background-color: lightblue;
}
.innerBar p {
margin: 0px;
}
<div class="leftBar">
<div class="innerBar">
<p>Some Text</p>
</div>
</div>
Okay got it, I just add to innerBar the property:
position: absolute;
I've had a look for similar questions and tried removing the position attribute but unfortunatley that didn't work.
I have a container with 2 divs inside, and both those divs contain one image each. The images display correctly but the overall container has a height of 0px. Here is an image with the developer console open: https://gyazo.com/277d635619eb80d2d3f63a1c28c80314
This happened after trying to make the images responsive with width: 100%; and height: auto;
#landing-images {
width: 100%;
height: auto;
margin-top: 10%;
margin-bottom: 5%;
border: solid 2px black;
}
.leftLanding {
/*position: relative;*/
width: 80%;
float: left;
}
.rightLanding {
/*position: relative;*/
width: 80%;
float: right;
}
.landingImage {
width: 100%;
height: auto;
}
<div id="landing-images">
<div class="leftLanding">
<img class="landingImage" src="http://www.hlgjyl888.com/data/wallpapers/57/WDF_1035782.png">
</div>
<div class="rightLanding">
<img class="landingImage" src="http://www.hlgjyl888.com/data/wallpapers/57/WDF_1035782.png">
</div>
</div>
You just need to add.
overflow:auto; to #landing-images.
So, Your CSS will be like,
#landing-images {
width: 100%;
height: auto;
overflow:auto;
margin-top: 10%;
margin-bottom: 5%;
border: solid 2px black;
}
Because floating the child element removes it from the document flow and the parent will collapse. By adding the overflow rule, the desired behavior is restored.
The problem are the float attributes, use display: block and margin instead.
#landing-images {
width: 100%;
height: auto;
margin-top: 10%;
margin-bottom: 5%;
border: solid 2px black;
position:relative;
}
.leftLanding {
position: relative;
width: 80%;
display:block;
margin-right:auto;
}
.rightLanding {
position: relative;
width: 80%;
margin-left:auto;
}
.landingImage {
width: 100%;
height: auto;
}
<div id="landing-images">
<div class="leftLanding">
<img class="landingImage" src="http://cdn.playbuzz.com/cdn/d2b06305-f201-4127-8eb7-7410bcc0de02/2d6c2415-2b8c-430c-87a4-c516409d8488.jpg">
</div>
<div class="rightLanding">
<img class="landingImage" src="http://www.nationalgeographic.com/content/dam/animals/pictures/mammals/g/gray-wolf/gray-wolf_01.ngsversion.1484679603276.JPG">
</div>
</div>
You must clear the wrapper whenever there is a floating element inside it.
#landing-images {
width: 100%;
height: auto;
margin-top: 10%;
margin-bottom: 5%;
border: solid 2px black;
}
.leftLanding {
/*position: relative;*/
width: 80%;
float: left;
}
.rightLanding {
/*position: relative;*/
width: 80%;
float: right;
}
.landingImage {
width: 100%;
height: auto;
}
.clearfix::after {
visibility: hidden;
display: block;
content: "";
clear: both;
height: 0;
}
<div id="landing-images" class="clearfix">
<div class="leftLanding">
<img class="landingImage" src="http://www.hlgjyl888.com/data/wallpapers/57/WDF_1035782.png">
</div>
<div class="rightLanding">
<img class="landingImage" src="http://www.hlgjyl888.com/data/wallpapers/57/WDF_1035782.png">
</div>
</div>
I always use the standard clearfix class with the following style:
.clearfix::after {
visibility: hidden;
display: block;
content: "";
clear: both;
height: 0;
}
So, always have such a class on your global CSS. And add this class to all the wrappers which has floating elements inside it.
Read more about clearfix concept at:
https://css-tricks.com/snippets/css/clear-fix/
Hey I can't figure out why my divs are overlapping and what i should do...
You can watch the site here: http://hersing.dk/job/
I would like for the div carrying the hr to appear underneed the header-info div
Heres is the code from the site:
#font-face {
font-family: hersing;
src: url(lmroman10-regular.otf);
}
html,
body {
font-family: hersing;
height: 100%;
margin: 0px;
}
.container {
width: 90%;
height: 90%;
left: 5%;
top: 5%;
background: green;
position: absolute;
display: block;
clear: both;
}
.info-name {
left: 5%;
top: 10%;
position: absolute;
display: block;
}
.info-picture {
min-width: 250px;
min-height: 250px;
padding: 4px;
position: absolute;
top: 10%;
right: 5%;
background: black;
display: block;
}
.info-picture img {
height: 100%;
width: 100%;
}
#info-header {
font-size: 400%;
}
#info-title {
font-size: 150%;
font-weight: bold;
}
.header-info {
display: block;
padding: 20px;
position: relative;
width: 100%;
}
.stang-1 {
display: block;
width: 100%;
color: blue;
position: relative;
}
#hr-1 {
display: block;
height: 1px;
border: 0;
border-top: 1px solid #ccc;
margin: 1em 0;
padding: 0;
background-color: #f1a857;
}
<div class="container">
<div class="header-info">
<div class="info-name" id="info-name">
...
</div>
<div class="info-picture" id="info-picture">
<img src="images/picture.png" />
</div>
</div>
<div class="stang-1" id="stang-1">
<hr id="hr-1">
</div>
</div>
I hope someome can figure this out, cause i'm pretty lost
Both .info-name and .info-picture are absolute positioned and .header-info has no height defined.
You'd rather use relative positioning + float + clear and/or display: inline-block for both .info-* rules and everything will be fine.
<div class="container">
<div class="header-info">
<div class="info-name" id="info-name">
.....
</div>
<div class="info-picture" id="info-picture">
<img src="images/picture.png" />
</div>
</div>
<div class="stang-1" id="stang-1">
<hr id="hr-1">
</div>
</div>
<style>
#font-face {
font-family: hersing;
src: url(lmroman10-regular.otf);
}
html,
body {
font-family: hersing;
height: 100%;
margin: 0px;
}
.container {
width: 90%;
height: 90%;
left: 5%;
top: 5%;
background: green;
position: absolute;
display: block;
clear: both;
}
.info-name {
left: 5%;
top: 10%;
position: absolute;
display: block;
}
.info-picture {
width: 250px;
height: 250px;
padding: 4px;
position: relative;
top: 10%;
left:70%;
background: black;
display: block;
}
.info-picture img {
height: 100%;
width: 100%;
}
#info-header {
font-size: 400%;
}
#info-title {
font-size: 150%;
font-weight: bold;
}
.header-info {
display: block;
padding: 20px;
position: relative;
width: 100%;
}
.stang-1 {
display: block;
width: 100%;
color: blue;
position: absolute;
}
#hr-1 {
display: block;
height: 1px;
border: 0;
border-top: 1px solid #ccc;
margin: 1em 0;
padding: 0;
background-color: #f1a857;
}
</style>
I think this will solve your problem...
In this case, although very impractical, the solution would be to add a line break <br> after the .header-info div.
I repeat, this solution is not the best one by far, and you should, as pointed out in the comments by Paulie_D, change your positioning layout method.
Everything inside the absolutely positioned .container would be better positioned relative. Use css float:left; or float:right; to position elements and clear:both; when you want the next element to start below all floated elements. Use padding on the container and margins on the floated elements for positioning.
Also give .container css class of overflow:auto; to wrap around all elements inside without having to set the height every time.
I am currently working on a HTML5 and CSS project and am having a problem getting the containers to display properly.
What I want to have is a header bar at the top, a wrapper that contains 2 other divs and then a footer at the bottom which is always at the bottom of the window or at the bottom of the content whichever is further down.
Here's a snippet:
html, body
{
padding: 0;
margin: 0;
}
#wrapper
{
position: absolute;
background-color: purple;
height: 100%;
width: 100%;
margin-top: 0px;
}
header
{
position: absolute;
width: 100%;
height: 80px;
background-color: black;
color: white;
}
#articleContainer
{
background-color: blue;
width: 75%;
margin-left: auto;
margin-right: auto;
height: auto;
margin-top: 80px;
}
#articleContent
{
width: 70%;
background-color: yellow;
float: left;
}
#articleSideBar
{
position: relative;
width: 28%;
background-color: green;
float: right;
margin-left: 2px;
margin-right: 2px;
display: inline;
margin-top: 0px;
float: right;
height: auto;
}
<html>
<head>
<title>index</title>
<link href="ArticleStyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
<header>
Header
</header>
<div id="articleContainer">
Article Container
<div id="articleContent">
The quick brown fox jumped over the lazy dogs back. All good men must come to the aid of the party
</div>
<div id="articleSidebar">
Article Sidebar
</div>
</div>
</div>
</body>
</html>
At the moment the articleContainer is only the height of however many lines there are. What I want to have is the formContainer to fill the rest of the screen, I've tried adding the height: 100%; attribute but then this feels the form container over the screen size. I.e. a vertical scrollbar appears which is about the same height as the header. How can I get the formContainer to fill the available screen space without the scroll bar. However, if the content is larger than the form container should expand to fill the extra space.
Thanks for any help you can provide.
If you really want a css3 solution the one you're looking for is setting height: calc(100% - 80px); on #articleContainer as demonstrated in this fiddle, however this will not work in all browsers.
Example using old flexbox model css:
html, body
{
padding: 0;
margin: 0;
height: 100%;
}
#wrapper
{
display: -webkit-box;
-webkit-box-orient: vertical;
min-height: 100%;
background-color: purple;
width: 100%;
margin-top: 0px;
}
header
{
width: 100%;
height: 80px;
background-color: black;
color: white;
}
#articleContainer {
width: 75%;
margin: auto;
background-color: blue;
display: -webkit-box;
-webkit-box-flex: 1;
}
#articleContent
{
width: 70%;
background-color: yellow;
}
#articleSideBar
{
position: relative;
width: 28%;
background-color: green;
margin-left: 2px;
margin-right: 2px;
display: inline;
margin-top: 0px;
height: auto;
}
same thing, but this time using new flexbox model
css
html, body
{
padding: 0;
margin: 0;
height: 100%;
}
#wrapper
{
display: -webkit-flex;
-webkit-flex-direction: column;
min-height: 100%;
background-color: purple;
width: 100%;
margin-top: 0px;
}
header
{
width: 100%;
height: 80px;
background-color: black;
color: white;
}
#articleContainer {
width: 75%;
margin: auto;
background-color: blue;
display: -webkit-flex;
-webkit-flex: 1;
}
#articleContent
{
width: 70%;
background-color: yellow;
}
#articleSideBar
{
position: relative;
width: 28%;
background-color: green;
margin-left: 2px;
margin-right: 2px;
display: inline;
margin-top: 0px;
height: auto;
}
version with only the paragraph in yellow
I've used this method before, the tricky part is getting the header and footer in the right location. Once you have that the rest should fall into place:
jsfiddle:
http://jsfiddle.net/Ug5JR/
css:
html, body { margin: 0; padding: 0; height: 100%; }
header {
position: relative;
display: block;
background: red;
height: 100px;
z-index: 1;
}
article {
display: block;
background: yellow;
min-height: 100%;
margin-top: -100px;
margin-bottom: -100px;
}
article section {
display: block;
padding-top: 100px;
padding-bottom: 100px;
overflow: hidden;
}
footer{
display: block;
background: blue;
height: 100px;
}
p:hover {
height: 4000px;
}
markup:
<header></header>
<article>
<section>
<p>Hover me and I'll push the content larger than the page</p>
</section>
</article>
<footer></footer>
The trick is to get the negative margins to absorb the space used by the header and footer, this causes the 100% calculation to correct itself. You can then use any internal element to counter the negative margins with padding or margin on top and bottom. So whilst your article element is pretty much 100% height of the page, your article > section element will appear the right height and lay it's children out correctly.