HTML Grid Image Display

I am new to HTML and struggling to create a grid with images where the text appears over the image. I have tried multiple methods but nothing seems to work. I am making progress in my studies, but this time I need some help. Here is the code snippet:

<!DOCTYPE html>
<html>
<head>
<style>
div.img {
    margin: 5px;
    border: 1px solid #ccc;
    float: left;
    width: 180px;
    position:relative;
}

div.img:hover {
    border: px solid #777;
}

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

#NomeAnime {
    position:relative;
    width:98%;
    left:2px;
    top:-20%; 
    color: white; 
    font: bold 13px arial, sans-serif; 
    text-align: center; 
    height: 16px;
    overflow: hidden;
    background-color:green;
}
</style>
</head>
<body>

<div class="img">
    <img src="http://iv1.lisimg.com/image/7814282/600full-asa-butterfield.jpg" alt="Trolltunga Norway" width="300" height="200">
<div id="NomeAnime">teste</div></div>
</div>

<div class="img">
    <img src="http://iv1.lisimg.com/image/7814282/600full-asa-butterfield.jpg" alt="Forest" width="600" height="400">
<div id="NomeAnime">teste</div></div>

<div class="img">
    <img src="http://iv1.lisimg.com/image/7814282/600full-asa-butterfield.jpg" alt="Northern Lights" width="600" height="400">
<div id="NomeAnime">teste</div></div>
</div>

<div class="img">
    <img src="http://iv1.lisimg.com/image/7814282/600full-asa-butterfield.jpg" alt="Mountains" width="600" height="400">
<div id="NomeAnime">teste</div></div>
</div>

</body>
</html>

Answer №1

It is recommended to update the id="NomeAnime" to class="NomeAnime", as the id attribute should be unique for each HTML element;

Modify your CSS class declaration from #NomeAnime to .NomeAnime to align with the class selector;

Lastly, adjust the position:relative to 'position:absolute' and change top:-20% to top:20% or any value of your preference.

Answer №2

<!DOCTYPE html>
<html>
<head>
<style>
div.img {
    margin: 5px;
    border: 1px solid #ccc;
    float: left;
    width: 180px;
    height: 10px;
    position:relative;
}

div.img:hover {
    border: px solid #777;
}

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

#NomeAnime {
    position:relative;
    width:98%;
    left:2px;
    top:-20px; /* Vertical Position */
    color: white; /* Text Color */
    font: bold 13px arial, sans-serif; /* Font */
    text-align: center; /* Alignment */
    height: 16px;
    overflow: hidden;
    background-color:green;
}
</style>
</head>
<body>

<div class="img">
    <img src="http://iv1.lisimg.com/image/7814282/600full-asa-butterfield.jpg" alt="Trolltunga Norway" width="300" height="200">
    <div id="NomeAnime">teste</div>
</div>

<div class="img">
    <img src="http://iv1.lisimg.com/image/7814282/600full-asa-butterfield.jpg" alt="Forest" width="600" height="400">
    <div id="NomeAnime">teste</div>
</div>

<div class="img">
    <img src="http://iv1.lisimg.com/image/7814282/600full-asa-butterfield.jpg" alt="Northern Lights" width="600" height="400">
<div id="NomeAnime">teste</div>
</div>

<div class="img">
    <img src="http://iv1.lisimg.com/image/7814282/600full-asa-butterfield.jpg" alt="Mountains" width="600" height="400">
    <div id="NomeAnime">teste</div>
</div>

</body>
</html>

Answer №3

Check out this new feature I added to the text block - it rotates on hover!

div.img {
        margin: 5px;
        border: 1px solid #ccc;
        float: left;
        width: 180px;
        position: relative;
    }

    div.img:hover {
        border: px solid #777;
    }

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

    #NomeAnime {
        position: relative;
        width: 98%;
        left: 2px;
        top: -20%;
        /* Vertical Position */
        color: white;
        /* Text Color */
        font: bold 13px arial, sans-serif;
        /* Font */
        text-align: center;
        /* Alignment */
        height: 16px;
        overflow: hidden;
        background-color: green;
        -moz-transition: -moz-transform ease 0.6s;
        -webkit-transition: -webkit-transform ease 0.6s;
        -o-transition: -o-transform ease 0.6s;
        -ms-transition: -ms-transform ease 0.6s;
        transition: transform ease 0.6s;
    }

    div.img:hover #NomeAnime {
        color: #00FF22;
        -moz-transform: rotate(360deg);
        -webkit-transform: rotate(360deg);
        -o-transform: rotate(360deg);
        -ms-transform: rotate(360deg);
        transform: rotate(360deg);
    }

HTML

<div class="img">
    <img src="http://iv1.lisimg.com/image/7814282/600full-asa-butterfield.jpg" alt="Trolltunga Norway" width="300" height="200">
    <div id="NomeAnime">
        <span>teste</span>
    </div>
</div>

<div class="img">
    <img src="http://iv1.lisimg.com/image/7814282/600full-asa-butterfield.jpg" alt="Forest" width="600" height="400">
    <div id="NomeAnime">
        <span>teste</span>
    </div>
</div>

<div class="img">
    <img src="http://iv1.lisimg.com/image/7814282/600full-asa-butterfield.jpg" alt="Northern Lights" width="600" height="400">
    <div id="NomeAnime">
        <span>teste</span>
    </div>
