CSS Positioning - Best Way To Position - css

Whats' the best way to position? Float, Relative, Absolute?
Lets say I want to position something like this:
How do I position something like this and what's the best way to do it?
Float, Relative, Absolute?

If you want a fluid layout, use floats.
Positioning elements relative/absolute causes them to display as inline therefore a height/width is required and they then become non-fluid.

You would have to create a div that acts as a container
Then create three more divs one for the right side and two for the left side.
I have created a jSfiddle file for you to reference. Something quick I created
http://jsfiddle.net/wSp7F/
It all depends on which type of layout you are going for. Responsive, Fluid or Fixed.
HTML
<div id="container">
<div id="rightside">
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
</div>
<div class="leftside">
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
</div>
<div class="leftside">
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
</div>
</div>​
CSS
#container {width:500px;position:relative;overflow:Hidden;outline:1px solid red}
.leftside {float:left;width:225px;margin:0 0 5px 0}
#rightside {float:right;width:200px}
.leftside,#rightside {outline:1px solid black;padding:5px}
​

There isn't a single best way, but here's how I'd do it.
http://jsfiddle.net/Wuj35/
HTML
<div id="wrapper">
<div id="div3">div 3</div>
<div id="div1">div 1</div>
<div id="div2">div 2</div>
</div>
CSS
#wrapper{
width:450px;
}
#div1,
#div2,
#div3{
float:left;
margin:1em;
padding:1em;
border:5px solid #000;
border-radius:8px;
}
#div1,
#div2{
width:150px;
height:75px;
}
#div2{
clear:left;
}
#div3{
float:right;
width:150px;
height:225px;
}

Related

Show/Hide DIV not working- HTML/CSS only

So I have a problem with my hiding/showing of DIVs without the use of JavaScript.
So basically, the moment you click on a link within the div that has been selected, the div will close and you won't be taken to that link.
The code is below. I am not using JavaScript for this so please don't give me that as a solution/suggestion. I am using Chrome, however I have tested with other browsers and it is a problem within them as well.
I've just filled it with some test data, but it functions exactly the same as with the live data. Just click on the heading, and try to click on the 'Google' link and you'll understand my problem.
If anyone can fix it, that would be great because I can't seem to do it. :/
<html>
<head>
<style>
.collapse > * + *{
display:none;
}
.collapse > *{
cursor:pointer;
}
.collapse:focus{
outline:none;
}
.collapse:focus > * + *{
display:block;
}
.collapse h1 {
background-color:#BF3131;
}
</style>
</head>
<body>
<div class="collapse" tabindex="1">
<h1 id="test">LOREM IPSUM</h1>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat</p>
<p>Google
</div>
<div class="collapse" tabindex="1">
<h1 id="lol">Test</h1>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat</p>
<p>Google
</div>
</body>
</html>
This was originally from : Show hide divs on click in HTML and CSS without jQuery
Unfortunately CSS3 doesn't support parent selectors. :(
So the only way to do it is to use javascript like the jQuery parent method.
CSS Selectors Level 4 support parent selectors but it wouldn't work on every browsers right now.
You can test it :
http://css4-selectors.com/browser-selector-test/
As you already figured out, this is some kind of timing problem. When you are clicking on the Link your div loses focus and the content collapses before the actual click action is triggered ...
I would use the checkbox hack for that kind of thing. Something like that maybe:
div {
outline: none;
cursor: pointer;
}
input {
display: none;
}
input:checked ~ p {
display: block;
}
label ~ p {
display:none;
}
h1 {
cursor: pointer;
background-color:#BF3131;
}
<div tabindex="1">
<input id="header-1" type="radio" name="headers">
<label for="header-1">
<h1 id="test">LOREM IPSUM</h1>
</label>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat</p>
<p>Google
</div>
<div tabindex="1">
<input id="header-2" type="radio" name="headers">
<label for="header-2">
<h1 id="lol">Test</h1>
</label>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat</p>
<p>Google
</div>

How can I make the image stay within certain bounds while also floating?

I have a (WordPress) page on which there are a list of different programs. On the left-hand side is a text description of the program and on the right is an image. Back in the day I would have used a table to make this happen:
<table>
<tr>
<td>Text goes here.</td>
<td><img src="myimage.jpg"></td>
<tr>
</table>
Now I am trying to align it w/out tables:
<p style="text-align: left">Text goes here.</p>
<img class="alignright size-medium wp-image-119" src="imageurl.jpg" width="300" height="199" />
<hr>
Problem is that if there isn't enough text, the image begins to take up space in the next program's section. Here is a screenshot:
You can see that the placeholder image is not remaining above the HR and beginning to slide into the next program's segment. How can I prevent this?
You can still use the table display model:
p {
display:table;
width:80%;
margin:auto;
}
p span {
display:table-cell;
vertical-align:top;
}
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.
<span><img src="http://lorempixel.com/300/199"/></span></p>
<hr/>
Personally, I would use ul and li to get the job done.
<ul id="program_list">
<li id="program_item" class="item01">
// HTML for program item goes here
</li>
<li id="program_item" class="item02">
// HTML for program item goes here
</li>
</ul>
Then you would use some CSS to set the width of the ul and make sure that each li is the full width, and does not float the next item, with this you could also so a border-bottom: to separate each program.
#program_list {
width: 600px;
}
#program_item {
width: 100%;
overflow: hidden;
float: none;
padding: 10px 0;
border-bottom: 1px solid #666;
}
Assuming you have already cleared your CSS initially; if not, you may have to add more CSS to style correctly.

