Ensuring Consistent Vertical Alignment Across Any Imagery

Trying to maintain vertical rhythm on a page with images of unknown heights is proving to be a challenge. I came across a solution on Stack Overflow, however, since I am using vanilla javascript instead of jquery, utilizing a jquery plugin is not an option for me. The alternative answer involves setting the baseline in JavaScript, but I have already set it in my SCSS files and do not want to duplicate the hard-coded values all over again.

Another potential solution from another Stack Overflow thread involves additional markup that I find cumbersome due to style concerns. It feels reminiscent of the days when nested <div> tags were necessary just to achieve rounded corners.

An idea emerged to use generated content to pass JSON to JavaScript. However, upon further research, I discovered an example employing the same method, making me question if there could be a better approach.

Is there a more efficient way to accomplish this task? My ultimate goal is to specify that the height of an element must be a multiple of a given value. Some sources suggest that CSS alone cannot achieve this, although I believe calc might come close - yet, I lack the expertise to implement it effectively. Is there a superior method to transmit the required value to JavaScript?

Any advice or guidance would be greatly appreciated.

Answer №1

After some consideration, I realized that utilizing CSS variables and then retrieving those values from JavaScript is a more efficient approach as detailed here.

This method offers several benefits:

  1. It leverages the cascade to allow different sections of the page to each have their unique vertical rhythm.
  2. It eliminates the need to parse the content of the ::after pseudo-element as JSON. Some browsers tend to handle escape embedding quotes in the content attribute inconsistently.

However, one drawback is that custom attributes are not as universally supported as pseudo elements. This may result in some browsers breaking. Since this issue pertains only to aesthetics, I consider it an acceptable compromise.

const foo = document.getElementsByClassName('foo')[0];
const bar = document.getElementsByClassName('bar')[0];
const fooStyles = window.getComputedStyle(foo);
const barStyles = window.getComputedStyle(bar);
const fooVR = fooStyles.getPropertyValue('--vertical-rhythm');
const barVR = barStyles.getPropertyValue('--vertical-rhythm');

foo.innerHTML = '--vertical-rhythm: ' + fooVR;
bar.innerHTML = '--vertical-rhythm: ' + barVR;
:root {
  line-height: 1.5rem;
  /* $line-height */
  --vertical-rhythm: 3rem;
  /* 2 * $line-height */
}

.foo {
  --vertical-rhythm: 6rem;
  /* 4 * $line-height */
}
<div class="foo"></div>
<div class="bar"></div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide on transmitting two multidimensional arrays using json_encode in PHP

When working with Laravel PHP, I am trying to retrieve arrays for my ajax function using json_encode. The issue I am facing is that I need to send two multidimensional arrays from my controller in order to perform nested loops inside my ajax function using ...

Manipulating the InnerHTML content of a total of 144 individual cells

I am currently developing a tile-based game using tables. Each td cell contains the following code: <td> <div id="image" style="display:none; display: fixed; right: 5; top:2;"><img src="http://thumb7.shutterstock.com/display_pic_with_logo ...

Enhance Your Profile with Bootstrap 4 Dropdowns

I am trying to incorporate a dropdown profile into my navbar, but it is not appearing where it should be located: pic1 <link href="https://bootswatch.com/4/materia/bootstrap.min.css" rel="stylesheet"/> <script src="https://code.jquery.com/j ...

Unable to create a pie chart with chartjs using JSON data

I am having trouble creating a pie chart using canvas.JS. I have already tried iterating through them in a loop. The JSON data returned looks like this: [[{"label":"belum","x":1},{"label":"sudah","x":5}]] Below is the code I am using to retrieve the JS ...

Utilizing Props to Manage State within Child Components

Is it possible to assign the props received from a Parent Component as the state of a Component? export default class SomeComp extends Component { constructor(props) { super(props); this.state = someProps; // <-- I want to set the ...

Tips for managing back and forth navigation in react native webview component using custom components on iOS

I'm currently faced with a task that involves creating a webview component with custom back and forward buttons in React Native for IOS. My current application includes a web-view component which only displays a basic browser without any built-in con ...

form for submitting multiple data via Ajax

I am working with two forms (request and feedback) where I need to use jQuery Ajax to send the data. When a user submits a request, the subject line will display "Request". If they submit feedback, the subject line will display "Feedback". Here is my cur ...

Displaying data from a JSON file retrieved through AJAX within AMP

I am currently in the process of integrating an AMP-Version of a website that connects to a third-party REST endpoint. The endpoint returns JSON data, and I need to display a portion of this data on the website. I have already attempted using amp-list, bu ...

Leveraging Axios for Retrieving XML Information

I am new to creating REST API requests and recently learned about using Axios in my React application. I have a form where users input their first name, last name, department, and start date. axios.get('MyAPILocationHere/users', { params: { ...

What is the procedure for incorporating JavaScript into my tic-tac-toe game's HTML and CSS?

I have created a simple Tic Tac Toe game using HTML and CSS. Although there is no embedded Javascript, I do reference a Javascript file for functionality. Can anyone provide me with the most straightforward Javascript code that will display an 'X&apos ...

The content selected in a bootstrap multiselect dropdown may break across multiple lines

I am currently using a bootstrap multiselect dropdown to display all the selected values in the dropdown. However, I now have a large number of values and would like to break them into multiple lines. This is how it currently looks like displayed in the im ...

Dynamic Search Feature Using AJAX on Key Press

Currently, I have developed an AJAX search function that retrieves keyword values upon key up and triggers the script. The objective is to update the content area with results in alphabetical order as the user types each key. However, the issue I am facin ...

"Obtaining JSON data with jQuery results in an empty response

I've been attempting to make a simple jQuery get JSON call work, but neither the success handler nor the error handler seem to be triggered. Additionally, Firebug indicates that the data body is empty. The server I'm working with is a basic piec ...

mention a particular anchor by clicking on it to refresh the webpage

Can a webpage automatically navigate to a specific anchor/tag when reloaded in the browser? Thanks in advance I came across this method: window.location.reload(); However, I'm searching for a solution that can detect the reload and then run a basi ...

The Material UI Select Component has the ability to transform a controlled text input into an uncontrolled one

I encountered a warning in the console while trying to update the value of a Select input. The warning message is as follows: index.js:1446 Warning: A component is changing a controlled input of type text to be uncontrolled. Input elements should not swi ...

Expand Elements to occupy entire width

My CSS skills are still in the early stages. I haven't delved too deeply into it. I have a query regarding how to achieve full width for an element. I've included my code below, but the basic approach I tried using 'width: 100%' didn&a ...

How to use React hooks to flip an array

Is it possible to efficiently swap two items in an array using JavaScript? If we are dealing with a boolean, one could achieve this by: const [isTrue, setIsTrue] = useState(false); setIsTrue(!isTrue); However, what if we have an array? // Let's ...

Insightful examination of listview functionality on Android devices

The listView I have displays property names and functions correctly. However, I am encountering issues with showing the detailed view of each announcement. When clicking on items within the listView, I am experiencing an empty activity_details, and the deb ...

The issue of jquery fade out and redirection not functioning properly in Internet Explorer is causing

I recently developed a website where I included a specific script to manage all links: $(document).ready(function(){ $("body").css("display","none"); $("body").fadeIn(2000); $("a").click(function(event){event.preventDefault(); linkLocation=this.href; if(! ...

Unable to submit a fetch request

I've been searching for quite some time without finding any answers to this particular issue. The problem I'm facing is that when I attempt to send an asynchronous request to another web page, I'm not receiving any response. I suspect that t ...