Set height to be equal width minus some value - css

<div>
<div class="row">
<div class="col-md-4">
<div class="cta-item cta-1">
<a href="#">
<div class="hover"><p>Serving food fine to fast. Come <span class="arrow">Hungry</span></p></div>
<p class="cta-title">Visit</p>
</a>
</div>
</div>
<div class="col-md-4">
<div class="cta-item cta-2">
<a href="#">
<div class="hover"><p>Serving food fine to fast. Come <span class="arrow">Hungry</span></p></div>
<p class="cta-title">Shop</p>
</a>
</div>
</div>
<div class="col-md-4">
<div class="cta-item cta-3">
<a href="#">
<div class="hover"><p>Serving food fine to fast. Come <span class="arrow">Hungry</span></p></div>
<p class="cta-title">Eat</p>
</a>
</div>
</div>
</div>
</div>
<script>
$(document).ready(cta);
$(window).resize(cta);
function cta() {
var ctaWidth = $('.cta-item').width();
var newHeight = ctaWidth - 40;
$('.cta-item').css( 'height', newHeight );
}
</script>
My question is how to do height = width - 40px for particular element by pure css?
Thanks.

If you need CSS to work in older browsers, too, there is a way utilizing the fact, that vertical padding is always calculated in relation to the parent element’s width. So you can do like this to enforce the height:
// additional styles when Bootstrap css is loaded
.cta-item {
position: relative;
background-color: #ffcccc;
*zoom: 1;
}
.cta-item::before {
width: 0;
float: left;
display: block;
content: ' ';
padding-bottom: 100%; /* makes height match width */
margin-top: -40px; /* value substracted from height */
}
// clearfix
.cta-item::after {
content: ' ';
display: table;
clear: both;
}
Working example: https://jsfiddle.net/7m5vyaqs/1/
It works like a min-height. If you want it to be a max-height, you have to wrap the .cta-item’s contents into a container and position it absolute. Like:
.cta-item > *:first-child {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
}

Related

Positioning of DIV and elements using Z-index [duplicate]

This question already has answers here:
Why can't an element with a z-index value cover its child?
(5 answers)
Closed 3 years ago.
I have 2 sections (banner and content section)
content section overlaps in the banner because of the design. So I need to bring front the content section.
My problem is my search element, if you are going to add keyword it auto suggest and the suggestion box will appear on the banner just like searching on google. but the problem is the suggestion box also moved the back of the content section (behind the 3 images).
<div class="body">
<div class="section-banner">
<div class="search-input">
<input type="text" value="search button here">
<div class="float-suggestion-box">
<!--Suggestion Box code here-->
</div>
</div>
</div>
<div class="section-content">
<img src="#1"/>
<img src="#2"/>
<img src="#3"/>
</div>
<div>
<stlye>
.section-banner
{
z-index: -1;
position: relative;
}
.section-content
{
z-index: 9;
position: relative;
}
.search-input .float-suggestion-box
{
z-index: 9999;
position: relative;
}
</style>
I need make the float suggestion box at the front. please help me thank you!
Just tart typing
var sugg = document.querySelector('.float-suggestion-box');
function openSugg(el){
if(el.value) {
sugg.innerHTML = ['abs','def', 'ghi', 'jkl', 'mno', 'pqr'].map(t => "<p>"+t+"</p>").join('');
} else {
sugg.innerHTML = '';
}
}
.section-banner
{
position: relative;
}
.section-content
{
position: relative;
}
img {
width: 30%;
}
.search-input .float-suggestion-box
{
z-index: 1;
position: absolute;
top:100%;
min-width: 300px;
left: 0;
background-color: #d1d1d1;
}
<div class="body">
<div class="section-banner">
<div class="search-input">
<input type="text" value="search button here" oninput="openSugg(this)">
<div class="float-suggestion-box">
<!--Suggestion Box code here-->
</div>
</div>
</div>
<div class="section-content">
<img src="https://data.whicdn.com/images/172081452/original.gif"/>
<img src="https://data.whicdn.com/images/172081452/original.gif"/>
<img src="https://data.whicdn.com/images/172081452/original.gif"/>
</div>
<div>

Owl carousel 2 images with 16:9 aspect ratio (responsive)

