Using HTML/CSS to create custom styled boxes for reusability in Bookdown

I'm looking to create reusable sections for my bookdown project.

Successes: I've created a style.css including:

.titeldefbox{
  display: flex;
  width: 100%;
  left: 0;
  top: 0;
  position: absolute;
  background-color: darkslategray;
  height: 30px;
  color: white;
  border-radius: 10px 10px 0 0;
  padding: 10px 0 10px 0;
}
.defbox{
  background-position: 0px;
  background: linear-gradient(
  315deg,
  hsl(240deg 50% 95%) 0%,
  hsl(158deg 13% 64%) 100%);
  padding: 1em 1em 1em 1em;
  border-radius: 10px;
  position: relative;
}
.svgdefbox{
  width: auto;
  height: 25px;
  padding-left: 1em;
}
.defbox h2{
font-size: 16px;
margin-top: 0px;
width: 100%;
height: 20px;
padding: 5px 0 10px 15px;
}

.defbox p{
  margin-top: 40px;
  padding: 10px 10px 0 0;
}

In each .Rmd file, I can use the following code snippet to call a div span:

::::: {.defbox}
A **definition** is a statement of the meaning of a term.
:::::

This will render as:

https://i.sstatic.net/0a21Y.png


Challenges: Attempting to implement a titled box with an SVG image as seen in various places like this example.

https://i.sstatic.net/nFbRX.png

The following structure:

<div class="defbox">
  <div class="titeldefbox">
  <svg  class="svgdefbox" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 382.59 429.36"><defs><style>.cls-1{fill:#ffffff;}</style></defs><g id="Ebene_2" data-name="Ebene 2"><g id="Ebene_6" data-name="Ebene 6"><g id="definition"><path class="cls-1" d="M81.17,128.33c3.88-2.38,7.76-4.4,11.64-6.29,1.94-1,3.88-1.82,5.82-2.74s3.88-1.7,5.82-2.5a5.39,5.39,0,0,1,2.9,0,23.59,23.59,0,0,0,2.91.1l5.81,0,11.61,0,46...

is meant to render as:

https://i.sstatic.net/8gi6c.png

However, implementing it within my markdown structure has proven to be a challenge. I could either utilize pure css in the YAML header or include an entire .html file using the includes option such as:

output: 
  bookdown::gitbook:
    includes:
      before_body: top_message.html

Answer №1

To achieve this effect, one approach is to utilize a small snippet of javascript code to dynamically insert an SVG element in all instances where the classes .defbox and .titeldefbox are utilized.

For instance, if you wish to incorporate Definition boxes in your 01-intro.Rmd file, you can employ pandoc divs as demonstrated below:

01-intro.Rmd

# Hello bookdown 

All chapters begin with a first-level heading followed by your chapter title, similar to the line above. There should only be one first-level heading (`#`) per .Rmd file.

:::: {.defbox}

::: {.titeldefbox}

<h2> Definition </h2>

:::

A **definition** is a statement of the meaning of a term.

::::

## More styled box...

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tristique nibh mauris, id ullamcorper massa convallis et. Aliquam pharetra rutrum bibendum. Quisque diam lorem, varius ac tincidunt vel, volutpat et est.

:::: {.defbox}

::: {.titeldefbox}

<h2> More Definition </h2>

:::

Praesent iaculis sed metus sed imperdiet. Nunc vitae augue bibendum, pulvinar neque id, suscipit justo. Quisque convallis, erat vel consectetur mattis, ante est euismod mauris, at condimentum enim erat id lorem. Phasellus volutpat id sapien a sollicitudin. Maecenas nec hendrerit felis.

::::

These divs do not currently include the svg icon. To add them dynamically using JavaScript in an HTML file, include the html file using the includes: after_body: YAML key in the _output.yml.

_output.yml

bookdown::gitbook:
  css: style.css
  includes:
    after_body: custom_def.html

custom_def.html

<div class="svg">
  Inserted SVG code here...
</div>

<script>
Functionality to add SVG elements dynamically using JavaScript...
</script>

Additionally, ensure you have the following CSS rules defined in your style.css:

style.css

CSS rules for styling .titeldefbox, .defbox, and .svgdefbox…

https://i.sstatic.net/nBX8r.png


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

Interacting with jQuery UI Droppable by setting acceptance criteria before submitting a form

My goal is to create a draggable item that can be dropped onto a specific zone. The challenge I'm facing is in displaying a form for inputting information and storing it in a database. If the input is successfully stored, the drop action will be succe ...

Using JavaScript to store CSS style properties in an array

