I've a HTML markup, where the typographical content are inside of <p>-tags. Between these tags, there I want to place some images. These images are always the same size: 100% wide, 50% high.
To avoid some distortions, I set a <div>-tag with this size and set the image as a background-image with the cover-size.
This <div> doesn't contains any content, except the background-image. So my sizing won't work, because I can't set it to position: absolute / fixed;, beacuase it wouldn't fit anymore between the <p>-tags.
So how I'm able to size the empty div without losing the the fit?
The HTML markup:
<div class="container">
<section class="about">
<div class="content">
<p>Lorem ipsum dolor</p>
<div class="img"></div>
<p>Lorem ipsum dolor</p>
</div>
</section>
</div>
And the CSS style
.container,
.container > section{
position: fixed;
height: 100%;
width: 100%;
}
.container > section{
overflow:auto;
}
.container > section > .content > p{
padding: 5% 15% 5% 15%;
font-family: Arial, sans-serif;
font-size: 1.8em;
line-height: 1.5;
}
.container > section > .content > .img{
width:100%;
height:50%;
background: url(http://www.hdcarwallpapers.com/walls/widescreen_lamborghini_lp710-wide.jpg) no-repeat center center;
background-size:cover;
}
And a CODEPEN DEMO
I think the problem is the height. Try removing the 50% height and instead add padding of 50%
.container > section > .content > .img{
display: block;
width:100%;
padding: 0 0 50% 0;
background: url(http://www.hdcarwallpapers.com/walls/widescreen_lamborghini_lp710-wide.jpg) no-repeat center center;
background-size:cover;
}
Here's a demo.
I'm not sure what problems you're referring to when you say you want to avoid 'distortions', as by far the simplest solution here is to include the image(s) themselves actually in the markup, and then add some styling to make them responsive.
However, you can achieve what you want with your approach (only browser support suffers). You know the ratio that you want the image to display at. First off, remove the width: 100% from the .img div (it's a div, it will fill the horizontal space). Then, add your 50% as padding:
.img {
padding-bottom: 50%;
}
When you set a height (or vertical padding here) as a percentage, you're actually setting it as a percentage of the width of the parent. That means your .img div will always have padding equal to half of the .content div, which is what you're after.
This is the same approach that's necessary to get fluid videos and iframes working. Check out this CSS Tricks article for a good explanation of what's going on.
Related
Unfortunately I have a problem that I just can not solve. I have a picture, which thanks to the css resizes depending on the size of the open window in your browser. But I must enter into this text and textbox. The problem comes when after you have centered the text and textbox with CSS, resizing the browser window, my text and my textbox do not follow the "image" but they go where they want.
for me to understand better my image looks like this: http://i41.tinypic.com/27xe6nm.jpg and I need that to be displayed inside the lines of the text and textbox. As stated previously, however, when the browser auto resizes the image, while placing on the div relative to the texbox text and margin-top: 10%; margin-bottom: 70%; etc. ... the text goes outside of the lines.
my CSS
#text1
{
position:absolute;
height: auto;
margin-left:40%;
margin-right:auto;
margin-top:-35%;
margin-bottom:auto;
width: auto\9;
}
div.imgg img
{
max-width: 100%;
height: auto;
width: auto\9; /* ie8 */
}
my HTML
<div class="imgg">
<img src="imgtest.png"/>
<div id="text1">
my text
</div>
</div>
Viewport units to the rescue: http://jsfiddle.net/X6yJB/. And, for older browsers just use a polyfill: http://html5polyfill.com/.
HTML:
<div class="imgg">
<img src="http://i41.tinypic.com/27xe6nm.jpg"/>
<div id="text1">
This example uses viewport units. This family
of measures includes vh, vw, vmin, and vmax. Because
the size of your image is affected by the width, the vw
units are used. Specifically, 1vw is 1% of the viewport
width. As viewport changes, so does your font-size.
Neat, huh?!
</div>
</div>
CSS:
.imgg {
position: relative;
}
.imgg > div {
position:absolute;
top: 2.5vw;
font: normal 2vw/2.5 Sans-Serif;
}
.imgg > img {
max-width: 100%;
width: auto\9;
}
jsBin demo (not perfect, just to get you an idea)
So your image is 100% wide...
Means we need to count your "paper lines" (can't believe I just did that!)
which results in ~23 and use that value to resize the text using CSS3's vw (viewport) unit:
100vw / 23 lines = your approx. font size
<div id="text1">
My text
</div>
#text1{
position:absolute;
background: url(imgtest.png) no-repeat center 0 / 100%;
height:100%;
width:100%;
padding-top: 1.38vw; /* used some here */
padding-left: 2vw;
font-size: calc(100vw / 22); /* But mostly here :) */
line-height: 1.145em;
}
I was under the impression that when using % or auto for margins on a div contained within another div the position would be calculated in respect to the parent div.
So if I have a div with height: 50%, margin-top: 25% and margin-bottom: 25% the box should centre vertically within the parent div.
When I do this though the div centres on the page not the parent div.
The CSS
div#header {
width: 100%;
height: 100px;
margin: 0px;
position: fixed;
}
div#leftnavigation {
height: 50%;
margin-top: 25%;
margin-bottom: 25%;
float: left;
}
And the HTML
<!--Title and navigation bar-->
<div id='header'>
<!--Left navigation container-->
<div id='leftnavigation'>
<p>efwfwgwegwegweg</p>
</div>
</div>
In my case there are other divs floated to the right of the one detailed above, but any one of them behaves the same way. I'm assuming I'm doing something daft but I've been over all the other questions I could find along these lines and still can't figure it out.
EDIT
Here's the JSFiddle as requested http://jsfiddle.net/ChtVv/
UPDATE
I've tried removing the margin constraints and setting the leftnavigation div to height: 100%, this works so the issue is with the margin attribute?
The reason it didn't work is that percentage-margins are percentages of the parent's width, not its height. You can tell this by using margin-top: 25px; margin-bottom: 25px;, and also by increasing the width of the right-panel in jsFiddle.
In all cases % (percentage) is a valid value, but needs to be used
with care; such values are calculated as a proportion of the parent
element’s width, and careless provision of values might have
unintended consequences.
W3 reference
CSS is tricky!! :D
This is a borrowed technique to centre vertically and horizontally, but it would involve changing your HTML and CSS. I am not sure how flexible you are with your code:
CSS:
#outer {width: 100%; border: 3px solid red;}
#middle {width: 100%; text-align: center;border: 3px solid green;}
#inner {width: 200px; margin-left: auto; margin-right: auto; text-align: left;border: 3px solid blue;}
/* Courtesy: http://www.jakpsatweb.cz/css/css-vertical-center-solution.html */
HTML
<!--Title and navigation bar-->
<div id='outer'>
<!--Left navigation container-->
<div id='middle'>
<p id="inner">efwfwgwegwegweg</p>
</div>
</div>
You can build upon this to achieve whatever you are after!
fiddle: http://jsfiddle.net/pratik136/ChtVv/2/
Ok, so there are a lot of reasons why this would not work.
The main reason would be that your container has position:fixed;
When adding position:fixed; to a element, it no longer reserved it's space in the DOM and won't contain it's children.
I have made a example of the best way (in my Opinion) to center your child both Vertically & Horizontally
Here is a demo.
Demo
And here is the code.
<div id="container">
<div id="child"></div>
</div>
#container{
width:100%;
height:500px;
background:#CCC;
margin:0;
}
#child{
width:50%;
height:50%;
background:#EEE;
position:relative;
top:25%;
left:25%;
}
I have two divs in my page: leftpart and rightpart which has the following css:
.leftpart{
width:280px;
background:#0054a6;
color:#fff;
padding-top:40px;
height:100%;
min-height:637px;
box-shadow:3px 3px 10px #bebebe;
position:relative;
}
.rightpart{
width:75%;
padding-left:10px;
}
I want this sidebar(leftpart) till the end of my page(till the bottom). I've set the height to 100% but when I minimize the browser it shows the white space below the bar instead of showing blue background. I mean it does not take its height as 100%. How can I get that?
For a full length sidebar your best bet is probably the old faux columns method. You could do this in CSS but this is probably easier for you.
Put basically you want an image with your column background's in a thin long strip. You then add this as a background image to your parent div and it acts as pretend full height columns.
eg.
.container {
background: url(your_image) repeat-y left top;
}
<div class="container">
<div class="sidebar">SIDEBAR</div>
<div class="content">CONTENT</div>
</div>
You can read more about it here - http://www.alistapart.com/articles/fauxcolumns/
If you want to try this in CSS you could try the negative margins trick.
You set your container up with overflow set to hidden, then on each div add negative margin-bottom and equal positive padding-bottom.
#container { overflow: hidden; }
#container div { float: left; background: #ccc; width: 200px; margin-bottom: -2000px; padding-bottom: 2000px; }
#container .col2 { background: #eee; }
<div id="container">
<div>
SIDEBAR
</div>
<div class="col2">
CONTENT
</div>
</div>
So yet another question about vertically aligning an image within a div, but I think mine is different than the others I've found on here. I can't seem to find a solution that works for my situation.
I have a DIV that is 100% width (to it's container, which is floating left and has a set pixel width) and has a set pixel height. I have an image inside that I am positioning absolute to get it to the background of content within the DIV. The image is fluid with a width of 100%.
All works well, but I want to get the image to vertically align to the middle of the container and height is unknown.
Here is some sample code that shows what I'm trying to do:
<div class="container">
<div class="image-wrapper">
<img src="http://farm5.staticflickr.com/4111/4968056789_d872094672_o.jpg"
width="100%" />
</div>
<p>Some text</p>
</div>
And some sample CSS:
.container {
width:100%;
margin-top:10px;
height:100px;
overflow:hidden;
}
.image-wrapper {
position: relative;
}
.image-wrapper > img {
position: absolute;
z-index: -1;
}
p {
text-align: center;
padding-top: 10px;
color:#fff;
font-weight: bold;
}
But the flower should show up with it's center visible within the container div.
Any thoughts? I'm trying to avoid any Javascript sizing (the outer container, not shown in this sample, is already being sized). I'm not opposed to more DIVs, tables.. whatever you got!
A jsFiddle to demo this:
http://jsfiddle.net/JonMcL/sNz9h/
Why not go for the background-image property? That allows vertical centering...
http://jsfiddle.net/urrWS/
Assuming you want to only scale the image down and not stretch it beyond its native resolution this should do the trick. A little bit of jQuery is involved but it's minimal. Essentially, this adjusts the top-margin of the IMG on the window.resize event.
HTML
<div id="container">
<img id="image" src="image.jpg"> <!-- native size is 480x300 -->
</div>
CSS
#container {
margin: auto;
width: 80%;
height: 300px;
border: 1px solid gray;
}
#image {
display: block;
width: 100%;
max-width: 480px;
margin: auto;
}
jQuery
function adjustImage() {
$("#image").css('margin-top', ($("#container").height() - $("#image").height()) / 2);
}
$(window).load(function() {
adjustImage();
$(window).resize(function() {
adjustImage();
});
});
If I get what you need I would suggest setting the background image via css, then you can set the position correctly etc.
.container {
width:100%;
margin-top:10px;
background-image:url("http://farm5.staticflickr.com/4111/4968056789_d872094672_o.jpg");
background-repeat:no-repeat;
background-position:left center;
height:100px;
overflow:hidden;
}
http://jsfiddle.net/sNz9h/6/
How should I make this with CSS:
I would like to have 2 divs or more and their width should be in percent, but the margin between the divs should be fixed, in this example 30px
The problem for me is the margin between the two divs because I can put the divs into a bigger div and set left and right padding to 30px and thats ok, but what should I do with the margin between the two divs?
If I try to add for example to the first div margin-right:30px; then the width of the div will be 70% + 30px what will be overall greater than 100% and the second div will fall down.
So what is the solution for this?
Is this close enough?
Live Demo
HTML:
<div id="container">
<div id="left"><div id="left2">leftgggg</div></div>
<div id="right">right</div>
</div>
CSS:
#container {
margin: 0 30px 0 30px;
overflow: hidden;
background: #f3c
}
#left {
float: left;
width: 70%;
position: relative;
left: -30px;
}
#left2 {
height: 200px;
margin: 0 0 0 30px;
background: #ccc
}
#right {
height: 200px;
float: right;
width: 30%;
background: #666
}
Calc support is decent now, so you can use that to mix and match units. Using that, you can come up with something that works pretty well:
http://jsfiddle.net/CYTTA/1/
#a {
margin-left: 30px;
width: calc(70% - 30px - 15px);
}
#b {
margin-left: 30px;
margin-right: 30px;
width: calc(30% - 30px - 15px);
}
You may have to prefix calc with -webkit or -moz.
The width of the first one is 70% minus the left margin (30px) and minus half the middle margin (15px). The second one is 30% minus the right margin (30px) and minus half the middle margin (15px).
While the widths won't be exactly equal to 70% and 30%, you'll have a fluid layout with fixed margins.
I found a way to do this keeping the ratio of the widths of the containers exactly 70% : 30%. Try this, works for me...
HTML:
<div id="page">
<div id="a"><div id="aWrap">This is 70% of the available space...</div></div>
<div id="b"><div id="bWrap">This is 30% of the available space...</div></div>
</div>
CSS:
*{
margin:0;
padding:0;
}
#page{
margin:30px;
}
#a{
width:70%;
float:left;
}
#aWrap{
margin-right:15px;
background:#aaa;
}
#b{
width:30%;
float:right;
}
#bWrap{
margin-left:15px;
background:#ddd;
}
Best of luck!
It may be obvious, and you've probably already twigged, but (70% + 30% + 30px) > 100%. Without some kind of calculative ability, this won't work, and CSS2 doesn't appear to have that ability. Javascript could do it, as another poster has suggested, and CSS 3 is due to add it, apparently.
Not that it's a solution to your original enquiry, but you can enforce a fixed width on the right hand container, and maintain fluidity on the left.
<div style="margin-left: 30px; float: right; width: 270px;">
<p>Content text ...</p>
</div>
<div style="margin-right: 300px;">
<p>Sidebar text ...</p>
</div>
The original commenter is correct though, your best bet is one or the other.
Here my solution.
html
<div id="wrapper">
<div id="left"></div>
<div id="rightWrapper">
<div id="right"></div>
</div>
</div>
css
#wrapper {
margin:0 30px;
}
#left {
width:70%;
height:200px;
background:black;
float:left;
}
#rightWrapper {
margin-left:99px;
}
#right {
width:30%;
height:200px;
float:right;
background:grey;
}
Demo: http://jsfiddle.net/GEkG7/1/
Yeah, my solution was similar to others. A #wrap has 30px padding, #main and #side have their percentages set and floated left and right respectively. #main has an extra <div> inside it with 30px of right margin.
http://jsfiddle.net/Marcel/FdMFh/embedded/result/
Works fine in all the modern browsers I have installed (Chrome, Firefox, Safari, Opera, IE9 RC), I'm sure it'll break down somewhere older but should be fixable.
Thirtydot has a nice answer (up vote from me) to adjust the left positioning of the div relative to its container, I would only suggest that it may need testing in certain browsers, such as older versions of Internet Explorer.
Alternatively you could consider that adjusting a left position by a fixed amount is more confusing than just applying a different width.
Your also asking for a fluid width and a fixed margin, overall this is no longer a fluid layout... your 30px will look the same in a large or small resolution.. but your widths will change, either fix the widths to pixels or set the margin to a percentage (Maybe try using max-width for some browsers too for bigger resolutions). Newer browsers also adjust a pixel layout when increasing the text/zoom size, older browsers require use of EMs for text size changes.
example with percentages and padding:
#container {
margin: 0 8% 0 8%;
overflow: hidden;
background: #f3c
}
#left {
float: left;
width: 62%;
position: relative;
padding-right: 8%;
}
You can use the javascript onload and onresize functions. In each you first find the width of the container grid and then calculate the width of your 70pc and 30pc grids in pixels and set them via JS.
For example use the following code in your onload and onresize functions for the page:
container_width = document.getElementById('container_box').style.width
width_70 = (container_width - 90) * 0.7
width_30 = (container_width - 90) * 0.3
document.getElementById('seventy_pc_box').style.width = width_70
document.getElementById('thirty_pc_box').style.width = width_30