I have a page where a Submit button is supposed to be positioned on the right side of the bar as shown in the picture here:
However, I want the button to stay there even when the bar gets scrolled horizontally, which I have achieved via position: fixed;. This works until the screen size is adjusted to larger than the window, as seen here: https://media.giphy.com/media/SiErhserLwSYciEdS3/giphy.gif To fix that, I saw some recommendations for making position: absolute; which works for the screen size adjustments, but does not leave the button in a fixed position, as evidenced here: https://media.giphy.com/media/wHeXCgQ3fEIlPv2tr4/giphy.gif
My question is how I can achieve this expected result, where the button is fixed to the right side of the table toolbar, unaffected by horizontal scrolling, but is not affected by the window/screen size increasing/decreasing.
The code for this element looks as follows:
<div class="one">
<div class="two">
<button type="submit" class="btn btn-danger">Submit</button>
</div>
</div>
.one {
width: 70%;
overflow: auto;
z-index: 10;
position: relative;
float: left;
height: 100%;
background: #fff;
border: 1px solid #dbdbdb;
border-radius: 4px;
}
.two {
height: 49px;
width: 100%;
border-bottom: 1px solid #dbdbdb;
}
.btn-danger {
position: absolute;
}
This behavior was achieved by changing the CSS to .btn-danger to:
.btn-danger {
position: sticky;
margin-top: 7px;
left: 10px;
}
It is acceptable that the button stay on the left in this case.
Why not use bootstrap responsive #media query to create breakpoints like so
#media (min-width: 576px) { ... } // Medium devices (tablets, 768px and up) #media (min-width: 768px) { ... } // Large devices (desktops, 992px and up) #media (min-width: 992px) { ... } // Extra large devices (large desktops, 1200px and up) #media (min-width: 1200px) { ... }
Related
I am trying to set a fall back in my CSS to change the background image to color instead when using a mobile browser.
Is there a way to set a fall back that will go work when the pixels reach the max-width.
As shown below in my snippet the "media only screen and (max-width: 600px) works when then top body class is not being used.
body {
margin: 0;
background-image: url(https://api.timeforkids.com/wp-content/uploads/2019/09/final-cover-forest.jpg);
background-size: auto;
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%)
}
#media only screen and (max-width: 600px) {
body {
background-color: lightblue;
}
}
input{
background-color: #03fcd3;
border: none;
color: white;
padding: 16px 32px;
text-decoration: none;
margin: 4px 2px;
cursor: pointer;
}
You are not overriding the background-image rule, which is why the image is still showing. You set the background colour, but the background-image is still going to show.
Also, you should add the image in the media query for larger screens instead of removing it in smaller screens - otherwise it could load anyway. Mobile-first is always the recommended way to go :)
Try this (if you make your browser window small, you will see the blue background):
body {
background-color: lightblue;
/* the rest of the CSS that applies on all screen sizes, e.g.: */
margin: 0;
background-size: auto;
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%)
}
/* Note: we show min 601px because it was max: 600px*/
#media only screen and (min-width: 601px) {
body {
background-image: url(https://api.timeforkids.com/wp-content/uploads/2019/09/final-cover-forest.jpg);
/* Add any other CSS for 600+ screens here */
}
}
Note that the new media query is set at 601px - you were using max: 600px for small screens so CSS for small screens wil lapply up to and including 600px.
Using a mix of max-width:600px and min-width:600px can cause unexpected behaviour because both media queries will apply at 600px.
Try to set two media queries and don’t set background in the beginning.
Initial body background unset
For mobile devices:
#media only screen and (max-width: 600px) {
body {
background-color: lightblue;
}
}
For computers:
#media only screen and (min-width: 600px) {
body {
background-image: url (https://api.timeforkids.com/wp/content/uploads/2019/09/final-cover-forest.jpg);
background-size: auto;
}
}
I have a simple form with an input field. Now on a laptop screen the width of the input takes up 30% of the screen which is correct.
However, what I want to do is that if the device is 600px in width or less (mobile device) then increase the width to 60%.
The problem is that it is not changing the width in the mobile device. Seems like it is taking up only 30% on the mobile device and not 60%. I am not sure what I am doing incorretly as I am using the #media tag based on research.
<section id="marketing-email">
<form class="marketing-email-form" method="post" action="https://metis-online.com/marketing-email.php">
<div>
<label for="email"><b>Be part of our mailing list to receive exclusive promotional offers on our courses and services:</b></label><br/>
<input type="email" id="market-email" name="market-email" required placeholder="Email"/>
<button type="submit" class="marketing-btn">Send</button>
</div>
</form>
</section>
#media only screen and (max-width: 600px) {
#market-email{
width:60%;
}
}
/*--------Marketing Email-------*/
#marketing-email{
padding: 1em;
width:100%;
text-align:center;
}
#market-email{
padding: 0.5em;
width:30%;
}
.marketing-btn{
background: #1034A6;
color:white;
padding: 0.5em;
}
The reason why the width didn't increase to 60% on mobile device is because in your css code your first say that #market-email should have a width of 60% and just after, you reset it to 30%.
Here is how it should be done to work correctly:
#marketing-email{
padding: 1em;
width:100%;
text-align:center;
}
#market-email{
padding: 0.5em;
width:30%;
}
.marketing-btn{
background: #1034A6;
color:white;
padding: 0.5em;
}
#media only screen and (max-width: 600px) {
#market-email{
width:60%;
}
}
Just move #media only screen and (max-width: 600px) part to the end of the page. Because now the second code #market-email with width:30%; rewriting your #media rule.
In CSS, styles which come later in the document override earlier ones. Therefore the most specific #media query must always come last in the document.
At the moment, width: 30%; is applied to all screen sizes and comes later in the document, therefore overriding the earlier style of width: 60%;.
The solution is simply to change the position of the #media query within your CSS:
#marketing-email {
padding: 1em;
width: 100%;
text-align: center;
}
#market-email {
padding: 0.5em;
width: 30%;
}
.marketing-btn {
background: #1034A6;
color: white;
padding: 0.5em;
}
/* Media query comes after other styles */
#media only screen and (max-width: 600px) {
#market-email {
width: 60%;
}
}
I have this code . When i resize the browser to min-width: 480px it doesn't change the background color to blue and width to 100px
this is my code so far:
#media screen and (min-width: 480px) {
.boxcontainer {
background-color: blue;
width: 100px;
}
}
.boxcontainer{
width: 1300px;
background-color: green;
height: 200px;
}
<div class="boxcontainer">
</div>
Thank you
Switch the order of the CSS rule, Change your CSS into
.boxcontainer {
width: 1300px;
background-color: green;
height: 200px;
}
#media screen and (min-width: 480px) {
.boxcontainer {
background-color: blue;
width: 100px;
}
}
As in this JSFiddle example. the background is blue as long as the width is not less than 480px, otherwise it turns green.
IF by any chance you meant to do the opposite, because .boxcontainer{width:1300px} makes me think you want that , then just change the media query break point to #media screen and (max-width: 480px) instead of #media screen and (min-width: 480px).
You can see the second option in this JSFiddle
Note: Yes I did make a research first trying to find a solution and tried to implement a number of options to fix the problem, nothing I could find worked though!
UPDATE
Because media queries are not an optimal solution for my problem, as I have to take into account multiple cases of width/height combinations in a responsive layout, I used some javascript at the end in order to calculate the difference in height of the banner div and the content div in order to readjust height accordingly. Here is the code I used
function resizeContainers()
{
var bannerContainer = $('#banner');
var contentContainer = $('#homeFormContainer');
var bannerContainerHeight = bannerContainer.height();
var bannerContainerBottom = $(window).height() - bannerContainerHeight;
var contentContainerBottom = $(window).height() - contentContainer.height();
var containersDiff = bannerContainerBottom - contentContainerBottom;
if (containersDiff > -200) {
var newBannerContainerHeight = bannerContainerHeight+contentContainerBottom+20;
bannerContainer.css('height', newBannerContainerHeight);
}
}
$(document).ready(function () {
// check and resize after page loads
resizeContainers();
// check and resize containers on window resize
$(window).resize(function() {
resizeContainers();
});
});
I am struggling with a div in bootstrap that won't adapt to the height of it's inner content. As far as I can tell, CSS looks OK (but it probably is not, so I could use some help).
You can see the fiddle here
http://jsfiddle.net/spairus/fbmssraw/6/
The basic HTML structure looks like this
<div id="banner" class="banner">
<div class="banner-image"></div>
<div class="banner-caption" style="display: table;">
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
(content here)
</div>
</div>
</div>
</div>
</div>
And the CSS
html,
body {
height: 100%;
}
img {
display: block;
max-width: 100%;
height: auto;
}
.banner {
width: 100%;
height:100%;
min-height: 100%;
position: relative;
color: #fff;
}
.banner-image {
vertical-align: middle;
min-height: 100%;
width: 100%;
}
.banner:after {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.55);
content: "";
}
.banner-caption {
position: absolute;
top: 22%;
width: 100%;
z-index: 2;
}
.subfooter {
background-color: #fafafa;
border-top: 1px solid #f3f3f3;
border-bottom: 1px solid #f3f3f3;
padding: 40px 0;
}
/* Backgrounds
----------------------------------------------------------------------------- */
.default-bg {
background-color: #222222;
color: #ffffff;
}
.space {
padding: 20px 0;
}
I need the .banner and .banner-caption to expand along with the content.
If you're going to have the following code:
html,
body {
height: 100%;
}
and then on your main container:
.banner {
width: 100%;
height:100%;
min-height: 100%;
position: relative;
color: #fff;
}
the container is not going to be responsive to the content inside it, because what you've said in the CSS is basically "hey container (banner), I want you to be 100% of the screen size of whatever device you show up on" .
Instead you need to change the css to said "hey container I want you to have a min-width or be responsive to whatever content you have inside you", so you could do the following:
The best solution I have for this problem (which I face quite often) is as follows.
#media only screen and (min-width : 992px) {
.banner {
width: 100%;
height:120%; // adjust this to whatever size you think your content will stretch to there is no golden rule saying it has to be 100% only.
min-height: 100%;
position: relative;
color: #fff;
}
}
Edit
Why does the above solution work? Notice how I have added a media query that says min width of 992px, now it's safe to say that the dimension of devices above 992px are predictable, and thus it's safe to use a height:120%; it's only when you go below 992px or 768px that the screen resolution (height & width) become unpredictable to counter this you add absolutely no styling to the height of the container.
How effective is this technique? Well, it is pretty effective, but on some screens (the screens above 992px) there might arise a problem of excessive white spacing or slight overlapping of content.
Now to counter this problem you can use multiple media queries : eg.
#media only screen and (min-width : 768px) {
height : 125 %;
}
/* Medium Devices, Desktops */
#media only screen and (min-width : 992px) {
height : 120 %
}
/* Large Devices, Wide Screens */
#media only screen and (min-width : 1200px) {
height : 100 %
}
but IMHO, this is not needed unless you need pin point accuracy (multiple media queries are a pain!). Worst case scenario, use two media queries.
Anyway, here's the technique in action. Try to reduce the screen size and voila! Still looks pretty, and nowhere have a added height:400% for smaller screens: it adapts by itself.
I have header text overlaid on an image. The issue is that on higher resolution desktop screens (e.g., > 1600px) the header only takes up a small section of the image width. I want the header text to take up ~90-100% of the available width regardless of the res.
http://www.dailyspiro.com
<div class="container-fluid">
<div class="col-md-12 landing-container">
<img src="images/pig.jpg" class="main-image" width="70%">
<div class="uvp">
<h1>Spread Compassion & Track Your Impact</h1>
<button class="join-button">Join Now</button>
</div>
</div>
</div>
.uvp {
padding: 5px 5px 5px 14px;
width: 70%;
background: rgba(66,51,51,.77);
margin: -119px auto 0px auto;
display: block;
text-align: left;
}
.uvp h1 {
color: #fff;
font-size: 247%;
margin-top: 12px;;
}
.landing-container {
padding: 0;
margin: -15px auto 0 auto;
text-align: center;
}
.main-image {
z-index: -1;
position: relative;
}
If you want the header take up ~90-100% of the available width space for higher resolution desktop screens (e.g., > 1600px), style the header accordingly using specific Media Queries.
You can use Media Queries, Some media queries for Standard Devices are:
/* Large screens ----------- */
#media only screen
and (min-width : 1600px) {
/* Styles */
/* Set your font-size here */
}
CSS:
/* Large screen above 1400px */
#media only screen and (min-width: 1400px) {
body {
.uvp h1 {
font-size: your larger size here;
margin-top: your larger size here;
}
}
}
Note: you have double (;;) semicolon in your above margin-top marking.
use cssmediaqueries
CSS Media Queries are a feature in CSS3 which allows you to specify
when certain CSS rules should be applied. This allows you to apply a
special CSS for mobile, or adjust a layout for print.
#media only screen and (max-width: 1633px) and (min-width: 1400px) {
.uvp h1 {
color: #fff;
font-size: 247%; //use your desired bigger font size
margin-top: 12px;;
}
}