css - Arranging pictures in a perfect row

On my webpage, I have 5 images and a div. Currently, the images are displaying one under another, but I want them to appear in a straight line while only showing one image at a time within the div.

HTML CODE:

<html>

<head>
<title>My City Karachi</title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<link rel="stylesheet" href="css/slider.css" type="text/css" />
</head>

<body style="margin: 0; padding: 0;">

<div class="slider">
    <img src="asd.jpg"/>
    <img src="baba dedo.jpg"/>
    <img src="picccccture 3.jpg"/>
    <img src="Page-3.jpg"/>
    <img src="Page 4 copy.jpg" />
</div>
</body>

</html>

CSS CODE:

@charset "utf-8";

html, body {
    margin: 0;
    padding: 0;

}

.slider {
    margin: 0 auto;
    overflow: hidden;
}

.slider img {
    width: 900px;
    height: 650px;  
    float:left;
}

I hope that explanation was clear enough!

Your assistance would be greatly appreciated! :)

Answer №1

add

.slider {
 display: block;
}

next, include display:inline-block; in the .slider img properties

eliminate the float: left;

give it a try. This solution has worked wonders for me.

Answer №2

Experiment with:

.slider img {
    width: 900px;
    height: 650px;
    display: inline-block:
}

Answer №3

To see a demonstration in action, visit this link

Create a container with a fixed width and set it to have an overflow: auto property. Inside this container, place a child element that is as wide as the combined width of five images. This setup will generate a horizontal scrollbar.

By using a script, you can implement a slideshow functionality by controlling which image is displayed and which ones are hidden. Adjust the width of the container to 350px instead of 400px - the width of a single image. Additionally, consider adding navigation controls such as Next, Pause/Play, and potentially Previous buttons. It's recommended not to make the slideshow auto-start. For a helpful guide, check out this useful tutorial

Answer №4

If you wish to display your images in a horizontal line,

Ensure the following:

The width of the parent container of the images must be greater than or equal to the sum of the widths of individual images.

Only then will they appear in a horizontal line; otherwise, they will not.

In this specific scenario, it is not possible.

Now, since you only want to showcase one image at a time, set all images as display:none and then select one at a time (in a specified order) and switch its display to block.

As achieving this with just CSS is not feasible, JavaScript (jQuery) will be required.

Refer to this straightforward example that demonstrates what you are looking for.

All I am doing here is:

  • Hiding all images within div.slider

     $('.slider img').each(function(i,v){
        $(this).attr("data-image-index",i).hide();
     });
    

    Additionally, associating a new attribute with your images to denote the index:

    <img src="asd.jpg" alt="img1" data-image-index="0"  />
    <img src="baba dedo.jpg" alt="img2" data-image-index="1" />
     ..
     ..
    
    • Creating a function that hides all images within div.slider and displays the image with the data-image-index matching the specified index:

         var index=0;
         function play(){
             $('.slider img').each(function(){
                    var s = $(this).data("image-index");
                    if(parseInt(s)===index)
                      $(this).show();
                    else
                      $(this).hide();
              });
              index++;
              if(index>5)
                 index=0;
         }
      

    Since we need to trigger this function upon button click or at intervals, I have configured it to execute every 1 second:

        setInterval("play()",1000);
    

Answer №5

Construct a ul or ol element and include the child elements, like images, as li items within the same list. Use CSS to remove default list styling with list-style:none and apply margin, padding, or positioning as necessary.

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

Can a single volume control be used to manage the audio of multiple sources at once?

I am currently working on a project for my personal interactive video website. I am trying to figure out how to create one volume control that can adjust the audio for multiple tracks at once. Can anyone help me with this? So far, I have successfully crea ...

The DIV element with a line break tag displaying text on a new line

In my project, I am working with an XMLHTTPResponse object that contains nodes. My goal is to print the elements of one specific node (serps) in a div element, but in a formatted manner. The structure of the node is as follows: Currently, I need to create ...

How to arrange table data in Angular based on th values?

