CSS - full width element within narrow parent? - css

I have a blog post that is 960 pixels wide.
I want parts of this blogpost to cover 100% of the viewport (from left: 0 to right: 0). It's fairly easy to do with absolute positioning but using this approach it's impossible to clear these 100%-wide elements.
HTML:
<div class="wrapper">
<h1>A header</h1>
<p>Some content.</p>
<div class="out">
<blockquote>Some blockquote.<br/> Another line.<br/>And another.</blockquote>
</div>
<p>Clears don't work here and this content is invisible :( </p>
<p>And this sucks :( </p>
<div class="out">
<blockquote>And different blockquote.<br/> Another line.<br/></blockquote>
</div>
<p>Also this is behind blockquote as well.</p>
</div>
CSS:
body {
position: relative;
}
.wrapper {
margin: 0 auto;
padding: 15px;
background: #eee;
width: 400px;
height: 1000px;
}
.out {
position: absolute;
right: 0;
left: 0;
padding: 15px;
background: #aaa;
width: 100%:
}
blockquote {
margin: 0 auto;
width: 400px;
}
Here's a demo:
http://jsfiddle.net/2rC2S/1/
Note: all blockquotes have different height so I can't set it for them. I don't want to use JavaScript (because it's fairly easy to get elements height, set this and boom, but who renders content with JS?!).

You may do this by using before and after pseudo selectors as follows
.out:before, .out:after {
content:"";
background: black;
position: absolute;
top: 0;
bottom: 0;
width: 9999px;
}
.out:before {
right: 100%;
}
.out:after {
left: 100%;
}
http://jsfiddle.net/kM3Gf/
you may find original article here http://css-tricks.com/examples/FullBrowserWidthBars/
still I am not sure about browser compatibility!

Maybe you can avoid setting the width for the wrapper and instead set it for each of the content elements?
An absolutely positioned element won't take up space in the document and thus won't push any content down.
See this DEMO
.wrapper h1, .wrapper p {
margin: 0 auto;
padding: 15px;
background: #eee;
width: 400px;
}

Related

Can I add a ::before element to the top of it's parent responsively without knowing the height

I want do add a diagonal stripe to the top of my div. I'll use an SVG to create it (or I could use CSS). I could do this with media queries but I wanted to know if there was a way to automatically work this out using CSS.
I can do this if I know the height, and I can manually add the height to the media queries (it's not a very long job). But surely there is a cleverer way?
I tried looking at calc, but again it relies on us knowing the width or a percentage or similar.
I don't want to use JavaScript.
Here's my code:
.itemTitle--padder {
position: absolute;
bottom: 0;
width: 100%;
}
.itemTitle--padder::before {
background: url('../../../../images/joomla-london-brand-assets/videos-diagonal-background.svg?5d167918') no-repeat;
background-size: cover;
width: 100%;
height: 100%;
position: absolute;
top:0;
left:0;
display: block;
content: "";
margin-top: -56px;
}
.itemTitle--holder {
position: relative;
width: 100%;
height: 100%;
background: $dark-blue;
padding: 0.25rem 0.5rem;
}
<div class="itemTitle--padder">
<div class="itemTitle--holder">
<div class="itemTitle">
The Title of thing I'm creating
</div>
</div>
</div>
At the moment it relies entirely on my negative margin. Is there another way to achieve this?
The main reason for trying to achieve this is because content within the .itemTitle div can be a variable length
After Gregs suggestion I realised that using ::before wasn't really a great way to acheive this as it would always inherit something from it's parent.
I ended up doing this by using two divs.
HTML
<div class="itemTitle--padder">
<div class="itemTitle--image">
<img src="../../../../images/joomla-london-brand-assets/videos-diagonal-background.svg" alt="" width="100%" height="auto">
</div>
<div class="itemTitle--holder">
<div class="itemTitle">
The Title
</div>
</div>
</div>
SCSS
.itemTitle--padder {
position: absolute;
bottom: 0;
width: 100%;
}
.itemTitle--holder {
position: relative;
width: 100%;
height: 100%;
background: $dark-blue;
padding: 0.25rem 0.5rem;
}
.itemTitle--image {
width: 100%;
img {
position: absolute;
bottom: calc(100% - 1px);
}
}

Why defining body attributes in css?

I have seen some web design lessons that always start with a css like this:
body,html {
display: block;
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
I'm trying to figure out what's the point of declaring attributes like width, height or display for body and html that are, if I'm not wrong, by default in browsers.
I thought it would be to prevent and undefined return or similar when accessing the css with js, but the result is the same when the attributes are defined in the css than when left to default:
console.log($("BODY").css('width')); // Always returns the width of the body
I also thought it could be to start the inheritance in cascade elements, but a div inside the body inherits the value just the same.
Anybody knows a solid reason for this approach? any browser / device issue I have missed? future compatibility? plain pedantry?
I'm kind of curious about it.
I found a good reason to define the html and body width and height to 100%. Say you want to vertically align a relative positioned div, you need to put it into an absolute positioned container:
html,
body {
margin: 0;
padding: 0;
}
#container {
position: absolute;
width: 100%;
height: 100%;
}
#main {
background: lightgrey;
position: relative;
top: 50%;
transform: translateY(-50%);
width: 100px;
height: 100px;
text-align: center;
margin-left: auto;
margin-right: auto;
}
<div id="container">
<div id="main">
<h1>MY DIV</h1>
</div>
</div>
But, setting the body width and height to 100% you get an absolute positioned container that covers the whole window:
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
#main {
background: lightgrey;
position: relative;
top: 50%;
transform: translateY(-50%);
width: 100px;
height: 100px;
text-align: center;
margin-left: auto;
margin-right: auto;
}
<div id="main">
<h1>MY DIV</h1>
</div>
You get the same result, but it saves you a div element.

make position:fixed DIV fit its parent container without javascript

Here is the code. I want the DIV.fixed-nav (position:fixed) to completely fit its parent DIV.container of which width may change. Is there a pure CSS solution for this?
CSS:
body {
margin: 0;
padding: 0;
}
.container {
border: 1px solid #000000;
margin: 0 auto;
max-width: 600px;
min-width: 400px;
}
.fixed-nav {
background-color: red;
height: 20px;
position: fixed;
width: 100%;
top: 0;
z-index: 99;
}
.content {
background-color: green;
height: 100px;
margin-top: 20px;
}
HTML:
<div class="container">
<div class="fixed-nav">
</div>
<div class="content">
</div>
</div>
Please check the DEMO.
The problem with fixed is that it will always be relative to the browser window. So if you set 100% height on your fixed container it will be 100% of the browser window.
The only way I could think of to achieve this is to use jQuery. Or if you don't need the menu to be fixed and it could be absolute then height 100% will work.

Best way to flow content out of the body

I'm working on a blog layout, where some info (blue box) is taken out from the post's body like this:
http://jsfiddle.net/rhr96/
What is the best of doing that?
Currently I'm doing:
position: absolute;
margin-left: negative value;
to flow the blue box to the left.
But I could also do:
position: relative;
float: left;
right: some px;
Any of these considered better? Or are there any other method?
Short Answer: POSITION ABSOLUTE
Reason: Designers use position: absolute because that is the right way to take out the element from the normal document flow, using float: left; wont take out the blue box out of the document flow...
Edit: Just understood what you actually wanted, here I've made a new 1, you can check it out..
Demo
HTML
<div class="container">
<div class="block">1</div>
<div class="content">This is a question</div>
</div>
CSS
.container {
height: 200px;
width: 500px;
border: 1px solid #eeeeee;
margin: 30px;
position: relative;
font-family: Arial;
}
.block {
position: absolute;
height: 80px;
width: 60px;
background-color: #eeeeee;
top: 10px;
left: 10px;
font-size: 36px;
text-align: center;
}
.content {
width: 410px;
float: right;
margin: 10px;
font-size: 18px;
}
I think the best way of doing this, may actually be this (well, I say best, I guess that's a matter of opinion in most cases)
HTML:
<div class="container">
<div class="outside">
hi
</div>
<div class="inside">
<p>Blah blah blah</p>
</div>
</div>
CSS:
.container { margin: 20px auto; width: 400px; }
.outside { background: #d8d8d8; float: left; margin: 0 5px 0 0; padding: 5px; }
.inside { background: #000; color: #fff; margin: 5px 0; overflow: hidden; }
Obviously you can repeat this multiple times on the same page (as I imagine you may if this is for blog posts)
EDIT: My answer uses floats to take the element out of the normal flow, the use of overflow: hidden on the content means that it doesn't wrap underneath the floated element.
(If you don't know much about overflow I'd suggest reading about it, it can be useful for all sorts of things, e.g. float clearing)

Overlap <div>s in CSS

So, I've seen tons of questions about this, but I would like a personal example. I'm rather new to programming, so I may be a little stupid...
Anyway, I have two <div>s, one with id bg and the other with class player.
This is what it looks like:
The red box is the player, and the large image is the bg.
I need the player to start in the center of the bg.
The bg is 640px X 640px.
This is the code I have so far in my CSS file:
#bg {
width: 640px;
height: 640px;
top: 0;
left: 0;
}
.player {
position:relative;
background-color:#FF0000;
width: 32px;
height: 32px;
top: 0px;
left: 0px;
}
Try changing your stylesheet to:
#bg {
width: 640px;
height: 640px;
top: 0;
left: 0;
position: relative;
}
.player {
position: absolute;
background-color: #FF0000;
width: 32px;
height: 32px;
top: 320px;
left: 320px;
z-index: 1;
}
And your HTML should look like this:
<div id="bg">
<!-- your bd code here -->
<div class="player"></div>
</div>
position: relative is relative to where the object would be placed normally. In your example, it would normally come below the first div, so that's where it will stay. (In other words position: relative used with a positioning of 0 won't move the objet anywhere.)
You could add top: -320px; left: 320px. That would position it it the space of the first div. But maxksbd19's answer is probably the better solution for your ultimate goal.
I try and avoid absolute positioning as it does not adapt to the container size and a change to the container requires you to go through your css and change all the absolute values.
I would do the following
CSS:
#bg {
overflow: auto; /* stops the .player from from moving #bg down */
width: 640px;
height: 640px;
background-color: blue;
text-align: center; /* center child div in IE */
}
.player {
background-color: White;
width: 32px;
height: 32px;
margin: 0 auto; /* center div in parent for non IE browsers */
margin-top: 304px; /* 50% from top minus div size */
}
HTML:
<div id="bg">
<div class="player"></div>
</div>
Now you only have to keep track of the top margin of the child container.

Resources