In the midst of building a React-datagrid project, I am streamlining CSS properties for an array. However, there seems to be redundant CSS properties that I would like to consolidate into an array format. let _columns = []; let commonStyle = {"width":"20 ...

Determine the preceding element in a table by utilizing jQuery

I currently have an HTML table with the following content: <table class='main'> <tr id='r1' class='tbl'> <td>V1</td> </tr> <tr id='r2' class='tbl'> <td>V2& ...

The onmessage event in the websocket client seems to be malfunctioning and is not triggering

In my implementation using node.js, I have set up a websocket server and client. The handshake process between the server and client appears as follows: Request URL: ws://localhost:8015/ Request Method: GET Status Code: 101 Switching Protocols Request ...

Arrange divs in a stack when one becomes too full

Struggling to find a pure CSS solution for this issue, so seeking advice. Imagine three divs aligned horizontally next to each other. If one div overflows onto the next line, I want them to switch to a vertical stack layout. How can this be achieved? I h ...

Position the text in the exact center of an HTML element using absolute positioning

Hey there! I am currently working on a website and I'm trying to create a simple delete button that can be used throughout the site. However, I'm having trouble getting it positioned correctly... Here's what I have so far: <button id="c ...

Tips on choosing the initial h4 element within the same parent element but at different levels of depth

Consider the following code snippet: <div class="main-element"> <h4>Title 1</h4> <ul> <li></li> <li></li> <li> <h4>Title2</h4> & ...

Trouble with toggling class "collapsed" in Bootstrap v3 Navbar-toggle when using multiple data-targets

When using the data-target attribute with multiple CSS selectors, the behavior of toggling the "collapsed" class in navbar-toggle does not work as expected. If only one selector is used, the class is toggled properly, but when multiple selectors are specif ...

Automatically populate the next dropdown menu depending on the answer provided in the previous field

Can you help guide me in the right direction with 2 questions I have? If the answer to the first question is 1, then I would like the following answer to automatically be populated as Yes. Please assist me! <div class="field"> <!-- Number of Em ...

What is the best way to dynamically implement text ellipsis using CSS in conjunction with Angular 7?

i need to display a list of cards in a component, each card has a description coming from the server. In my component.html, I am using a ngFor like this: <div [style.background-image]="'url('+row.companyId?.coverUrl+')'" class="img- ...

Safari not properly adjusting height of bootstrap cards within column in row

I'm currently working on a bootstrap and angular project. I've noticed that in Safari, the card heights are not behaving as expected compared to other browsers like Firefox, Chrome, and Edge. I have tried setting the height to 100%, but it seems ...

Three divs arranged side by side with responsive design for mobile viewing

This code generates 3 divs for display. However, I am looking to optimize them for mobile devices. What steps can I take to accomplish this? Currently, the divs are oriented to the left and are not visible on a mobile device. I would like them to display ...

Issues with Converting JSON to HTML Table using JavaScript

Issues with Converting JSON to HTML Table Using JavaScript I've been trying to create a function that will display the contents of a JSON file in an HTML table. However, I keep getting an undefined error. Despite being able to see the data displayed ...

Links arranged in semicircles along the perimeter of the page

Here is some code that I have created to display two links in circles positioned on the left and right sides of a webpage. [class*="navigation"] .nav-previous, [class*="navigation"] .nav-next { position: fixed; z-index: 999; top: 50%; text-align ...

Safari iOS does not display any background for Buttons

Encountering an unusual problem with buttons on iOS devices specifically in Safari browser - the background color isn't being applied like it is on Desktop and Android devices. Here are the CSS properties for the button: export const Button = styled.b ...

Adaptable positioned picture

I'm trying to figure out how to make a series of positioned images adjust to the window's dimensions. Should the images be relative and the page wrap absolute, or vice versa? I've been experimenting on jsfiddle but can't seem to get it ...

Using jQuery to update a table, however, the browser is failing to automatically refresh the

I have developed a node.js and sockets app to create a Single Page Application (SPA). The user can fill out a form to create a new lobby. Upon submission, a socket event is sent to the server, where the new lobby is added. The server then emits an event to ...

What methods can be employed to prevent a custom CSS from overriding my personal styles?

Issue I've created a website with unique styles that I really like. However, I want to enhance its functionality by incorporating a custom dialog box from the BootBox library. The problem is that the extensive style sheet that comes with BootBox com ...

Exploring the ways to access inner contents of a Div element using ajax

Looking to update the SQL database with a list of variables sent from an HTML page. Some data is being sent correctly, while others are not. The list is placed in a div divided into two parts: "h1" and another "div". The header data is being sent correctly ...

What is the best way to vertically center the `tab-content` without relying on the tablist for

After examining the form below, my goal is to center only the fields while keeping the navigation buttons in their original position. Specifically, I want only the tab-content to be centered, excluding the tablists. <link rel="stylesheet" href="https ...