I'm working on a template for WordPress and need to render images to the left and to the right side of the main content div.
I have a wrapping div of 100% width, so the images don't overflow the screen, then I have given the images position: absolute (relative to the content div), and a negative value (the width of the image) on the right/left parameters.
This works fine, and renders the images nicely to the left and right of the main content. But the problem is, these images need to change every once in a while, and the width of the images varies, so the left and right offsets are not correct anymore.
How can I render the images outside of the div, without knowing the width? I've tried Left/Right:-100%;, but that doesnt work.
The images are purely decorational, and dont have to be (fully) visible when the screen is too small.
Thanks for any help!
If you can't specify the width and want to display the image as is (not scaling it), you can float the images to the right and left and use overflow:hidden on the content
.left {
float: left;
margin-right: 20px;}
.content {
overflow:hidden;}
.right {
float: right;
margin-left: 20px;}
However, you will need to have the following struture in your html for the layout to render correcty:
.wrap > img + img + .content
http://jsfiddle.net/h6u65hp8/1/
How about something like this? http://jsfiddle.net/usL5vcqg/
<div id="wrap">
<div id="leftImage">
<img src="https://www.google.com.au/images/srpr/logo11w.png"/>
</div>
<div id="content">
text goes here
</div>
<div id="rightImage">
<img src="https://www.google.com.au/images/srpr/logo11w.png"/>
</div>
</div>
* {
margin:0;
padding:0;
}
#wrap {
width:100%;
}
#leftImage {
width:20%;
float:left;
}
#rightImage {
width:20%;
float:left;
}
#content {
width:60%;
float:left;
}
img {
width:100%;
}
The images will scale depending on the size of the screen and they will always sit on either side of the content.
I actually came up with something myself. The content div has a static width, so what I did was switch the left and right parameters around, and set it to the width of the content div (plus some padding).
<div class='prevent-overflow'>
<div class='content'>
<img src='http://placehold.it/200X200' class='left-side-image' />
<img src='http://placehold.it/200X200' class='right-side-image' />
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec at libero nec ex vestibulum luctus ut eget odio. Nunc mollis, nulla eget cursus porttitor, neque sem rhoncus est, vel consequat leo lacus at nisl. Nulla ac sem eget lorem malesuada malesuada. Fusce in erat tempus, elementum sem id, dapibus sapien. Vivamus vestibulum enim nulla, id porttitor odio condimentum ac. Pellentesque lacinia mollis elit tristique luctus. Vivamus luctus ultricies ullamcorper. Vivamus eros orci, luctus et pellentesque commodo, egestas et velit. Duis ante velit, feugiat sed gravida ac, tempus sed odio. Praesent tincidunt risus id ex placerat rhoncus. Maecenas sit amet pharetra neque. Aenean dapibus a velit quis ultricies. Quisque aliquam erat nec est rutrum, nec ornare urna ultrices. Quisque accumsan purus non ex sagittis, in facilisis massa facilisis. Vivamus fermentum libero dictum neque laoreet, sit amet pretium dui luctus.
</div>
</div>
.prevent-overflow {
overflow:hidden;
width:100%;
}
.left-side-image{
position:absolute;
right:320px;
}
.right-side-image{
position:absolute;
left:320px;
}
.content {
width:300px;
margin:0 auto;
position:relative;
}
In my initial code, I had a negative Left/Right (depending on the size of the image) in the .left-side-image and .right-side-image classess.
http://jsfiddle.net/6wp1h52c/
Related
I am trying to make responsive one of my sites... :)
In standard view (pc), I have a div container called "article" containing more divs on 2 cols, all of these have width: 350px.
Resizing the screen, I want the divs into "article" are centered into it in a single col.
I have tried a lot of ways, including margin: 0 auto, display: inline-block, display: table, but does not works, and divs are always flotted to left of "article". :(
Using this method in media query:
left: 50%
margin-left: 175px;
It center the div when "article" has width: 707px (708 is just I need for divs on two cols including a few padding),
but reducing width under 707px value, the centering is losed because margin-left has a fixed value!
you only need to margin: 0 auto; your div, like so:
.container {background-color:red;}
.content {background-color:aqua;width: 350px;margin:10px auto;}
<div class="container">
<div class="content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse eleifend eu leo quis euismod. Phasellus blandit suscipit ipsum non pretium. In facilisis scelerisque sodales. Integer posuere nunc eget orci tincidunt, et dapibus dui porta. Duis pellentesque commodo mi, non congue erat imperdiet ac. Morbi facilisis accumsan dapibus. Quisque metus risus, ullamcorper ut faucibus ac, elementum in arcu. Aliquam vulputate sapien sed ullamcorper dignissim. Cras aliquet nisl nibh, a feugiat tortor pellentesque ac. Donec id mollis ante, sit amet ultricies velit.
</div>
</div>
I would start by using percents on your sub columns. Fixed widths will cause unwanted problems.
Then on your mobile layout, have your sub column go full width and remove the float. That will have the columns stack on top of each other.
.article .subColumn {
width:48%;
float:left;
}
#media screen only and (max-width:480px) {
.article subColumn {
float:none;
width:100%;
padding:0 25px;
}
}
Hi all been searching hard but found various answers that doesn't seem to resolve my issue.
I have one div box that needs to be vertically align centered to the div box next to it matching its height which is not defined as the content will change
But i need the content in the left box to be centered no matter what the height of the div box next to it.
any help would be appreciated.
Thanks
<div class="container">
<div class="leftCol">
Content or image in here needs to be vertical center based on div next to it height <
/div>
<div class="rightCol">
Content here
</div>
</div>
Here is an example using CSS tables.
.table-panel {
display: table;
}
.table-panel div {
display: table-cell;
vertical-align: middle;
border: 1px dotted gray;
}
<div class="table-panel">
<div>Left panel</div>
<div>Right panel: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse eu pulvinar risus. Vestibulum imperdiet velit nisi, eget ullamcorper urna rutrum vel. Aliquam tristique elit augue, nec lobortis eros pretium quis. Proin ac quam pretium,
fringilla est et, sodales erat. Cras quis odio est. Integer ornare, neque in efficitur ultricies, justo magna ornare enim, quis dictum enim ipsum vel urna. Cras consectetur orci id quam cursus feugiat. Proin laoreet ullamcorper est vitae sagittis.
Aliquam faucibus elit sed sodales varius. Phasellus maximus turpis non nisl lobortis, sit amet efficitur lacus suscipit. Nunc a ligula a est feugiat mollis in a lacus.</div>
</div>
If I understand your question right what you are looking for is a wrapper around the two left- and right-boxes. Then inside the wrapper you want the two boxes to be inline-blocks and with vertical-align:middle perhaps?
See this Fiddle:
<div id="wrap">
<div class="left-box"></div>
<div class="right-box"></div>
</div>
#wrap{
width:250px;
height:auto;
background:#efefef;
}
.left-box{
display:inline-block;
width:100px;
height:150px;
background:#00ff00;
vertical-align:middle;
}
.right-box{
display:inline-block;
width:100px;
height:180px;
background:#ff0000;
vertical-align:middle;
}
http://jsfiddle.net/0kpdskf2/
Is this what you want?
I am having trouble making one div height move with the other/be equal. I need these to be equal as the left sidebar will have more content but I would still like the right div height to move with it as its a sidebar.
<div id="maincontainer">
<div class="contentcontainer">
<div class="postcontentcon">Nam eu auctor enim, id tincidunt dolor. Sed et lacinia sem. Donec pretium quam eget nunc vestibulum, vel sagittis nibh bibendum. Fusce eleifend sagittis ultrices. Nullam lobortis ultricies justo, nec tempus metus sollicitudin at. Proin sit amet turpis a orci ullamcorper pulvinar id vitae erat. Fusce sodales iaculis nulla ac faucibus. Vivamus blandit placerat nunc, nec dictum velit tincidunt in. Pellentesque elementum odio metus, eget fringilla nisi imperdiet quis. Etiam facilisis magna pellentesque lorem luctus condimentum. Nulla blandit ac ligula nec aliquam. Cras massa felis, condimentum condimentum ligula in, pharetra fermentum felis. Proin sed lorem interdum, lobortis lectus non, porta tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
<div class="postsidebarcon">Hello</div>
</div>
CSS
body { background-color:#606061; }
.contentcontainer {
margin:0 auto;
position:relative;
width: 1017px;
height:auto;
}
.postcontentcon {
margin:0 auto;
position:relative;
width: 694px;
background-color:#525253;
float:left;
}
.postsidebarcon {
margin:0 auto;
position:relative;
width: 323px;
height:inherit;
background-color:#484848;
float:left;
}
You have a few ways to do his :
display:flex and sizing your containers, default will draw child
side by side demo
display:table on parent + width and display:table-cell; on child + width : demo
clearing floats . float first element, set margin to second, add a pseudo with clear:both (or left/right) demo
faux-column technique, oldish but solid , fine here since parent container has a fixed width The idea is to draw the column onto background of parent : demo (here i used a gradient for the demo, but usally it is an image. You can use multiple background nowdays.
there is other tricky ways with: floatting & hudge heights , negative margin ,overflow. some will even use absolute positionning but you should stay away from using these.
WHat I'm trying to do is create a layout that utilises the best of fluid resizing and absolute positioning at the same time.
What I have is a set size centered div, within it I have absolute images the width of the div, and below those images I have a legend. Now I have the images resizing when the window is shrunk, but I cannot get the behaviour on the legend correct. What I want to happen is that when I resize the window, the legend shrinks accordingly and is always below the images, but never overlaps. And for the life of me I cannot solve this.
Code is as follows (you can also view at jsFiddle here http://jsfiddle.net/Sonictail/2cms6/1/);
css
html, body {
margin:0;
padding:0;
}
#container {
position:relative;
min-width: 640px;
max-width: 1044px;
max-height: 200px;
height:100%;
margin:0 auto;
padding:0;
background-color:red;
z-index:0;
}
#shim {
position: relative;
float:left;
min-width:640px;
max-width: 100%;
height:100%;
}
#legend {
position:relative;
float:left;
bottom:0;
left:0;
min-width:640px;
max-width:1024px;
height:200px;
background-color:blue;
z-index:20;
}
img {
position:absolute;
top:0;
left:0;
min-width:640px;
max-width: 100%;
background-color:green;
float:left;
z-index:1;
}
html
<div id="container">
<div id="map">
<img src="image.png" />
<img src="image.png" />
</div>
<div id="shim"> </div>
<div id="legend">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc viverra, est tempor rhoncus dapibus, elit tellus convallis enim, ac ornare felis diam eget velit. Proin a lorem erat. Nulla orci tortor, ornare et erat consequat, porttitor mattis purus. Vestibulum at neque quis neque elementum egestas quis non magna. Aenean porttitor arcu mattis urna sollicitudin, non euismod mauris consequat. In magna erat, viverra vitae urna convallis, sodales lobortis nibh. Quisque suscipit tincidunt lacus, id varius nunc lacinia sed. Proin at nunc consectetur, tincidunt ligula non, bibendum nisl. Suspendisse euismod ac metus vel posuere. Praesent ipsum risus, pellentesque in lectus adipiscing, egestas faucibus risus. Aliquam volutpat odio id pulvinar scelerisque. Maecenas convallis eleifend ante, non feugiat odio viverra nec.</p>
</div>
</div>
What I was trying to do is have a relative div resize in the same manner as an absolute div using min/max-width & height without using jQuery. The absolute images over the top stuffed everything around.
I gave it all in. You can view the jsFiddle here http://jsfiddle.net/Sonictail/rd3eq/1/ and the js I used to modify the height is below. Nothing fancy.
jQuery(document).ready(function ($) {
resizeDiv();
});
var resizeTimer;
jQuery(window).resize(function ($) {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(resizeDiv, 100);
});
var resizeDiv = function () {
var img = document.getElementById('mapHeight');
var height = img.clientHeight;
jQuery("#shim").css("height", height);
};
i have this layout and i have these two divs, maincontent and extracontent. there supposed to float beside eachother. but when i have more than one extracontent div it slides down the main content for some reason. can you help me fix it?
i have provided a js fiddle, http://jsfiddle.net/XzRun/
HTML
<div id="container">
<div id="content">
<div class="extracontent">
<h1>Other header</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus quis nunc orci, dignissim sagittis urna.</p>
</div>
<div class="extracontent">
<h1>Other header</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus quis nunc orci, dignissim sagittis urna.</p>
</div>
<div class="maincontent">
<h1>Some header text</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse feugiat aliquam justo, nec faucibus nulla porta eget. Fusce ipsum quam, interdum posuere aliquam non, laoreet sed leo. Maecenas luctus, tellus varius fermentum gravida, libero metus pharetra sem, ac scelerisque erat felis vestibulum diam. Donec vulputate eleifend interdum. Etiam ultrices, ante vitae luctus hendrerit, quam justo tempor tortor, pulvinar euismod quam ligula vel eros. Duis vel tellus mi, congue gravida purus. Nulla facilisi. Fusce ac magna arcu, sed vulputate justo. Quisque nec ante vitae lorem laoreet lobortis. Phasellus euismod urna sed turpis tincidunt vehicula. Aenean consequat rutrum sapien vel tincidunt. Mauris tincidunt pretium nisi nec ultricies. Aenean a sem nunc. Nunc luctus, metus in adipiscing hendrerit, lacus felis mollis dui, quis feugiat leo mi nec dolor.</p>
</div>
</div>
</div>
CSS
#container {
width:1000px;
margin:0 auto;
padding:20px 0;
text-align:left;
}
#content {
margin-left:10px;
float:left; /* lines up the left #content and the right #sidebar div's beside eachother */
width:810px;
}
.maincontent {
float:left;
margin:10px 10px 10px 0; /* double the right side because left has 0 */
padding:0 20px;
width:506px;
color:rgb(50,50,50);
background:rgb(255,255,255);
}
.extracontent {
clear:left;
float:left;
margin:10px 10px 10px 0; /* double the right side because left has 0 */
padding:0 20px;
width:200px;
height:200px;
color:rgb(50,50,50);
background:rgb(255,255,255);
}
The problem is that divs are always floated horizontally, not vertically. What you want to do here is to float the .extracontents vertically, while keeping the horizontal relationship. To do this, wrap all the .extracontent divs in a '.allextras` div, with the following css:
.allextras { clear: left; float: left; width: 220px; }
Or however you want it styled to get the margins the way you want.
Im not particularly sure of what you want to achieve.
If you want to have two extra content panels beside each other, your either going to have to widen or shrink extra and main content.
To do this i recommend that you use some JQuery to determine the amount of .extracontent and then change the width according to that.
The best way would be to put .extracontent inside a container with static width and then do something like:
divide extracontent.width by (extracontent in parent).count.
You could also stretch #content to have a min-width instead of fixing the width to be 1000
As defined in your CSS, your #content has width 810px, your .maincontent has width 506px and your .extracontent has width 200px. If you add an extra .extracontent, you will have total 906px. So it goes beyond 810px and slides down.
Your #content that surrounds the one #maincontent and two #extracontents has a smaller width then all of them. 506 + 200 + 200 + all of the margins > 810px. Give it some more room and your divs won't drop to other levels.