I am struggling to clear 3 div in the correct manner I can do it by adding padding to the bottom of the wrapping div but that really isn't practical.
Demo of the problem
What do you think the best treatment is for this?
HTML
<section class="audience">
<div class="container">
<div class="audience_col1">
<h1>title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam a consequat ipsum. Praesent a pellentesque nibh, vitae blandit leo. Fusce arcu orci, eleifend vel nunc vel, pellentesque eleifend lorem.</p>
</div>
<div class="audience_col2">
<h1>title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam a consequat ipsum. Praesent a pellentesque nibh, vitae blandit leo. Fusce arcu orci, eleifend vel nunc vel, pellentesque eleifend lorem.</p>
</div>
<div class="audience_col3">
<h1>title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam a consequat ipsum. Praesent a pellentesque nibh, vitae blandit leo. Fusce arcu orci, eleifend vel nunc vel, pellentesque eleifend lorem.</p>
</div>
</div>
</section>
CSS
.audience{
width: 100%;
background-color: #f6f5fa;
height: auto;
overflow:auto;
text-align:center;
padding: 20px 0 20px 0;
}
.audience_col1{
width: 33.33%;
height: 100px;
float: left;
box-sizing:border-box;
padding: 20px;
}
.audience_col2{
width: 33.33%;
height: 100px;
float: left;
box-sizing:border-box;
padding: 20px;
}
.audience_col3{
width: 33.33%;
height: 100px;
float: left;
box-sizing:border-box;
padding: 20px;
}
If I'm right in thinking what you're trying to accomplish, why not display the div elements inline?
Demo Fiddle
CSS
.audience {
width: 100%;
background-color: #f6f5fa;
text-align:center;
padding: 20px 0 20px 0;
font-size:0; /* <--- trick to prevent inline 'spacing' of multiline HTML */
}
.audience_col1, .audience_col2, .audience_col3 {
width: 33.33%;
height: 100px;
display:inline-block;
box-sizing:border-box;
padding: 20px;
font-size:14px; /* <--- revert font-size */
}
.audience{
display:block;
}
.audience_col1,
.audience_col2,
.audience_col3{
display:inline-block;
}
This is the CSS you should use if you want to work with float concept, else you can change the code and move ahead with the display: inline-block concept as shown on the other answer :-
Demo Fiddle
.audience{
background-color: #f6f5fa;
}
.container {
overflow: hidden;
text-align: center;
}
.audience_col1, .audience_col2, .audience_col3 {
width: 33.33%;
float: left;
box-sizing:border-box;
padding: 20px;
}
Related
I want to have two stacked divs on one side, and then have a single column on the other side with the same height as the left divs.
Kind of like this:
I have the two divs and a side bar, but the two divs won't stack.
Here is what I have so far Fiddle
#import url(https://fonts.googleapis.com/css?family=Oxygen);
body {
background-color: #222;
}
.description h1 {
text-align: left;
padding: 20px;
}
#wrapper {
text-align: center;
}
.description,
.sidebar,
.demo-container {
display: inline-block;
vertical-align: top;
}
.description {
background: #eee;
width: 50%;
font-family: "Oxygen";
font-size: 14px;
color: #000;
line-height: 1.2;
}
.sidebar {
background: #eee;
width: 15%;
height: 575px;
}
.demo-container {
background: #eee;
width: 50%;
font-family: "Oxygen";
font-size: 14px;
color: #000;
line-height: 1.2;
}
<div id='wrapper'>
<div class="demo-container">
<h1>Title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam pretium lorem nec tortor elementum.</p>
</div>
<div class="description">
<h1>Title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam pretium lorem nec tortor elementum.</p>
</div>
<div class="sidebar">
</div>
</div>
you are complicating a lot, here is a basic demo of what you want using flexbox
*,
*::before,
*::after {
box-sizing: border-box
}
body {
margin: 0
}
.flex {
display: flex;
flex-basis: 100%
}
.fl {
flex: 1;
display: flex;
flex-direction: column;
margin: 0 5px;
justify-content: space-between
}
.flex-item {
border: 1px solid black
}
.flex-item:not(:first-of-type) {
margin: 10px 0 0
}
.sidebar {
border: 1px solid black;
}
<div class="flex">
<div class="fl">
<div class="flex-item">
<h1>Title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam pretium lorem nec tortor elementum, et aliquam erat feugiat. Duis interdum enim vitae justo cursus pulvinar eu ac nulla. Donec consectetur vehicula turpis. Nunc laoreet tincidunt elit</p>
</div>
<div class="flex-item">
<h1>Title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam pretium lorem nec tortor elementum, et aliquam erat feugiat. Duis interdum enim vitae justo cursus pulvinar eu ac nulla. Donec consectetur vehicula turpis. Nunc laoreet tincidunt elit
ultrices elementum. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Curabitur augue magna, posuere id tortor vel, condimentum consectetur lacus. Pellentesque dui est, ornare vitae semper et, dapibus ut lacus.
Etiam sed porta dui. Phasellus non nisl eget dolor commodo imperdiet.</p>
</div>
</div>
<div class="fl sidebar"></div>
</div>
Just put <div class="sidebar"></div> before the other two divs, then float them all right. See fiddle https://jsfiddle.net/y71tkmtw/1/
.description,
.sidebar, .demo-container {
float: right;
margin: 40px;
}
Just add another <div> surrounding the 2 divs on the left-hand side, with float:left. Add float:right to the sidebar.
.left-container
{
width: 85%;
float:left;
}
.sidebar {
background: #eee;
width: 15%;
height: 575px;
float:right;
}
Fiddle: https://jsfiddle.net/dncgytef/2/
How could to put the right box next to the left box (inline-block) with precise sizes? I can put it up, but the size will not be the same in different browsers. Then how could I put the left box near to the right box without that whitespace? Thank you.
div {
border: 1px solid green;
}
.entry {
width: 560px;
margin: auto;
border: 1px solid red;
display: block;
position: relative;
}
.entry .img-cont,.body-cont {
vertical-align: top;
}
.entry .img-cont {
width: 50px;
display: inline-block;
}
.entry .body-cont {
width: 508px;
display: inline-block;
}
.entry .body-cont p {
display: table;
}
<div class="entry">
<div class="img-cont">
<img src="http://bit.ly/1RabLNk"/>
</div>
<div class="body-cont">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi nulla libero, sollicitudin a erat semper, gravida pharetra augue. Phasellus convallis ultrices dolor vitae imperdiet. Curabitur mollis odio neque, in dictum nisi finibus nec. Vivamus pulvinar, turpis a volutpat semper, lacus diam convallis.</p>
</div>
</div>
see this
http://jsfiddle.net/leandroparrar/6omjqefj
Here is an example of what the problem looks like:
Your answer is pretty close to what you need.
If you zero-our the margins on the p element in .body-cont, that will get rid of the extra whitespace that appears on top of the paragraph.
If you try to use inline-block's, it is easy to get some extra whitespace between elements due to carriage-returns (newlines) in the HTML file.
If you use display: table-cell on .img-cont and .body-cont, then the
two elements will rest side-by-side and you can control the horizontal spacing using left/right padding as needed.
div {
border: 1px solid green;
}
.entry {
width: 560px;
margin: auto;
border: 1px solid red;
display: table;
}
.entry .img-cont, .entry .body-cont {
display: table-cell;
vertical-align: top;
}
.entry .img-cont img {
display: block;
}
.entry .body-cont p {
margin: 0;
}
<div class="entry">
<div class="img-cont">
<img src="http://bit.ly/1RabLNk" />
</div>
<div class="body-cont">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi nulla libero, sollicitudin a erat semper, gravida pharetra augue. Phasellus convallis ultrices dolor vitae imperdiet. Curabitur mollis odio neque, in dictum nisi finibus nec. Vivamus pulvinar,
turpis a volutpat semper, lacus diam convallis.</p>
</div>
</div>
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>
I know this will be easy for someone experienced in CSS. I made a mock-up of my code here to show what I have. I'm trying to get the background colors, pink and green, extend to the bottom of the white column... or whichever one is longest. I thought the clear:both would work but I'm missing something I know is simple. Help appreciated, snickers expected.
JSFiddle
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color:lightblue;
}
#mainColumn {
float: left;
margin-left: 0;
margin-right: 0;
width: 830px;
background-color: white;
}
#leftColumn {
float: left;
margin-left: 0;
margin-right: 0;
width: 195px; /* modified - shortened */
background-color:pink;
}
#rightColumn {
float: left;
width: 195px;
background-color:green;
}
#myWrapper {
background-color: black;
}
.clearit {
clear: both;
}
</style>
</head>
<body>
<div id="myWrapper">
<div id="leftColumn">
some content
</div>
<div id="mainColumn">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur ullamcorper urna a magna euismod, vitae dapibus justo feugiat. Pellentesque ac dui lorem. Fusce ligula urna, ultrices a lectus sit amet, luctus semper est. Curabitur a egestas elit, vitae tincidunt elit. Donec quis nunc id nibh fermentum lobortis egestas id eros. Aenean eget purus erat. In auctor, ipsum in dapibus imperdiet, nulla elit posuere neque, ultrices convallis ligula odio eget felis. Maecenas quis turpis nulla. Nam a velit non lorem semper tincidunt eget iaculis sem. Donec vitae venenatis libero. Duis consequat augue sed sapien cursus dapibus.
</div>
<div id="rightColumn">
even more content
</div>
</div>
<div id="EvenUp" class="clearit">
</div>
<p> On with life </p>
</body>
One solution is to place both left and right columns inside mainColumn and use display:table and display:table-cell
css
body {
background-color:lightblue;
}
#mainColumn {
margin-left: 0;
margin-right: 0;
width: 830px;
background-color: white;
display: table;
}
#leftColumn {
display: table-cell;
margin-left: 0;
margin-right: 0;
width: 195px;
/* modified - shortened */
background-color:pink;
}
#rightColumn {
display: table-cell;
width: 195px;
background-color:green;
}
#myWrapper {
background-color: black;
}
.clearit {
clear: both;
}
fiddle
I have simple site, with the classic elements: Container, Header, Content and Footer. The container has a background-color, which covers the whole content of the site (including header, content and footer). For some reason this won't work with floated elements within the container.
I have found a solution at StackOverflow, but it doesn't feel right. Solution is to set { display: table; } onto the container id.
The page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<style>
body
{
background-color: #999;
font-family: Tahoma;
font-size: 12px;
font-style:normal;
font-variant:normal;
font-weight:normal;
margin: 0px;
padding: 0px;
}
h1
{
color: green;
font-family:Tahoma;
font-size: 26px;
font-weight: bold;
margin: 5px 0;
text-indent: 10px;
width: 100%;
}
#container
{
background: #FFF;
margin: 0 auto;
width: 950px;
position: relative;
}
#header
{
position: relative;
height: 100px;
width: 950px;
}
#content, #content-ext
{
float: left;
margin: 0;
width: 950px;
}
#nav
{
float: left;
padding: 10px 10px 10px 0;
width: 200px;
}
ul#menu
{
cursor: pointer;
display: block;
list-style: none outside none;
margin: 0 auto;
padding: 0;
text-align: left;
width: 200px;
}
ul#menu li
{
margin: 0;
padding: 0;
}
ul#menu li a
{
color: #111;
display: inline-block;
height: 50px;
font-size: 18px;
line-height: 60px;
margin: 0 auto;
padding: 0 0 0 10px;
text-decoration: none;
width: 100%;
}
#mainImg
{
background: #111;
height: 150px;
float: right;
margin: 0 0 20px 0;
width: 710px;
}
#main-content
{
float: right;
width: 710px;
}
#extra
{
float: left;
width: 500px;
}
#contact
{
float: left;
width: 450px;
}
#footer
{
color:#999;
height:20px;
width:950px;
}
</style>
</head>
<body>
<div id="container">
<div id="header">
<h1>test</h1>
</div>
<div id="content">
<div id="nav">
<h1>Menu</h1>
<ul id="menu">
<li>Home</li>
<li>Contact</li>
</ul>
</div>
<div id="mainImg"></div>
<div id="main-content">
<h1>Welkom</h1>
<p><strong>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum sed arcu arcu, a interdum metus. Aenean vel libero nulla. Nulla facilisi. Maecenas malesuada libero a ante vulputate vestibulum. Cras id neque vitae lectus luctus tempor non non risus. Morbi aliquam porttitor facilisis. Sed pulvinar erat sit amet est auctor tincidunt. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin eget arcu lorem, non accumsan ipsum. Donec venenatis adipiscing massa, sed molestie augue ullamcorper et.</strong>
<br/><br/>
Morbi id eros vitae risus tristique bibendum. Quisque nec metus sit amet nunc tincidunt vehicula sit amet non nibh. Nullam risus orci, porttitor ut malesuada vel, volutpat eget sapien. Proin tempus nunc sit amet ligula viverra hendrerit. Donec tempus tristique risus. Fusce at semper est. Etiam ligula est, varius ut tempus at, laoreet bibendum eros. In hac habitasse platea dictumst.</p>
</div>
</div>
<div id="content-ext">
<div id="extra">
<h1>Extra</h1>
<p>Quisque nec metus sit amet nunc tincidunt vehicula sit amet non nibh. Nullam risus orci, porttitor ut malesuada vel..</p>
</div>
<div id="contact">
<h1>Contact</h1>
<p>Quisque nec metus sit amet nunc tincidunt vehicula sit amet non nibh. Nullam risus orci, porttitor ut malesuada vel..</p>
</div>
</div>
<div id="footer"></div>
</div>
</body>
</html>
Does any has a decent solution for using floated elements within a container? And showing the container as overall background?
Well, there is two classic solutions:
Set "overflow: hidden;" to #container. It will clear floats but have drawback — if you have elements with "position: absolute;" inside container that must be positioned partially outside it, they will be cut by overflow.
Use clearfix hack on #container: http://www.webtoolkit.info/css-clearfix.html it will fix it for you and don't have this drawbacks but is more complex.
Not sure is it better then "display: table;" or not, through.
Leave Display:table, it's not a good solution for a simple problem.
You are starting with a simple layout, and it's a good practice to start with a clean and tested layout.
My favorite resources are:
Little boxes
Free-css
code sucks
good luck