Can you code up the CSS and HTML for this design concept?

I have a design mockup that I would like to implement using CSS and HTML.

I've started a JSFiddle for it, which currently consists of a table that I need help styling.

<table border="1">
    <tr>
        <td>grey</td>
        <td>grey</td>
        <td>grey</td>
        <td>grey</td>
        <td>grey</td>
    </tr>
    <tr>
        <td>COL1</td>
        <td>COL2</td>
        <td>COL3</td>
        <td>COL4</td>
        <td>icon1|icon2|icon3|icon4</td>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
</table>

How can I alternate the background color between grey and white for every second row? Should I use a server-side variable to calculate a mod 2 or is there an easier way to achieve this using CSS?

Answer №1

You have the option to apply styling based on even or odd elements using CSS:

tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}

You can also target specific columns with different background colors:

col:first-child {background: #FF0}
col:nth-child(2n+3) {background: #CCC}

For a live demonstration, check out this helpful link: http://www.w3.org/Style/Examples/007/evenodd.en.html

Please note that this technique utilizes CSS3 features, so it may not be compatible with all web browsers. For better cross-browser support, consider creating a separate class like .even {} and dynamically applying it using PHP or JavaScript.

Answer №2

Take a look at this resource to see if it can assist you: JSfiddle

table{ width:100%; text-align:center;border:1px solid #00F; font-size:12px;}
th{background:#EEE;width:auto; text-align:center; padding:5px 0;border:1px solid #00F;}
td{width:auto; text-align:center; padding:5px 0;border:1px solid #00F;}
tr:nth-child(even) {background: #EEE;}

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

Dealing with problem of highlighting links in Android WebView

I am currently using an html app that is being loaded in a WebView. My goal is to prevent a specific link from loading when clicked by using shouldOverrideUrlLoading, where I return true to stop the URL from loading. While this method works well, the link ...

Contrast the values across various template sections

Due to specific requirements, I need to apply a bit of styling (margin) to the footer of the page. This styling should only be visible when the content section of the page is a product archive. I attempted to create a variable in the archive-products.php ...

Ways to bypass or replace the overflow:hidden property of the parent container even when unable to modify its CSS styling

I'm encountering an issue. I'm currently developing a widget to showcase certain elements on a webpage. I have created a fiddle, please take a look: .wrapper { width: 300px; display: block; position: relative; overflow: hidden; back ...

animate the alignment of right-aligned text to move to the right side and realign to the left

I need help with a small menu-list that should expand when the mouse is nearby. By default, the menu is aligned to the right, but on hover, every other item shifts to the left to accommodate the increased height (check out the JSFiddle). ul { font-siz ...

How can I implement user-specific changes using Flask?

I am a beginner with Flask and I am working on a project where users can sign up, and if the admin clicks a button next to their name, the user's homepage will change. Below is the Flask code snippet: from flask import Flask, redirect, url_for, render ...

Switch from HTML symbol to Java symbol

Is it possible to change an HTML symbol into a Java symbol? For instance, if I have &#xe000, is there a way to obtain the Java char representation like \ue000? What steps should I take to achieve this conversion? ...

Steps for displaying a division on clicking a hyperlink

I am currently working on my main menu layout in HTML, and here is the code snippet: <div id="main-container"> <div id="main-wrapper"> <div id="logo"> <h1 id="title">port ...

Utilizing Z-Index placement in CreateJS

I'm struggling to adjust the z-index for my line Graphic(). My goal is to ensure that the line always appears behind the other two shapes. Based on my understanding, the first element added has a z-index of 0, the second one has a z-index of 1, and s ...

Utilize Javascript to Populate Form Fields Based on "Selected Name"

I am currently facing a challenge in using javascript to automatically populate a form, specifically when it comes to setting the value for the country field. <form action="/payment" method="post" novalidate=""> <div class="input_group"> ...

Attempting to connect CSS to an HTML document within a Django development project

Directories Here is a photo of my directories. I am currently trying to utilize sudokuStyle.css in sudoku_board.html, but for some reason it does not appear to be linking properly in my HTML file. Below is the code snippet from my head section: {% load st ...

How to modify duplicated elements in JavaScript?

Imagine having text input A. Developing a new form element, F, using javascript without connecting it to the DOM is the next step. Subsequently, an input B (a copy of input A) is attached to F. Input B aims to replicate input A, therefore an event listener ...

Associating specific information with individual elements utilizing HTML text box

Seeking guidance on implementing annotations feature for a scatter-plot using d3.js v3. My goal is to show a text-box upon clicking each data point, then display that entered text as a tool-tip specific to that data point. Here's how I'm approach ...

Guide on aligning a division within another division?

Included below is my HTML code: <div id="background_div"> <img id="background_img" src="images/background.jpg" alt="dont matter"> <i class="material-icons">perm_identity</i> <div id="dropdown"> <ul id=" ...

What is the best way to implement two-way data binding to show local storage data on an input field in HTML?

After successfully fetching all the necessary data from a sign-up API, I am looking to display this data as default in specific fields on a page (such as First Name, Last Name, Email, and Phone Number). Any help with achieving this would be greatly appre ...

Prevent the display of hyperlinks in the status bar when hovering over a hyperlink or button

The application I'm working on has a default status bar at the bottom of each screen that displays URLs linked to buttons and icons. For example: https://i.stack.imgur.com/ZFTsp.jpg I am trying to prevent the display of URLs associated with hyperlin ...

Obtain Vue component reference using the @click event

I've been struggling to find an HTML reference to the component triggering an @click event in Vue. I have a custom component within a list: <ion-list lines="none" v-for="item in uiPosts" :key="item.createdAt"> < ...

Inserting a vertical barrier in the midst of the content

I am struggling to create a vertical divider and align the text properly in the top right corner of my screen. https://i.sstatic.net/UqURE.png Currently, I am facing issues with displaying the divider and positioning the text correctly. <div class="r ...

Position a div element on top of Google Maps to create a floating

Currently working on a website and attempting to add shadows over Google Maps by using a <div> element for a shadow effect: Unfortunately, the desired effect is not being achieved. This is the current CSS code in use: html { height: 100% } body ...

How to Enhance Your MUI DataGrid Rows with Borders

Trying to customize the borders of Material UI DataGrid rows to achieve a design similar to this screenshot (with some info censored): https://i.stack.imgur.com/0xRFd.png The issue I'm facing is that I'm unable to display the right border correc ...

Setting text alignment in Bootstrap based on breakpoints

Imagine this code: import React from 'react' import DarkPattern from '../Assets/dark-pattern.jpg' const Profile = () => { return ( <div> <section className="bg-dark text-light text-center " ...