Adjust the dimensions of an SVG element using CSS within an li element's ::before pseudo-element

I'm working on a list where SVGs are loaded as list items. How can I adjust the height and width of the ::before pseudo-element to be 30px x 30px? Setting the height and width directly didn't work for me. Can someone assist with this?

<div class="list-div">
<ul class="items-list">
          <li>This is item A</li>
          <li>This is item B</li>
          <li>This is item C</li>
          <li>This is item D</li>
</ul>
</div>

and the CSS :

.list-div li {
    opacity:0;
    position: relative;
    display: block;
    margin-bottom: 5px;
    padding: 10px;
    text-align: left;
    visibility: hidden;
    text-transform: uppercase;
}
.list-div li:nth-child(1){
    background: url(some_image.svg);
    list-style-type: none;
    margin: 0;
    padding: 10px 10px 10px 48px;
    vertical-align: middle;
    background-repeat: no-repeat;
    background-position-y: 50%;
}
.list-div li:nth-child(2),
.list-div li:nth-child(3),
.list-div li:nth-child(4){
    background: url(some_image.svg);
    list-style-type: none;
    margin: 0;
    padding: 10px 10px 10px 48px;
    vertical-align: middle;
    background-repeat: no-repeat;
    background-position-y: 50%;

}

Answer №1

Check out this neat CSS snippet:

.list-div li {
  display: block;
  margin-bottom: 5px;
  padding: 10px;
  text-transform: uppercase;
}

.list-div li span {
  line-height: 30px;
  height: 30px;
  vertical-align: top;
}

.list-div li:before {
  content: '';
  width: 30px;
  height: 30px;
  display: inline-block;
  margin-right: 10px;
  background-size: cover;
}

.list-div li:nth-child(1):before {
  background-image: url('https://cdn4.iconfinder.com/data/icons/wirecons-free-vector-icons/32/menu-alt-512.png');
}
.list-div li:nth-child(2):before {
  background-image: url('https://cdn1.iconfinder.com/data/icons/picons-social/57/stackoverflow-512.png');
}
.list-div li:nth-child(3):before {
  background-image: url('https://cdn4.iconfinder.com/data/icons/pictype-free-vector-icons/16/home-512.png');
}
.list-div li:nth-child(4):before {
  background-image: url('https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-ios7-arrow-back-512.png');
}
<div class="list-div">
  <ul class="items-list">
    <li><span>This is item A</span></li>
    <li><span>This is item B</span></li>
    <li><span>This is item C</span></li>
    <li><span>This is item D</span></li>
  </ul>
</div>

This approach utilizes the :before selector in conjunction with list items to customize icons for each item. It's essential to include the content property within the :before selector, even if it's empty, for it to function properly. The icon size has been set to 30x30px and positioned centrally with respect to the text. By adding background-size: cover, you ensure that the icons will retain their dimensions when scaled.

To align the text alongside the icon, each text element is enclosed within a <span> tag. This allows independent styling of the text without affecting the list item itself or the :before selector. Adjusting the line-height and height properties of the text ensures proper alignment with the image, warranting adjustments if the image dimensions are altered.

A slight refinement was made to condense repetitive code segments. Instead of duplicating styles within individual :nth-child(x) selectors, a unified code block now governs consistent styling across all list items. This streamlined setup simplifies future changes to the background images while maintaining efficiency.

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

jQuery image resizing for elements

I have successfully adjusted the images in the gallery to display one per row for horizontal orientation and two per row for vertical orientation. Now, I am facing a challenge in making the images scalable so they can resize dynamically with the window. A ...

What steps do I take to eliminate this additional content?

Currently working on a web messenger project and facing an issue with a specific bar that I want to remove from my interface: https://i.sstatic.net/sp05i.png Within the Home.blade.php file, the code snippet looks like this: @extends('layouts.texter& ...

Maintain text while transitioning to another page

On my webpage (refer to image for details), users need to select options through a series of steps. The process involves clicking on a link in the top box, then entering a search term in the searchbox to display relevant links in div1. Clicking on a link ...

svg rect mistakenly appearing as a rounded polygon

I found a cool SVG pie chart on this codepen that I want to modify. The original chart is 50 x 50 and animated, but I want to make it 20 x 20. To resize the pie chart, I tried changing the width, height, radius, and center points in the code. I also adjus ...

