Is it possible to include hyperlinks with images in a flexbox layout?

When images have no links, everything works smoothly:

<div id="gallery">
     <img src="1.jpg"/>
     <img src="2.jpg"/>
     <img src="3.jpg"/>
     <img src="4.jpg"/>
</div>
 #gallery {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    padding: 0 4px;
  }
  
  #gallery img {
    width: 25%;
    height: 300px;
    object-fit: cover;
    margin-top: 8px;
    padding: 0 4px;
    border-radius: 10px;
  }

However, things get messy when the images are linked:

<div id="gallery">
     <a href="1.jpg"><img src="1.jpg"/></a>
     <a href="2.jpg"><img src="2.jpg"/></a>
     <a href="3.jpg"><img src="3.jpg"/></a>
     <a href="4.jpg"><img src="4.jpg"/></a>
</div>

Adding the following helps slightly:

#gallery a {
    width: 100%;
}

But the layout is then just a vertical stack of images.
Does flexbox not support using images as links?

Answer №1

Make sure to transfer the styling from img to a as flexbox only affects direct child elements

#gallery {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  padding: 0 4px;
}

#gallery a {
  width: 25%;
  height: 300px;
  margin-top: 8px;
  padding: 0 4px;
  box-sizing: border-box;
}

#gallery img {
  border-radius: 10px;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
<div id="gallery">
  <a href="1.jpg"><img src="https://picsum.photos/200/300?random=1" /></a>
  <a href="2.jpg"><img src="https://picsum.photos/200/300?random=2" /></a>
  <a href="3.jpg"><img src="https://picsum.photos/200/300?random=3" /></a>
  <a href="4.jpg"><img src="https://picsum.photos/200/300?random=4" /></a>
</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

Is there a way to automatically close the Foundation topbar menu when a link is selected?

On my single page website, I am utilizing Zurb Foundation's fixed topbar which includes anchor links to different sections of the page. My goal is to have the mobile menu close automatically whenever a link inside it is clicked. As it stands now, whe ...

Utilize Google Maps to receive directions to a specific destination and discover current traffic conditions along the route using their comprehensive API

I am currently working on implementing the Google Maps direction identifier functionality with traffic details. However, I am able to obtain directions but not specific traffic details for a selected path; it seems to apply overall traffic data instead. ...

Instructions on how to insert the meta tag with the attribute "http-equiv" set to "REFRESH" and the content "0; URL="somedomain"" into a division on a

Trying to send an ajax request to a page that includes the following meta tag: <meta http-equiv="REFRESH" content="0; URL=https://www.ohterDomain.com/help?nodeId=2&view=content-only"> After making a successful ajax call, the correct content is ...

Can you explain the functionality of icon maps (or charts)?

As I was exploring the source code of various websites, I noticed a common pattern - all the icons were stored in one image map. Upon further investigation, I discovered that each icon is referenced individually when needed. I came across a helpful article ...

Achieving consistent text alignment in HTML and CSS without relying on tables

As I delve into the world of HTML and CSS, I've taken a traditional approach by crafting a login page complete with three input controls (two text inputs and one button). To ensure proper alignment of these elements, I initially turned to the trusty & ...

Tips on retrieving values from input files using jQuery

Is there a way to retrieve the value of an input type using jQuery? Specifically, when I add a file to the input file, I want to dynamically add more input types with the file's value. Currently, I am using the following code: $('#show_input&apo ...

Modify the button's background color for Django's image upload field

In one of the forms in my Django application, I have included a field that allows users to upload an image. Here is the code for this field: image = models.ImageField(upload_to=upload_location, null=True, blank=True) To display this field in my form tabl ...

"Embedding PHP code within HTML tags, which is then embedded within

Running into an issue within a while loop... echo 'var contentString = '<div id="content" > <div id="bodyContent"> <p>' + $row[name]+ '</p> ...

Using Selenium and Python to scrape text from a continuously refreshing webpage after each scroll

Currently, I am utilizing Selenium/python to automatically scroll down a social media platform and extract posts. At the moment, I am gathering all the text in one go after scrolling a set number of times (see code below), but my objective is to only gathe ...

Understanding XML parsing problems

I've decided to keep the live xml/atom-feed URL private, as it's hosted on LiveJournal. However, I'm a bit confused about how this all works: let XML_URL = "https://users.livejournal.com/<username>/data/atom"; $(function(){ ...

What do I do when I encounter the error message "Provider not found for RadioControlRegistry" in Angular 2? Which provider should

Which provider should be included in the provider component? <div class="radio"> <input let match id="male" type="radio" name="gender" value="true" [(ngModel)]="isMatching" (click)="isMatching(match.value)" > Take a look at my console output ...

CSS - How to specify a class within an id using syntax

How can I target a specific tag within an ID using the class name in selector syntax? For example, how can I make the inner "li" turn red in the code snippet below? <html> <head> <style type="text/css"> #navigation li ...

Injecting services differently in specific scenarios in AngularJS

I have a unique angular service called $superService that I utilize across many of my directives and controllers. However, there is one specific directive where I want to implement the following behavior: If another directive utilizes $superService in its ...

Switch between two fields using just one animation

I've been experimenting with creating an information tag that slides out from behind a circular picture. To achieve this effect, I created a block and circle to serve as the information field placed behind the image. However, I'm facing a challe ...

Blocking an image from appearing over an a:hover element in CSS (specifically a drop-down menu)

Currently, I have a dropdown menu (side menu) where #a represents the links and #b is the container that #a opens when hovered. #b {display:none;} #a:hover > #b {display:block;} #b:hover {display:block;} The CSS above allows the corresponding containe ...

Issue with Bootstrap navbar not highlighting section items when scrolled to

I have recently developed a webpage using html5, css3, and bootstrap. However, I am facing an issue with the navbar menu not highlighting the items that are pointing to specific sections on the page. I would like the navbar menu to automatically highlight ...

Using Ajax and jQuery to dynamically insert an option into a datalist

I'm in the process of building a search box using Flask, MySQL, and ajax. I've managed to retrieve the search results in JSON format within the console, but now I want to dynamically add them to the options in my datalist element in the HTML. He ...

Dropdown Placement Based on Button Click

Looking to create an interactive dropdown menu with the Alloy UI Dropdown Component that appears when a user clicks on one of four buttons. The goal is for this dropdown to be positioned to the left of the clicked button. var toolsDropdown = new Y.Dropdow ...

Tips for updating the current user's username value in local storage in Angular

https://i.stack.imgur.com/ZA32X.png https://i.stack.imgur.com/iDHZW.png I am facing an issue with updating the local storage of a current User for only username. When I update the username, it's not reflecting in local storage. Even though I can s ...

How to locate a div element using CSS selector in Selenium WebDriver with Ruby

My experience with CSS selectors in selenium webdriver has been positive, but I am facing difficulty in locating the element within the following div. <div class="class1 class2" dd:btnfloatingstyle="top-right" dd:entitytexttype="resultval" id="_78891a3 ...