I am using owl carousel 2 and my problem is that I am looking for a solution to maintain images' aspect ratio while users resizing the browser window (responsiveness) based on the graphist's design.
which makes the developing harder, each item is a carousel includes 3 pictures,
I use this function in Scss to make my images 16:9 :
#function aspect($width) {
#return $width * 9 / 16;
}
which is not very interactive and always returns the static size of the image (though I've written media queries in different scales, still it's not very stable)
here is my carousel init code:
$('.owl-carousel').owlCarousel({
nav: false,
dots: true,
items: 1,
center: true,
//autoWidth: true,
loop: true,
});
and my HTML code is :
<div class="col-lg-2 col-md-3 col-sm-4 col-xs-12">
<a href="#Url.Action("Video", "Service")">
<div class="owl-carousel owl-theme">
<div class="item">
<img src="#service.serviceThumbpath" alt="#service.serviceName" title="#service.serviceName" />
</div>
#foreach (var thumb in videoThumbpath)
{
<div class="item">
<img src="#thumb" alt="#serviceName" title="#serviceName" />
</div>
}
</div>
<h5> #service.serviceName</h5>
<div class="details">
#{
_serviceRate = float.Parse(service.serviceRate);
}
<p class="pull-left">#service.VideoCount </p>
</div>
</a>
</div>
is there any solution that I can make it responsive images through owl carousel 2?
thanks a lot
I found the solution, I added these styles to my Scss file:
.outer {
position: relative;
&:before {
display: block;
content: "";
width: 100%;
padding-top: (9 / 16) * 100%;
}
> .inner {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
height: 100%;
}
}
Then I changed my Html like this:
<div class="col-lg-2 col-md-3 col-sm-4 col-xs-12">
<a href="#Url.Action("Video", "Service")">
<div class="owl-carousel owl-theme">
<div class="item outer">
<img src="#service.serviceThumbpath" alt="#service.serviceName" title="#service.serviceName" class="inner"/>
</div>
#foreach (var thumb in videoThumbpath)
{
<div class="item outer">
<img src="#thumb" alt="#serviceName" title="#serviceName" class="inner"/>
</div>
}
</div>
<h5> #service.serviceName</h5>
<div class="details">
#{
_serviceRate = float.Parse(service.serviceRate);
}
<p class="pull-left">#service.VideoCount </p>
</div>
</a>
</div>
and it worked maintaining 16:9 aspect ratio with owl carousel 2 :)
for the grid system of the page, I used This CodePen which was really helpful for my custom sizes.
Something like this should work. Adding an fixed width to container. Then with the img css it should keep its aspect ratio, as you aspect.
.item {
width: 100px;
}
img {
display: block;
width: 100%;
height: auto;
}
<p>This image is originally 400x400 pixels, but should get resized by the CSS:</p>
<div class="item">
<img width="400" height="400" src="#service.serviceThumbpath" alt="#service.serviceName" title="#service.serviceName" />
</div>
Just an example , you can use your own widths and heights. Could also add .item width only at certain #media query.
I set the height and width as below:
.owl-carousel {
height: 100%;
overflow: hidden;
}
.owl-carousel .owl-item img {
display:block;
height:100%;
width: auto;
}
jQuery:
owl.owlCarousel({
items:3,
autoWidth: true,
});

CSS Particles Js Overlap Issue

