search
HomeWeb Front-endFront-end Q&AHow to achieve side-by-side images in javascript

In web design, pictures often need to be used and displayed side by side, such as in product displays or in news reports. How to achieve side-by-side pictures? The following will introduce how to use JavaScript to achieve side-by-side images.

  1. Use Flexbox layout

Flexbox is a new layout mode in CSS3 that can easily align and arrange elements. Using Flexbox layout, we can easily display images side by side.

First, you need to set up a container in CSS, set the display property of the container to flex, and then wrap the images that need to be displayed side by side in the container. Next, you can adjust the size and position of the image by setting the flex property of the element within the container.

As shown below:

HTML code:

<div>
  <img  src="/static/imghwm/default1.png" data-src="image1.jpg" class="lazy" alt="How to achieve side-by-side images in javascript" >
  <img  src="/static/imghwm/default1.png" data-src="image2.jpg" class="lazy" alt="How to achieve side-by-side images in javascript" >
  <img  src="/static/imghwm/default1.png" data-src="image3.jpg" class="lazy" alt="How to achieve side-by-side images in javascript" >
</div>

CSS code:

.container {
  display: flex;
  justify-content: space-between;
}
  1. Use CSS Float property

CSS Float property can float an element along the left or right side of its container, with other elements surrounding it. Use this attribute to display images side by side.

First, you need to set the float attribute of the images that need to be displayed side by side in CSS, and set it to left or right. Next, you can use the margin property to adjust the spacing of the images.

As shown below:

HTML code:

<div>
  <img  src="/static/imghwm/default1.png" data-src="image1.jpg" class="lazy" alt="How to achieve side-by-side images in javascript" >
  <img  src="/static/imghwm/default1.png" data-src="image2.jpg" class="lazy" alt="How to achieve side-by-side images in javascript" >
  <img  src="/static/imghwm/default1.png" data-src="image3.jpg" class="lazy" alt="How to achieve side-by-side images in javascript" >
</div>

CSS code:

.container img {
  margin: 10px;
}
  1. Use CSS Grid layout

CSS Grid layout is a table-like layout that can divide the page into rows and columns, and is more flexible than Flexbox layout. Using CSS Grid layout, you can easily display images side by side.

First, you need to set up a container in CSS, set the display property of the container to grid, and then set the column width of the image through the grid-template-columns property. Next, you can put the pictures that need to be displayed side by side into the grid and adjust the position and size of the pictures.

As shown below:

HTML code:

<div>
  <img  src="/static/imghwm/default1.png" data-src="image1.jpg" class="lazy" alt="How to achieve side-by-side images in javascript" >
  <img  src="/static/imghwm/default1.png" data-src="image2.jpg" class="lazy" alt="How to achieve side-by-side images in javascript" >
  <img  src="/static/imghwm/default1.png" data-src="image3.jpg" class="lazy" alt="How to achieve side-by-side images in javascript" >
</div>

CSS code:

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-gap: 10px;
}

.container img {
  width: 100%;
  height: auto;
}

The above code divides the container into three columns, each column has equal width, The spacing is 10px. The image width is set to 100% and the height is adaptive.

Summary

The above introduces three methods of using JavaScript to achieve side-by-side images: using Flexbox layout, CSS Float property and CSS Grid layout. Each of these methods has its own advantages and disadvantages, and can be selected and used according to specific needs. Either way, you can easily display images side by side to make your web page more beautiful and attractive.

The above is the detailed content of How to achieve side-by-side images in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Mastering CSS Selectors: Class vs. ID for Efficient StylingMastering CSS Selectors: Class vs. ID for Efficient StylingMay 16, 2025 am 12:19 AM

The use of class selectors and ID selectors depends on the specific use case: 1) Class selectors are suitable for multi-element, reusable styles, and 2) ID selectors are suitable for unique elements and specific styles. Class selectors are more flexible, ID selectors are faster to process but may affect code maintenance.

HTML5 Specification: Exploring the Key Goals and MotivationsHTML5 Specification: Exploring the Key Goals and MotivationsMay 16, 2025 am 12:19 AM

ThekeygoalsandmotivationsbehindHTML5weretoenhancesemanticstructure,improvemultimediasupport,andensurebetterperformanceandcompatibilityacrossdevices,drivenbytheneedtoaddressHTML4'slimitationsandmeetmodernwebdemands.1)HTML5aimedtoimprovesemanticstructu

CSS IDs and Classes : a simple guideCSS IDs and Classes : a simple guideMay 16, 2025 am 12:18 AM

IDsareuniqueandusedforsingleelements,whileclassesarereusableformultipleelements.1)UseIDsforuniqueelementslikeaspecificheader.2)Useclassesforconsistentstylingacrossmultipleelementslikebuttons.3)BecautiouswithspecificityasIDsoverrideclasses.4)Useclasse

HTML5 Goals: Understanding the Key Objectives of the SpecificationHTML5 Goals: Understanding the Key Objectives of the SpecificationMay 16, 2025 am 12:16 AM

HTML5aimstoenhancewebaccessibility,interactivity,andefficiency.1)Itsupportsmultimediawithoutplugins,simplifyinguserexperience.2)Semanticmarkupimprovesstructureandaccessibility.3)Enhancedformhandlingincreasesusability.4)Thecanvaselementenablesdynamicg

Is it difficult to use HTML5 to achieve its goals?Is it difficult to use HTML5 to achieve its goals?May 16, 2025 am 12:06 AM

HTML5isnotparticularlydifficulttousebutrequiresunderstandingitsfeatures.1)Semanticelementslike,,,andimprovestructure,readability,SEO,andaccessibility.2)Multimediasupportviaandelementsenhancesuserexperiencewithoutplugins.3)Theelementenablesdynamic2Dgr

CSS: Can I use multiple IDs in the same DOM?CSS: Can I use multiple IDs in the same DOM?May 14, 2025 am 12:20 AM

No,youshouldn'tusemultipleIDsinthesameDOM.1)IDsmustbeuniqueperHTMLspecification,andusingduplicatescancauseinconsistentbrowserbehavior.2)Useclassesforstylingmultipleelements,attributeselectorsfortargetingbyattributes,anddescendantselectorsforstructure

The Aims of HTML5: Creating a More Powerful and Accessible WebThe Aims of HTML5: Creating a More Powerful and Accessible WebMay 14, 2025 am 12:18 AM

HTML5aimstoenhancewebcapabilities,makingitmoredynamic,interactive,andaccessible.1)Itsupportsmultimediaelementslikeand,eliminatingtheneedforplugins.2)Semanticelementsimproveaccessibilityandcodereadability.3)Featureslikeenablepowerful,responsivewebappl

Significant Goals of HTML5: Enhancing Web Development and User ExperienceSignificant Goals of HTML5: Enhancing Web Development and User ExperienceMay 14, 2025 am 12:18 AM

HTML5aimstoenhancewebdevelopmentanduserexperiencethroughsemanticstructure,multimediaintegration,andperformanceimprovements.1)Semanticelementslike,,,andimprovereadabilityandaccessibility.2)andtagsallowseamlessmultimediaembeddingwithoutplugins.3)Featur

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.