Paragraph does not wrap with div

I am puzzled by a wrap that works 90% of the time - but breaks in specific word compositions.
It's based on the css-tricks "Don't overthink it grids" blog post, so it seems the problem already existed there at the end of the tutorial.
Live/Demo/Code: http://codepen.io/anon/pen/xdFhr
As you can see the following paragraph:
BIDFIK roolbool rackorack op deenoopaloomba ka jandalop me pep google lopski
perfect preference group call later go take foot pep universal.
Flows out of the parent div. Why?
<h1>Don't Overthink It Grids <em>(while we wait for flexbox!)</em></h1>
<div class="grid">
<div class="col-2-3">
<div class="module">
<h3>2/3</h3>
<p>BIDFIK roolbool rackorack op deenoopaloomba ka jandalop me pep google lopski perfect preference group call later go take foot pep universal.</p>
</div>
</div>
<div class="col-1-3">
<div class="module">
<h3>1/3</h3>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies</p>
</div>
</div>
</div>
<div class="grid grid-pad">
<div class="col-2-3">
<div class="module">
<h3>2/3 (Opt-in Outside Padding)</h3>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>
</div>
</div>
<div class="col-1-3">
<div class="module">
<h3>1/3</h3>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam.</p>
</div>
</div>
</div>
<div class="grid grid-pad">
<div class="col-1-8">
<div class="module">
<h3>1/8</h3>
</div>
</div>
<div class="col-1-8">
<div class="module">
<h3>1/8</h3>
</div>
</div>
<div class="col-1-8">
<div class="module">
<h3>1/8</h3>
</div>
</div>
<div class="col-1-8">
<div class="module">
<h3>1/8</h3>
</div>
</div>
<div class="col-1-8">
<div class="module">
<h3>1/8</h3>
</div>
</div>
<div class="col-1-8">
<div class="module">
<h3>1/8</h3>
</div>
</div>
<div class="col-1-8">
<div class="module">
<h3>1/8</h3>
</div>
</div>
<div class="col-1-8">
<div class="module">
<h3>1/8</h3>
</div>
</div>
</div>
<div class="grid grid-pad">
<div class="col-1-4">
<div class="module">
<h3>1/4</h3>
</div>
</div>
<div class="col-1-2">
<div class="module">
<h3>1/2</h3>
</div>
</div>
<div class="col-1-4">
<div class="module">
<h3>1/4</h3>
</div>
</div>
</div>
CSS:
* {
#include box-sizing(border-box);
}
$pad: 20px;
.grid {
background: white;
margin: 0 0 $pad 0;
&:after {
/* Or #extend clearfix */
content: "";
display: table;
clear: both;
}
}
[class*='col-'] {
float: left;
padding-right: $pad;
.grid &:last-of-type {
padding-right: 0;
}
}
.col-2-3 {
width: 66.66%;
}
.col-1-3 {
width: 33.33%;
}
.col-1-2 {
width: 50%;
}
.col-1-4 {
width: 25%;
}
.col-1-8 {
width: 12.5%;
}
.module {
padding: $pad;
background: #eee;
}
/* Opt-in outside padding */
.grid-pad {
padding: $pad 0 $pad $pad;
[class*='col-']:last-of-type {
padding-right: $pad;
}
}
body {
padding: 10px 50px 200px;
background: url(http://s.cdpn.io/3/dark_wall_#2X.png);
background-size: 300px 300px;
}
h1 {
color: white;
em {
color: #666;
font-size: 16px;
}
}
It was invisible chars/hidden characters.
I removed it with a Vim command:
:%s/\%xa0/ /gc
Where xa0 is the hex code of the char (0xA0).

css layout issue when setting a section height

I am writing a basic website in HTML5, i have all the site structure in place and so far everything has been working well.
However I have one section that seem to go off on its own a bit.
as soon as I had a height to the section the sections moves to the top of the article behind the top two section where the actual content within the section stays in place.
<article><section class="welcome-box">
<section class="welcome-wrapper">
<div class="welcome-hello">
<div class="welcome-hellotext">HELLO, WELCOME TO SAA RECRUITMENT </div>
</div>
<div class="welcome-line"></div>
<div class="welcome-textbox">
<div class="welcome-textboxtext">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.</div>
</div>
<div class="welcome-button">
<div class="welcome-buttontext">READ MORE</div>
</div>
</section>
</section>
<section class="home-slider">
<div class="slider-wrapper">
<div id="slider">
<div class="slide1">
<img src="images/slide_1.jpg" alt="" />
</div>
<div class="slide2">
<img src="images/slide_2.jpg" alt="" />
</div>
<div class="slide3">
<img src="images/slide_3.jpg" alt="" />
</div>
<div class="slide4">
<img src="images/slide_4.jpg" alt="" />
</div>
</div>
<div id="slider-direction-nav"></div>
<div id="slider-control-nav"></div>
</div>
</section>
<!-- Slider Script -->
<script type="text/javascript">
$(document).ready(function() {
var slider = $('#slider').leanSlider({
directionNav: '#slider-direction-nav',
controlNav: '#slider-control-nav'
});
});
</script>
<!-- End Slider Script -->
<section class="about-box">
<div class="about-boxtitle">TITLE HERE</div>
<div class="about-boxline"></div>
<div class="about-boxtext"></div>
</section>
<section class="news-box">
<div class="news-boxtitle">NEWS</div>
<div class="news-boxline"></div>
<div class="news-boxtext"></div>
</section>
<section class="clients-box">
<div class="clients-boxtitle">CLIENTS</div>
<div class="clients">client</div>
<div class="clients">client</div>
<div class="clients">client</div>
</section>
</article>
The section I am having trouble with is the one with a class of "clients-box" (very last section)
Here is the CSS:
.clients-box {
width: 960px;
margin-top: 10px;
background-color: #FFF;
}
ul.clients {
width: 940px;
height: 40px;
margin: 0 auto 0;
}
ul.clients li.client {
width: 150px;
height: 40px;
display: inline-block;
background-color: #039;
clear: both;
The website is live here: http://dev.saarecruitment.com/
.clients-box needs either float: left or float: right. You can add margin: 10px auto to center it with 10px top/bottom margins.
You have used float on all the above <sections> which means that they are not occupying space. Thus this box got to the top, under the floated elements.
Easiest way would be to apply a float: left to this box as well.
An alternative method, inspired by #Mr. Alien would be to just apply clear:left to this block.

How to make a <div> appear in front of regular text/tables

I have been trying to make a DIV box appear in front of the text/tables that I have on a webpage.
The DIV is made visible via a button press; but when visible it automatically moves the text/table downward and includes the DIV content above it.
Can anyone help?
You can use the stacking index of the div to make it appear on top of anything else. Make it a larger value that other elements and it well be on top of others.
use z-index property. See Specifying the stack level: the 'z-index' property and
Elaborate description of Stacking Contexts
Something like
#divOnTop { z-index: 1000; }
<div id="divOnTop">I am on top</div>
What you have to look out for will be IE6. In IE 6 some elements like <select> will be placed on top of an element with z-index value higher than the <select>. You can have a workaround for this by placing an <iframe> behind the div.
See this Internet Explorer z-index bug?
z-index only works on absolute or relatively positioned elements. I would use an outer div set to position relative. Set the div on top to position absolute to remove it from the flow of the document.
.wrapper {position:relative;width:500px;}
.front {
border:3px solid #c00;
background-color:#fff;
width:300px;
position:absolute;
z-index:10;
top:30px;
left:50px;
}
.behind {background-color:#ccc;}
<div class="wrapper">
<p class="front">Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>
<div class="behind">
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
<table>
<thead>
<tr>
<th>aaa</th>
<th>bbb</th>
<th>ccc</th>
<th>ddd</th>
</tr>
</thead>
<tbody>
<tr>
<td>111</td>
<td>222</td>
<td>333</td>
<td>444</td>
</tr>
</tbody>
</table>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
</div>
</div>
It moves table down because there is no much space, try to decrease/increase width of certain elements so that it finds some space and does not push the table down. Also you may want to use absolute positioning to position the div at exactly the place you want, for example:
<style>
#div_id
{
position:absolute;
top:100px; /* set top value */
left:100px; /* set left value */
width:100px; /* set width value */
}
</style>
If you want to appear it over something, you also need to give it z-index, so it might look like this:
<style>
#div_id
{
position:absolute;
z-index:999;
top:100px; /* set top value */
left:100px; /* set left value */
width:100px; /* set width value */
}
</style>
You may add a div with position:absolute within a table/div with position:relative. For example, if you want your overlay div to be shown at the bottom right of the main text div (width and height can be removed):
<div style="position:relative;width:300px;height:300px;background-color:#eef">
<div style="position:absolute;bottom:0;right:0;width:100px;height:100px;background-color:#fee">
I'm over you!
</div>
Your main text
</div>
See it here: http://jsfiddle.net/bptvt5kb/
make these changes in your div's style
z-index:100; some higher value makes sure that this element is above all
position:fixed; this makes sure that even if scrolling is done,
div lies on top and always visible
Use the display property in CSS:
<body>
<div id="invisible" style="display:none;">Invisible DIV</div>
<div>Another DIV
<button onclick="document.getElementById('invisible').style.display='block'">
Button
</button>
</div>
</body>
When the the display of the first div is set back to block it will appear and shift the second div down.

Resources