Here is the repository for my personal website: https://github.com/flakpanzer40/flakpanzer40.github.io
As you may notice, the particles I've used are simply showing below my name, image, and description. I've tried numerous times to overlap them so that the particles happen in the back, but to no avail. I've tried absolute positions, z-index, re-arranging the DIVs, etc. I'm terrible at CSS.
Can anyone help?
Please add following styles:
.container {
/*other css*/
position: relative;
z-index: 1;
}
.particles-js-canvas-el {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
z-index: 0;
}
Use the following Code:
HTML:
<div id="particles-js" style="">
<canvas class="particles-js-canvas-el" width="1903" height="952" style="width: 100%;height: 100%;position: absolute;top: 0;left: 0;"></canvas>
<div class="container">
<div class="row">
<div class="col-lg-12">
<img class="img-responsive" src="img/EvanRen.jpg" alt="Evan Ren" style="width:500px;height:600px;border:5px solid white;border-radius: 50%;">
<div class="intro-text">
<span class="name">University of Waterloo Computer Science</span>
<hr class="star-light">
<span class="skills">Problem_solving expert - Hardworking - Passionate</span>
</div>
</div>
</div>
</div>
</div>
CSS:
#particles-js {
width: 100%;
height: 100%;
background-color: #13717c;
position: relative;
top: 0;
}
It is important that the parent (#particles-js) has position: relative so that you can use absolute positioning for the children.

How to Center Masonry Container

I'm trying to center a masonry container on a page. At the moment, it's aligned to the left. I have margin auto in my CSS and isFitWidth: true in JS, but neither seems to be doing anything. I've also tried putting display:block in my CSS.
This is the HTML;
<div id="masonry_container" class="group">
<div class="masonry_item">
<a href="http://storyville.jonmarkoff.com/storyvillewp"target="_blank">
<img src="images/storyville_home.png" alt="Storyville Entertainment"/>
<h3>Storyville Entertainment</h3></a>
</div><!--masonry_item-->
<div class="masonry_item">
<a href="http://www.ducklingfarm.com"target="_blank">
<img src="images/udof_home.jpg" alt="Ugly Duckling Organic Farm"/>
<h3>Ugly Duckling Organic Farm</h3></a>
</div> <!--masonry_item-->
<div class="masonry_item">
<a href="http://www.underdonk.com"target="_blank">
<img src="images/underdonk_home.png" alt="underdonk"/>
<h3>Underdonk</h3></a>
</div> <!--masonry_item-->
<div class="masonry_item">
<a href="http://www.jaeeunlee.com" target="_blank">
<img src="images/jaeeunlee_home.png" alt="jaeeunlee"/>
<h3>www.jaeeunlee.com</h3></a>
</div> <!--masonry_item-->
<div class="masonry_item">
<img src="images/goindoor_hospitals.png" alt="goindoor"/>
<h3>Goindoor</h3>
</div> <!--masonry_item-->
<div class="masonry_item">
<img src="images/cakes_home.jpg" alt="wonderfully whimsical cakes"/>
<h3>Wonderfully Whimsical Cakes</h3>
</div> <!--masonry_item-->
</div><!--#masonry_container .group-->
CSS;
.group {
display: inline-block;
clear:both;
}
.group:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
#masonry_container {
margin:50px auto;
width:100%;
position:relative;
z-index:2001;
}
.masonry_item {
width:300px;
margin:0 0 20px 0px;
padding:20px;
}
.masonry_item:hover{
outline:1px solid white;
}
#masonry_container img {
width:100%;
}
and JS;
var container = document.querySelector('#masonry_container');
var msnry = new Masonry( container, {
// options
isFitWidth: true,
itemSelector: '.masonry_item'
});
I'd appreciate your help!
I was trying to figure this out for myself today and thought I'd share a possible solution.
As per Masonry's own options page "isFitWidth": true seems to be the key
http://masonry.desandro.com/options.html#isfitwidth
Here's their codepen example..
http://codepen.io/desandro/pen/nGLvx
Here's my simplified and bare bones method..
fiddle
https://jsfiddle.net/Hastig/xtw113wx/2/ - code play
https://jsfiddle.net/Hastig/xtw113wx/2/embedded/result/ - full screen
html
<div class="masonry-container js-masonry" data-masonry-options='{ "isFitWidth": true }'>
<div class="image-div">
<img class="image" src="" style="width: 200px; height: 100px;">
</div>
<!-- ..lots more divs in jsfiddle.. -->
</div>
css
.masonry-container {
margin: 0 auto; /* this is the css that keeps the container centered in page */
}
.image-div {
float: left;
width: 230px;
margin: 5px;
font-size: 0;
}
.image {
width: 230px;
height: auto;
}
Try this in the css:
.masonry_item {
width:300px;
margin:0 auto 20px auto;
padding:20px;
}
EDIT
I didn't read this correctly the first time. If you want to center the actual container you will need to set a fixed size for the container instead of 100%. Maybe 500px. Then remove the display: inline-block from the .group class. That should do it.
Set a fixed size for the container, such as width = 300px; height = 500px. Then, move the container with left: 50%; top: 50%. Finally, set the margin-left to -1/2 the value of the width, and margin-top to -1/2 the value of the height. This only works with absolute positioning.

Vertical aligning an absolute positioned div inside a containing div

