center element with unknown height without having it take up space - html

This is a tricky problem from what I can see. Basically, I have alert messages that need appear on the page just below the navigation and on top of the content below. This is best shown in a fiddle here http://jsfiddle.net/53b6g/1/ - notice the commented style - this style can't be used b/c I don't know how many alerts will be on the page. So the height of the wrapper element is unknown.
HTML:
<div class="nav">
<ul>
<li>first</li>
<li>second</li>
<li>last</li>
</ul>
</div>
<div class="wrapper">
<div class="table">
<div class="tr">
<div class="td">
Let us center this and have same width as below
</div>
</div>
<div class="tr">
<div class="td">
shorter but same width
</div>
</div>
<div class="tr">
<div class="td">
but wait - there could be 3 or 2 or 1 so can't use height calculations
</div>
</div>
</div>
</div>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum in dui rhoncus, porttitor nibh ornare, tristique est. Nullam condimentum quam ac metus varius, at fermentum nibh congue. Fusce vel posuere justo, vel vulputate enim. Cras varius libero et felis venenatis ultricies. Vivamus ante est, tempus et aliquet eget, varius id nisi. In a consectetur lacus, vitae lacinia mauris. Nulla pulvinar tellus id laoreet tempus. Integer feugiat quis odio eget ullamcorper.</p>
</div>
CSS:
/*can't use style*/
.wrapper{
position: relative;
top: -4px;
margin-bottom: -70px;
}
/*end can't use style*/
.table{
display: table;
margin: 0 auto;
}
.tr{
display: table-row
}
.td{
display: table-cell;
background: red;
}
.nav{
background: green;
}
.nav ul {
margin: 0;
}
.nav li{
display: inline-block;
}
.content{
background: blue;
}
Here is a fiddle without the commented code showing the problem at hand http://jsfiddle.net/8Awze/. Is this something that can be solved with CSS alone?
HTML:
<div class="nav">
<ul>
<li>first</li>
<li>second</li>
<li>last</li>
</ul>
</div>
<div class="wrapper">
<div class="table">
<div class="tr">
<div class="td">
Let us center this and have same width as below
</div>
</div>
<div class="tr">
<div class="td">
shorter but same width
</div>
</div>
</div>
</div>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum in dui rhoncus, porttitor nibh ornare, tristique est. Nullam condimentum quam ac metus varius, at fermentum nibh congue. Fusce vel posuere justo, vel vulputate enim. Cras varius libero et felis venenatis ultricies. Vivamus ante est, tempus et aliquet eget, varius id nisi. In a consectetur lacus, vitae lacinia mauris. Nulla pulvinar tellus id laoreet tempus. Integer feugiat quis odio eget ullamcorper.</p>
</div>
CSS:
.table{
display: table;
margin: 0 auto;
}
.tr{
display: table-row
}
.td{
display: table-cell;
background: red;
}
.nav{
background: green;
}
.nav ul {
margin: 0;
}
.nav li{
display: inline-block;
}
.content{
background: blue;
}

If I'm understanding you correctly, you should be able to use position: absolute as follows:
.wrapper{
position: absolute;
width: 100%;
top: 25px;
}
http://jsfiddle.net/53b6g/2/

Related

Assign divs be exactly 100% height

