Evenly distribute available responsive height over vertically stacked li elements - css

For responsiveness, I'm developing a website with bootstrap (1st time) and php. In the upper part of the page I want to use a row divided into 2 colums (col-sm-3 & 9), the wider one occupying an image carousel, the other one an unsorted list with 2 list elements, one image, one text. Now I want the elements to have the same height, i.e. 50% of the (responsive) height of the carousel image. The carousel image has a 100% width of the column, so when the display gets smaller, so will the height of the carousel. Thus the height of the list elements has to diminish accordingly.
<!-- Carousel Row -->
<div class="row" id="top">
<!-- Sidebar -->
<div class="col-sm-3 sidebar" id="sidebar">
<ul class="nav navbar-nav-custom">
<li class="logo">
<img>
</li>
<li class="text">
|
|
</li>
</ul>
</div>
<!-- Carousel -->
<div class="col-sm-9" id="carouselbar">
<div id="carousel" class="carousel slide" data-ride="carousel">
|
|
</div><!-- /.carousel -->
</div>
</div>
I tried to find a solution myself but didn't succeed nor could I find a solution after a real extensive internet search. Could anyone help me out giving these list elements a responsive height, preferably CSS only.

EDITED after comments below.
You can get a responsive height by setting the height to 0 and adding padding-bottom as a percentage. You would need to adjust the acutal percentage depending on your aspect ratio.
You could then add the text inside as an absolutely positioned div with height: 100% and width 100%.
Here's a fiddle I had to use media queries to get the height to stay at 50%.
<li class = 'text_holder'>
<div id ='text'>
My text here
</div>
</li>
.text_holder {
height: 0;
padding-bottom: 85%;
background-color: yellow;
}
#text {
position: absolute;
height: 100%;
width: 100%;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
#media (min-width: 992px) {
.logo {
padding-bottom: 83%;
}
.text_holder {
padding-bottom: 83%;
}
}
/* Large devices (large desktops, 1200px and up) */
#media (min-width: 1200px) {
.logo {
padding-bottom: 81%;
}
.text_holder {
padding-bottom: 81%;
}
}
For the image, you would just use the same method as the carousel, give it a percentage based width, and the height will scale accordingly.
Otherwise, you can adjust font-size to be smaller for lower res screens. You would need to adjust the following to suit your actual design.
/* Extra small devices (phones, less than 768px) */
/* No media query since this is the default in Bootstrap */
.text {
font-size: 1.1em;
}
/* Small devices (tablets, 768px and up) */
#media (min-width: 768px) {
.text {
font-size: .8em;
}
}
/* Medium devices (desktops, 992px and up) */
#media (min-width: 992px) {
.text {
font-size: .9em;
}
}
/* Large devices (large desktops, 1200px and up) */
#media (min-width: 1200px) {
.text {
font-size: 1em;
}
}

Related

How can I make a large image respond nicely on all screen sizes?

