Responsive Background Image (And keeping Text centered in the middle of that) - html

I am trying to code up a banner with a minimal amount of media queries, as in the past for header images and text, I can't figure out a way to use anything under 10.
My current issue:
I have a responsive background image that looks like this:
Header Image
However as the browser resizes, although the background image is being responsive, the container within the background div that houses the text is not resizing, therefore not keeping the text centered in the middle.
You can see something like this is happening:
Container overflowing
I've tried all kinds of things, heights on containers, removing heights. But all areas I end up getting to, I am having to do too many media queries to fix the text, or change the height of the image instead as the screen size goes down. I'd like to try and learn how to code a better header for this website that is more flexible and intuitive, that I can carry over too my future projects.
Here is the code HTML:
<section class="home_banner">
<?php
$featuredimage = get_the_post_thumbnail_url();
?>
<div class="banner_image" style="background-image: url('<?php echo $featuredimage; ?>'); ">
<div class="container">
<div class="banner_text_inner">
<p>Lorem Ipsum</p>
<div class="banner_excerpt">
<h1>Powerful engaging opening title</h1>
</div>
</div>
</div>
</div>
</section>
Here is the css:
.home_banner{
.banner_image {
background-size:100%;
height:100rem;
background-repeat: no-repeat;
}
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
text-align: center;
.banner_text_inner {
max-width: 1000px;
width: 100%;
margin: auto;
padding: 0 10px;
box-sizing: border-box;
font-family: $autumnchant;
font-size: 40px;
color: $color-primary;
#media (max-width: 1000px){
font-size: 30px;
p {
margin-bottom: 0;
}
}
#media (max-width: 400px){
font-size: 22px;
}
}
.banner_excerpt {
#media (max-width: 1000px){
h1 {
font-size: 40px;
}
}
#media (max-width: 400px){
h1 {
font-size: 30px;
line-height: 1;
letter-spacing: 5px;
margin-bottom: 0;
}
}
}
}
}

Try to add "background-size: cover;" to your banner-image and also add min-height.

Related

Ability to stack divs using flexbox when window resizes/gets small

I'm trying to get my divs to stack on top of each other when a page is responsive and resizes, and although I'm using flex wrappers, it doesn't seem to respond when I resize the screen. So on a regular screen, it aligns like this:
picture text
But when I resize the screen smaller, I am trying to make it align like this:
picture
text
Even when using the flex attribute in a wrapper class, it still does not respond to a resizing window, it just gets cut off. Am I utilizing this property correctly? This is what I'm doing:
HTML:
<div class="ext-wrapper">
<div data-bind="if: !gridVisible()">
<div class="ext-policy-line-break"></div>
<div class="ext-loading-width-height">
<div class="ext-small-screen">
<span class="ext-policy-left-load" data-bind="image: imagePath"></span>
</div>
<div class="msportalfx-font-bold">TITLE</div>
<br>
<div>SUBTITLE</div>
<div>DESCRIPTION</div>
</div>
<hr class='ext-section-line' />
</div>
</div>
CSS:
.ext-policy-line-break {
line-height: 20px;
}
.ext-policy-left-load {
float: left;
margin-top: 20px;
margin-right: 20px;
margin-bottom: 20px;
}
.ext-policy-loading-text {
float: right;
}
.ext-section-line {
color:#A19F90;
width:100%;
}
.ext-small-screen {
visibility: hidden;
}
#media (min-width: 320px) {
.ext-small-screen {
visibility:visible;
}
}
.ext-left-margin {
margin-left: 20px;
}
.ext-add-settings {
margin-bottom: 20px;
}
.ext-loading-width-height {
width: 562px;
height:170px;
align-items: center;
}
.ext-title {
font-size: 16px;
font-weight: bold;
}
.ext-wrapper {
display: flex;
width: 100%;
flex-wrap: wrap;
justify-content:flex-start;
}
I would recommend using a css grid, instead of float: left or float: right;
This would allow you to change the display when the screen is less then a given amount.
Codepen Example
https://codepen.io/mythius/pen/BazPgNW

How to fix responsive image display with inherited padding