My problem is the following: I have a website with a header, left navigation and a footer (not in the code right now). The header and the left navigation have to be static and only the rest of the page should move, that's why I started with making my left navigation menu 100% width. But height: 100%, doesn't seem to be working.
Could someone give me a tip or maybe anybody knows why I have this problem?
edit: that space under the footer and the left navigation shouldn't be there. That was a fail from when I took the screenshot.
Code: (index.php)
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="design.css">
<title>Hardvision</title>
<script src="https://kit.fontawesome.com/743ddd1f40.js" crossorigin="anonymous"></script>
</head>
<body>
<div class="wrapper">
<header>
<div class="container-fluid">
<div class="row">
<div class="col-2 logo">
<img src="logo2.png" height="90px" width="90px">
</div>
<nav class=" col-9 px-0">
<div class="mainnav">
<ul class="px-0">
<li>
<a class="li" href="#">Lorem ipsum</a>
</li>
<li>
<a class="li" href="#">Lorem ipsum</a>
</li>
<li>
<a class="act" href="#">Lorem ipsum</a>
</li>
<li>
<a class="li" href="#"><i class="fas fa-user fa-lg"></i></a>
</li>
</ul>
</div>
</nav>
</div>
</div>
</header>
<hr>
<main>
<div class="leftnav">
<nav>
<div class="leftnavtable">
<li>
<p>Lorem ipsum</p>
</li>
<li>
<p>Lorem ipsum</p>
</li>
<li>
<p>Lorem ipsum</p>
</li>
</div>
</nav>
</div>
<div class="fullheight">
<p>Lorem ipsum dolor Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet</p>
</div>
</main>
<footer>
</footer>
</div>
</body>
</html>
Code: (design.scss)
//variables
$defaultfont: Arial, sans-serif;
$defaultfontsize: 16px;
$gray1: #A9A9A9;
$gray2: #DDDDDD;
$break_small: 900px;
$break_large: 1170px;
$headerheight: 110px;
//general
html {
font-size: $defaultfontsize;
font-family: $defaultfont;
}
//tags
header {
background-color: $gray1;
}
hr {
border: none;
height: 1px;
background-color: #333;
margin: 0px;
}
a {
color: black;
}
//logo positioning
.container-fluid {
.row {
height: $headerheight;
}
.logo {
padding: 10px 0px 0px 100px;
#media screen and (max-width: $break_large) {
padding: 10px 0px 0px 50px;
}
#media screen and (max-width: $break_small) {
padding: 10px 0px 0px 40px;
}
}
}
//Header navigation
.mainnav {
ul, li {
display: flex;
list-style: none;
justify-content: space-around;
align-items: flex-end;
height: 70px;
}
}
//whole page height exactly 100%
html, body, main, .leftnav, .leftnavtable, .fullheight {
height: 100%;
margin: 0;
overflow: auto;
}
//Left navigation container
.leftnav {
padding: 0;
background-color: $gray2;
display: flex;
flex-flow: column;
width: 250px;
float: left;
}
//Left navigation
.leftnavtable {
width: 250px;
ul, li {
list-style: none;
padding: 40px 0px 0px 35px;
}
}
//Text area
main .leftnav {
float: left;
}
You need to set height in "vh" instead of "%".
But you also need to calculate the height.. see example below
height: calc(100vh - height of header); If header height = 100px;
.leftnav {
height: calc(100vh - 100px);
}
Assuming height of navbar 70px and footer 100px and assuming width of sidebar 250px
.fullheight {
height: calc(100vh -170px);
width: calc(100vh - 250px);
overflow:auto
}
Also good to know, building layouts with flexbox CSS gives you some more freedom to play with elements shrinking and growing automatically instead of defining the height and width of the elements.
This can help you in the future to make better decision on to how to structure your HTML templates.
I coded you a quick example provided with your screenshot.
Learn more about flexbox here:
https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Flexbox
header {
height: 80px;
background-color: grey;
}
.content-wrapper {
display: flex;
background-color: blue;
}
nav {
background-color: aqua;
min-width: 180px;
}
footer {
background-color: red;
padding: 20px;
}
/* misc styling */
header {
padding: 20px;
}
header ul {
list-style: none;
}
header ul li {
display: inline;
padding: 5px;
}
nav ul li {
padding: 5px 0;
}
main section {
padding: 20px;
}
p {
margin: 0;
}
<header>
<ul>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
</ul>
</header>
<div class="content-wrapper">
<nav>
<ul>
<li>Hello world</li>
<li>Hello world</li>
<li>Hello world</li>
<li>Hello world</li>
</ul>
</nav>
<main>
<section>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin eget tellus et leo lacinia facilisis nec nec ante. Maecenas sed purus dictum, dignissim ex sit amet, pretium augue. Sed sed diam felis. Aliquam facilisis non nunc ac mattis. In eget
magna ut mauris maximus tincidunt nec ac orci. Sed bibendum mauris erat, nec efficitur tortor aliquet non. Nulla facilisis velit nec nunc tincidunt aliquam. Phasellus et nisl sit amet lorem sodales volutpat. Vivamus mattis justo eu nulla tincidunt
porta. Pellentesque sed urna finibus, vehicula nunc a, vehicula dui. Ut finibus lectus sed odio faucibus dignissim. Nullam egestas posuere porta. Morbi ac tellus imperdiet, fringilla ex a, tincidunt lacus. Nulla facilisi. Vestibulum ante ipsum
primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque at eleifend elit, in dignissim arcu. Mauris bibendum semper sagittis. Nullam quis ex sed velit porttitor tincidunt. Class aptent taciti sociosqu ad litora torquent
per conubia nostra, per inceptos himenaeos. Vestibulum a convallis dolor. Aliquam in pharetra sem. Phasellus lobortis, diam vel convallis suscipit, nulla turpis pretium risus, sed dignissim tellus nunc vel enim. Fusce dignissim ante sit amet pulvinar
gravida. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; In congue mattis fermentum. Duis blandit ornare egestas. Cras at fringilla sem, at aliquet metus. Integer ac lectus sit amet neque efficitur dictum
in eu urna. Maecenas vulputate nec est vitae blandit. Sed aliquet posuere felis, ut tincidunt magna pharetra non. Maecenas facilisis pretium odio, tincidunt porttitor mauris feugiat vitae. Duis blandit quam non magna maximus dignissim non in tellus.
Nam ligula leo, varius iaculis commodo sed, vestibulum sit amet orci. Etiam quis quam eros. In scelerisque vitae risus sed egestas. Proin fermentum venenatis ex et condimentum. Maecenas eu massa ut augue laoreet pharetra at ut mi. Nunc gravida
est sed nibh eleifend condimentum.</p>
</section>
<footer>
Here should be a Footer.
</footer>
</main>
</div>
Use vertical height
.leftnav {
padding: 0;
background-color: $gray2;
display: flex;
flex-flow: column;
width: 250px;
float: left;
height: 100vh;
}