I am using Twitter Bootstrap's img-responsive class.
I have an image (1920x1200) that looks too big, in terms of height, on a lg screen and correct on an xs screen.
If I cut the height of the image, it looks correct on a lg screen, but way too small on an xs screen.
I tried setting the image's max-height, but it also changes the width, resulting in gray space on either side of the image.
How can I make a large image respond nicely on all screen sizes?
<div class="container-fluid text-center">
<div class="row hero-image-container vertical-align">
<img src="../../static/images/house.jpg" class="img-responsive">
<h1 class="hero-image-address">
<i class="hero-location-icon ion-ios-location" ariahidden="true"></i> Address Here
</h1>
<div class="hero-image-after"></div>
</div>
</div>
Yasin's answer looks quite practical.
Add media queries to make the image container's height look good in various common viewport size, like so:
.imageContainer{
max-height: 600px;
overflow: hidden;
}
/* common tablet portrait */
#media (min-width: 768px) {
.imageContainer{ max-height: 800px; }
}
/* common tablet landscape */
#media (min-width: 1024px) {
.imageContainer{ max-height: 900px; }
}
/* common 15" notebook */
#media (min-width: 1400px) {
.imageContainer{ max-height: 1000px; }
}
<div class="imageContainer"><img src="http://www.walldevil.com/wallpapers/a52/wallpapers-pixel-landscapes-wallpaper-mountain-mountains-large-landscape.jpg"></div>
You can set maxHeight on the parent container and set overflow to hidden. So it will cut off the image. Something like this. This image is 1080px but I am only showing 600px of it.
.imageContainer{
max-height: 600px;
overflow: hidden;
<div class="imageContainer"><img src="http://www.walldevil.com/wallpapers/a52/wallpapers-pixel-landscapes-wallpaper-mountain-mountains-large-landscape.jpg"></div>
.img-holder {
min-height: 500px;
background: url('http://wfiles.brothersoft.com/w/waterfall-hd-wallpaper_171535-1920x1200.jpg') center center no-repeat;
background-size: cover;
}
#media screen and (max-width:768px) {
.img-holder {
min-height: 300px;
}
}
#media screen and (max-width:400px) {
.img-holder {
min-height: 200px;
}
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/>
<div class="container-fluid text-center">
<div class="row hero-image-container vertical-align">
<div class="img-holder">
</div>
<h1 class="hero-image-address">
<i class="hero-location-icon ion-ios-location" ariahidden="true"></i> Address Here
</h1>
<div class="hero-image-after"></div>
</div>
</div>
Try using it as background-image like this with background-size:cover.

How to show divs in one column on web and two columns on mobile?

I have for divs:
<div id="1"></div>
<div id="2"></div>
<div id="3"></div>
<div id="4"></div>
On desktop, they are positioned like this:
1
2
3
4
On mobile, I would like them to be positioned like this:
1 3
2 4
if i do
div{
width: 50%;
float: left;
clear left;
}
#3,#4{
float: right;
clear: right;
}
They end up like this:
1
2 3
4
The divs are dynamically generated, so if I can avoid adding additional markup, that would be very good.
Demo Fiddle
Given the HTML
<div class='wrapper'>
<div id="1">1</div>
<div id="2">2</div>
<div id="3">3</div>
<div id="4">4</div>
</div>
You could use the CSS:
.wrapper > div {
border:1px solid; /* <-- not needed, used to show div locations */
height:100px; /* <-- not needed, used to show div locations */
}
#media screen and (max-width: 300px) { /* <-- apply styles on certian screen size */
.wrapper {
-webkit-column-count:2; /* <--put the divs in two columns */
-moz-column-count:2; /* <--put the divs in two columns */
column-count:2; /* <--put the divs in two columns */
}
}
Changing the 300px for whatever you deem a mobile device screen to be
I know this has been answered, but I thought I'd give you a different perspective and actually a more commonly used one as well. If you apply bootstrap framework (even if not to a global extend) but you can gut out the css the is valid to your problem, you will be able to do this a lot more comfortably. without getting into css specifity. All you have to do is make proper us of col-md-12 and col-xs-6
See DEMO
Another approach would simply use media queries to change the width of the divs. This would allow you to avoid adding more markup.
http://jsfiddle.net/14ecjy7n/1/
div {
box-sizing: border-box;
float: left;
}
#media all and (max-width: 639px) {
div {
width: 50%;
}
}
#media all and (min-width: 640px) {
div {
width: 100%;
}
}

How do I make the background color responsive on mobile devices?

