Using Twitter-Bootstrap, I have a section of a page that includes a sidebar (which does not take up the whole page, just spans down to the bottom of this section). I have several sections to the right of the sidebar, each of which contain an image on one side, some text on the other, and require a bottom-border.
I'm having trouble with the bottom-border. Since each section is contained in a div, and everything's floated using Bootstrap columns, the divs are 0 height and the borders all float to the top.
EDITED for clarification: I need two distinct columns in this section - a left-hand one (spanning 3 columns) that contains only the sidebar (and takes up the 3 columns all the way to the bottom, leaving blank space after the list), and one (spanning the remaining 9 columns) that contains the story-sections but that stays on the right-hand side of the page.
EDITED to include image (grey lines are included for reference and will not show up on the page, black lines are the borders I'm trying to apply)
Attempted solutions:
Bottom-border on individual elements: I need one solid line across the bottom of each section, so I can't apply bottom-border to both the image and the text.
Clearfix: Since I have this sidebar, I can't use a clearfix, because it pushes everything down past the sidebar.
Overflow: Using overflow causes the images to shrink (which I don't quite understand, but it doesn't help with the border either).
How can I create bottom borders for each of these sections?
<section class="stories">
<div class="container">
<h2>Section header</h2>
<div class="col-sm-3 sidebar">
<h6>list of stories</h6>
<ul>
<li>list-item</li>
<li>list-item</li>
<li>list-item</li>
<li>list-item</li>
</ul>
</div>
<section class="story-section">
<div class="col-sm-3">
<img src="http://placehold.it/330x220">
</div>
<div class="col-sm-6">
<h6>keyword</h6>
<h3>Header of section</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras eleifend dictum neque sed laoreet ...</p>
</div>
</section>
// (there are three more sections with the same class of "story-sections", plus other code on the page, but I've tried to simplify it as much as possible here.)
</div>
</section>
css:
html, body {
height: 100vh;
}
.stories {
.sidebar {
height: 100vh;
}
.story-section {
border-bottom: 1px solid $grey;
}
}
I'm still fairly new with Bootstrap, so there may be a very simple solution, but I haven't been able to track it down. Any help is very welcome!
if you want to have a sidebar with 3 columns + a left section with the remaining 9 columns, then you need to set col-*-9 in your story-section
Note: in bootstrap, you need to have .row right below .container or between nested col-*
html,
body {
height: 100vh;
}
.sidebar {
height: 100vh;
background: lightblue
}
.story-section {
border-bottom: 1px solid grey;
}
img {
margin-top: 10px
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<section class="stories">
<div class="container">
<div class="row">
<h2>Section header</h2>
<div class="col-xs-3 col-sm-3 sidebar">
<h6>list of stories</h6>
<ul>
<li>list-item</li>
<li>list-item</li>
<li>list-item</li>
<li>list-item</li>
</ul>
</div>
<section class="story-section col-xs-9 col-sm-9">
<div class="row">
<div class="col-xs-6 col-sm-6">
<img class="img-responsive" src="http://placehold.it/330x220">
</div>
<div class="col-xs-6 col-sm-6">
<h6>keyword</h6>
<h3>Header of section</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras eleifend dictum neque sed laoreet ...</p>
</div>
</div>
</section>
<section class="story-section col-xs-9 col-sm-9">
<div class="row">
<div class="col-xs-6 col-sm-6">
<img class="img-responsive" src="http://placehold.it/330x220">
</div>
<div class="col-xs-6 col-sm-6">
<h6>keyword</h6>
<h3>Header of section</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras eleifend dictum neque sed laoreet ...</p>
</div>
</div>
</section>
</div>
</div>
</section>
Related
I used img-fluid class inside my img tag but my image is not fully responsive. It is responsive for smaller screen but becomes unresponsive after the screen is enlarged to a certain dimension.
I've read How to make an image responsive using bootstrap without having it take up the entire width of the division? and Bootstrap: img-responsive vs img-fluid but couldn't solve the issue.
Here's how I tried
HTML:
<!-- THE HEADER -->
<div class="container-fluid header">
AUST CSE
</div>
<!-- IMAGE JUST BELOW HEADER -->
<div class="wrapper" style="background: blue">
<img src="images2/banner.jpg" class="img-fluid">
</div>
CSS:
.wrapper {
width: 100%;
}
In smaller screens, the page looks like this:
however, in larger screen, the image don't occupy 100% of the space and looks like this:
I want the image to occupy 100% of the width and scale up its height, just like it does when the screen is smaller.
img-fluid uses max-width: 100%;. Once the containing element is the same size or larger than the image's width, it will stop resizing.
Two options:
1) Use an image with a resolution that is at least the widest width of your container element. If the width of your container element does not have a fixed top end (i.e. will always be 80% of viewport width), then pick a sufficiently large image so that it will look good on the majority of displays (lookup stats on most common browser resolutions).
#import url( 'https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css' );
/* For demo purposes and is not required. */
.demo {
background-color: rebeccapurple;
}
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="demo">
<img class="img-fluid" src="https://via.placeholder.com/1000x1000">
</div>
</div>
<div class="col-md-8">
<p>
Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor.
Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor.
Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor.
</p>
</div>
</div>
2) Override .img-fluid so that the image will resize beyond native resolution. The drawback here is the image will get grainy. The smaller the native resolution, the more grainy it will become when scaled to large areas. You can see in my example that the text is quite fuzzy.
#import url( 'https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css' );
/* For demo purposes and is not required. */
.demo {
background-color: rebeccapurple;
}
/* Will override all instances of .img-fluid. */
.img-fluid {
width: 100%;
}
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="demo">
<img class="img-fluid" src="https://via.placeholder.com/100x100">
</div>
</div>
<div class="col-md-8">
<p>
Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor.
Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor.
Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor.
</p>
</div>
</div>
</div>
I've also included a scoping example below which allows you to override only specific images to extend past their native resolution.
#import url( 'https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css' );
/* For demo purposes and is not required. */
.demo {
background-color: rebeccapurple;
}
/* Scoped so that we can target specific images. */
.img-fluid.img-full-width {
width: 100%;
}
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="demo">
<img class="img-fluid img-full-width" src="https://via.placeholder.com/100x100">
</div>
</div>
<div class="col-md-8">
<div class="demo">
<img class="img-fluid" src="https://via.placeholder.com/100x100">
</div>
</div>
</div>
</div>
If you want the image to span all the way across the width of the page on larger screens, then you need to make sure that your image width is just as large as the screen width.
Based off of your issue, it looks like your dimensions are not large enough. The img-fluid class will resize your image, but only to the max of its dimensions.
There are 1 of 2 things you can do to fix it.
(The preferred method) Pick an image that has the correct width for the max size screen you want. (Most of the time, that would be 1920px)
You can add width: 100% to your image so that it will span the full width of your page. But, if the width of your image is smaller than your screen, then the image will not be as clear, which is why it's best to use images that are the correct dimensions.
Example:
Here is an image that has smaller dimensions (your issue): JSFiddle
.container {
border: 2px solid red;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<h2>Image</h2>
<p>The .img-fluid class makes the image scale nicely to the parent element (resize the browser window to see the effect):</p>
<img class="img-fluid" src="https://cdn-static.denofgeek.com/sites/denofgeek/files/styles/main_wide/public/2015/11/main_0.jpg?itok=k1tyTR75" alt="HL2">
</div>
Here is an image that has larger dimensions (1920px): JSFiddle
.container {
border: 2px solid red;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<h2>Image</h2>
<p>The .img-fluid class makes the image scale nicely to the parent element (resize the browser window to see the effect):</p>
<img class="img-fluid" src="https://s.aolcdn.com/hss/storage/midas/f8eff2a90708d58814bb4adc93634cbb/205752037/half-life-2-15622-1920x1080.jpg" alt="HL2">
</div>
I want to display the text (and if the image is smaller, then the image) in the middle by vertically. But i can't accomplish it. Any help?
Here is what i have right now: Jsfiddle
HTML:
<div class="container overview-sm">
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<div class="box2">
<img class="image" src="http://via.placeholder.com/450x450">
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<div class="box2">
<h1 class="content-title">Lorem impsum</h1>
<hr>
<p class="content-sub-title">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sit amet purus ac turpis finibus auctor. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec in dictum arcu, dapibus porta lorem. </p>
</div>
</div>
</div>
</div>
CSS:
.box2 {
margin: 5px 5px 5px 0;
text-align: center;
display: inline-block;
width: 100%;
}
.image{
width: 100%;
max-width: 450px;
}
.row{
padding-bottom: 30px;
}
Image of what i want it to be: Image
The easiest way to do this would be to use Flexbox. Note that older browsers may not have great/any Flexbox support: https://caniuse.com/#search=flexbox)
If you are using Bootstrap 4, its grid uses Flexbox, so the columns in the row should already match each other's height. You can then use Bootstrap 4's Flexbox utility classes to vertically center the column's content: class="d-flex align-items-center". Ex:
<div class="row">
<div class="col d-flex align-items-center">Centered Content</div>
<div class="col">
<ul>
<li>Longer</li>
<li>Multi</li>
<li>Line</li>
<li>Content</li>
</ul>
</div>
</div>
Codepen
If you are using Bootstrap 3, you're going to have to add the appropriate flexbox code to both get your columns to match in height and vertically center your content. Here is a little cheatsheet I find helpful for building out flexbox layouts: http://jonibologna.com/flexbox-cheatsheet/
Edit: Here is the same example with Bootstrap 3:
HTML:
<div class="row row-flex">
<div class="col-md-6 col-v-centered">Centered Content</div>
<div class="col-md-6">
<ul>
<li>Longer</li>
<li>Multi</li>
<li>Line</li>
<li>Content</li>
</ul>
</div>
</div>
CSS:
.row-flex {
display: flex;
}
.col-v-centered {
display: flex;
align-items: center;
}
Codepen
If you want the text to be displayed underneath the second row then you will need to create another image div and add a class so it only displays on mobiles.
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 visible-xs ">
<div class="box2">
<img class="image" src="http://via.placeholder.com/450x450">
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<div class="box2">
<h1 class="content-title">Lorem impsum</h1>
<hr>
<p class="content-sub-title">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sit amet purus ac turpis finibus auctor. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec in dictum arcu, dapibus porta lorem. </p>
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 hidden-xs ">
<div class="box2">
<img class="image" src="http://via.placeholder.com/450x450">
</div>
</div>
https://jsfiddle.net/7goLo3xy/2/
If you need more control than CSS provides you can use a simple jQuery or JavaScript implementable.
$(window).resize(function() {//on resize
setBoxSizes();
});
function setBoxSizes() {
$('.image').each(function() {
let $imageBox = $(this).closest('.box2');
let imageBoxTop = $imageBox.offset().top;
let $otherBoxes = $imageBox.closest('.row').find('.box2');
$otherBoxes.each(function(){
$box = $(this);
if(!$box.find('.image').length) {//doesn't contain .image
if(Math.abs($box.offset().top - imageBoxTop) < 3) {//on the same layout row
$box.height($imageBox.height());
}
else $box.css('height', 'auto');//restores single column layout
}
});
});
}
setBoxSizes();//initial configuration
Fiddle Example! - Handles multiple column layout, changing columns layouts, and ensures centering is performed against equal horizon boxes.
If you don't want to use Flexbox you could always just set a height of 350px and use table and table-cell with vertical-align set to middle.
However this method does restrict you to having a fixed height so, if the height of your image changes the box height won't be relative. If you want the box to relative you could always set a height of 0 and use padding-bottom. More on this here
<div class="box2">
<div class="center">
<div class="center-v">
<h1 class="content-title">Lorem impsum</h1>
<hr>
<p class="content-sub-title">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sit amet purus ac turpis finibus auctor. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec in dictum arcu, dapibus porta lorem. </p>
</div>
</div>
</div>
.center {
display: table;
height: 345px;
}
.center-v {
display: table-cell;
vertical-align: middle;
}
I'm having trouble figuring out how to position items in HTML/CSS, as I'm pretty much a newb when it comes to coding. But I wanted to do this myself without paying a developer.
Basically, I want to create a 3 column width "How it works" with 3 elements(classes?) in each column. An Icon, the heading, and subheading.
It looks like this: https://www.screencast.com/t/KAKYgJQYVLS
Can someone point me in the direction of just getting the headings on top of one another and the each bullet points next to each other?
TIA!
Use <ul> and <li>
ul {
display: flex;
}
<ul>
<li>1. Heading 2 lines of text</li>
<li>2. Heading 2 lines of text</li>
<li>3. Heading 2 lines of text</li>
</ul>
You can try this one. If you want these bullets much bigger, you should replace these by using image or anything else.
* {
margin: 0;
}
ul {
display: flex;
list-style-image: url('http://icons.iconarchive.com/icons/paomedia/small-n-flat/32/sign-check-icon.png');
}
ul li {
margin: 20px;
}
<ul>
<li>
<h3>1. Heading</h3>
two lines of text
</li>
<li>
<h3>2. Heading</h3>
two lines of text
</li>
<li>
<h3>3. Heading</h3>
two lines of text
</li>
</ul>
You can use bootstrap
<div class="row">
<div class="col-sm-4">.col-sm-4</div>
<div class="col-sm-4">.col-sm-4</div>
<div class="col-sm-4">.col-sm-4</div>
</div>
If your starting out I would take a look at a framework like Bootstrap it makes this stuff a lot easier and has good examples of how to do things.
http://getbootstrap.com/
<div class="container">
<div class="row">
<div class="col-sm-4 text-center">
<h3>
heading 1
</h3>
<p>
sub heading
</p>
</div>
<div class="col-sm-4 text-center">
<h3>
heading 2
</h3>
<p>
sub heading
</p>
</div>
<div class="col-sm-4 text-center">
<h3>
heading 3
</h3>
<p>
sub heading
</p>
</div>
</div>
</div>
https://jsfiddle.net/vo1npqdx/475/
May be you would like to tweak with below HTML and CSS.
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
* {
margin:0px;
padding:0px;
}
.wrapper {
width:100%;
float:left;
}
.center {
float:none;
margin:0px auto;
width:600px;
}
.content {
float:left;
width:100%;
}
.col-3 {
width:33%;
float:left;
}
.col-3 p {
text-align: justify;
padding: 5px;
}
.cell {
width: 100%;
float:left;
}
.rounded-icon {
width: 16px;
height: 16px;
border: 1px solid #a84909;
border-radius: 50%;
float:left;
margin-right: 5px;
margin-top: 4px;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="center">
<div class="content">
<div class="col-3">
<div class="cell">
<h3><div class="rounded-icon"></div>Heading 1</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc lectus felis, tristique...</p>
</div>
</div>
<div class="col-3">
<div class="cell">
<h3><div class="rounded-icon"></div>Heading 2</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc lectus felis, tristique...</p>
</div>
</div>
<div class="col-3">
<div class="cell">
<h3><div class="rounded-icon"></div>Heading 3</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc lectus felis, tristique...</p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
I'm trying to get an image to stick to the bottom of one div AND overlap the div above it.
fiddle is here: http://jsfiddle.net/5U34m/1/
There are 2 divs side by side the image in the right side
- if the content of the left is shorter than the right, the image should overlap the row above it
- if the content of the left is longer than the height of the image then the image should stick to the bottom of the right div.
- the image should also be centered in the div
here is the HTML:
<div class="container clearfix">
<div class="wrap header">
<div class="grid_6 logo">
<img src="http://cba.thelibertylab.com/wp-content/uploads/cba-logo-placeholder.png" />
</div>
<div class="grid_6 phone">
<!-- <p>p. 905-579-5302</p> -->
</div>
</div>
<div class="wrap upper-content">
<div class="grid_6">
<p>Nunc id porttitor lectus, et auctor ante. Morbi ullamcorper quam in leo auctor tempor. </p>
<p> Morbi a enim nibh. Vestibulum molestie augue libero, vitae fringilla massa eleifend quis. </p>
</div>
<div class="grid_6 headshot"><div class="img-container"><img src="http://www.manncontractors.com.au/media/pics/site/imagecache/1/1/1176381DF49D6256D968FFB72490208C.jpg" height="400" width="300"/></div></div>
</div>
<div class="wrap lower-content">
<div class="grid_6">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p>
</div>
<div class="grid_6">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p>
</div>
</div>
</div>
The CSS on the fiddle does not work correctly - I'm not sure what to try from here.
Your problem is multiple-fold.
You actually want the text to push down the image when it is longer, so your image should be UNDER your text in the HTML, not adjacent to it. Your structure should be like this:
<div> <!-- First row contains the text -->
<div class="grid_6 txt>YOUR TEXT</div>
<div class="grid_6></div>
</div>
<div class="grid_12"></div><!-- This one to prevent the float from stacking -->
<div> <!-- Second row contains the image and has height = 0 -->
<div class="grid_6></div>
<div class="grid_6 headshot">
<div class="img-container">
<img src="yourimg.jpg">
</div>
</div>
</div>
The CSS
First of all clear the styling height:100%; from .container. This styling rule inhibits the element from expanding and makes it impossible for children to inherit height. (Check for yourself in the console)
If you want the image to truly stick to the bottom, you will have to set .grid6.headshot { line-height: 0; } The line-height property actually inserts unwanted space in elements where you don't want it.
Replace .img-container property top: 70px; with margin-top: -450px; (your image height)
Set a min-height on the grid_6 div which has text in it, equal to your image height (450px) - your header height (94px) + some padding (10px) => 346px; If you don't do this your image will overlap your navigation
For #media only screen and (max-width: 767px), set .img-container { margin-top: 0; } else the image will overlap the text on mobile.
I've updated your fiddle here: http://jsfiddle.net/5U34m/4/
Note:
Consider using id's to target specific divs. The classes here are quite a mess.
Consider using responsive CSS frameworks (% and ems) instead of fixed-width ones.
You'll need to use position: absolute for the styling of your image. Then use the Left, Right, Top, Bottom attributes to position it as you like.
To adjust what element goes on top of each other, use the z-index attribute!
We have a fix set of CSS rules, but when we modify the HTML markup to include a canvas, a weird padding appears on the neighboring cell. Here is the CSS:
.wrap{
width:100%;
display: table;
}
.row {
display: table-row;
}
.left{
width: 100px;
display: table-cell;
background-color: #0f0;
}
.right{
background-color: #f00;
display: table-cell;
}
Normal case:
See the fiddle here. Note the position of the text in the red cell: top, aligned with the top of the cell.
<div class="wrap">
<div class="row">
<div class="left">
Lorem<br>Lorem<br>Lorem<br>Lorem<br>Lorem
</div>
<div class="right">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
</div>
</div>
Canvas case:
See the fiddle here. We swapped the Lorem text in the left cell for a 90x90 canvas. See how the text on the red cell is now aligned with the bottom of the canvas and a padding is applied to the cell.
<div class="wrap">
<div class="row">
<div class="left">
<canvas width='90px' height='90x'></canvas>
</div>
<div class="right">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
</div>
</div>
Question: Could you explain why this padding appears on the left cell, and if this is an expected behavior? Also, could you propose a solution that gets rid of this 'bug'?