How put 4 images and title and description side by side in a html box

How put 4 images and title and description side by side in a html box? No link, only text. As shown below:
Flex box is the way to go:
https://jsfiddle.net/s1mreoe8/1/
.flex{
display:flex;
}
Here is a guide : https://css-tricks.com/snippets/css/a-guide-to-flexbox/
.first {
float: left;
width: 50%;
}
.img {
float: left;
width:39%;
padding:10px;
}
.text
{
width:39%;
float: left;
padding:10px;
}
.img img {
width: 100%;
}
<div class="row">
<div class="first">
<div class="img">
<img src="http://www.fogosglobo.com.br/img/250x250-circle.png" alt="img">
</div>
<div class="text">
<p><strong>Lorem ipsum dolor sit amet</strong>, consectetur adipiscing elit. Nunc molestie, nibh eu molestie vestibulum, urna neque interdum orci, ut egestas diam massa vel magna. Mauris eget nunc nec sem ultrices varius.</p>
</div>
</div>
<div class="first">
<div class="img">
<img src="http://www.fogosglobo.com.br/img/250x250-circle.png" alt="img">
</div>
<div class="text">
<p><strong>Lorem ipsum dolor sit amet</strong>, consectetur adipiscing elit. Nunc molestie, nibh eu molestie vestibulum, urna neque interdum orci, ut egestas diam massa vel magna. Mauris eget nunc nec sem ultrices varius.</p>
</div>
</div>
</div>

Make divs same size as the biggest one inside the parent

