Transferring parameters from HTML to CSS using Less and addressing two key design problems

I am currently in the process of designing a webpage, and I've encountered three distinct issues - two are related to pure .css, while one is related to less.

Firstly, I have set margin: auto and padding: auto on the body tag, but it's not centering as expected (see code snippet below). Secondly, I'm struggling with aligning a h1 tag vertically in the middle using css.

The main issue, however, is handling multiple divs each with a unique background color and shared properties. Is there a way to pass the background color name from the HTML when using handlebars as a templating engine?

Each div has a class of .item, and I would like to specify the color in the HTML code. My current CSS code looks something like this:

CSS:


html {
  font-size: 12px;
  font-family: Arial, sans-serif;
}

body {
  margin: auto;
  padding: auto; /* Why isn't the page centered? */
}

h1 {
  background-color: red;
  text-align: center;
  height: 50px;
  vertical-align: /* How can I vertically center it? */
}
.flex {
  display: flex;
}

/* How can I avoid repetitive color styling for .item classes by passing variables from HTML to my mixin? */

.item(@fontColor) {
  .item {
    width: 30%;

    h3 {
      color: grey;
    }

    span {
      font-style: italic;
    }
  }
}

This is what my HTML code looks like:


<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet/less" href="style.less" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/less.js/2.6.1/less.min.js"></script>
  <script src="script.js"></script>
</head>
<body>
  <h1>Hello Plunker!</h1>

  <div class="flex">
    <div class="item red">
      <h3>Project 1</h3>
      <span>Time: 10:30AM</span>
    </div>
    <div class="item green">
      <h3>Project 2</h3>
      <span>Time: 10:30AM</span>
    </div>
    <div class="item blue">
      <h3>Project 3</h3>
      <span>Time: 10:30AM</span>
    </div>
    <div class="item yellow">
      <h3>Project 4</h3>
      <span>Time: 10:30AM</span>
    </div>
    <div class="item navy">
      <h3>Project 5</h3>
      <span>Time: 10:30AM</span>
    </div>
  </div>
</body>
</html>

A live example of my code can be found here: https://plnkr.co/edit/ROp2tLBKeSNYJVBCYwUL?p=preview

Thank you for assisting me with these queries.

Answer №1

To improve the efficiency of receiving responses, it is advisable to ask one question at a time rather than multiple questions in a single post.

Below are the answers to your queries along with an updated plunker link at the end:

If you want to center the body content, create a div inside the body tag with a fixed width. You can then use margin: 0 auto to center the div within the body.

<body>
  <div id="page-wrap">
    <!-- HTML content here -->
  </div>
</body>

body{
  #pagewrap{
   width:800px;
   margin:0 auto;
 }
}

For styling the H1 heading:

Add a wrapping element around the H1, set its display to table, specify a height, and apply the following CSS to the H1:

Update your HTML as follows:

 <div class="header-container">
     <h1>Hello Plunker!</h1>  
 </div>

Update your LESS styles as below:

 .header-container{
      background-color: red;
      height: 50px;
      display:table;
      width:100%;
      h1{
        vertical-align: middle;
        text-align: center;
        display:table-cell;
      }
    }

To define colors without using a mixin, leverage Less like this:

The code snippet would be:

.item{
  width: 30%;

  h3{
    color: grey;
  }
  span{
    font-style: italic;
  }
  &.red{
     background-color: red;
  }
  &.green{
     background-color: green;
   }
 }
}

This will result in generated CSS for different colors based on assigned classes (e.g., .item.red applies red background).

Check out the updated Plunker here: https://plnkr.co/edit/WU2pQfwtq6dkLy124tLD?p=preview

Answer №2

To create a perfectly centered content section, you'll need to define a width for your content. Follow JanR's advice by using a wrapper element, specifying a width (or max-width for responsiveness), and applying margin: 0 auto;. Remember that padding: auto has no effect in this context.

Regarding your H1 element, consider your objectives - there are various approaches such as the table trick or setting height and line-height properties. Keep in mind the default margin applied to H1 elements. Flexbox is another option to explore based on your specific layout requirements.

As for your color styling concern, remember that HTML classes cannot be directly passed to CSS. Stick to simple class names without complicating matters with Less or similar tools. Keep it straightforward! :)

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

Adjust the styling of the minimized file input to work like a download button when the window is resized

I successfully minimized the fileInput function by removing the upload area and loading bar, giving it a similar appearance to a downloadButton. However, I'm facing difficulties in determining which part of the CSS code needs to be modified to make it ...

Repairing the formatting of the content inside a dynamically growing div