I am modifying a joomla virtuemart template. I have added the banner image in a custom code block that I have placed in an existing module position. As such it has inherited some css properties around it that appear to be giving it padding. My banner should display the full width of the top menu bar (you can see it here: https://www.artisanbelle.com). I don't know how to remove the padding and fix the display. It has displayed the correct image, which is 1920px wide and when I had this same image displayed in the pre-existing slider, it fitted across the menu bar fine.
The code I have tried is below. Also this is the first time I am using srcset and sizes, so if I am doing anything wrong there, please let me know.
<div class='illustrationz'>
<img src='https://www.artisanbelle.com/images/stories/mainlg.jpg'
srcset='https://www.artisanbelle.com/images/stories/mainxsm.jpg 600w,
https://www.artisanbelle.com/images/stories/mainsm.jpg 960w,
https://www.artisanbelle.com/images/stories/mainmd.jpg 1280w,
https://www.artisanbelle.com/images/stories/mainlg.jpg 1920w'
sizes='(min-width: 600px) 960px, (min-width: 960px) 1280px, (min-width: 1280px) 1920px, 100vw'/>
</div>
</div>
.illustrationz img {
width: 100%;
display: block;
}
.sectionz {
width: 100%;
display: flex;
justify-content: center;
align-items: center;
margin: 0;
padding: 0;
}
.headerz {
height: auto;
justify-content: inherit;
align-items: inherit;
}
And this is the code that the template is wrapping this section in:
<div class="mod-slider-cont">
<div class="container">
<div class="inner-container">
<div class="custom"
And the template css that goes with it. I'm having trouble finding mod-slider-cont:
.vpf-topbar .inner-container {
border-bottom: 1px solid transparent;
padding: 5px 0;
margin-bottom: -1px;
}
#vpf-header .navbar .container .inner-container {
padding-bottom: 20px;
margin-bottom: -1px;
}
There's a bit of a formatting issue here, you could use this as a fix:
.illustrationz img {
width: 100vw; // Use viewport width here instead.
display: block;
}
Although I've seen it used like this, the sizes attribute is typically only used it rel=icon and you'd be better off using css media queries for this.

How do I make this div extend to 100% of the screen on mobile/tablet?

EDITED FOR CLARITY
Currently I have 2 divs that are side by side on DESKTOP "kt_mobile_left" and "kt_mobile_right" -- each div is 47% of the page wide.
I want it so that on mobile AND tablet, the left div ("kt_mobile_left") stretches to 100% of the page and pushes "kt_mobile_right" underneath it. Right now it stays at only 47% of the page. I set a media query to make the width 100%, but it doesn't seem to be adding it to the div.
Is my media query correct for this?
Here's my code.
<style type="text/css">
/*desktop css */
.kt_mobile_left {
width: 47%; display: inline-block; vertical-align: text-top; margin-right:25px; padding: 10px;
}
.kt_mobile_right {
width: 47%; display: inline-block;vertical-align: text-top; max-width: 457px;
}
/* #### Tablets or mobile css */
#media screen and (max-device-width: 867px){
.kt_mobile_left {
width: 100% !important;
min-width: 800px;
display: block !important;
margin-right: 0px;
}
.kt_mobile_right {
width: 100% !important;
float: none !important;
display: block !important;
}
}
</style>
<div style="margin: 0 auto; text-align: center; margin-top: 25px; padding: 25px; width: 100%">
<div class="kt_mobile_left">
<p style="font-size: 40px; line-height: 45px; margin: 0 0 40px; position: relative; text-align: center; font-weight: 300; color: #000; text-align: left">Learn more about who may be calling or texting your teen</p>
<p style="font-size: 15px; line-height: 150%; text-align: left"> TeenSafe has partnered with BeenVerified, a one-stop bakground check service, to help you learn more about the people who are calling or texting your child. Try it for just $1 for 5 days. </p>
</div>
<div class="kt_mobile_right">
<img src="http://storage.googleapis.com/instapage-user-media/ce3c4430/5232032-0-BeenVerified.png">
</div>
</div>
Here's my JSfiddle: https://jsfiddle.net/kn4xrue5/
Can anyone help?
You have just messed up the code. I have cleaned some of your code and it seems that media queries works fine.
.kt_mobile_left {
background: red;
width: 47%; display: inline-block; vertical-align: text-top; margin-right:25px; padding: 10px;
}
.kt_mobile_right {
background: green;
width: 47%; display: inline-block;vertical-align: text-top; max-width: 457px;
}
#media screen and (min-width: 320px) and (max-width: 1024px) {
.kt_mobile_left {
width: 100%;
}
.kt_mobile_right {
width: 100%;
}
}
Check out the fiddle
https://jsfiddle.net/mgautam1408/xjdcqvu3/
I still don't understand what your trying to do, but a couple things first and hope this helps. On KT-mobile-right you have a max-width. You need to clear that in your media query by doing max-width: auto otherwise nothing it won't go 100%. Then to just get the two containers to overlap you can just add position: absolute; to the classes in the media query.
Another suggestion would be to just hide the one that's going to be on the bottom by added display: none to that class in the media query.
My solution uses bootstrap.
<div class="col-xs-12">
<div class="row">
<div class="col-xs-12 col-sm-6">
<h1 style="font-size: 40px; line-height: 45px; margin: 0 0 40px; position: relative; text-align: center; font-weight: 300; color: #000; text-align: left">Learn more about who may be calling or texting your teen</h1>
<p style="font-size: 15px; line-height: 150%; text-align: left"> TeenSafe has partnered with BeenVerified, a one-stop bakground check service, to help you learn more about the people who are calling or texting your child. Try it for just $1 for 5 days. </p>
</div>
<div class="col-xs-12 col-sm-6">
<img src="http://storage.googleapis.com/instapage-user-media/ce3c4430/5232032-0-BeenVerified.png">
</div>
</div>
</div>
Using Bootstrap has its pros and cons but it is a very popular framework. This solution does not use any of the CSS that you provided because it uses Bootstrap's classes. I did pull out the CSS that you had left inline since it is generally not a best practice.
If you have questions or want tweaks to the code let me know.