So I have this div with 3 childs, but one of them has less text than the others, so the image and the button move, I want the position of the image not to be dependent of the size of the text, so it can stay in the same position as the biggest child image.
Parent position: relative; and child position: absolute; doesn't work, because the image and the button traslape
HTML:
<div id="child">
<h5>About us</h5>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec molestie. .</p>
<img class="img-responsive" src="http://www.marijazaric.com/minimalism_responsive/images/picture1.jpg" alt=""/>
<button>Read More</button>
</div>
SASS:
.parent
display: flex
flex-wrap: wrap
justify-content: center
#child
border-style: solid
margin-left: 25px
margin-right: 25px
If you also declare each of the "child" elements as a flex container, you can get the layout you want.
This quick example assumes a parent element with three elements where "child" is a class value instead of an id value. In each "child" the p element is allowed to grow to take all of the available vertical space. The third "child" includes a half-height image to show how the img and button stay pinned to the bottom.
.parent {
display: flex;
flex-direction: row;
justify-content: center;
}
.child {
border: 1px solid #000;
display: flex;
flex-direction: column;
width: 30%;
margin: 1em;
padding: 1em;
}
.child h5 {
}
/* should probably be replaced with a class selector */
.child p {
flex-grow: 1;
}
.child img {
}
.child button {
}
<div class="parent">
<div class="child">
<h5>About us</h5>
<p> Maecenas aliquet enim ut mi lobortis, a faucibus orci interdum. Suspendisse potenti. Ut lobortis varius fringilla. Nunc nec urna metus. Ut vel ligula rhoncus nulla ultrices egestas ac ut arcu. Cras eu odio est. In quis bibendum arcu. </p>
<img src="http://placehold.it/326x290">
<button>Read More</button>
</div>
<div class="child">
<h5>Services</h5>
<p> Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Ut dui risus, placerat non lorem vitae, laoreet imperdiet mauris. Aenean eleifend augue vel purus tincidunt egestas. Morbi sagittis neque efficitur posuere fermentum. Fusce non lectus at enim sodales dapibus. Nam congue neque nunc, ut accumsan mauris ornare sit amet. Nulla quis quam ut urna ullamcorper commodo. Curabitur ac elit at urna molestie pulvinar id at nisi. Cras in quam at magna hendrerit fermentum. Vestibulum vel nulla aliquet, molestie ante eu, laoreet quam. </p>
<img src="http://placehold.it/326x290">
<button>Read More</button>
</div>
<div class="child">
<h5>Latest news</h5>
<p> Maecenas a dolor pretium, maximus mi eu, molestie ligula. Donec aliquam mollis eros at sodales. Pellentesque vitae dui feugiat, tempor eros eu, bibendum massa. Nulla accumsan finibus leo, et pellentesque urna vestibulum ut. Nullam arcu lectus, consectetur a varius sed, finibus nec metus. Morbi lorem nulla, maximus vitae mi in, efficitur elementum mi. Praesent sit amet risus est. </p>
<img src="http://placehold.it/326x145">
<button>Read More</button>
</div>
</div>
SASS version of the code snippet's CSS:
.parent
display: flex
flex-direction: row
justify-content: center
.child
border: 1px solid #000
display: flex
flex-direction: column
width: 30%
margin: 1em
padding: 1em
h5
p
flex-grow: 1
img, button
You can do it with nested flexbox, and use margin-top:auto to push the images to the bottom and keep them in the same line.
I added a <span> tag around each <img> and <button>.
.container {
display: flex;
justify-content: space-around;
}
.item {
flex: 0 0 20%;
display: flex;
flex-direction: column;
border: 1px solid;
}
.item h5 {
margin: 0;
}
.item .pic {
margin-top: auto;
}
.item .pic img {
display: block;
width: 100%;
}
<div class="container">
<div class="item">
<h5>About us</h5>
<p>Donec molestie.</p>
<span class="pic"><img src="//dummyimage.com/200" alt="" /></span>
<span class="btn"><button>Read More</button></span>
</div>
<div class="item">
<h5>About us</h5>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p>
<span class="pic"><img src="//dummyimage.com/200" alt="" /></span>
<span class="btn"><button>Read More</button></span>
</div>
<div class="item">
<h5>About us</h5>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec molestie. .</p>
<span class="pic"><img src="//dummyimage.com/200" alt="" /></span>
<span class="btn"><button>Read More</button></span>
</div>
</div>
Use something like :
<div id="child">
<ul>
<li><h5>About us</h5></li>
<li><p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec molestie. .</p></li>
<li><img class="img-responsive" src="http://www.marijazaric.com/minimalism_responsive/images/picture1.jpg" alt=""/></li>
<li><button>Read More</button></li>
</ul>
</div>
SASS:
.parent
display: flex
flex-wrap: wrap
justify-content: center
#child
border-style: solid
margin-left: 25px
margin-right: 25px
#child li {display:block};
#child ul {display:inline-table;}

3 column layout rendering issue