I have created a website using bootstrap and the responsive features works fine, except for the background color of the div. The elements (images) are stacked but the background color remains only behind the first image in the row. I am looking for a way to extend the background-color on mobile devices.
HTML:
<div id="omos">
<div class="row">
<div class="col-md-6 col-xs-12">
<h2>Kristoffer Andreasen</h2>
<img style="height:280px; width:420px;" src="http://i.imgur.com/874qJmM.png" class="img-responsive">
<div class="Krille">
<p>Indhold</p>
<p>Marketing</p>
<p>Webdesign</p>
</div>
</div>
<div class="col-md-6 col-xs-12">
<div class="Kalli">
<h2>Kasper Hjortsballe</h2>
<img style="height:200px; width:200px;" src="http://i.imgur.com/kTleong.png" class="img-responsive">
<p>Programmør</p>
<p>Layout</p>
<p>Grafik</p>
</div>
</div>
</div>
</div>
CSS:
#omos {
background-color: #f7f7f7;
height: 80vh;
clear: both;
text-align: center;
}
I have tried several options but no one seems to solve the problem. Does anyone know what to do?
Remove the height property of the div, and it should work.
Bootstrap uses media queries to show things differently on differnet sized screens. If you're saying your CSS works on desktop but not on mobile, it would be because you're not using media queries.
/* Custom, iPhone Retina */
#media only screen and (min-width : 320px) {
}
/* Extra Small Devices, Phones */
#media only screen and (min-width : 480px) {
}
/* Small Devices, Tablets */
#media only screen and (min-width : 768px) {
/* your mobile css, change this as you please */
#omos {
background-color: #f7f7f7;
height: 80vh;
clear: both;
text-align: center;
}
}
/* Medium Devices, Desktops */
#media only screen and (min-width : 992px) {
}
/* Large Devices, Wide Screens */
#media only screen and (min-width : 1200px) {
/* your desktop css */
#omos {
background-color: #f7f7f7;
height: 80vh;
clear: both;
text-align: center;
}
}
let's think about what your style sheet does,
height: 80vh;
that makes us do something that is 80% of the viewports height, so if our div stacks outside of that when switching to 12 col it is not going to get the color, as your color is not based on the div, but is based relative to the viewport, twitters media queries however will change your child elements and override this wrapping divs height, so you get stuck, I'd see if min-height will work, or remove height all together if possible(not sure what you're vision is exactly)
Set the height property to auto, it will work fine.
height: auto

Media Query Not Working With Larger Viewport Sizes

I have a set of social media icons at the top of my sidebar: outtacontext. But I'm having trouble positioning them in the right place at different viewport sizes. Actually, it's working for all the smaller viewports but the positioning is off for the larger screens.
Here's my main css for this positioning:
#top #sidebar_bg .social_bookmarks {
position: absolute;
right: 69px;
z-index: 152;
margin-top: 102px;
}
#media only screen and (max-width: 989px) and (min-width: 768px){
#top #sidebar_bg .social_bookmarks {
right: 33px;
margin-top: 65px;
}
}
For some reason, with the larger screens the icons appear to get centered within the sidebar. But any media queries I make to adjust just start adding problems.
Can anyone point me in the right direction? Thx.
First of all, I'm Argentinean, so forgive me for my English!
Here's my help...
The HTML markup is:
<div id="sidebar_bg">
<ul class="social_bookmarks">
<li class='twitter'>
<a href='http://twitter.com/outtacontext'>Follow us on Twitter</a>
</li>
<li class='facebook'>
<a href='http://facebook.com/chamomileteaparty'>Join our Facebook Group</a>
</li>
<li class='gplus'>
<a href='https://plus.google.com/106461359737856957875/posts'>Join me on Google Plus</a>
</li>
<li class='linkedin'>
<a href='http://linkedin.com/jeffgates'>Add me on Linkedin</a>
</li>
<li class="rss">
RSS
</li>
<li class="flickr">
Flickr
</li>
</ul>
</div>
The CSS styles:
#top #sidebar_bg {
text-align: center; /* This will center EXACTLY on the middle the icons (acording to the #sidebar_bg width on every screen resolution */
}
#top #sidebar_bg .social_bookmarks {
display: inline-block;
z-index: 152;
margin-top: 102px;
}
#media only screen and (max-width: 989px) and (min-width: 768px){
#top #sidebar_bg .social_bookmarks {
margin-top: 65px;
}
}
ALTERNATIVE CSS - if you want to put it right or left, and use media queries to determine the separation width from one of the sides, you can use this:
#top #sidebar_bg {
text-align: *****; /* right or left */
}
#top #sidebar_bg .social_bookmarks {
margin-*****: 40px; /* right or left */
display: inline-block;
z-index: 152;
margin-top: 102px;
}
#media only screen and (max-width: 989px) and (min-width: 768px){
#top #sidebar_bg .social_bookmarks {
margin-*****: 25px; /* right or left */
margin-top: 65px;
}
}
I solved the problem. I had tried making a media query for what I thought was the largest viewport size. As it turns out, I wrote it for the wrong width (just "barely" wrong but you know what that means -g). The media query for positioning should have been set for a min-width of 1140 px. I had set it for 1122px. So close. LOL