IE7 is not applying the conditional CSS styles to content loaded through AJAX

I am currently tackling some challenges with IE7 compatibility in a Rails application that I am developing. It appears that CSS styles implemented from a stylesheet applied with a conditional comment are not being rendered properly when using HAML like thi ...

What are some effective ways to manage repetitive HTML elements like headers and footers in Angular 4?

Within my Angular web project, I find myself using a variety of different headers on multiple pages. Is there a way to streamline this process by coding the header and footer once and having them automatically included in one or more pages? I am looking ...

Is there a way to prevent navbar links from wrapping when closed with CSS?

Upon closing my side navbar, I encountered an issue where the links warp to the size of the navbar. I am seeking a solution to keep the links (highlighted in pink in the image below) the same size without warping. Is there a CSS technique to achieve this? ...

Select a Date: Input for Date Selection

Is it possible to restrict the selection of certain days using HTML date input validation? Some booking websites have a feature where an interactive calendar only allows users to select specific dates for events, while others are greyed out and cannot be c ...

The background-size:cover property fails to function properly on iPhone models 4 and 5

I have taken on the task of educating my younger sister about programming, and we collaborated on creating this project together. Nicki Minaj Website However, we encountered an issue where the background image does not fully cover the screen when using b ...

Interactive data visualization with hover-over details

I am utilizing datamaps to showcase the countries of the world, however, I want the graph to be centered. The issue arises when I hover over a country and the pop up appears all the way to the left, aligned with where the country would be if it wasn't ...

Execute a task prior to invoking a different task

I'm currently working on an image slider that has some interactive features. Here's how it functions: When a user clicks on an image, it slides and comes to the front. On the next click, the image is flipped and enlarged. Then, on another click ...

What is the reason behind IE's decision to reduce the size of password boxes compared to text

Take a look at the uncomplicated form right below. You'll notice that it consists of a text box positioned above a password box. In older versions of Internet Explorer like 7 and 8, as well as possibly in other browsers, you might observe that the tex ...

Can a CSS <div> element have margins applied to it?

Can a margin be defined for a text area? My WYSIWYG editor wraps my text within <div> tags instead of using line breaks <br /> So I was thinking of adding a margin to the <div> tag? Is this possible in CSS? If so, how can it be done? ...

Developing my React application with Material UI. The fonts I've implemented don't appear consistent on Mac operating systems

Why does the font change in Mac and iOS platforms, and how can I ensure consistency across all devices? Here is an excerpt from my index.css file: body { margin: 0; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto&apos ...

Creating a dynamic scene with three.js within an HTML div element

I've been searching for solutions but can't seem to figure it out for this particular scenario. https://codepen.io/rkimo/pen/vdwxJj Essentially, I just want to include this blob in a div so that I can add content before and after it and be able ...

Designs created using ASCII characters in HTML that are not under my control

Hey there! I've been attempting to incorporate some ASCII art onto a website that I don't have control over. When I input text into their editor, it is then enveloped in <p> tags which makes adding ASCII art quite challenging. Firstly, I ca ...

JQuery is blocking the submission of an HTML form

During my exploration of an AJAX/JQuery tutorial for a registration script that interacts with PHP/MySQL and is submitted via JQuery, I encountered a recurring issue. The problem lies in the form submitting directly to the action page instead of its intend ...

I keep encountering an issue when trying to load the website <a href="linktext.com">linktext.com</a> using SQL and PHP

I recently encountered an issue while trying to retrieve data from an SQL database on a webpage using PHP. The process was interrupted whenever there was a hyperlink present in the SQL text data, such as linktext.com. I attempted to use HTML special chara ...

Curved divs display outlines on more compact dimensions

Check out this relevant Codesandbox There seems to be a recurring issue in my application where rounded divs display edges when they are of smaller sizes. Refer to the image below for a visual representation. What could be causing this problem and how can ...

Is there a way for me to activate onMouseEnter on elements that are positioned behind other elements

I'm attempting to activate the mouseEnter event when the mouse is over multiple elements simultaneously. The goal is for both mouseEnter events to be triggered when the mouse reaches the center, and ideally for both elements to change to a yellow col ...