I need to organize data in a table using <th> tags for alignment purposes. Currently, I am utilizing the ng-zorro table, but standard HTML tags can also be used. The data obtained from the server (via C# web API) is structured like this: [ { ...

Having trouble making changes to FontAwesome CSS using jQuery?

I am currently working on an MVC project where I am using Ajax to update a specific section of a View that contains a FontAwesome 5 icon. The FontAwesome icons are set using CSS classes, so my initial assumption was that it would be simple to remove and ad ...

Is feature recognition possible in a jQuery plugin?

Can I integrate a tool similar to Modernizr into my plugin? I want to be able to test for specific CSS3 properties without starting from scratch. ...

Problem with using Twitter Bootstrap in IE11 when Browser Profile Enterprise is enabled

I am facing an issue with a Bootstrap 3 web page. Some machines have IE configured to load intranet pages in Enterprise profile mode, while others have the default Desktop profile set in IE11. This configuration has caused the layout to break completely. ...

Ascending row placement

I'm currently working on a JavaScript timeline project, where clicking on an item in the left column reveals details in the right column. While there may be simpler methods to achieve this, the HTML code below appears to be quite elegant. Typically, ...

Create a cohesive user experience by implementing the same CSS animation when hovering over two distinct elements

For a learning exercise, I attempted to create an animation. My goal was to have the circle trigger an animation when hovered over, while also revealing the text in the .hide element. However, as soon as I hover over the circle and then move my mouse onto ...

Mouse cursor does not display as a pointer when utilizing an SVG image on Internet Explorer

Consider this html: <a href="http://www.wikipedia.com"> <object data="http://upload.wikimedia.org/wikipedia/en/4/4a/Commons-logo.svg" type="image/svg+xml"> <img src="http://herdeirodocaos.files.wordpress.com/2007/11/wikipedia-lo ...

Refrain from using inline JavaScript code within your

Here is the basic structure of my code: <a href="javascript:void(0);" onClick="viewServices(1)">Services</a> <a href="javascript:void(0);" onClick="viewServices(43)">Services</a> <a href="javascript:void(0);" onClick="viewServic ...

Determine the maximum width of each column in a table using JavaScript or jQuery

I have been attempting to iterate through each column in my table, retrieve the width of the largest cell in that column, and store that width value in an array. Here is how my table appears: table image Therefore, the largest cell in the first column la ...

JavaScript in a copied style

When using JavaScript to copy a table cell, I encountered an issue where the style was not being copied along with the content. Attempting to directly assign the style from one cell to another did not work as expected. To ensure that the text-align proper ...

avoid triggering the on hover state

When working with jQuery, I have a CSS style targeting a specific div: div.right-sm:hover{background-color: blue} Now, I am trying to prevent the hover effect using jQuery: $(this).parent('div').removeClass('right-sm:hover'); Howev ...

Separate your buttons with style using the Bootstrap button groups with

Is there a way to add border line separators between buttons in Bootstrap button groups? <div class="btn-group" role="group" aria-label="Button group with nested dropdown"> <button type="button" class=&quo ...

Is it considered fashionable to utilize HTML5 data attributes in conjunction with JavaScript?

Currently, I am utilizing HTML5 data attributes to save information such as the targeted DOM element and to initialize events using jQuery's delegation method. An example of this would be: <a href="#" data-target="#target" data-action="/update"> ...

Obtain the href using a combination of xpath and id

I need to extract the href links for 9 search results from this specific website. The xpath and selectors for the first, second, and third items are as follows: '//*[@id="search-results"]/div[4]/div/ctl:cache/div[3]/div[1]/div/div[2]/div[2]/div[2]/p[ ...

Issue with launching Android application from website

I am looking to add a feature to my Android app that will open the app when a specific link (for example, www.example.org) is visited in the web browser. I have been experimenting with intent filters, but haven't had much success so far. Although I h ...

Is there a way to modify the background color of ::before when hovering over it?

I have a ul-li list and when i hover last li ::before element looks white and not so good. I want to change it when i hover the last li element. How can I achieve this? Here are my codes: <div className="dropup-content"> <ul> & ...

Swapping link positions with jQuery

Is it possible to rearrange links using jQuery? Under the navigation menu, there will be content div for each tab. The selected link should always be positioned next to the first "sixrevision" link. The code snippet is shown below: <ul id="crumbs" ...

The jQuery dynamic fields vanish mysteriously after being validated

I am facing an issue with my jQuery and validation. Below is the code snippet causing the problem: var fielddd = 1; $(document).on('click', '.btn.add-field', function() { fielddd++; $('#newfield').append('< ...