Adjusting text size in sidebar based on screen width

I have a sidebar div that takes up 12% of the total screen width (set as a css property). I also have an <h1> block within this div, with a title. When I switch monitors to a smaller one, the sidebar ends up being skinnier, resulting in the title to extend OUT of the sidebar.
How can I format so that the text will always stay within the line? ("MY TI..." is fine for a result)
If the title text is known, you may be able to using viewport units vw for the font-size either in the original style or in the media queries.
You would also need to set the sidebar width to vw too, or a percentage value to make it all responsive.
html, body {
height: 100%;
margin: 0;
}
.sidebar {
border-right: solid;
height: 100%;
float: left;
width: 15vw;
}
.sidebar h1 {
font-size: 4vw;
text-align: center;
}
<div class="sidebar">
<h1>MyTitle</h1>
</div>
jsFiddle
Another solution would be using CSS ellipses, replace the overflow text with "...".
html, body {
height: 100%;
margin: 0;
}
.sidebar {
border-right: solid;
height: 100%;
width: 15%;
float: left;
}
.sidebar h1 {
white-space: nowrap;
width: 100%;
overflow: hidden;
text-overflow: ellipsis;
}
<div class="sidebar">
<h1>MyTitle MyTitle MyTitle</h1>
</div>
jsFiddle
There is no 100% sure way when it comes to CSS but the title should normally go onto two lines which would be better than what its doing in your screen shots. Post your code if you want someone to look at that.
What you should do though is use media queries to make the sidebar wider when its on a smaller screen:
.sidebar
{
width:12%;
}
#media screen and (max-width: 600px) {
.sidebar
{
width:30%;
}
}
Here is an example
http://codepen.io/nathanfelix/pen/KzZPGy
Also, here you can read more about media queries:
http://www.w3schools.com/cssref/css3_pr_mediaquery.asp
Please try like this:
<div class="sidebar">
<h1>
MY TITLE
</h1>
</div>
.sidebar {
border-right: 1px solid black;
height: 600px;
width: 186px;
}

How to scale jumbotron bootstrap on larger monitors?

I currently have a website that uses the jumbotron feature from bootstrap taken from the Codecademy "how to make a website" tutorial.
In it, it uses the following html:
<div class="jumbotron">
<div class="container">
<h1>Heading</h1>
<p>Tagline</p>
<button type="button" class="btn btn-lg btn-danger">Learn More</button>
<!--Learn More-->
</div>
</div>
and the following CSS:
.jumbotron {
background-image:url("flight.jpg");
height: 500px;
background-repeat: no-repeat;
background-size: cover;
margin-bottom:0px;
}
.jumbotron .container {
position: relative;
top:100px;
}
.jumbotron h1 {
color: #CC0000;
font-size: 48px;
font-family: 'Shift', sans-serif;
text-shadow:none;
}
.jumbotron p {
font-size: 20px;
color: #CC0000;
text-shadow:none;
}
The background image is not scaling properly when a larger monitor is used, and the image increases in size within the div, but the div stays the same size. The result looks like a zoom on the image rather than the image staying whole.
Any idea how to make it scale properly on larger monitors?
Change to
Oh I apologive for misunderstanding your question you need to edit the css for .jumbotron needs to be cover and position to center.
.jumbotron { background-size: cover; background-position: center; }
Add Container Div around Jumbotron!
<div class="container">
<div class="jumbotron">
<div class="container">
<h1>Heading</h1>
<p>Tagline</p>
<button type="button" class="btn btn-lg btn-danger">Learn More</button>
<!--Learn More-->
</div>
</div>
</div>
UPDATE 2:
This is helpful link. I think, your solution in jQuery.
UPDATE:
Here is solution.
background: url("flight.jpg") no-repeat center center;
height: 500px;
background-repeat: no-repeat;
background-size: 100%;
margin-bottom: 0px;
}
You can use #media query like this:
#media (max-width: 768px) {
.btn-responsive {
padding:2px 4px;
font-size:11px;
line-height: 1;
border-radius:3px;
}
}
#media (min-width: 769px) and (max-width: 992px) {
.btn-responsive {
padding:4px 9px;
font-size:12px;
line-height: 1.2;
}
}
With a #media query, you can write different CSS code for different
media types.
This helps when you want different layout for different media types
such as a screen or a printer, but also when you want different layout
for different devices, which is very useful when making web pages with
responsive design.
You can also have different layout when a user resizes the browser
window up or down to a certain width, or height.
More info.