I have 3 elements in my header (A, B, and C), where A and C are static images, and B is a fluid div of text.
What I would like to happen when I reduce my screen size (or view on a mobile device) is that B drops below both, while A and C remain in place--each at opposite sides of the screen.
See this illustration.
http://craigaranha.com/responsiveheader/
thanks
Float the left box to the left, the right box to the right, and let the middle box fill the remaining space. Then when the screen is smaller, just add clear:both on the middle box to make it drop down.
Something like this:
.left, .right {
width:50px;
height:50px;
text-align:center;
line-height:50px;
}
.left {
background-color:blue;
float:left;
}
.right {
background-color:magenta;
float:right;
}
.middle {
background-color:silver;
height:50px;
line-height:50px;
}
#media all and (max-width:800px) {
.middle {
clear:both;
}
}
Assuming markup like this:
<div class="left">A</div>
<div class="right">C</div>
<div class="middle">B is a fluid box of text.</div>
Example codepen
Without seeing your code I can't be 100% sure what your CSS structure is but you could start with #media screen and (min-device-width: XXXpx) { } or #media screen and (min-width: XXXpx){ }. They'll detect the device screen size and give you some conditional CSS rules. Within those those rules, if your right/left divs are floated, you could float the third and use clear:both, or your could use position: fixed; bottom: 0;. It really depends on your document structure and current CSS. Hopefully this has given you a starting point though.
Personally I think position:fixed; would be too much of a quick and dirty solution, the same would go for position:absolute;.
Related
I am runnning across an issue in which I am not sure how to solve.
I have a grid system doing the following, BUT I will do a standard "div" solution. But here is my dilemna.
I have a "LogoComponent" That displays my companies logo on the left, and a partner's logo on the right.
I have two headers that display in two different conditions.
Centered Display (just the logos)
Left Aligned Display (left aligned logos, with other content on the right)
Caveats: the "partner logo" needs to confine within the div/space as sometimes the svg's are large, so I "can" offer a height, but not a width.
The image shows the two views. The "LogoComponent" I am having an issue as I was using a flexbox, but not sure that is gonna work since why I try to make it "left" as a component, it moves off the container div. Any ideas how to solve this?
I can solve it, but I feel it will be too generalized, as I'm looking to make this "LogoComponent" be wide enough for the logos, and then appropriately resize if the partner logo is there or not.
As said in the comment, you can center LogoComponent using margin: 0 auto; when it's the :only-child.
If it's not the only child, using margin-right: auto; will push all other content to the right as we are in a flex container.
.parent {
display: flex;
}
.LogoComponent {
margin-right: auto;
}
.LogoComponent:only-child {
margin: 0 auto;
}
/* for styling only */
.LogoComponent {
width: 50px;
height: 50px;
background-color: lightblue;
}
/* for styling only */
.OtherContent {
min-width: 200px;
background-color: lightgreen;
}
<div class="parent">
<div class="LogoComponent"></div>
</div>
<hr>
<div class="parent">
<div class="LogoComponent"></div>
<div class="OtherContent"></div>
</div>
When I view my React app on mobile there is always extra space to the right:
https://dsousadev.github.io/23imgs/
I can't see this extra space when I use dev tools to simulate a mobile view, but when I view the site on a variety of mobile browsers the space is there and the app doesn't auto fit.
Here's the current CSS in my media query:
#media all and (max-width: 414px) {
.ImageCard,
#CardImage {
width: 314px;
}
#CardImage {
height: 314px;
}
ul {
font-size: 1.2rem;
padding: 0 15px;
}
}
The element <input accept="image/*" name type="file"> is causing the issue.
If I apply display: none to .fileContainer input, the space goes away.
Edit
Alright, so looking into the issue a bit more, it looks like semantic.min.css is applying a font-size: 100% to button, input, optgroup, select, textarea. This is causing your input to grow in size (here's the input when visible):
If we disable the font-size: 100%, the input renders correctly:
After which, your original styles work:
So display: none or reverting font-size will work :)
I have to begin by apologizing for my inability to grasp what can only be described as logic. I have photoshopped the proposed layout. Here is a snapshot:
Layout:
I do not have example code. This is because I can't write code if I don't know how to approach it. Which brings me to:
I'm trying to design an overlayed SVG navigation menu (achieved as red rectangle + trapezoid polygon) that's always at the bottom of the page and pops out when clicked/touched to show expanded menu options. From what I've read and understand, using absolute (do I mean "static" here?) values to define position (pixels vs percent) is bad design because of wildly varying resolutions and DPI across many devices. I've tried a simple example in W3's Try Me editor just to get a grasp of how I'll position the red rectangle. I used:
<svg>
<rect width="100%" height="10%" x="0%" y="90%" style="fill:#ff0000;" />
</svg>
This places a red rectangle spanning the entire width of the page (regardless of resolution) at the bottom, without clipping the rectangle outside the viewport. Success! Right..? Well.. the menu isnt just a rectangle. There will be a polygon trapezoid "bump" in the center where the hamburger menu icon will go. This led to my problem. The polygon is defined by coordinates. And coordinates are absolute (or static?). Defined in pixels or inches or various other units. It doesn't support percentages from what I've read, and I'm not sure that's what I'd want anyway. Using percentages means that rectangle will stretch to fill whatever "10%" of the viewport height is. On a desktop it will be thinner than on mobile in portrait. It also means that the polygon won't necessarily be centered, as 550px will not always represent 50% (example values).
I'm going about this wrong. Help me find the proper logic for laying out this page. Honestly, after this, dynamically loading content underneath seems like the easy part.
FYI, I'd like to avoid JS and plugins as much as possible. By that I mean, I don't want to use them AT ALL. CLARIFICATION: If it can be avoided.
I'd be more than happy to clarify ad nauseum. Though this is the first time I'm actively using SO, and have only been previously exposed to it via google searches.
EDIT: I'm asking in the context of working with SVG, since this is what I'm having trouble finding proper documentation on. What I find is blog posts and "download this code/plugin to achieve this effect" instead of conceptual descriptions of how to achieve those effects. I've coded websites (personal, not professional) when HTML4 and CSS2 were the tools. Back then, you didn't have to worry about mobile phones. You could work within commonly used resolutions and aspect ratios (16:9 and 4:3 were pretty much it). But now those devices have wildly varying pixel densities and methods of rendering and 2 aspect ratios (portrait and landscape). I'm going from a background in fairly static webpages to mighty morphin' power webpages.
TLDR: How to do SVG? How do I put that trapezoid in the center, and have it retain its width/height regardless of the viewport's. Also, if I'm supposed to avoid absolute values (defining the height of the rectangle in pixels), how do I maintain a consistent height and proper positioning of the absolutely positioned polygon with relative values?
EDIT2: Thank you Paul! This has helped me immensely!! Being able to see the code and have it explained why and under what circumstance it does or does not work allows me to understand how this kind of tool is used. Especially when explained in context to to my own ideas. The CSS version is also very helpful. I've never used it beyond basic styling of elements. Both answers are excellent.
You don't need SVG for this. What you want to achieve is very easy to do with CSS.
var tabcontainer = document.querySelector(".tabcontainer");
var tabbutton = document.querySelector(".tabbutton");
tabbutton.addEventListener("click", function() {
tabcontainer.classList.toggle("open");
});
.container
{
width: 500px;
height: 500px;
position: relative;
background-color: black;
overflow: hidden;
}
.tabcontainer
{
position: absolute;
width: 100%;
bottom: -44px;
}
.tabcontainer.open
{
bottom: 0px;
}
.tabbutton
{
width:100px;
height: 0;
margin: 0 auto;
border-bottom: solid 20px red;
border-left: solid 16px transparent;
border-right: solid 16px transparent;
text-align: center;
cursor: pointer;
}
.tabbutton I
{
color: white;
}
.tabbody
{
width: 100%;
height: 60px;
background-color: red;
}
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="container">
<div class="tabcontainer">
<div class="tabbutton"><i class="fa fa-bars"></i></div>
<div class="tabbody"></div>
</div>
</div>
Using SVG
Generally, anything in an SVG gets scaled if the SVG scales. This can cause a problem when people want to create responsive SVG documents on a page.
However, with some particular SVG layouts it is possible to do what you want. In particular it is when you have simple designs with elements that need to sit in the corners, or centred on one of the sides.
Here's a version of the above CSS-only example that uses SVGs for the tab part.
How this works is that we are using the feature where, if the aspect ratio of the contents (defined by the viewBox) is different from the SVG (defined by width and height), the contents will automatically get scaled and centred.
So here we are defining a button that is 132x20, and putting it in an SVG that is 100% x 20. Where "100%" here is the width of the container/page. Because the aspect ratio is different, <polygon> representing the button will get automatically centred horizontally.
var tabcontainer = document.querySelector(".tabcontainer");
var tabbutton = document.querySelector(".tabbutton");
tabbutton.addEventListener("click", function() {
tabcontainer.classList.toggle("open");
});
.container
{
width: 100%;
height: 500px;
position: relative;
background-color: black;
overflow: hidden;
}
.tabcontainer
{
position: absolute;
width: 100%;
bottom: -44px;
}
.tabcontainer.open
{
bottom: 0px;
}
.tabbutton
{
fill: red;
cursor: pointer;
}
.tabbody
{
fill: red;
}
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="container">
<svg width="100%" height="80" class="tabcontainer">
<svg width="100%" height="20" viewBox="0 0 132 20" class="tabbutton">
<polygon points="16,0, 116,0, 132,20, 0,20"/>
<g transform="translate(66,0)" fill="white">
<rect x="-8" y="4" width="16" height="3"/>
<rect x="-8" y="9" width="16" height="3"/>
<rect x="-8" y="14" width="16" height="3"/>
</g>
</svg>
<rect y="20" width="100%" height="60" fill="red" class="tabbody"/>
</svg>
</div>
I am trying to add a banner above my fixed-top navbar. I have looked around and understand some basic concepts on how to do this but my example is a little more complex and I am getting a little lost.
User can add a custom banner to the top of my page. If this happens I need to push everything (including the fixed navbar) down the height of the banner (say 20 px).
I have created a class that I can apply to the nav element:
.top-banner-nav {
top: 20px;
}
Navbar:
<nav class="navbar navbar-default navbar-fixed-top" ng-class="{'top-banner-nav': banner == true}">
That works great to push the static navbar down 20px if there is a banner. However I have to problems.
When you have a static navbar like this you have to add a top padding to the body to push the body content below the banner. Now all of the sudden I would need to push the body content down 70px. Not sure if there is a good way to make this happen dynamically.
I have another static navbar directly underneath the very top navbar. When I move the very top navbar down 20px I need to move this other navbar down 20px as well. Unfortunately this lower nav is fixed as well with top: 50px to place it right below the upper navbar.
I have a plunker here that demonstrates the problem. And I am not really sure where to go from here. Might have to mess with things in javascript, but I would like to try and avoid that is at all possible to avoid things jumping around on page load.
http://plnkr.co/edit/DYYMz1?p=preview
I looked at your plunker. I made some changes to your CSS and HTML to accomplish what you are looking for. The change I made to the HTML was the body tag. The new tag looks like this:
<body ng-controller="MainController" ng-class="{'bodyModified': banner == true}">
The new CSS file looks like this:
body {
padding-top: 50px; Need this to move down body under fixed header
}
.bodyModified {
padding-top: 20px;
}
.settings-nav {
position: relative;
right: 0;
left: 0;
background-color: #E2E2E2;
z-index: 1029;
top:0px;
}
.settings-content {
padding-top: 70px;
}
.top-banner-nav {
position: relative;
margin: 0;
height: 20px;
}
.top-banner {
position: fixed;
right: 0;
left: 0;
top:0;
margin-bottom: 0px;
background-color: #FFFF00;
}
You can also use javascript and jQuery to accomplish this as well. Hope that helps. Let me know how it goes.
Here is a plunker demonstrating the changes
http://plnkr.co/edit/39LhQA2gdMremnTOGXe4?p=preview
you can try jquery solution for this.for example if you are using top-nav-fixed-banner class you can code something like this.
function fixBannerHeight(){
var bannerHeight = $(".top-banner-div").height();
$(".top-nav-fixed-banner").each(function(e){
var height = $(this).height();
bannerHeight = bannerHeight + height;
})
$('.top-banner-div').css({"height":bannerHeight+"px"});
}
call fixBannerHeight() when you are adding dynamically banner.
Hope this helps.
I have simple web page which consist of two columns : sidebar and content. There is no problem if both columns are dynamic but the width of sidebar must be fixed and the content width should be dynamic depending on browser width.
I don't know how to calculate the width of the columns.
Thanks.
You could do this:
FIDDLE
#sidebar {
width: 200px;
float: left;
}
#content {
overflow: auto;
/* trigger a block formatting context (this fills remaining horizontal width) */
}
I imagine you're looking for something like this:
http://jsfiddle.net/ekVJe/
What I've done is float the sidebar left, and then give the content a left-margin that's equivalent to the width of the sidebar:
#sidebar {
width: 200px;
float: left;
}
#content {
margin-left: 200px;
}