I have a 3 col layout, left and mid col are images, and the last col is text. I want to keep the height of all col the same at all times even when the browser resizes. I don't mind having scroll bar on the last col with text.
HTML:
<div class="mid-col">
<img src=""/>
</div>
<div class="right-col">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed semper, eros nec sollicitudin porta, nibh justo consectetur elit,
a ultricies libero est ac justo. Nulla dictum dignissim placerat.
Donec non eros nisl. Morbi diam est, volutpat a orci tempus, mollis
maximus dui. Quisque consequat risus et sagittis dapibus. Mauris nulla
quam, ullamcorper a mattis sed, pretium sit amet dolor. Etiam pharetra
velit id lacus cursus imperdiet. Phasellus ex ipsum, finibus vitae
rhoncus et, suscipit at risus. Nam dignissim sapien tortor, ut
egestas tortor pulvinar ut.
</p>
</div>
</div>
CSS:
#content-container {
width: 100%;
height: 33.333%;
}
.left-col img {
width: 33.333%;
float: left;
display:block;
box-sizing: border-box;
-moz-box-sizing: border-box;
}
.mid-col img {
width: 33.333%;
float: left;
display:block;
box-sizing: border-box;
-moz-box-sizing: border-box;
}
.right-col {
width: 33.333%;
height: 33.333%;
height: 100%;
display:block;
overflow: scroll;
}
you can achieve that by using display:table-[cell]
#content-container {
width: 100%;
display: table;
}
#content-container > div {
display: table-cell;
width: 33%;
vertical-align: top;
border: 1px solid green
}
img {
width: 100%;
display: block;
height: auto;
}
<div id="content-container">
<div class="left-col">
<img src="http://lorempixel.com/100/100" />
</div>
<div class="mid-col">
<img src="http://lorempixel.com/100/100" />
</div>
<div class="right-col">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed semper, eros nec sollicitudin porta, nibh justo consectetur elit, a ultricies libero est ac justo. Nulla dictum dignissim placerat. Donec non eros nisl. Morbi diam est, volutpat a orci tempus,
mollis maximus dui. Quisque consequat risus et sagittis dapibus. Mauris nulla quam, ullamcorper a mattis sed, pretium sit amet dolor. Etiam pharetra velit id lacus cursus imperdiet. Phasellus ex ipsum, finibus vitae rhoncus et, suscipit at risus.
Nam dignissim sapien tortor, ut egestas tortor pulvinar ut.
</p>
</div>
</div>
The browser renders from top to bottom so you are getting the opposite effect. Try:
<div class="container">
<div class="child1"></div>
<div class="child2"></div>
<div class="child3"></div>
</div>
.container {
height: 100%;
}
.child1 {
width: 33.333%;
height: 33.333%;
float: left;
box-sizing: border-box;
}
.child2 {
width: 33.333%;
height: 33.333%;
float: left;
box-sizing: border-box;
}
.child3 {
width: 33.333%;
height: 33.333%;
float: left;
box-sizing: border-box;
overflow-y: auto;
}
The parent container can take up all the space. Each child can float to the left. Changing the box sizing keeps a consistent width for percentages.
I like #beautifulcoder's answer but I have the same thing on one of my sites and handled it with jquery.
$(function() {
var divHeight = $(window).height()/3;
$('.left-col').css('height',divHeight));
$('.mid-col').css('height',divHeight);
$('.right-col').css('height',divHeight);
$('.right-col').css('overflow-y','auto');
})

Div refuses to center

The problem should be able to be seen live at this link (on every page, as the main content of the page is contained by my "content" div): http://tucsonbagley.com/index.html
The "content" class (a container div) has a much larger margin on the left than on the right and, after going over my CSS ad nauseum, I just cannot figure out why. I've broken something for sure (it was working not long ago!), but I just can't figure out what.
If I remove my id Navbar or id Header divs, the Content div will default back to the left... yeah, at this point I'm lost.
The CSS in question:
.content{
display:inline-block;
margin:0 auto;
width: 68%;
overflow:hidden;
text-align: center;
padding: 10px;
background-color: #FFFFFF;
font-size: 12px;
border-radius: 10px 10px 10px 10px;
}
Example HTML:
<body>
<div class="header">
<p>Tucson Bagley</p>
</div>
<div id="socialmedia">
<img src="images/Twitter_logo_blue.png"/>
<img src="images/linkedin.png"/>
<img src="images/facebook.png"/>
</div>
<div id="header">
<small>BagelHero#gmail.com</small>
</div>
<div id="navbar">
<ul>
<li><span>Gallery</span></li>
<li><span>Resume/CV</span></li>
<li><span>Contact me</span></li>
</ul>
</div>
<div class="content">
<div class="thumbleft"><h2>This is some content.</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin non varius metus. Pellentesque eu nunc tortor. Aliquam id lectus orci. Sed id consectetur eros. Curabitur semper nisl nibh, rhoncus lacinia nibh volutpat at. Pellentesque sollicitudin vitae ipsum ut dictum. Proin ac risus ac nisi interdum hendrerit. Pellentesque sodales mauris ac eleifend vehicula. Nulla convallis aliquet urna varius auctor. Donec eget ipsum ut mauris consequat auctor eget sit amet odio. Nullam sed lorem erat. Praesent consequat porttitor magna, sit amet feugiat odio tincidunt ut. Fusce congue eros vel quam condimentum, vel consectetur quam imperdiet. </p>
</div>
</br>
<div id="copyright">
<p>Copyright 2012-2014 | Tucson Bagley</p>
</div>
</body>
Some help would be appreciated. Thanks in advance!
Add a <div style="clear:both"></div> before .content div and apply display:block to .content instead of display:inline-block and that's it
change the css of your .content to look like this
.content{
position: absolute;
left:0px;
right:0px;
display:inline-block;
margin:10px auto;
width: 68%;
overflow:hidden;
text-align: center;
padding: 10px;
background-color: #FFFFFF;
font-size: 12px;
border-radius: 10px 10px 10px 10px;
}
the magic is the left:0px combined with right:0px and margin:10px auto which will only work in absolute mode.
Edit - updated css as per comments.