Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Alternative To This CSS-jQuery Code?

Tags:

html

jquery

css

I have two columns side by side that are different colors. The background has a unique color as well. The right column contains text that will expand to an unknown height. The other column contains little to nothing.

<div id="container">
    <div id="leftColumn">
        <p>Only one small paragraph here</p>
    </div>

    <div id="rightColumn">
        <p>Many large paragraphs inside here.</p>
    </div>
</div>

I would like the left column to be the exact height as the right column.

Here's the CSS...

#leftColumn {
    display:inline-block;
    width: 200px;
}
#rightColumn {
    display:inline-block;
    width: 600px;
    vertical-align: top;
}

So when the page loads I use jQuery to set the height of the left column based on the height of the right column.

$(document).ready(function() {
    $('#leftColumn').css('height', $('#rightColumn').innerHeight());
});

Is there a way to do this with only CSS?

like image 384
user182666 Avatar asked Jan 31 '10 03:01

user182666


People also ask

Can we change CSS using jQuery?

You can change CSS using the jQuery css() method which is used for the purpose of getting or setting style properties of an element. Using this method you can apply multiple styles to an HTML all at once by manipulating CSS style properties.

What is the use of CSS () method in jQuery?

jQuery css() Method The css() method sets or returns one or more style properties for the selected elements. When used to return properties: This method returns the specified CSS property value of the FIRST matched element.

What is alternative to jQuery?

A progressive JavaScript framework, Vue. js is considered a good alternative to jQuery. It is an open-source, MVVM, front-end JS framework that is considered ideal to create user interfaces and single-page apps. It is also considered good for web interfaces, desktop, and mobile app development.

Is jQuery better than CSS?

What's the difference? In a nutshell, CSS uses your graphic card to process the transition, where as using jQuery uses your main processor. However jQuery is generally more backwards compatible than CSS. That said, CSS transitions will revert to jQuery if the browser doesn't support CSS transitions.


1 Answers

There are a few other ways to achieve this layout besides using Javascript.

Methods include:

  • Using display:table on the elements
  • Faux columns (background image on the parent element)
  • Adding multiple div containers for each background
  • Use a table (not very popular for obvious reasons)

All of these have different advantages, drawbacks and each introduces thier own headaches to the development of the site. I'd vote for using faux columns because it keeps the html the simplest and is compatible with all browsers.

Additional reading:

  • http://buildinternet.com/2009/07/four-methods-to-create-equal-height-columns/
  • http://www.alistapart.com/articles/fauxcolumns/
  • http://www.code-sucks.com/css%20layouts/faux-css-layouts/
  • http://www.addedbytes.com/articles/faux-columns-for-liquid-layouts/
like image 73
Lance McNearney Avatar answered Oct 06 '22 00:10

Lance McNearney