Variable width DIV using pixel minimum and maximum widths?

Kinda stuck on a small issue trying to use a div with a background image in the top left [a logo] not sure how to get this done.... since the variable width is not dependent on a percentage width... i.e.
the maximum width of the div is 1200px
the minimum width of the div is 900px
When someone resizes their browser I need that div to expand or contract depending on the viewport size.
Any thoughts on how I can do this [is this possible without javascript?]?
UPDATE
This is where I got to - seems to work well in most browsers until I hit IE7..
<div id="viewport" class="[[*layout]]">
<div id="contentwrapper">
<div class="wrapper logo">
<div id="header">
[[$TopNav]]
</div>
<div id="content" class="homepage">
[[!If? &subject=`[[*id]]` &operator=`==` &operand=`1` &then=`[[$HomePageTpl]]` &else=`[[$DefaultPageTpl]]` ]]
</div>
</div>
</div>
<div class="wrapper footer">
<div id="footer">
<div id="footnav">[[$FootNav]]</div>
<div id="copyright">[[$Copyright]]</div>
<div id="news-feed">[[$NewsFeed]]</div>
</div>
</div>
div {border: 1px dotted #ccc;}
div#viewport {width:100%;float:left;min-height:100%;position:relative;background-color:#000000;}
div#contentwrapper {width:100%;float:left;background-color:#ffffff;margin-top:8px;}
div#content, div#footer, div#header {float:right;width:900px;padding-left:100px;}
div#header {}
.wrapper {
margin:0 auto;
height:150px;
width:100%;
max-width:1110px;
min-width:1060px;
text-align:left;
}
.wrapper.logo {
background:transparent
url(/assets/images/layout/anderson-lyall-consulting-group-logo.png) no-repeat left top;
}
div#topnav {width:900px;float:right;margin:0 auto;border:1px solid #cc0000;}
CSS has 2 properties for those scenarios, that work from IE7+ called:
min-width: https://developer.mozilla.org/en/CSS/min-width
max-width: https://developer.mozilla.org/en/CSS/max-width
That's probably what you are looking for, you could set the width to 100% first then add the min/max width to control it.
For a no-js solution on modern browser you can use CSS media queries like so
#media screen and (max-width: 1199px) {
div { width: 900px; }
}
#media screen and (min-width: 1200px) {
div { width: 1200px; }
}
this will automatically resize the div depending on your window width and not on the content. Media queries support: http://caniuse.com/css-mediaqueries
a simple proof-of-concept demo
<html>
<head>
<style>
div { margin: 0 auto; border: 1px red solid }
div:after { display: block; }
#media screen and (max-width: 1199px) {
div { width: 900px; }
div:after { content: " max-width is 1199px" }
}
#media screen and (min-width: 1200px) {
div { width: 1200px; }
div:after { content: " min-width is 1200px" }
}
</style>
</head>
<body>
<div>Resize your browser</div>
</body>
</html>

Resources