</div>

<div class="img">
    <img src="http://iv1.lisimg.com/image/7814282/600full-asa-butterfield.jpg" alt="Mountains" width="600" height="400">
    <div id="NomeAnime">
        <span>teste</span>
    </div>
</div>

Fiddle

https://jsfiddle.net/Jaffar29sha/nc7a09ns/1/

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

What are the characteristics of a well-crafted HTML inline CSS form?

I am looking to create bubbles on a webpage to display content. I decided to create a CSS class called .bubble and added positioning values as inline styles. However, this resulted in long lines of code. My experience with various programming languages has ...

Another option instead of using componentWillMount to set up axios interceptors in React

Here's my current dilemma. The challenge I'm facing involves registering interceptors on the request and response of my axios instance to handle errors. To achieve this, I've created a wrapper component called withErrorHandler that manages ...

Issues with Monospace Google Fonts on JSFiddle are preventing them from displaying correctly

It's strange how monospace fonts from Google Fonts don't display properly on JSFiddle. Only sans-serif and serif fonts seem to be functioning correctly. ...

Does the react-google-login library utilize the services provided by Google Identity?

Currently incorporating the react-google-login library (https://www.npmjs.com/package/react-google-login/v/5.2.2) in my JavaScript codebase to grant users access to my website. Can anyone confirm whether this library utilizes "Google Identity Services" or ...

The click interactions of SVG elements are altered when switching between <img> and <object> elements

I currently have 4 SVG buttons featured in the main menu of my personal website. https://i.stack.imgur.com/gA7En.png /----------------------------------------------------------------------------------------------------------------------------------/ Upo ...

An assortment of the most similar values from a pair of arrays

I am seeking an algorithm optimization for solving a specific problem that may be challenging to explain. My focus is not on speed or performance, but rather on simplicity and readability of the code. I wonder if someone has a more elegant solution than mi ...

Converting nested JSON to CSV for simplified data organization

I need help flattening JSON in order to parse it as a CSV. The current flattening process is not working correctly, as the customer.addresses field is being filled with 'addresstype: r' and then skipping all other fields such as city, countrycode ...

What is causing the redirect to continue even after calling preventDefault() during form submission?

I've been struggling to figure out why my form submission using jQuery keeps redirecting after I submit it. Can anyone help me find the mistake in my code? Here is a simplified version of my form: <form action="http://xxxxxxx" method="post" id="m ...

When a child SPAN surpasses the height of the parent TD/TABLE, trim it down

I am attempting to make my outer table/td clip the inner SPAN when the content of the span exceeds the height of the outer table, all while maintaining vertical and center alignment. Instead of the current layout: https://i.sstatic.net/s33q7.png I want ...

Incorporating a setup file into my JavaScript project

In my JavaScript project, I have both frontend and backend codes (NodeJS). Here is the folder structure for my production environment: /prod /server sourceCode1.js sourceCode2.js ... sourceCodeN.js index.js ...

How can I add styling to materializecss chip elements?

When working with tags on a web page, Materializecss makes it easy. However, I have a requirement where the tags need to be of different colors based on their types. Although this is feasible, another complication arises when supporting Materializecss keyb ...

Troubleshooting a 400 error with Express and React: A step-by-step guide

When I check the browser console, I see this error message: `xhr.js:251 POST http://localhost:325/budget 400 (Bad Request) BudgetNew.js:30 catch AxiosError {message: 'Request failed with status code 400', name: 'AxiosError', code: ...

Tips for creating a unique controlled checkbox selection with shared state for each component in React

I have multiple components on the left side, each with a switch. When I click on a component's switch, a list of checkboxes opens on the right side based on the component ID. The data for these checkboxes is fetched from the backend. For example, if C ...

I am looking to implement a straightforward drag-and-drop feature using jQuery

Is it possible to drag and select 4 buttons in a browser, similar to how you would do on Windows, using jQuery locally? ...

Utilize the appendTo() function and make a switch to dynamically insert content stored in variables into a specified

I am working on developing a page with a central content div, where users have the ability to choose various options that will introduce different additional content. This added content will then present more opportunities for adding new elements, ultimate ...

error in css styling detected

Why was the CSS style "background-position: center;" missed? It was because the background will override the background-position property. <html> <head> <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" ...

How can one selectively apply a class to the initial DIV within a collection of dynamically generated DIV elements without relying on jQuery?

I am currently working on implementing a slideshow within a modal using AngularJS and Bootstrap. My challenge lies in dynamically creating DIVs, but specifically adding the active class to only the first DIV. Is there a way to achieve this without relying ...

The <a> tag is failing to cover the appropriate section of the <div> element

I am facing the following issue: HTML: <div id="about" class="menu1"> <a href="#">About</a></div> <div id="aboutsub"> <div id="team" class="menu2"> <a href="">Team</a></div> &l ...

Tips for showcasing JavaScript alert rather than a yellow-page error

I need to limit the file size that users can upload on a specific page. To achieve this, I have configured it in web.config as follows: <location path="SubSection/TestPage"> <system.web> <httpRuntime maxRequestLength="2048" /> ...

What are some ways I can customize the appearance of a canvas element?

After discovering a canvas animation, I decided to customize it for my own needs. However, due to the fact that a canvas and its shapes are not DOM elements, I am struggling to style it properly. You can view my current progress here. When you hover over ...