Check out the code on this Fiddle link, <!DOCTYPE html> <body> <div class="test"> <h1>?</h1> <p>This paragraph gives more information that can be accessed by hovering over the question mark (Move your mouse awa ...

Interactive CSS animation with a single click

I am in the process of developing a website and have incorporated CSS animations for dropdowns. However, I am looking to change it so that the dropdowns appear on click rather than hover in the submenu. I am struggling to make this adjustment with the curr ...

Positioning Elements Absolutely in Relation to List Items

I am looking to style a list item by adding background-color, border, and other properties. Below is the CSS and HTML I have tried so far. Additionally, I want to use the div element to position more absolute elements in relation to the li > div. ul { ...

Developing an editor

Currently, I am delving into the concept of object inheritance in JavaScript through a practical exercise where I am creating an online editor similar to the one I am currently using. However, I find myself slightly confused as I aim to utilize data-* att ...

Placing a static navigation bar at the top of the form

Currently, I am utilizing Bootstrap 4 and have a div set up as a container for three rows. The first and third row are meant to be fixed, while the middle section (content) consists of a large form that should scroll. The goal is to keep the first and thi ...

Ways to eliminate the extra space in a dropdown menu

Is there a way to eliminate padding on the "dropdown-menu" <ul> tag when the list is empty, so that no space is displayed in place of the padding? I want to avoid using .dropdown{padding: 0} because it will remove necessary padding when the list is ...

Ways to customize the appearance of a search box

I am looking to revamp the appearance of a search bar that currently looks like this: <div class="searchbase input-group animated trans" id="searchbase"> <input type="text" placeholder="<?php _e(_l('Searching...'));?>" class=" ...

Utilizing FontAwesome icons instead of png images as a visual source

Is there anyone who can assist me? I currently have SiteSearch360 set up on my website with a full screen search bar that is activated by clicking on an image of a magnifying glass. My goal is to replace the default image (src="https://cdn.sitesearch360. ...

Enhancing elements with fade-in effects upon hovering

Is there a way to incorporate a subtle fade in/fade out effect when hovering over items on this webpage: http://jsfiddle.net/7vKFN/ I'm curious about the best approach to achieve this using jQuery. var $container = $("#color-container"), ...

The PHP Admin Panel conveniently shows the Edit post form right at the bottom of the page

After following a PHP CMS tutorial, I created this awesome website for a school. It's my first dynamic website, and I even managed to create an admin panel that looks like this. Admin Panel Pretty cool, right? Initially, I followed a basic tutorial ...

What is the solution for getting `overflow-x` to function in place of `overflow-y`?

I have a main container with several sub-containers inside. When the main container's width exceeds its limit, I want to display a horizontal scroll bar. Instead of using the 'display: inline-block' property because it creates unwanted whit ...

What is the best approach for scaling @material-ui Skeleton in a grid row with variable heights?

I am working on creating a grid of Avatar images with a transition state where I want to show a skeleton representation of the image. To achieve this, I am using @material-ui/lab/Skeleton. The issue I'm facing is that since my images are set to autos ...

Minimize the size of the MUI date selector widget

I've been incorporating the MUI date picker into a data list, but I'm looking to decrease the height of the input field and adjust the positioning of the close date and calendar icons. They're currently taking up too much space between them ...

Creating visually appealing Jquery-ui tab designs

Trying to customize the jquery tabs, I experimented with styling them to my liking. One thing I did was attempt to reduce the size of the tabs by adding a height:45px; to the UI stylesheet shown below. .ui-tabs-vertical .ui-tabs-nav li { clear: left; ...

Animating lines with JQuery beneath navigation links

Currently in the process of revamping my portfolio website, and it's still a work in progress. Recently stumbled upon a jquery tutorial that enables a line to animate under anchor elements on hover, which I find quite intriguing. However, I am facing ...

Showing formatted JSON in the view using ASP.NET MVC

Is there a method to modify JSON for presentation in the interface? I am looking for a way to automatically update my API documentation when adding new properties. It would be great if I could also apply CSS styling to certain elements. This feature is som ...

What can be done to prevent an image from vanishing along with its containing div?

I recently created a website/application where users can interact with elements by dragging them around the page. However, I encountered an issue where one element disappears when dragged outside of its original container. How can I prevent this from happe ...

Tips for creating a unique scroll page arrow button with adjustable thickness?

Seeking guidance on how to adjust the thickness of the arrow used as a scroll button on my website. I am aiming for a similar look to the arrow on this particular website: example of arrow https://i.sstatic.net/wO5br.png To view my code, please visit: Cod ...

Google Map in Bootstrap FullScreen Mode Not Mobile-Friendly

Could you please take a look at the provided link and advise me on why the map is not responding correctly? I am also facing issues with the positioning of the map on the page, so any guidance on adjusting it appropriately would be greatly appreciated. Be ...