I have recently Discovered that it is incredibly difficult to mix fluid and fixed layout, So when I finally figured out a solution to a problem Ive been having for quiet a while now, i couldn't resist but to share it with the community that has helped me so much in the past.
i wanted the following look:
by dynamic space i mean it should be very much like when one applies a "margin:0 auto;" CSS rule to a containing div, the white space left and right of the element is the "Dynamic Space" in the example.
I had 6 Requirements:
had to be responsive.
the container had to have a max width of 960px and always needed to be centered.
had to work on IE8 and up.
The Dynamic space on the left had to have a different height, and contain a different color.
The Dynamic Space on the right had to be the same height has the container but a different color.
Has to work with Twitter Bootstrap.
At this point I struggled for 3 days, i tried everything from css table-cells to using bootstrap columns (neither worked out).
I also realized that the only way to have the div in the "same" position on huge screen sizes was to make it 50%.
So at this point i had the Following:
JSFIDDLE DEMO
which was pretty good, only problem was that the red stuck out underneath the container element.
So that My Wonderful Not At All Mathematical brain kicked in and thought:
if my containing element will always be 960px and i need my div on the left to be 50% to stay in the same position, what if i simply took 960/2 = 480px and simply applied margin:-480px.
which worked brilliantly.. until you scale your screen down to about 768px, so add a media query that changes it to margin-left:-370px;.
And it Finally Worked! Here's the Final Code:
JSFIDDLE DEMO
AND HTML:
<div class="" style="background: #000099; position: relative">
<div class="left">l</div>
<div class="container" style="background: #002500">contain</div>
</div>
AND FINALLY CSS:
.container {
max-width: 960px;
z-index: 1;
position: relative;
padding:0;
}
.left {
position: absolute;
left: 0;
width: 50%;
z-index: 1;
background: red;
height: 50px;
margin-left:-480px;
}
#media (min-width: 768px) {
.left {
margin-left:-375px; /*I Used -370 but for some reason it doesn't work now*/
}
}
#media (min-width: 992px) {
.left {
margin-left:-480px;
}
}
I Hope this Helps Someone, Sorry that its so long just wanted to explain the logic as clearly as possible.
Related
I'm optimizing my website for mobile devices at the moment, but ran into a problem. If i view the site on a mobile device or a small browser window some objects on the site wont have an effective width of 100% anymore, but others will. So you can scroll to the side and half of the content kinda sticks to the left. As Long as the width of the viewport is above 1000Px there are absolutely no problems. I use percent for measurement of the elements, so that shouldn't be a problem.
>1000Px screenwidth
<1001Px screenwidth
The effect isn't obvious instantly, as there wont be a horizontal scroll bar, but you can click and hold at the right side of the site and pull it over. The real problem is, that the header gets pushed out to the side, as it is one of the objects, which still uses the "full" 100% width.
I have a media querie, which changes the header at 1000Px
#media screen and (min-width: 1000Px) {.mobilenav {display: none;}
If i disable this one, the problem is gone, however if i delete the only div using this class
<div class="mobilenav">
<span style="font-size:25px; cursor:pointer;" onclick="openNav()">☰</span>
</div>
the problem is still there… The div does not stretch across the screen, i checked that and at this point i'm really out of solutions.
Here is the css for .mobilenav if it helps
.mobilenav {
position: relative;
}
.mobilenav span {
position: absolute;
bottom: 10%;
}
This may happen because of padding of your header element. You can check in example below it will be as width + padding
.container {
width: 300px;
height: 100px;
background: #ccc;
}
.article {
width: 100%;
height: 20px;
margin-top: 8px;
background: #999;
}
<div class="container">
<div class="article">article 1</div>
<div class="article" style="padding: 4px">article 2</div>
</div>
I want to prevent page-reflow, caused by image loading on a web page.
Page reflow occurs when images load after the page's text content has already rendered. There's a 'jerk' caused by the said page-reflow. It makes for awful user experience.
My requirements are:
(i) All images be fully responsive
(ii) Have a max-width of 450px (while maintaining aspect-ratio)
(iii) Be center-aligned within their containers
There can be several images on the page. All have different aspect ratios (but scaled to the same width - i.e. 450px). I know their dimensions beforehand.
Currently my code is simply:
.container {
text-align:center;
overflow:hidden;
background:whitesmoke;
border-top:1px solid #F0F0F0;
border-bottom:1px solid #F0F0F0;
}
.container img {
width:100%;
max-width:450px;
vertical-align: top;
}
<div class="container">
<img src="https://s3.ap-southeast-1.amazonaws.com/damadam-2019/public/31a1b420-59c9-405a-a197-e04dd1e2eaf9.jpg" alt="image">
</div>
This fulfils all my requirements - except it can't prevent page reflow. How do I tweak this to get my desired result?
Traditional solutions to prevent such page-reflow go something like this:
HTML
<div class="container">
<img src="https://s3.ap-southeast-1.amazonaws.com/damadam-2019/public/31a1b420-59c9-405a-a197-e04dd1e2eaf9.jpg" alt="image">
</div>
CSS
.container {
display: block;
position: relative;
padding-bottom: calc(100%/(450/562));/* example width=450px height=562px*/
height: 0;
}
.container img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
This works fine. But it doesn't impose a max-width like I need it to. The image fills the entire container - as large as that container is (e.g. the full width of the screen on a laptop).
To tweak it, I tried adding max-width:450px;max-height:562px in .container img. That corrected the image's dimensions. But it gave the container extra padding at the bottom:
That's a shame. What I really wanted was for it to look like below:
Note that the gray colouration above is the background container, which simply disappears on smaller resolutions:
What's the best way for me to achieve my requirements? An illustrative example would be great.
Note: adding max-width: 450px;max-height: 561px; in .container doesn't solve the problem either.
Background:
I'm trying to visualize the structure for my future pages and containers in the full screen.
Current JSFiddle:
Available here.
<div class="top">top</div>
<div class="middleleft">left</div>
<div class="center">center</div>
<div class="middleright">right</div>
<div class="bottom">bottom</div>
.top
{
background-color: yellow;
height: 20%;
}
.middleleft
{
float: left;
background-color: blue;
width: 20%;
}
.center
{
float: left;
background-color: white;
width: 60%;
margin: auto;
height: 60%;
}
.middleright
{
float:left;
background-color: red;
width: 20%;
}
.bottom
{
height: 20%;
background-color: green;
}
Problem:
For some reason, even with height defined on the CSS, it does not fill the entire screen to the bottom, linking only enough background color height and width until the text ends.
Need:
What code change is needed to fill the screen to the dimensions it has (like the 60% width on the center div), without having to write characters to the bottom to fill out the div on the screen?
Code type restrictions:
I do not wish to use JavaScript or JQuery in the solution, only CSS and HTML.
Many Thanks
JSFIDDLE CODE
I added this:
html, body {height:100%;}
Then I set your center div to height 100% (and made it pink so it can be seen more easily).
EDIT: I left your side divs alone as I'm not sure what you want to do with those, but I hope this helps.
Wrap the top/center/bottom content into one class allowing the left/right divs to merely sit outside of the wrapper.
This can be achieved, but I think you need to ask yourself why you want to do this. Today, with mobile there is no standard screen size, so a full screen for one screen is not going to be a full screen for another.
Instead, why not try adding some real content to your proposed structure and see how it stacks up then?
Or, if it's purely for mockup purposes, then maybe use some graphics software where it's much easier and quicker to move things around.
I have a website, already built in bootstrap, it's a wordpress theme and it's responsive.
It's not up on the web so i can't show you exactly example of my theme but it follows this kind of markup http://getbootstrap.com/examples/carousel/
My header with menu is full width and slider is full width, and content is wrapped in container very similar as example from getbootstrap.
Now i need to add ads to the left and right side of the content, and this need to be something like this http://www.bigblue.rs/
but since bootstrap layout is responsive i tested with various width % and floats but i can't make it work. when i watch site on 21" they need to cover whole left and right emptiness, as site is resized, they should simply go out of the screen and not interfere with rest of the center content.
EDIT: heres demo on bootply with layout that i have on my website http://bootply.com/108951
While it won't get you the exact feel of the linked site, where the ads slide off the edge of the page, probably the easiest thing to do would be to use Bootstrap's responsive utilities classes (http://getbootstrap.com/css/#responsive-utilities-classes) to show and hide the ad divs as you resize the screen. I've edited your Bootply demo to show what I mean, in context: http://bootply.com/108988#
If you don't feel like digging through the code:
Choose at which point you would like your content to appear. Bootstrap's defaults (which can be found under "grid media queries" in the CSS documentation, sorry, I'm out of links) are sm, md, or lg. We'll go with lg for the sake of brevity here.
Add one <div class="visible-lg col-lg-1>Ad Content goes here!</div> above and below your existing three <div>s, for a total of 5 divs in the row.
Change the # on your existing <div class="col-lg-#>s so that there are 12 per row. This might be somewhat wonky, since you now have 5 columns.
This will cause the ad divs to show and hide at the breakpoints. While they are visible, they will resize responsively, just like the rest of the site.
Alright the approach is very simple.
On top of the page i added 2 divs. But to solve the problem with responsiveness i had to set margin of those 2 divs for each media size. Here is the styling.
.advertis-left {
left: 50%;
margin-left: -1050px;
position: fixed;
text-align: right;
top: 0;
width: 500px;
}
.advertis-right {
left: 50%;
margin-left: 570px;
position: fixed;
top: 0;
width: 500px;
}
/* Responsive Styles */
#media (max-width: 1199px) {
.advertis-left {
margin-left: -950px;
}
.advertis-right {
margin-left: 470px;
}
}
#media (max-width: 991px) {
.advertis-left {
margin-left: -840px;
}
.advertis-right {
margin-left: 360px;
}
}
#media (max-width: 767px) {
.navbar-nav .dropdown-submenu > .dropdown-menu {
margin-left:10px;
display:block;
}
.advertis-left,
.advertis-right {
display:none;
}
}
So now i have 2 banners fixed on left and right side not overlaping with main content and when switched to other screen size it follows #media size untill it's on smallest device where it's no point to be visible at all.
Here is a bootply example but i don't know why it's overlapping there, i guess i use different padding and margin on my content div.http://bootply.com/110193
They say, that a single picture can explain more than a tousand words, so here's my "more": http://www.imagebanana.com/view/hcqsz5fs/cols.png
My goal is to have the columns as shown on the image, with them together having 100% body width.
And my fiddle is here: http://jsfiddle.net/c2JH3/ (note that this is just a mockup of my current work).
How can I achieve this?
A Quick Note
In your comments, you are saying that you can't use a background image because the height is variable.
The way to fix this is using multiple images, and tell them to repeat or not to repeat on different parts of the page. But I'll go over this after I answer your question directly.
Short Answer
To get 100% body width, you'll want to use percentages (%) on for your width rules. Like this:
#left {
width: 60%;
}
#right {
width: 40%;
}
Fixing Some Problems
One problem you are bound to encounter when you have content that passes the bottom of the screen. In this case, you need to tell the divs to stay side by side.
This should do the trick:
#left {
position: absolute;
top: 0;
left: 0;
width: 60%;
}
#right {
position: absolute;
top: 0;
right: 0;
width: 40%;
}
Back to the Note
Believe me, you don't want to be using percentages on your widths. It makes sizing and scaling extremely hard to design nicely, and changes that you try to make in the future probably won't work without a complete redesign of your css.
Like I was saying before, you want to use multiple images. You'll have background image on the body tag that - going by the design you provided - has the gray-to-orange fade in it. The css would look like this:
body {
background-image: url('path/to/header.jpg') no-repeat;
}
You would then have a wrapper div like the one you already have, that holds the content and such. Inside the wrapper you have:
A header (the logo and navbar), which would have no background (so you can see the body background).
A featured section which holds that really big image in the middle. You can use negative margins to get it centered.
A subnav section for those images in the middle. This would have it's own background image that has a matching part of the background of the body so that it appears to flow in as the image does.
A content section that holds all of the content of the page. This would have an image repeating vertically to look continuous.
#content {
background-image: url('path/to/slice.jpg') repeat-y;
}
A subfooter section that has the curved part of the page (that gray-to-orange curve at the bottom).
And finally a footer section that has all the stuff on the very bottom.
You can use the same structure on the inner pages, you would just use different images and spacing to change the look of the page.
To Sum Up
You will never, ever need to have a 100% width for your wrapping div. (I say this to generalize, there are certain styles that use this, but they aren't the same kind of design).
What you should always try to do first is create images for the body, header, content, and footer sections that create the look you want.
Then have your wrap be a set width in pixels that will stay in the center of the page, while the margins increase and reveal more of the background image.
Have fun and good luck with your design!
I don't know how to use fiddle. But this worked out fine for me.
I just used the background colors and borders to properly show the differences
<!doctype html>
<html>
<head>
<title>xxx</title>
<style>
body{
margin: 0;
}
#wrap {
width: 100%;
}
#left {
width: 600px;
float: right;
border: 1px solid #000000;
}
#right {
width: 350px;
border: 1px solid #000000;
}
#container_left {
width: 55%;
float: left;
background: red;
}
#container_right {
width: 45%;
float: right;
background: blue;
}
</style>
</head>
<body>
<div id="wrap">
<div id="container_left">
<div id="left">
<p>Content</p>
</div>
</div>
<div id="container_right">
<aside id="right">
<p>Sidebar</p>
</aside>
</div>
</div>
</body>
Cheers!
Thank you all for your answers and ideas. They were helpful and I did learn something new (my biggest 'thank you' goes to #Jon for a really great, great post). But, since I can't solve this neither with percents nor backgrounds (since my design is a little more complicated that the one provided), I made my way with jQuery. To sum up, here's my mockup fiddle.
Note: sometimes you'll need to change left 125 to 126, just to make sure both ends meet.