I'm using the jQuery Cycle plugin to rotate images in a slideshow type fashion. That works fine. The problem I'm having is getting these images (of different sizes) to center in the containing div. The images are inside a slidshow div that has it's position set to absolute by the Cycle plugin.
I've tried setting line-height/vertical-align and whatnot but no dice. Here is the relevant HTML and CSS
HTML:
<div id="projects">
<div class="gallery">
<span class="span1">◄</span><span class="span2">►</span>
<div class="slideshow">
<img src="images/img1.png" />
<img src="images/img1.png" />
<img src="images/img1.png" />
</div>
</div>
</div>
CSS:
#main #home-column-2 #projects
{
width: 330px;
background: #fefff5;
height: 405px;
padding: 12px;
}
#main #home-column-2 #projects .gallery
{
width: 328px;
height: 363px;
position: relative;
background: url('images/bg-home-gallery.jpg');
}
#main #home-column-2 #projects .gallery img
{
position: relative;
z-index: 10;
}
And in case you want to see it, the jQuery:
$('#home-column-2 #projects .gallery .slideshow').cycle(
{
fx: 'scrollHorz',
timeout: 0,
next: "#home-column-2 #projects .gallery span.span2",
prev: "#home-column-2 #projects .gallery span.span1"
});
Any ideas on getting these images to center?
Try this:
http://www.brunildo.org/test/img_center.html
Vertical centering is a pain! Here's what the W3C page says about the vertical center:
CSS level 2 doesn't have a property
for centering things vertically. There
will probably be one in CSS level 3.
But even in CSS2 you can center blocks
vertically, by combining a few
properties. The trick is to specify
that the outer block is to be
formatted as a table cell, because the
contents of a table cell can be
centered vertically.
This method involves a little jquery, but works fantastic in most situations...
let me explain:
if all the images of the slideshow are contained within their own element div pos:absolute and those images are pos:relative, then on a $(window).load() you can run a .each() and find each img in the slideshow and adjust it's top positioning to be offset a certain number of pixels from the top..
jcycle automatically sets each parent div containing the image to pos:absolute on every onafter() so it's useless to apply this pos adjustment to them... instead target each img you have set to pos:relative...
Here is the example:
$(window).load(function() {
// move all slides to the middle of the slideshow stage
var slideshowHeight = 600; //this can dynamic or hard-coded
$('.slideImg').each(function(index) {
var thisHeight = $(this).innerHeight();
var vertAdj = ((slideshowHeight - thisHeight) / 2);
$(this).css('top', vertAdj);
});
});
and this is the html it's working on...
<div class="slideshow" style="position: relative; ">
<div style="position: absolute; top: 0px; left: 0px; display: none; width: 1000px; height: 600px; " id="img0">
<img class="slideImg" src="/images/picture-1.jpg" style="top: 0px; "><!-- the style=top:0 is a result of the jquery -->
</div>
<div style="position: absolute; top: 0px; left: 0px; display: none; width: 1000px; height: 600px; " id="img1">
<img class="slideImg" src="/images/picture-1.jpg" style="top: 89.5px; "><!-- the style=top:89.5px is a result of the jquery -->
</div>
<div style="position: absolute; top: 0px; left: 0px; display: none; width: 1000px; height: 600px; " id="img2">
<img class="slideImg" src="/images/picture-1.jpg" style="top: 13px; "><!-- the style=top:13px is a result of the jquery -->
</div>
</div>
just make sure
.slideImg {
position:relative;
}
I think that's everything... I have an example, but it's on a dev site.. so this link might not last.. but you can take a look at it here:
http://beta.gluemgmt.com/portfolio/rae-scarton-editorial.html
The positions are relative according to the style sheet, so did you try setting them to display: block and margin-top: auto; margin-bottom: auto; ?
Another option is to align them manually in javascript based on the containing div's height.
You need to nest two divs inside each cycle item. The first must have the display: inline-table; and the second must have display: table-cell; both these divs have vertical-align: middle.
So the structure would look something like this:
<div class="slide-container">
<div class="slide">
<div class="outer-container">
<div class="inner-container">
Centered content
</div>
</div>
</div>
<div class="slide">
<div class="outer-container">
<div class="inner-container">
Centered content
</div>
</div>
</div>
</div>
With the following css:
.slide-container {
height: 300px;
}
.outer-container {
height: 300px;
display: inline-table;
vertical-align: middle;
}
.inner-container{
vertical-align: middle;
display: table-cell;
}
You can see it working here http://jsfiddle.net/alsweeet/H9ZSf/6/

Resources